/// <summary> /// Uses vector math to update the ship's location. /// </summary> /// <param name="shippyUpdate">Ship to be updated.</param> /// <param name="starry">star for gravity</param> private void updateShipLocation(ship shippyUpdate, star starry) { Vector2D gravity = starry.loc - shippyUpdate.loc; // Find the vector from the ship to the star. gravity.Normalize(); // Turn the vector into a unit-length direction by normalizing. gravity *= starry.mass; // Adjust strentth of vector by multiplying star's mass. Vector2D thrust; //We need to only do this thrus if the ship is thrusting, otherwise the star will only have the impact on the ship if (shippyUpdate.getThrust() == true) { thrust = shippyUpdate.GetDirections(); // So long as rotate is only ever applied, should always be normalized. thrust = thrust * (engineStrength); // Adjust the length of the vector by multiplying by the engine strength. shippyUpdate.setThrust(false); } else { thrust = new Vector2D(0, 0); // no thrust for you. } Vector2D acceleration = gravity + thrust; // combine all forces. shippyUpdate.velocity += acceleration; // Add acceleration to velocity. shippyUpdate.loc = shippyUpdate.loc + shippyUpdate.velocity; // Apply new velocity to position. checkIfOffScreen(shippyUpdate); // Wraparound if offscreen. }
public void GetDirectionY() { World world = new World(); Socket socket; IPAddress ip; networking.MakeSocket("localhost", out socket, out ip); SocketState ss = new SocketState(socket); ship sh = new ship(1, ss, 5, new Vector2D(8, 8), "JoCee", 0, new Vector2D(8, 9), 7); Assert.AreEqual(sh.GetDirections().GetY(), 9); }
// This method is invoked when the DrawingPanel needs to be re-drawn /// <summary> /// Draws all the players, stars and projectiles each time the form is invalidated. /// The lock is used so cross-threads can't screw it up! /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { // Draw the players // Draw the stars // Draw the projectiles. lock (world) { foreach (var shipper in world.playersInWorld.ToList()) { ship p = shipper.Value as ship; if (p.hp > 0) // If the HP is above zero, the ship deserves to be drawn. { DrawObjectWithTransform(e, p, world.worldPanel.Size.Width, p.GetLocation().GetX(), p.GetLocation().GetY(), p.GetDirections().ToAngle(), shipDrawer); } } foreach (var starry in world.starsInWorld.ToList()) { star p = starry.Value as star; DrawObjectWithTransform(e, p, world.worldPanel.Size.Width, p.GetLocation().GetX(), p.GetLocation().GetY(), 0, starDrawer); } foreach (var proj in world.projectilesInWorld.ToList()) { projectile p = proj.Value as projectile; if (p.alive == true) { DrawObjectWithTransform(e, p, world.worldPanel.Size.Width, p.GetLocation().GetX(), p.GetLocation().GetY(), p.GetDirections().ToAngle(), projectileDrawer); } else // If it's dead, can be removed from the projectile list. { world.projectilesInWorld.Remove(p.ID); } } } // Do anything that Panel (from which we inherit) needs to do base.OnPaint(e); }