I was hoping to get some help with the pseudo code of my 2D java game,
Monsters (Orange balls) are running along the black line via pathing coordinates(Point):
if(i.monsterx < i.chasePoint.getX()){
i.monsterx++;
}else if(i.monstery < i.chasePoint.getY()){
i.monstery++;
}else if(i.monsterx > i.chasePoint.getX()){
i.monsterx--;
}else if(i.monstery > i.chasePoint.getY()){
i.monstery--;
}
Every time the monster reaches a "chasePoint" it gets a new chasePoint, etc...
The black square is supposed to look like it's shooting the monsters, with the green circle being the shooting range. But how do I decide where the bullet goes? I would prefer if the black square shot the mob in the front, but since the mobs can get slowed/stunned, and therefor aren't in a specific order, how do I know which mob is longest ahead?
The simplest solution would be to have each monster track the distance its travelled (rather than the time its lived; due to different speeds, stunning etc) then target the one thats travelled the longest distance. Assuming the black rail doesn't change this will allow the monster closest to the end to be targeted.
If the black rail does change a lot (due to placing new towers) then ask the object that generated the black rail (and regenerates it on tower placement) to mark how many blocks a particular chase point has left to get to the end (it already knows the path so this should be simple enough; i'm assuming you're using the A* algorithm or similar to generate this path; if so this number pretty much just falls out of the algorithm anyway). Then the monster can just be given that value on its chasePoint update. The cannon then asks all the monsters in range how much further they have to go to the end and targets the one with the smallest number