Sign in to follow this  
FantasyVII

[Solved]Tower which enemy to shoot at?

Recommended Posts

FantasyVII    1073
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:-

[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;
}
}
}
[/CODE]


[CODE]
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);
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[i].ShootAt(gameTime,enemies[ShootAtEnemyIndex].Position, bulletManager.Bullets[i].GetPosition() + bulletManager.Bullets[i].GetVelocity());

bulletManager.Update();
}
[/CODE] Edited by FantasyVII

Share this post


Link to post
Share on other sites
FantasyVII    1073
[quote name='IADaveMark' timestamp='1351317769' post='4994351']
So what is your problem?
[/quote]

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.

Share this post


Link to post
Share on other sites
rpiller    839
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.

Share this post


Link to post
Share on other sites
IADaveMark    3731
[quote name='rpiller' timestamp='1351351644' post='4994433']
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.
[/quote]

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?")

Share this post


Link to post
Share on other sites
FantasyVII    1073
[quote name='rpiller' timestamp='1351351644' post='4994433']
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.
[/quote]
I just can't get my mind around it. Starting to hate this project because of this.

[quote name='IADaveMark' timestamp='1351358051' post='4994458']
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?")
[/quote]

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.

[CODE]
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;
[/CODE]

And I do this 10 times which is very very wrong and bad and messy.
[CODE]
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[i].Initialize(graphics);
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].LoadContent(Content);
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();
}
[/CODE] Edited by FantasyVII

Share this post


Link to post
Share on other sites
IADaveMark    3731
Dude... think through this for a second.
[list]
[*]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!!
[/list]

Share this post


Link to post
Share on other sites
FantasyVII    1073
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.

Share this post


Link to post
Share on other sites
IADaveMark    3731
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.

Share this post


Link to post
Share on other sites
FantasyVII    1073
[quote name='IADaveMark' timestamp='1351605753' post='4995410']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this