public void TestShipVelocityWith10000FramesOfThrust() { World testWorld = new World(); testWorld.ShipAccel = 0.08; testWorld.SpawnShip(1, "testname"); Ship testShip = (Ship)testWorld.getPlayers()[1]; double xShipStart = testShip.GetLocation().GetX(); double yShipStart = testShip.GetLocation().GetY(); double xShipVel = testShip.GetVelocity().GetX(); double yShipVel = testShip.GetVelocity().GetY(); double xShip = testShip.GetLocation().GetX(); double yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0); Assert.IsTrue(xShipStart == xShip); Assert.IsTrue(yShipStart == yShip); for (int i = 0; i < 10000; i++) { testShip.Thrust(); } xShipVel = testShip.GetVelocity().GetX(); yShipVel = testShip.GetVelocity().GetY(); Assert.IsTrue(xShipVel == 0); Assert.AreEqual(yShipVel, 0.08 * -10000, 0.000001); }
/// <summary> /// Acts as a drawing delegate for DrawObjectWithTransform /// After performing the necessary transformation (translate/rotate) /// DrawObjectWithTransform will invoke this method /// </summary> /// <param name="o">The object to draw</param> /// <param name="e">The PaintEventArgs to access the graphics</param> private void ShipDrawer(object o, PaintEventArgs e) { Ship ship = o as Ship; Bitmap shipSprite; int x, y; Point p; x = WorldSpaceToImageSpace(this.Size.Width, (int)ship.GetLocation().GetX() - (SHIP_SIZE.Width / 2)); y = WorldSpaceToImageSpace(this.Size.Width, (int)ship.GetLocation().GetY() - (SHIP_SIZE.Height / 2)); p = new Point(x, y); if (theWorld.GetMarioMode() && ship.GetID() == theWorld.GetCurrentPlayer()) { shipSprite = marioShip; } else { if (ship.GetThrust()) { shipSprite = shipSpritesThrust[ship.GetID() % shipSprites.Count]; } else { shipSprite = shipSprites[ship.GetID() % shipSprites.Count]; } } e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.DrawImage(shipSprite, p); }
/// <summary> /// Updates the the ships dictionary to contain the given ship /// </summary> /// <param name="ship"></param> public void Update(Ship ship) { int id = ship.GetShipID(); if (ship.GetHp() == 0) { Ships.Remove(ship.GetShipID()); if (!Explosions.ContainsKey(ship.GetShipID())) { Explosions.Add(id, new Explosion(id, ship.GetLocation().GetX(), ship.GetLocation().GetY())); } foreach (Ship s in sortedPlayerList) { if (s.GetShipID() == ship.GetShipID()) { s.UpdateShip(ship); } } } else { int oldScore = -1; if (Explosions.ContainsKey(id) && Explosions[id].Dead) { Explosions.Remove(id); } bool found = false; foreach (Ship s in sortedPlayerList) { if (s.GetShipID() == ship.GetShipID()) { found = true; oldScore = s.GetScore(); s.UpdateShip(ship); break; } } Ships[id] = ship; if (!found) { sortedPlayerList.Add(Ships[ship.GetShipID()]); sortedPlayerList.Sort((a, b) => b.GetScore().CompareTo(a.GetScore())); } if (found && (ship.GetScore() > oldScore)) { sortedPlayerList.Sort((a, b) => b.GetScore().CompareTo(a.GetScore())); } } }
//******************** World methods for the collisions ********************// /// <summary> /// Checks to see if the passed in ship touched a star /// </summary> private void CollisionWithAStar(Ship ship, Star star) { if (WithinARadius(ship.GetLocation(), star.GetLocation(), shipSize + starSize)) { // the passed in ship hit a star so we update the ship's health HitAStar(ship); } }
public void TestProjectileVelocity() { World testWorld = new World(); testWorld.SpawnShip(1, "testname"); Ship testShip = (Ship)testWorld.getPlayers()[1]; Assert.IsTrue(testWorld.getProjs().Count == 0); testWorld.SpawnProjectile(testShip); Projectile testProj = ((Dictionary <int, Projectile>)testWorld.getProjs())[0]; double xProj = testProj.GetLocation().GetX(); double yProj = testProj.GetLocation().GetY(); double xShip = testShip.GetLocation().GetX(); double yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xProj == xShip); Assert.IsTrue(yProj == yShip); testWorld.updateProjectiles(); xProj = testProj.GetLocation().GetX(); yProj = testProj.GetLocation().GetY(); Assert.IsTrue(xProj == xShip); Assert.IsTrue(yProj == yShip); testProj.SetVelocity(new Vector2D(1, 2)); testWorld.updateProjectiles(); xProj = testProj.GetLocation().GetX(); yProj = testProj.GetLocation().GetY(); Assert.IsTrue(xProj == xShip + 1); Assert.IsTrue(yProj == yShip + 2); }
/// <summary> /// Detects whether or not a ship is hit by a projectile /// </summary> private void CollisionWithAProjectile(Ship ship, Projectile projectile) { if (ship.GetID() != projectile.GetOwner() && ship.IsAlive()) { if (WithinARadius(ship.GetLocation(), projectile.GetLocation(), shipSize)) { // the passed in ship was hit by a projectile so we update health and remove // the projectile from the world HitAProjectile(ship, projectile); } } }
/// <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> /// Returns true if ship and projectile are within collision distance, otherwise false. /// </summary> /// <param name="ship"></param> /// <param name="proj"></param> /// <returns></returns> public bool HasCollidedShipProj(Ship ship, Projectile proj) { Vector2D shipLoc = ship.GetLocation(); Vector2D projLoc = proj.GetLocation(); Vector2D distanceVector = shipLoc - projLoc; double distanceLength = distanceVector.Length(); if (distanceLength <= ShipSize && (ship.GetID() != proj.GetOwner())) { return(true); } return(false); }
/// <summary> /// Returns true if ship and star are within collision distance, otherwise false. /// </summary> /// <param name="ship"></param> /// <param name="star"></param> /// <returns></returns> public bool HasCollidedShipStar(Ship ship, Star star) { Vector2D shipLoc = ship.GetLocation(); Vector2D starLoc = star.GetLocation(); Vector2D distanceVector = shipLoc - starLoc; double distanceLength = distanceVector.Length(); if (distanceLength <= StarSize) { return(true); } return(false); }
public void TestUpdatingShipLocationInWorldWithNewShip() { World testWorld = new World(); testWorld.SpawnShip(1, "testName"); Ship firstFrame = (Ship)testWorld.getPlayers()[1]; double xShipFirstFrame = firstFrame.GetLocation().GetX(); double yShipFirstFrame = firstFrame.GetLocation().GetY(); Ship secondFrame = new Ship(1, firstFrame.GetLocation() + firstFrame.GetLocation(), firstFrame.GetOrientation(), false, "testName", 5, 0); testWorld.addShip(secondFrame); Assert.IsTrue(testWorld.getPlayers().Count == 1); Ship sameShip = (Ship)testWorld.getPlayers()[1]; double xShipSecondFrame = sameShip.GetLocation().GetX(); double yShipSecondFrame = sameShip.GetLocation().GetY(); Assert.IsTrue(xShipFirstFrame != xShipSecondFrame); Assert.IsTrue(yShipFirstFrame != yShipSecondFrame); }
/// <summary> /// Creates a projectile with the same location and orientation of the ship which fired it, and adds it to the world. Then tells the ship that is projectile has been fired. /// </summary> /// <param name="currentShip">ship which fired this projectile</param> public void SpawnProjectile(Ship currentShip) { if (currentShip.ReadyToFire()) { // Need to adjust spawn location Y-axis Vector2D projLocation = new Vector2D(currentShip.GetLocation().GetX(), currentShip.GetLocation().GetY()); Vector2D projDir = new Vector2D(currentShip.GetOrientation()); Vector2D projVeloc = new Vector2D(currentShip.GetOrientation().GetX() * ProjVelocity, currentShip.GetOrientation().GetY() * ProjVelocity); Projectile newProj = new Projectile(projIDs, projLocation, projDir, true, currentShip.GetID()); // May want to shift projectile newProj.SetVelocity(projVeloc); this.addProjectile(newProj); projIDs++; lock (shipDictionary) { this.shipDictionary[currentShip.GetID()].FireProjectile(); } } }
public void TestShipVelocityWith1FrameOfThrust() { World testWorld = new World(); testWorld.ShipAccel = 0.08; testWorld.SpawnShip(1, "testname"); Ship testShip = (Ship)testWorld.getPlayers()[1]; double xShipStart = testShip.GetLocation().GetX(); double yShipStart = testShip.GetLocation().GetY(); double xShipVel = testShip.GetVelocity().GetX(); double yShipVel = testShip.GetVelocity().GetY(); double xShip = testShip.GetLocation().GetX(); double yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0); Assert.IsTrue(xShipStart == xShip); Assert.IsTrue(yShipStart == yShip); testShip.Thrust(); xShipVel = testShip.GetVelocity().GetX(); yShipVel = testShip.GetVelocity().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0.08 * -1); testWorld.updateShips(); xShip = testShip.GetLocation().GetX(); yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipStart + xShipVel == xShip); Assert.IsTrue(yShipStart + yShipVel == yShip); }
/// <summary> /// Checks to see if the passed in ship is on the edge of the world /// </summary> private void Wraparound(Ship ship) { int borderCoordinate = universeSize / 2; if (Math.Abs(ship.GetLocation().GetX()) >= borderCoordinate) { // times x by -1 double x = ship.GetLocation().GetX() * -1; double y = ship.GetLocation().GetY(); // set the ship's new location ship.SetLocation(x, y); } if (Math.Abs(ship.GetLocation().GetY()) >= borderCoordinate) { // times y by -1 double y = ship.GetLocation().GetY() * -1; double x = ship.GetLocation().GetX(); // set the ship's new location ship.SetLocation(x, y); } }
/// <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(); }
private bool CollidedWith(Projectile proj, Ship ship) { Vector2D distance = proj.GetLocation() - ship.GetLocation(); return(distance.Length() < shipRadius); }
private bool CollidedWith(Star star, Ship ship) { Vector2D distance = star.GetLocation() - ship.GetLocation(); return(distance.Length() < (starRadius + shipRadius)); }