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.
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());
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.
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?")
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.
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();
}
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.
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