/// <summary> /// Creates a ship at a random location in the world in a random direction /// </summary> public void Respawn(Ship ship) { // find random empty location in the world FindRandomLocation(ship); // make a new normalized direction vector pointing up Vector2D dir = new Vector2D(0, -1); dir.Normalize(); // make a randomizing object Random r = new Random(); int randAngle = r.Next(0, 360); //rotate by random angle dir.Rotate(randAngle); // sets a random direction for the ship ship.SetDirection(dir); // reset ship's velocity ship.SetVelocity(new Vector2D(0, 0)); // if dead ship is king if (ship.IsKing()) { // hit points go up beacuse ship is now king ship.SetHP(startingHitPoints + 3); } else { // reset the ship's health to original health ship.SetHP(startingHitPoints); } // reset ships death timer ship.ResetRespawnTimer(); }
/// <summary> /// Computes the acceleration, velocity, and position of the passed in ship /// </summary> public void MotionForShips(Ship ship) { // if the ship isn't alive, just skip it if (!ship.IsAlive()) { return; } // handle left turn command if (ship.TurnLeft) { ship.GetDirection().Rotate(-turningRate); ship.TurnLeft = false; } // handle right turn command else if (ship.TurnRight) { ship.GetDirection().Rotate(turningRate); ship.TurnRight = false; } // spawn a projectile if projectile command has been given if (ship.FireProjectile) { Projectile p = new Projectile(ship.GetID(), projectileID++, ship.GetLocation(), ship.GetDirection()); AddProjectile(p); ship.FireProjectile = false; ship.ResetFireTimer(); // we just fired a projectile, increment total number of fired projectiles ship.ShotsFired++; } //get a zero vector Vector2D acceleration = new Vector2D(0.0, 0.0); //compute the acceleration caused by the star foreach (Star star in stars.Values) { Vector2D g = star.GetLocation() - ship.GetLocation(); g.Normalize(); acceleration = acceleration + g * star.GetMass(); } if (ship.HasThrust()) { //compute the acceleration due to thrust Vector2D t = new Vector2D(ship.GetDirection()); t = t * engineStrength; acceleration = acceleration + t; } // recalculate velocity and location ship.SetVelocity(ship.GetVelocity() + acceleration); ship.SetLocation(ship.GetVelocity() + ship.GetLocation()); // check to see if ship is off screen Wraparound(ship); // check for collisions with any star foreach (Star star in stars.Values) { CollisionWithAStar(ship, star); } // check for collisions with any projectiles foreach (Projectile proj in projectiles.Values) { CollisionWithAProjectile(ship, proj); } ship.IncrementFireTimer(); }