Thursday, August 11, 2011

Wandering NPCs

Click the link in the main text to play
There really isn't a game yet, so I suppose it depends on your definition of playable...  But I am going to post it anyways.  Try this example of the game, the first build ever of my new game.

Why So Many NPCs?
In this example, there are many many NPCs wandering around.  I had some fun increasing the amount of NPCs to see what the game can handle before it starts to slow down, and you should get a totally normal framerate with that many NPCs.  In fact, I was able to go up to 400 NPCs before I started seeing the framerate drop.  Once all the other elements of the game are added in (especially multiplayer networking) this won't be the case, but right now it was fun to see so many characters on the screen at once without lag.  Lag is a serious problem when you are coding a game with AS3, especially one that is online multiplayer with real time combat (as opposed to turn based combat).  I will definitely write another article about that in the future.

Class Structure & Pathfinding
So I started working on my client, obviously, and quickly flushed out a movement system for the character.  The character you control is of class 'Player' which is extended from class 'Character'.  Class 'NPC' also extends class 'Character'.  I can already tell that the class 'Character' is going to be one of my biggest, messiest classes that the game revolves around.

Because I want both NPCs and Players to be capable of pathfinding to a given spot on the map, all my movement and pathfinding has to be part of the Character class.  In fact, right now my player class is very very small.  It simply checks to see what keys the user is pressing, and passes that on to the Character class via an (x,y) point variable, p.  p.x=-1 is analogous to pressing the left arrow key, etc.  Theres only about 10 lines of functional code in class Player so far.

I was seriously considering spending a LOT of time on this and making sure the pathfinding algorithm was perfect.  To do that I would need 3rd party code, and I considered it a while.  I think in the future I will need to adopt 3rd party code and a better system.  Right now, the Character class can be told to go to a tile, but if there is an object blocking it's way it will fail to reach its intended target.  If I choose to make a quest system, cinematic system, or multiplayer content then this could be a serious problem.  Characters would constantly get stuck on things, and then I would have to put in some quick fix code to make the character jump to their intended location.  It would be buggy and messy looking.  So its something to keep in mind for the future.

Click for full size image.  This diagram shows real results
from use of my wander radius code
Wander Radius
So I have a basic NPC movement code that moves the NPC to their target location (part of the Character class).  I still needed to add in code to have the NPCs decide where to walk.  Thats when I did my wander radius thing that you can read about in the picture attached to the right.

The NPC wanders randomly around, and if they move too far from their origin point then they will only wander in directions towards that point.  A value I call wanderRadius controls how far away from that point they like to wander.  The code for this is about 50 lines long.

I am opting to use real physics like I usually do for this game.  So there is acceleration, velocity, and location variables for each object (players, NPCs, and moving spells) in the game.  But I have the friction coefficient up so high right now (0.5 per looping frame) and the acceleration so high (2) that you cannot tell.  I chose to use real physics because I know I might want to have some slippery ice floor or something of that sort in the future in the game.  :-)


No comments:

Post a Comment