/// <summary> /// Returns whether a given vector is within the world. If not, adjusts the vector to be in the world. /// </summary> /// <param name="location">The vector location (of a ship or projectile)</param> /// <returns>whether the vector was changed</returns> private bool WrapLocation(ref Vector2D location) { double xFromZero = Math.Abs(location.GetX()); double newX = location.GetX(); double yFromZero = Math.Abs(location.GetY()); double newY = location.GetY(); bool locIsOutOfThisWorld = false; double xOverhang = xFromZero - (size / 2); if (xOverhang > 0) { locIsOutOfThisWorld = true; newX = (size / 2) - xOverhang; // now on right side if (location.GetX() > 0) // started on right side { newX *= -1; // now on left side } } double yOverhang = yFromZero - (size / 2); if (yOverhang > 0) { locIsOutOfThisWorld = true; newY = (size / 2) - yOverhang; // now on right side if (location.GetY() > 0) // started on right side { newY *= -1; // now on left side } } if (locIsOutOfThisWorld) { location = new Vector2D(newX, newY); } return(locIsOutOfThisWorld); }
/// <summary> /// Returns true if the two vectors are within the given radius of each other /// otherwise returns false /// </summary> private bool WithinARadius(Vector2D location1, Vector2D location2, int radius) { double xDifference = location1.GetX() - location2.GetX(); double yDifference = location1.GetY() - location2.GetY(); double distanceBetweenVectors = Math.Sqrt(xDifference * xDifference + yDifference * yDifference); if (distanceBetweenVectors < radius) { return(true); } return(false); }
/// <summary> /// Creates a projectile with the same location as the star which fired it, and adds it to the world. Orientation must be provided as stars do not have an orientation. /// </summary> /// <param name="currentStar">star which fired this projectile</param> /// <param name="dir">orientation of the projectile</param> public void SpawnProjectile(Star currentStar, Vector2D dir) { // Need to adjust spawn location Y-axis Vector2D projLocation = new Vector2D(currentStar.GetLocation().GetX(), currentStar.GetLocation().GetY()); Vector2D projDir = new Vector2D(dir); Vector2D projVeloc = new Vector2D(dir.GetX() * ProjVelocity / 3, dir.GetY() * ProjVelocity / 3); Projectile newProj = new Projectile(projIDs, projLocation, projDir, true, int.MaxValue); // hard coding this, bad design but... yeah... newProj.SetVelocity(projVeloc); this.addProjectile(newProj); projIDs++; }
/// <summary> /// Converts the given world-space based vector to an image-space based point. /// </summary> /// <param name="vector">The world-space vector to convert.</param> /// <returns>A new point containing the converted vector coordinates.</returns> private Point WorldVectorToImagePoint(Vector2D vector) { // Convert the world coordinates to screen coordinates by adding half the size of the screen. return(new Point((int)(vector.GetX() * _scaleMultipliers[0]) + Width / 2, (int)(vector.GetY() * _scaleMultipliers[1]) + Height / 2)); }
/// <summary> /// This method is invoked when the DrawingPanel needs to be re-drawn /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { if (theWorld != null) { lock (this.theWorld) { // Draw the ships foreach (Ship ship in this.theWorld.GetShips().Values) { Vector2D shipLoc = ship.GetLocation(); Vector2D shipDir = ship.GetDirection(); if (shipDir == null) { DrawObjectWithTransform(e, ship, this.Size.Width, shipLoc.GetX(), shipLoc.GetY(), 0, new ObjectDrawer(ShipDrawer)); DrawObjectWithTransform(e, ship, this.Size.Width, shipLoc.GetX(), shipLoc.GetY(), 0, ShipInfoDrawer); } else { if (ship.GetHealth() == 0) { DrawObjectWithTransform(e, ship, this.Size.Width, shipLoc.GetX(), shipLoc.GetY(), 0, ShipDrawer); } else { DrawObjectWithTransform(e, ship, this.Size.Width, shipLoc.GetX(), shipLoc.GetY(), shipDir.ToAngle(), ShipDrawer); } DrawObjectWithTransform(e, ship, this.Size.Width, shipLoc.GetX(), shipLoc.GetY(), 0, ShipInfoDrawer); } } // Draw the projectiles foreach (Projectile projectile in this.theWorld.GetProjectile().Values) { Vector2D proLoc = projectile.GetLocation(); Vector2D proDir = projectile.GetDirection(); if (proDir == null) { DrawObjectWithTransform(e, projectile, this.Size.Width, proLoc.GetX(), proLoc.GetY(), 0, new ObjectDrawer(this.ProjectileDrawer)); } else { DrawObjectWithTransform(e, projectile, this.Size.Width, proLoc.GetX(), proLoc.GetY(), proDir.ToAngle(), new ObjectDrawer(this.ProjectileDrawer)); } } // Draw the projectiles foreach (Star star in this.theWorld.GetStar().Values) { Vector2D proLoc = star.GetLoc(); DrawObjectWithTransform(e, star, this.Size.Width, proLoc.GetX(), proLoc.GetY(), 0, new ObjectDrawer(this.StarDrawer)); } } } // Do anything that Panel (from which we inherit) needs to do base.OnPaint(e); }