help with tower defense

I was working on a tower defense game when I kind of hit a brick wall: I can not figure out how to make the towers track to one enemy destroy it then track to another enemy, how could I achieve this?

You’d have to use python, get a list of the enemies somehow, then set a property to a enemy, set tracking object to it until it’s health it’s 0 then remove from list and switch.

I realy do not know all that much python, could I do that using just logic bricks?

I don’t think so. At least it would be really messy (one shooter per enemy… it sends a message when it dies, delete… so on). Check this out.

That looks like it will take a lomg time before I could read all of that and figure out how to use that information in my game, could you give me like some sample python for that?

OK.
This one is not tested. It might give out a bug…
Give each enemy a property “enemy”.
Check the comments.

import GameLogic as g
c=g.getCurrentController()
o=c.getOwner()

#fill enemy list once. enemy list=enemies
if hasattr(o, "init")==0:
    ol=g.getCurrentScene()
    enemies=[]
    for ob in ol:
        if hasattr(ob, "enemy"):
            enemies.append(ob)

shoot=c.getActuator("shoot")#shoots projectile thing (add actuator)
track=c.getActuator("track")#3d trackto actuator

track.setObject(enemies[0])

g.addActiveActuator(shoot, 1)
g.addActiveActuator(track, 1)

if enemies[0].health<=0:
    enemies.remove(enemies[0])

all right let me test it out,

You will need to sort the enemies in order of distance, and you need a motion actuator on the projectile, so. Updated:

cont = GameLogic.getCurrentController()
own = GameLogic.getOwnder()

scn = GameLogic.getCurrentScene()
obl = scn.getObjectList()

tower = obl['OBtower']

shoot=cont.getActuator('shoot) #shoots projectile thing (add actuator)
track=cont.getActuator('track') #3d trackto actuator (on projectile)
motion = cont.getActuator('motion') #motion actuator (on projectile)

if not hasattr(GameLogic, 'enemies'):
    GameLogic.enemies = [ ] #create a list

    for ob in obl:
        if hasattr(ob, 'enemy'):
            GameLogic.enimies.append(ob) #add the enemies to the list

target = GameLogic.enemies[0]
for enemy in GameLogic.enemies:
    if tower.getDistanceTo(enemy) < tower.getDistanceTo(target):
        target = enemy #choose the closest enemy


track.setObject(target)
motion.setDLoc(0,0.02,0,1)

GameLogic.addActiveActuator(track, 1)
GameLogic.addActiveActuator(motion, 1)
GameLogic.addActiveActuator(shoot, 1)

I could not get it to work, could you post a blend?

That script had a lot of errors, I fixed the syntax but it still didn’t work right.

Could any one get a blend for me?

Here, I rigged up a simple example of a tower shooting (suzzanes) at any object with the property “evil”.

To add more towers, you should be able to use the same setup. To make objects targeted by the towers, you just have to be sure they have a property called “evil” attached to them.

Couple of things are still missing. Primarily, the suzannes still don’t “kill” the spheres. That shouldn’t be too hard, just use a collision detector on the spheres set to a property on the suzannes. Also, adding path-following for the spheres so they don’t just go in a straight line and figuring out how to add the towers in-game.

Hopefully this provides a good start, though, and I wish you the best of luck.

Attachments


towerdefense.blend (228 KB)

Thank you so much!!!