Wednesday, May 26, 2010

Random NPC Movement

A randomly moving non-player character is essential to a role playing game. I dreaded the thought of figuring this out, but it came to me in a flash of genius when I was on a bike ride this morning.

How this system essentially works:
There are 3 values.
The first value, A, is assigned a random number 1 or 2. This number determines the distance that will be traveled by the NPC.
The second value, B, is assigned a random number 1 through 4. This corresponds to the direction that the NPC will be traveling. 1=right, 2=left, 3=down, 4=up.
The third value, C, gets a value based on what the first value, A, came out to be. For instance, if value A = 2, then value C is set to 16.
Whenever value C is greater than 0, the NPC moves a space over and value C is subtracted from, until it gets back down to 0.
When that happens, the whole process starts over again.


Here is some video footage of it in action:



Pretty dandy. There are still some collision detection bugs that need to be hammered out. With the current code, when the NPC checks to see if there is an obstacle in the way, it sometimes glitches up. This is bad because it knocks the NPC off of the 16x16 grid. I'm not too worried about it right now. Time for lunch!

2 comments:

  1. Good work ! So you're doing a pixel by pixel movement system with that ?

    ReplyDelete
  2. Yep. The actual player movement is very similar to how an NPC moves. Collision detectors check to see if an obstacle is in the way, and if there isn't, a 16 is added to the player's alterable value, and subtracted from as it moves. There is also a run button that subtracts more from the alterable value, making the player move faster.

    ReplyDelete