Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NPC Help, please  (Read 2260 times)
0 Members and 1 Guest are viewing this topic.
Offline Kakashi266

Junior Devvie

« Posted 2007-12-27 04:16:06 »

Hello, everyone. I'm making a game that is basically a clone of Pokemon. As those of you who have played know, there are hundreds and hundred of NPCs, each with a name, and something stupid and inane to say. I'm not quite sure how to store these guys. The best example I have is just the stuff from my Java gaming books, where there are no NPCs.

What I'm thinking is that I should have an array of the NPCs, and one class that takes a name (of an NPC), and updates that specific NPC.  There would be a method that uses the player's current position to detect NPC paths within the view (I want to animate the NPC even if only the path, and not the NPC itself, is in view). Then, for each one, it would update it. Oh, and the paths are numbered according to which NPC it is (if the path is 4, it will update npcArray[4]).

Sorry for the convoluted idea. Right now it is the best I have. If there is a better way of doing it, or a good example somewhere in this forum I didn't find (I looked), I would love if someone could share with me. Oh, and thanks for your help with the last problem I had, too (the keyboard thing).

Offline fletchergames

Senior Devvie

« Reply #1 - Posted 2007-12-27 05:08:00 »

In tile-based RPGs, I've always just made NPCs be regular objects in the map.  They were just stored in the object layer of the map with everything else.  It was just a 2D array, one element for each position in the map.  When they moved, I just set their old position to empty and their new position to their object.

If you have multi-tile sized NPCs you have to either give them multiple tiles in the object layer or just check multiple tiles for things like collisions and drawing.  I found the second way to be easier.

I used breadth first search from the player's location if I had to find an NPC from his name.

If I did it again now, I would probably due the same thing.

An alternative is store NPCs in a separate array along with other scripted objects.  I might use quad trees for collision detection (you'll have to look them up - I don't know much about them at this point) in this case.  I would also have a hash map linking to the npcs by name so that I wouldn't have to search for them.
Offline Kakashi266

Junior Devvie

« Reply #2 - Posted 2007-12-27 05:20:27 »

Hm, thank you, that was a good reply. I think I might have thought about it the wrong way. I suppose what tripped me up the most is that when the NPCs walk, they of course change images (still, left step, right step). You see, to avoid issues (although perhaps it makes things harder, I don't know), my main character is drawing separately, not as an object on the map. he just stays in place, points where you tell him to, and marches in place when you make him. I guess I was thinking to do that with the NPC. Although (I just thought of something), the NPCs will still move independently of the map, because I made walking smooth by 12 passes of moving 8 pixels (my tiles are 96 pixels large [Yes, I have 15X9 tiles on my 1440X900 screen]), and the map is dependent on those "steps" to determine position. Soooo... the NPC would stay in one place till he has taken one whole step, at which point he would jump to the next tile, taking his background with him.

So, (thanks for bearing with my rambling- I'm working things out as I type), I guess I would need to implement either individual NPCs, or a separate layer for the NPCs. The problem with the latter option is that I suppose I would suffer the same jumping problem, just without tearing the ground underneath him away, too.

God, this was so much easier when I was programming this game completely incorrectly.

EDIT- Okay, I think I solved one of my problems, by looking at my old (jokingly incorrect) code. I was unsure of how long it would take to update all of the visible NPCs (there could be a few at some points). Buuut, it seems as if I took the number of sprites there were visible, and randomly decided which one to move at a time. So I only need to move one at a time.

I am also piecing together a working way of doing my NPCs.

EDIT 2- Yeah, so I have created a separate layer just for NPCs. I forgot that even though there is a finite point where the NPC switches spots in an array, I can still render it in increments of 8 pixels difference from its previous spot, and after 12 times adjust the layer array. A lot of gratitude to fletchergames for the help. I'd love to program everything now, but it's one in the morning, and I'ma gonna go to sleep.

Pages: [1]
  ignore  |  Print  

EgonOlsen (1850 views)
2018-06-10 19:43:48

EgonOlsen (1879 views)
2018-06-10 19:43:44

EgonOlsen (1245 views)
2018-06-10 19:43:20

DesertCoockie (1680 views)
2018-05-13 18:23:11

nelsongames (1345 views)
2018-04-24 18:15:36

nelsongames (1963 views)
2018-04-24 18:14:32

ivj94 (2736 views)
2018-03-24 14:47:39

ivj94 (1939 views)
2018-03-24 14:46:31

ivj94 (3029 views)
2018-03-24 14:43:53

Solater (1078 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!