/// <summary> /// When a ship is hit by a projectile then remove the a health point remove /// the projectile form the world /// </summary> private void HitAProjectile(Ship ship, Projectile projectile) { if (kingIsOn) { // find the owner of the projectile int shipID = projectile.GetOwner(); if (allShips.TryGetValue(shipID, out Ship projectileShip)) { // if the projectile is from the king or the ship is the king then... if (ship.IsKing() || projectileShip.IsKing()) { // subtract a health point from the ship ship.SetHP(ship.GetHP() - 1); if (!ship.IsAlive()) { // increase the ship's score who shot the projectile by 1 projectileShip.SetScore(projectileShip.GetScore() + 1); } // register that the projectile hit a non-team member ship.ShotsHit++; } if (ship.IsKing() && !ship.IsAlive()) { // make sure that the ship that died is no longer king ship.SetKing(false); ship.SetName(ship.GetName().Substring(5)); // select a new king Ship newKingShip = RandomShip(); newKingShip.SetKing(true); newKingShip.SetName("King " + newKingShip.GetName()); } } // set the projectile to dead projectile.Alive(false); } else { // subtract a health point ship.SetHP(ship.GetHP() - 1); if (!ship.IsAlive()) { // find the owner of the projectile int shipID = projectile.GetOwner(); allShips.TryGetValue(shipID, out Ship projectileShip); // increase the ship's score who shot the projectile by 1 projectileShip.SetScore(projectileShip.GetScore() + 1); } // set the projectile to dead projectile.Alive(false); // register that the projectile hit a ship ship.ShotsHit++; } }
/// <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> /// 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> /// 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 ProjectileDrawer(object o, PaintEventArgs e) { Projectile proj = o as Projectile; Bitmap projSprite; int x, y; Point p; x = WorldSpaceToImageSpace(this.Size.Width, (int)proj.GetLocation().GetX() - (PROJECTILE_SIZE.Width / 2)); y = WorldSpaceToImageSpace(this.Size.Width, (int)proj.GetLocation().GetY() - (PROJECTILE_SIZE.Height / 2)); p = new Point(x, y); if (theWorld.GetMarioMode() && proj.GetOwner() == theWorld.GetCurrentPlayer()) { projSprite = marioFireball; } else { projSprite = projSprites[proj.GetOwner() % projSprites.Count]; } e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.DrawImage(projSprite, p); }
/// <summary> /// Acts as a drawing delegate for DrawObjectWithTransform /// After performing the necessary transformation (translate/rotate) /// DrawObjectWithTransform will invoke this method /// Draws a projectile accroding to the projectile images in the LoadImages method. /// </summary> /// <param name="o">The object to draw</param> /// <param name="e">The PaintEventArgs to access the graphics</param> private void ProjectileDrawer(object o, PaintEventArgs e) { Projectile p = o as Projectile; int width = 20; int height = 20; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; // Rectangles are drawn starting from the top-left corner. // So if we want the rectangle centered on the projectiles's location, we have to offset it // by half its size to the left (-width/2) and up (-height/2) Rectangle r = new Rectangle(-(width / 2), -height, width, height); int colorID = (p.GetOwner() % 8) + 16; e.Graphics.DrawImage(shipImages[colorID], r); }