/// <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> /// Detects whether or not a ship is hit by a projectile /// </summary> private void CollisionBetweenAStarAndProjectile(Star star, Projectile projectile) { if (WithinARadius(star.GetLocation(), projectile.GetLocation(), starSize)) { // the passed in projectile hit a star so we remove it from the world projectile.Alive(false); } }
//******************** 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); } }
/// <summary> /// Spawns a star using the properties of an existing star. Used for respawning existing stars which have been killed (boss mode only.) Spawn location is retained from initial star. /// </summary> /// <param name="star">star to be respawned</param> public void respawnStar(Star star) { Vector2D newLoc = new Vector2D(star.GetLocation()); Star newStar = new Star(star.GetID(), newLoc, star.GetMass()); // Set ship's modifiable variables newStar.SetHP(BossHealth); this.addStar(newStar); }
/// <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 projectile and star are within collision distance, otherwise false. /// </summary> /// <param name="star"></param> /// <param name="proj"></param> /// <returns></returns> public bool HasCollidedProjStar(Star star, Projectile proj) { Vector2D starLoc = star.GetLocation(); Vector2D projLoc = proj.GetLocation(); Vector2D distanceVector = starLoc - projLoc; double distanceLength = distanceVector.Length(); if (distanceLength <= StarSize) { 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); }
/// <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 StarDrawer(object o, PaintEventArgs e) { Star star = o as Star; Bitmap starSprite; int x, y; Point p; x = WorldSpaceToImageSpace(this.Size.Width, (int)star.GetLocation().GetX()); y = WorldSpaceToImageSpace(this.Size.Width, (int)star.GetLocation().GetY()); p = new Point(x - (STAR_SIZE.Width / 2), y - (STAR_SIZE.Height / 2)); if (theWorld.GetMarioMode()) { starSprite = marioStar; } else { starSprite = starSprites[star.GetID() % starSprites.Count]; } e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.DrawImage(starSprite, p); }
public void TestUpdatingStarLocationInWorldWithNewStar() { World testWorld = new World(); Vector2D newStarLoc = new Vector2D(50, 50); testWorld.SpawnStars(0, 0, 1); Star firstFrame = (Star)testWorld.getStars()[0]; double xStarFirstFrame = firstFrame.GetLocation().GetX(); double yStarFirstFrame = firstFrame.GetLocation().GetY(); Star secondFrame = new Star(0, newStarLoc, 1); testWorld.addStar(secondFrame); Assert.IsTrue(testWorld.getStars().Count == 1); Star sameStar = (Star)testWorld.getStars()[0]; double xStarSecondFrame = sameStar.GetLocation().GetX(); double yStarSecondFrame = sameStar.GetLocation().GetY(); Assert.IsTrue(xStarFirstFrame != xStarSecondFrame); Assert.IsTrue(yStarFirstFrame != yStarSecondFrame); }
private bool CollidedWith(Projectile proj, Star star) { Vector2D distance = proj.GetLocation() - star.GetLocation(); return(distance.Length() < starRadius); }
private bool CollidedWith(Star star, Ship ship) { Vector2D distance = star.GetLocation() - ship.GetLocation(); return(distance.Length() < (starRadius + shipRadius)); }