We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Tower Defense Project
Page Index Toggle Pages: 1
Tower Defense Project (Read 517 times)
Tower Defense Project
Feb 18th, 2009, 3:44am
 
As one of my school projects, I'd like to make a simplified version of a tower defense game. For those of you who don't know what a tower defense game is, the object of the game is to build towers to kill a line of mindless enemies that follow a path. If an enemy reaches the end of the path, you lose a life. If you lose all your lives, you lose the game. If you successfully thwart all the enemies, you win. This game (http://blog.gordaen.com/wp-content/uploads/2007/08/vector_tower_defense.png) contains the essential elements of a tower defense game.

As a school project, I'm not looking for anyone to write my code for me. I'd like to learn from this, but the quicker I learn the material, the more advanced I can make the project.

Essentially, the game breaks down into this:
- Create an array of enemy objects that, one after another, spawn at the beginning area and mindlessly follow a path, rotating at corners and continuing their trek until they reach the end.
- When an enemy reaches the end, the enemy disappears and a life is lost. When all lives are lost, the player loses.
- Enemies have health dependent on the level. As the level increases, as does their total health.
- The player builds towers (one type of tower, but if I have the time I would like to incorporate varying tower types) outside the path that will shoot at enemies if the enemies are within range. Each tower deals a certain amount of damage, and when an enemy takes enough damage, it dies.
- The player gains money every time an enemy dies, which they could then use to either upgrade existing towers or build new ones.

Most of the minor coding I can figure out on my own. What I'd like assistance on, though, is on two major aspects of the game:

1) How can I move an object along a pre-determined path, rotating at the corners to face its new direction?

2) What would be the best way of having towers decrease the health of one enemy at a time, when multiple enemies could be within range of the tower?

Thank you all for your consideration, and a preemptive thanks to those who would help me.
Re: Tower Defense Project
Reply #1 - Feb 18th, 2009, 6:52pm
 
Cool project.  I hope you post the script when you're finished so we can play it.  :)

1. I would take a parametric approach here.  Define the path however you want--as some mathematical function, as a set of connected line segments, whatever--but arrange it such that for a given time value 't' (measured as milliseconds since that particular enemy entered the screen), you can directly calculate the enemy's position.  For example, this pair of functions:

x = t
y = sin(t)

will cause the enemy to move in a wave-shaped path along the screen (although obviously you have to scale both the x and y values appropriately so that the overall path fits your screen dimensions well).

To get the enemy to face the correct direction along the path, you need a corresponding set of functions that give you the derivative of your x and y functions, also as functions of time:

x' = 1
y' = cos(t)

(although note that for this simple example, the x' value is just a constant.)

The same concept works if your path is composed of a set of connected line segments, or even a set of connected mathematical functions, you just have to have code that deals with what happens at the junctions.  You could have the enemies just "snap" from one heading to the next as they move across a junction, but that's probably going to look bad.  Another option would be to have the enemy pause there briefly while you transition their heading from one value to the next over several time steps.  For example, if your path is a horizontal line that meets a vertical line at one corner, then at the corner the enemy could pause for 5 time steps or something while you smoothly transition it from a horizontal to a vertical heading.  The general case there is that if two path segments (be they lines or general parametric curves) meet at a point, you get the derivative of each one at that point, and you smoothly transition between those values.

If you haven't worked with parametric equations before, wiki has a nice write-up here:

http://en.wikipedia.org/wiki/Parametric_equation

2. I'm not quite sure what you're asking.  Is it "how do I have a tower target just one enemy when multiple could be in range?" or is it "if a tower inflicts damage in an area of effect (e.g. a bomb instead of a bullet), how do I efficiently determine which enemies to apply the damage to?"

For targetting, hey, that's totally up to you but be aware that the choice you make will affect the dynamics of the game.  Most TD games I've seen have towers target the nearest enemy that's in range, but stick with that enemy until it dies or goes out of range, at which time they acquire a new target.  But you could go with "always shoot at the closest one" or "always shoot at the weakest one" or any other strategy you like.  Different tower types could have different rules, leading to lots of interesting tower interactions and player strategies.

If you're talking about area-of-effect damage, there are a couple of obvious options.  One is simple, and for a tower defense game where you're not going to be dealing with large numbers of enemies at any one time, is what I would go with: just iterate over all the enemies, see if it's in range of the damage effect, and apply the damage.  It's a brute-force solution, but is dead easy to implement and will be plenty fast enough on modern hardware.

The other option is that for each tower you can keep an arraylist (or similar) of enemies that are in range, and on each time step when an enemy moves you check to see whether it passes into or out of each tower's range, and if so, you update the lists.  Then, when a damage effect happens, you only need to apply it to the enemies in the list.
Re: Tower Defense Project
Reply #2 - Feb 23rd, 2009, 7:41pm
 
Thanks for all the help :] I'll be sure to upload the finished product at some point. Today's a full-blown programming day for me xD
Page Index Toggle Pages: 1