• Create Account

## [Solved]Tower which enemy to shoot at?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1FantasyVII  Members

Posted 26 October 2012 - 09:14 PM

Hello everyone,

I have a tower and 10 enemies. now if an enemy get in the tower range, the tower will shoot a bullet and the bullet will keep following the enemy until it hits him.

Now if the first enemy get in the tower range, the tower will shoot a bullet and the bullet class will calculate the enemy position and keep following him. However if the second enemy get in range while the bullet didn't reach the first enemy, the bullet will leave the first enemy and calculate the second enemy position.

I want if the tower shoot a bullet, the bullet should keep following the first enemy even if another enemy get in the tower range. and If the second enemy get in range the tower should shoot a new bullet with the second enemy position.

Here is the code:-

class Bullet
{
public void ShootAt(GameTime gameTime, Vector2 SourcePosition, Vector2 TargetPosition)
{
if (Alive)
{
ElapsedGameTimeSpeed = (float)Speed * (float)gameTime.ElapsedGameTime.TotalSeconds * 10.0f;
DeltaPosition.X = SourcePosition.X - TargetPosition.X;
DeltaPosition.Y = SourcePosition.Y - TargetPosition.Y;
double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));
Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}
}
}


class Tower
{
int ShootAtEnemyIndex;

public void IsInRange(List<enemy> enemies)
{
for (int i = 0; i < enemies.Count; i++)
{
Distance.X = enemies[i].Position.X - TowerPosition.X;
Distance.Y = enemies[i].Position.Y - TowerPosition.Y;
DistanceSquare = (Distance.X * Distance.X) + (Distance.Y * Distance.Y);
{
ShootAtEnemyIndex = i;
InRange = true;
}
else
InRange = false;
}
}

public void Update(GameTime gameTime,List<enemy> enemies)
{
IsInRange(enemies);

if (InRange)
{
bulletManager.Fire(gameTime, enemies[ShootAtEnemyIndex], Damage, AttackSpeed);
}

//Calculate the distance between the bullet position and the enemy position.
for (int i = 0; i < bulletManager.Bullets.Count; i++)
bulletManager.Bullets[i].ShootAt(gameTime,enemies[ShootAtEnemyIndex].Position, bulletManager.Bullets[i].GetPosition() + bulletManager.Bullets[i].GetVelocity());

bulletManager.Update();
}


Edited by FantasyVII, 26 October 2012 - 11:00 PM.

Posted 27 October 2012 - 12:02 AM

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #3FantasyVII  Members

Posted 27 October 2012 - 08:09 AM

the problem was the when a bullet was fired at enemy 1 and enemy 2 get in the tower range the bullet will stop moving toward enemy 1 and start following enemy 2.

but now i got it fixed thanks.

### #4rpiller  Members

Posted 27 October 2012 - 09:27 AM

Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.

Posted 27 October 2012 - 11:14 AM

Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.

Yeah, that was my idea... but that was too easy of a solution so I wanted to clarify. Looks like it is taken care of.

(I forgot to ask my usual first question... "did you trace your code?")
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #6FantasyVII  Members

Posted 29 October 2012 - 08:46 AM

Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.

I just can't get my mind around it. Starting to hate this project because of this.

Yeah, that was my idea... but that was too easy of a solution so I wanted to clarify. Looks like it is taken care of.

(I forgot to ask my usual first question... "did you trace your code?")

the problem is I can't use the ShootAt function only when I shoot because I need to keep track of where is the enemy position. So I need to call it every update. The solution I found was to create a bullet list for each enemy and when the enemy is in rage let that list only shoot at him. the problem is that its very messy and looks very bad. I need another solution but I don't know what.

		List<Bullet> Bullets1;
List<Bullet> Bullets2;
List<Bullet> Bullets3;
List<Bullet> Bullets4;
List<Bullet> Bullets5;
List<Bullet> Bullets6;
List<Bullet> Bullets7;
List<Bullet> Bullets8;
List<Bullet> Bullets9;
List<Bullet> Bullets10;


And I do this 10 times which is very very wrong and bad and messy.
if(enemyManager.enemy.Count > 0)
{
if (IsInRange(enemyManager, 0))
{
Time = (float)gameTime.TotalGameTime.TotalMilliseconds;
deltaTime = Time - LastTime;
if (deltaTime > (1000 / AttackSpeed))
{
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].Initialize(graphics);
for (int i = 0; i < Bullets1.Count; i++)
LastTime = Time;
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].SetBulletPosition(Position);
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].ShootAt(gameTime, enemyManager.enemy[0].Position, Bullets1[i].GetPosition() + Bullets1[i].GetVelocity());

for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].Update();
}


Edited by FantasyVII, 29 October 2012 - 08:49 AM.

Posted 29 October 2012 - 12:06 PM

Dude... think through this for a second.
• There can be 0..n bullets in the game
• When a bullet is fired, you create a bullet object and put it in a list of "active bullets"
• When a bullet object is created, you give it a pointer to the enemy that it is tracking
• On update, iterate through the list of bullets
• For each bullet, you steer toward the location of the enemy it is tracking
• When the bullet leaves the game, you remove it from the list
• ???
• Profit!!

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #8FantasyVII  Members

Posted 29 October 2012 - 02:14 PM

I know. I did that exact thing. But I need a way to make every bullet separate from each other and make each bullet behaive differently. The problem is I go through all the bullets in the List and I update them and change their position. I need to update each bullet separately. I need a way to keep track of each bullet in the list. And thats what i don't know how to do. going to try this again tonight.

Posted 30 October 2012 - 08:02 AM

Nooo.... you didn't do that exact thing. What I'm saying is make a bullet class. Each bullet you fire is a new object of that class. Each of these objects has its own data. That data includes the target that it is intended for (among other things). Therefore, each bullet object has, in its own data, where it is supposed to be going. It doesn't matter if you have 1, 10, 100, or 1000 bullets... each one will have its own target embedded in it. As you iterate through the list, you take each bullet's position and update it towards THAT bullet's target.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #10FantasyVII  Members

Posted 30 October 2012 - 10:21 AM

Nooo.... you didn't do that exact thing. What I'm saying is make a bullet class. Each bullet you fire is a new object of that class. Each of these objects has its own data. That data includes the target that it is intended for (among other things). Therefore, each bullet object has, in its own data, where it is supposed to be going. It doesn't matter if you have 1, 10, 100, or 1000 bullets... each one will have its own target embedded in it. As you iterate through the list, you take each bullet's position and update it towards THAT bullet's target.

Thank you very much. Sorry for being such a newbie

It worked

If can give more than 1 Reputation, I would stay all night pressing that button

thank you again

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.