🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[Solved]Tower which enemy to shoot at?

Started by
8 comments, last by FantasyVII 11 years, 8 months ago
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.Position.X - TowerPosition.X;
Distance.Y = enemies.Position.Y - TowerPosition.Y;
DistanceSquare = (Distance.X * Distance.X) + (Distance.Y * Distance.Y);
if (DistanceSquare <= Radius * Radius)
{
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.ShootAt(gameTime,enemies[ShootAtEnemyIndex].Position, bulletManager.Bullets.GetPosition() + bulletManager.Bullets.GetVelocity());

bulletManager.Update();
}
Advertisement
So what is your problem?

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
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!"


So what is your problem?


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.
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.

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-founder and 10 year advisor of the GDC AI Summit
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!"


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))
{
Bullets1.Add(new Bullet(Damage));
for (int i = 0; i < Bullets1.Count; i++)
Bullets1.Initialize(graphics);
for (int i = 0; i < Bullets1.Count; i++)
Bullets1.LoadContent(Content);
LastTime = Time;
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1.SetBulletPosition(Position);
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1.ShootAt(gameTime, enemyManager.enemy[0].Position, Bullets1.GetPosition() + Bullets1.GetVelocity());

for (int i = 0; i < Bullets1.Count; i++)
Bullets1.Update();
}
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-founder and 10 year advisor of the GDC AI Summit
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!"

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.
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-founder and 10 year advisor of the GDC AI Summit
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!"


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 :D

thank you again :)

This topic is closed to new replies.

Advertisement