Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 1674 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!