public void Rotate(int num, double turnRate) { if (dir.Length() != 0) { dir.Normalize(); } dir.Rotate(num * turnRate); }
/// <summary> /// Calculates gravity between a ship and a star. /// </summary> /// <returns>a Vector2D gravity</returns> private Vector2D CalculateForce(Ship ship, Star star) { // get direction Vector2D g = star.GetLocation() - ship.GetLocation(); g.Normalize(); // get magnitude g *= star.GetSize(); return(g); }
/// <summary> /// Spawns any new or respawning ships in an empty location /// </summary> private void SpawnShips() { // Check every ship. foreach (var ship in _world.GetComponents <Ship>()) { // Ignore alive ships if (ship.Health > 0) { continue; } // Check if the ship is waiting to respawn. if (ship.RespawnFrames > 0) { // Decrease the frames counter by 1. ship.RespawnFrames--; continue; } // Check if the ship has a connected client if (!_clients.TryGetValue(ship.Id, out var _)) { continue; } // Respawn the ship, since it is dead but its frame counter is 0. // Compute a spawn location for the ship. var spawnLocation = _world.FindShipSpawnLocation(Configuration.StarCollisionRadius, Configuration.ShipCollisionRadius); ship.Location = spawnLocation; // Compute a random direction for the ship. var random = new Random(); var spawnDirection = new Vector2D((random.NextDouble() * 1 - 0.5) * 2, (random.NextDouble() * 1 - 0.5) * 2); spawnDirection.Normalize(); ship.Direction = spawnDirection; // Restore the ship's health. ship.Health = Configuration.ShipHitpoints; } }
/// <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> /// Creates a new ship with the given ship name and id and located at /// the specified location and orientation /// </summary> public Ship(String shipName, int id, Vector2D location, Vector2D orientation, int startingHitPoints, int respawnDelay, int fireDelay) { ID = id; name = shipName; loc = location; dir = orientation; dir.Normalize(); thrust = false; hp = startingHitPoints; score = 0; respawnLimit = respawnDelay; respawnTimer = 0; fireTimer = fireDelay; fireLimit = fireDelay; fireRequest = false; // new information needed keep player stats in db ShotsFired = 0; ShotsHit = 0; }
/// <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(); }
/// <summary> /// Sets the direction of the projectile /// </summary> public void SetDirection(Vector2D direction) { direction.Normalize(); dir = direction; }