Reputation: 63
I have the following method:
private void TryShoot(Tower t)
{
double x1 = (GameMap.GetMapSquare(t).X * TILE_SIZE) + (TILE_SIZE / 4); // center location of square
double y1 = (GameMap.GetMapSquare(t).Y * TILE_SIZE) + (TILE_SIZE / 4);
foreach (Monster m in GameWave.Sent)
{
double x2 = GameMap.Path[m.PathLocation].X * TILE_SIZE + (TILE_SIZE / 4); // center location of monster
double y2 = GameMap.Path[m.PathLocation].Y * TILE_SIZE + (TILE_SIZE / 4);
double distance = Math.Sqrt((Math.Pow((x2 - x1), 2)) + (Math.Pow((y2 - y1), 2)));
if (Math.Abs(distance) < t.Range)
{
// TODO: Shoot logic
SpriteBatch.Draw(BulletTexture, new Rectangle((int)x1, (int)y1, 32, 32), Color.White);
SpriteBatch.Draw(BulletTexture, new Rectangle((int)x1, (int)y1, 32, 32), Color.White);
t.Shoot(m);
if (m.Health <= 0)
{
Player.Gold += m.Worth;
GameWave.Monsters.Remove(m);
}
break;
}
}
}
Right after that TODO statement is where the shooting projectile logic should happen.
x1
and y1
is the location of the center square of the mapSquare object.
x2
and y2
is the location of the center square of the Enemy position on the mapSquare object.
This is a bird sprite being hit by a "bullet" that's being created at the tower, and also where that bullet is hitting.
It's ugly with a bunch of If else statements and while loops to do checking. There has to be a more efficient way of doing it, and if so, how? I want to simulate a bullet moving from point A to point B (the while loop should work in theory but it's still just teleporting and not moving)
http://pastebin.com/4P05XgaD (ugly code)
Thanks!
Upvotes: 1
Views: 977
Reputation: 2017
Do not mix update and draw functions. To fix your problem, you need 4 functions.
Init Bullet : simple function that will init a bullet and add it to bullet list (start position, velocity, isEnemyBullet, bullet type...) this one is called single time, only when enemy or player shoot bullet.
Update Bullet : funciton that is called and loop over bullet list and increase position by velocity
Draw Bullet : function that loop over bullets and draw them.
Collision Test: function that loop over enemies and bullets and check collision
Upvotes: 1