• entries
16
19
• views
17136

# Zombies

594 views

What I have done is:

• Integrated the zombie AI and player controls into the turn system

[media]
[/media]

Mouse Input

Trace a ray into the map plane and get the X and Z coordinates of the collision.

Player Lighting

It's not really lighting. It's just a cheap trick, I just fade off the brightness the further a tile is from the player. Makes it more atmospheric though.

Zombies

The zombies move slower than the player. They cannot currently attack (there is no combat) and they just move in a random direction during their turn at the moment. They are completely oblivious to the player. They are blocked by walls but at the moment they can walk through each other and the player. Below is the source code for the zombie class.

class Zombie : Entity{ public Zombie() { _moveTurnUnits = 0.5; //length of game time it takes a zombie to move one tile } protected override Texture2D CurrentTexture { get { return _map.Game.Content.Load("zombie"); } } public override double Update() { return Move(Map.GenerateRandomCompassPoint()); }}

Time and Turn System

This is what the bulk of work has gone into. I kept redoing it, even once deleting the whole thing and starting again.

This is what I was after:

• At the start of the player's turn the game pauses waiting for the player to input the action they will take.
• Possible actions include moving to a neighboring tile, picking an item off the floor, throwing something, etc.
• The player can only perform one action per turn.
• The length of the turn in "game time" is determined by the action the player takes.
• Once the player has acted it is the turn of each creature on the map to act. They get as long in "game time" as the player took during their turn.
• A creature might be able to perform multiple actions during their turn. Eg if the player took a lengthy action of reading a book a creature might have time to take several movement actions.
• Creature turn must be fairly instant. There can't be much of a delay, perhaps 200ms at most. It should be possible for the player to hold that cursor key down and move quite fast.

A complication of this is that creatures can move at different speeds. Take the example of a Tortoise. The Tortoise is 10 times slower than the player. During the players turn, the player presses W and so the player moves North by one tile. It's now the tortoises turn. The tortoise chooses to move North too...but it's 10 times slower than the player so it should only move 0.1 tiles. But all creatures must be on discrete tiles, they can't be partially in one tile and partially on another.

What I settled on was to actually use a timeline of events. There is a game time. It's single number that starts from zero and increases as the game progresses. Events are scheduled to occur at given times. Each creature schedules their turn as an event on the timeline. When time reaches that event the creature performs their turn. The event is then reinserted into the future by the amount of time it took the creature to perform their action.

The events on the timeline are sorted by time, so that the first event on the timeline is the earliest.

1. Advance the game time to the earliest event in the list
2. Remove the earliest event from the list
3. Process that event
4. Go to 1

When each creature is added to the map they insert an event into the timeline. This is effectively a callback for their turn. When the event is pulled the creature is notified and performs their action. The creature returns how long the action took. The event is then reinserted onto the timeline with that delay from the current time. This allows different speeds for different creatures and different speeds for different actions. The timeline should also allow other things to be scheduled in the game. Eg timed actions, fuses, etc.

There are no comments to display.

## Create an account

Register a new account