cloister
Full Member
Offline
Posts: 138
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.