Tower defense question

Hi,

I’m trying to build a tower defense game and sp far it’s working well. I do however have a problem.

Right now my tower checks there’s anenemy within reach, and if there is, the tower will fire.

The problem I’m having is to determine what enemy is the first one (closest to the exit)

this is no problem the first few waves, but for example if you have a wave with some slow enemies first and some fast enemies behind them (which will be fast enough to pass the slow ones) my towers will fire at the slow ones first even if the fast ones are ahead.

This boils down to the fact the when I check if the tower should fire I basiclly do this;

for t=1,towersLayer.numChildren do for e=1, enemyLayer.numChildren do ... if within range, fire.. end end

which will catch the slow enemies first because theey are added first.

Does anyone have a bright idea on how I can go about this?

Thanks!

I would suggest you use some soft of proximity sensing in order to identify which enemies are closest, instead of the iteration function you have above. Using non-physics collision testing would be the best way that I can think of to achieve this; that way, you can just have shots fired on a loop for as long as an enemy is overlapping a certain newRect object.

http://www.coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Thanks!

I think I was a little unclear about the problem thou.

Let’s say I have two enemies that are within reach of one tower.

The tower senses the two enemies and fires a shot.

When the tower fires, it needs a way to sense with one of all the enemies within reach that is closest to the exit of the enemy path and fire on that one.

One idea I had was that when I spawn all the enemies I mark the first one as first (set a property or whatever), when ever an enemy ‘collides’ with that enemy I count it as an overtake and that one is marked as first.

The problem with that ofcourse is that when you kill the first you don’t know which one should be the new first.

I could chrck the remaining route for each enemy, but that would have to done each frame when any enemy is within reach of any tower so that seems abit much.

Not sure if you are using the physics engine, but ray casting could solve your problem. It’s an extremely underrated feature of the physics engine.

http://docs.coronalabs.com/api/library/physics/rayCast.html

In certain cases I’ve used the physics engine in non-physics based games just to use this powerful piece of functionality.

Hi there Richard,

when I needed something similar I used something like this to find out the enemy that is closer to any given tower:

local closestEnemy = nil local closestDistance = nil for i =1, #enemiesWithinRange do local distanceX = enemiesWithinRange[i].x - tower.x local distanceY = enemiesWithinRange[i].y - tower.y local distanceStraightLine = ( distanceX ^ 2 + distanceY ^ 2 ) ^ 0.5 if (not closestDistance) or (closestDistance \> distanceStraightLine) then closestDistance = distanceStraightLine closestEnemy = enemiesWithinRange[i] end end if closestEnemy then tower.fireAt( closestEnemy ) end

Hope it helps…

I would suggest you use some soft of proximity sensing in order to identify which enemies are closest, instead of the iteration function you have above. Using non-physics collision testing would be the best way that I can think of to achieve this; that way, you can just have shots fired on a loop for as long as an enemy is overlapping a certain newRect object.

http://www.coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Thanks!

I think I was a little unclear about the problem thou.

Let’s say I have two enemies that are within reach of one tower.

The tower senses the two enemies and fires a shot.

When the tower fires, it needs a way to sense with one of all the enemies within reach that is closest to the exit of the enemy path and fire on that one.

One idea I had was that when I spawn all the enemies I mark the first one as first (set a property or whatever), when ever an enemy ‘collides’ with that enemy I count it as an overtake and that one is marked as first.

The problem with that ofcourse is that when you kill the first you don’t know which one should be the new first.

I could chrck the remaining route for each enemy, but that would have to done each frame when any enemy is within reach of any tower so that seems abit much.

Not sure if you are using the physics engine, but ray casting could solve your problem. It’s an extremely underrated feature of the physics engine.

http://docs.coronalabs.com/api/library/physics/rayCast.html

In certain cases I’ve used the physics engine in non-physics based games just to use this powerful piece of functionality.

Hi there Richard,

when I needed something similar I used something like this to find out the enemy that is closer to any given tower:

local closestEnemy = nil local closestDistance = nil for i =1, #enemiesWithinRange do local distanceX = enemiesWithinRange[i].x - tower.x local distanceY = enemiesWithinRange[i].y - tower.y local distanceStraightLine = ( distanceX ^ 2 + distanceY ^ 2 ) ^ 0.5 if (not closestDistance) or (closestDistance \> distanceStraightLine) then closestDistance = distanceStraightLine closestEnemy = enemiesWithinRange[i] end end if closestEnemy then tower.fireAt( closestEnemy ) end

Hope it helps…