3D Tower Defense, Part 3

August 21st, 2008 | by phantom |

While todays update was somewhat minor to code it also represents an important set of basic milestones for the game in general in that we now have a game of sorts to speak of.

The main two changes are that towers can now shoot things within a certain area and creatures can die and be respawned at a new random location keeping their overall population the same.

 Towers shooting things is just a simple matter of asking the game if a creature is in it’s attack zone and if so telling the game to kill it. The way the logic is setup means that the towers can only shoot one creature per update, thus if a group of creatures enters the zone at once then some will get passed and potentally into the city itself.

Once the game has done shooting it respawns any missing creatures so that on the next update they can be sent on their merry way towards the city once again in a never ending cycle. The respawn code is just the inital grid setup code but pushed into it’s own function and called from both locations.

The other creature related update is that once a creature makes it to the town it needs to removed. Originally the code used an array to contain the creatures however I’ve since changed it to a single RemoveAll() call and my first anonymous function to do the checking.

creatures.RemoveAll(creature => (town.InTown(grid.ConvertCellToGridPoint(creature.WorldCell))));

This single line of code removes all the creatures which have got into the town allowing them to be respawned at the end of the update loop.

All these updates combined give us the basics for a game, and with a couple of UI Updates to allow the stats for the game to be tracked constant pushing of the ‘Update’ button lets the test act like a game.

It was about this point that I got bored of pushing the button, so instead I changed the code to use a timer which fires every 500ms to run the update loop. This lets the game continue and you can place towers via the right mouse button in “real time”.

My next job is to get rid of the grid for the monster spawning and adjust the spawn code so it takes into account tower locations and exclusion zones; I’ll probably end up abusing LINQ for this, if only to play around with it for a bit. I also need to prevent you being able to place new towers ontop of old. In fact, in such a situation you should be given the option to upgrade your towers instead.

Another thing I’ve been considering is an XNA/XB360 version of the game; while the core logic can stay the same the biggest problem I’ll have is with control method. On the PC point and click is perfect but, with the XB360 at least, that isn’t an option. Instead I was considering something like Civ:Revolutions with regards to how the map moves about and doing a ’stop the world’ method of placing and upgrading towers. It will probably still be simple enough;

  1. Hover selection box over tower
  2. Click A
  3. Menu pops up with valid selections on it
  4. Navigate to selection
  5. Press A to select
  6. (optional) confirm step

Pressing ‘B’ would back you out at any time. I feel it would work well, certainly if the graphics engine behind it allows for the correct expressiveness with regards to the interface and the way the world moves about. Zooming out to a decent level will also be a requirement of both PC and XB360 versions as I’d like to have ‘large’ maps to play with.

But, that’s the future, for now I need to carry on nailing down the main game attributes and logic.

You must be logged in to post a comment.