private void CreateNewAsteroid(int Size, Vector2 Position) { Particle2D AstInfo; Vector2 AstSpeed; AstInfo = new Particle2D(GraphicsDevice); AstInfo.Width = Size; AstInfo.Height = Size; if ((Position.X == -1) && (Position.Y == -1)) { AstInfo.TopLeft.X = AstInfo.Width * -1; AstInfo.TopLeft.Y = AstInfo.Height * -1; } else { AstInfo.TopLeft.X = Position.X; AstInfo.TopLeft.Y = Position.Y; } AstInfo.Image = cTextureDict[Textures.Asteroid]; AstInfo.TimeToLive = -1; AstSpeed = MGMath.CalculateXYMagnitude((float)cRandom.NextDouble() * 6.28318531f, 2); AstInfo.SpeedX = AstSpeed.X; AstInfo.SpeedY = AstSpeed.Y; AstInfo.Tint = new Color(150 + cRandom.Next(0, 105), 150 + cRandom.Next(0, 105), 150 + cRandom.Next(0, 105), 255); AstInfo.SpeedRotate = ((float)cRandom.NextDouble() * 0.2f) - 0.1f; cAsteroids.AddParticle(AstInfo); }
private void CreateParticleBurst(Vector2 Position, int Count, int Size, Color Tint, Texture2D Image) { Particle2D NewSparkle; Vector2 Speed; for (int Ctr = 0; Ctr < Count; Ctr++) { NewSparkle = new Particle2D(GraphicsDevice); NewSparkle.AlphaFade = true; NewSparkle.TimeToLive = 100 + (cRandom.NextDouble() * 1000); NewSparkle.Height = Size; NewSparkle.Width = Size; NewSparkle.TopLeft = Position; NewSparkle.Image = Image; NewSparkle.Rotation = (float)(cRandom.NextDouble() * 6.2f); Speed = MGMath.CalculateXYMagnitude(NewSparkle.Rotation, (float)(cRandom.NextDouble() * (70 / Size))); NewSparkle.SpeedX = Speed.X; NewSparkle.SpeedY = Speed.Y; NewSparkle.SpeedRotate = (float)cRandom.NextDouble() * 0.25f; NewSparkle.Tint = Tint; cSparkles.AddParticle(NewSparkle); } }
protected override void MouseEventButtonDown(MouseState CurrMouse, MouseButton Button) { int Ctr; if (Button != MouseButton.Left) { return; } cMouseDown = CurrMouse.Position; for (Ctr = 0; Ctr < cOptionList.Count; Ctr++) { if (MGMath.IsPointInRect(cMouseDown, cOptionLocList[Ctr]) == true) { cMouseCardIndex = Ctr; cOptionList[Ctr].Include += 1; if (cOptionList[Ctr].Include > cOptionList[Ctr].Count) { cOptionList[Ctr].Include = 0; } break; } } HasChanged = true; }
private void PlayerFireBullet() { Vector2 BulletOrigin, BulletOffset; int Ctr; Color BulletColor = new Color(75, 75, 255, 255); if (cEnemyKills >= 50) //Spread shot { cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation - 0.628f, 10, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation - 0.314f, 10, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation, 10, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation + 0.314f, 10, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation + 0.628f, 10, BulletColor); } else if (cEnemyKills >= 30) //Multi-shot { BulletOrigin = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation, cPlayerShip.Width / 4); BulletOffset = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation + 1.570796f, cPlayerShip.Width / 5); //Adjust it so that it's relative to the top left screen corner BulletOrigin.Y += cPlayerShip.Top + (cPlayerShip.Height / 2); BulletOrigin.X += cPlayerShip.Left + (cPlayerShip.Height / 2); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y + (BulletOffset.Y * 2) - 10, BulletOrigin.X + (BulletOffset.X * 2) - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y + BulletOffset.Y - 10, BulletOrigin.X + BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - 10, BulletOrigin.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - BulletOffset.Y - 10, BulletOrigin.X - BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - (BulletOffset.Y * 2) - 10, BulletOrigin.X - (BulletOffset.X * 2) - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); } else if (cEnemyKills >= 10) { BulletOrigin = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation, cPlayerShip.Width / 4); BulletOffset = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation + 1.570796f, cPlayerShip.Width / 5); //Adjust it so that it's relative to the top left screen corner BulletOrigin.Y += cPlayerShip.Top + (cPlayerShip.Height / 2); BulletOrigin.X += cPlayerShip.Left + (cPlayerShip.Height / 2); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y + BulletOffset.Y - 10, BulletOrigin.X + BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - 10, BulletOrigin.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - BulletOffset.Y - 10, BulletOrigin.X - BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); } else //Single shot //Calculate coordinates of ship tip relative to its center { BulletOrigin = MDLN.MGTools.MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation, cPlayerShip.Width / 4); //Adjust it so that it's relative to the top left screen corner BulletOrigin.Y += cPlayerShip.Top + (cPlayerShip.Height / 2); BulletOrigin.X += cPlayerShip.Left + (cPlayerShip.Height / 2); for (Ctr = 0; Ctr <= cEnemyKillsMax; Ctr += 100) { cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - 10, BulletOrigin.X - 10, 20, 20, cPlayerShip.cRotation, 15, BulletColor); } } }
/// <summary> /// Called to actually render the game content to the backbuffer, which will be flipped with /// the currently displayed buffer to show the next frame. /// </summary> /// <param name="gameTime">Current time information of the application</param> protected override void Draw(GameTime gameTime) { Rectangle CircleDest; int nVertCtr, nPolyCtr; List <Vector2> VertList = new List <Vector2>(); List <Vector2> CurveVerts; Rectangle rectCurveBounds; Texture2D LineTexture = new Texture2D(GraphicsDevice, 1, 1); LineTexture.SetData(new[] { Color.Cyan }); cGraphDevMgr.GraphicsDevice.Clear(Color.Black); cDrawBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); for (nPolyCtr = 0; nPolyCtr < cPolyList.Count; nPolyCtr++) { VertList.Clear(); VertList.AddRange(cPolyList[nPolyCtr].GetVertexes(false)); //Draw circles at each vertex for (nVertCtr = 0; nVertCtr < VertList.Count; nVertCtr++) { CircleDest.X = (int)(VertList[nVertCtr].X - CIRCLERADIUS); CircleDest.Y = (int)(VertList[nVertCtr].Y - CIRCLERADIUS); CircleDest.Height = CIRCLERADIUS * 2; CircleDest.Width = CIRCLERADIUS * 2; cDrawBatch.Draw(cCircleTexture, CircleDest, Color.Gray); } //Draw the polygons cPolyList[nPolyCtr].Draw(); } VertList.Clear(); VertList.AddRange(cPolyList[1].GetVertexes(false)); MGMath.CubicBezierCurvePoints(VertList[0], VertList[3], VertList[1], VertList[2], 10, out CurveVerts); MGMath.CubicBezierCurveBoundaries(VertList[0], VertList[3], VertList[1], VertList[2], out rectCurveBounds); DrawTools.DrawLineSeries(GraphicsDevice, cDrawBatch, Color.LightYellow, 2, CurveVerts); DrawTools.DrawRectangle(GraphicsDevice, cDrawBatch, Color.RosyBrown, 2, rectCurveBounds); //Always draw console last cDevConsole.Draw(cDrawBatch); cDrawBatch.End(); //Use monogame draw base.Draw(gameTime); }
public bool TestCollision(IEnumerable <CollisionRegion> TestRegions) { foreach (CollisionRegion CurrRegion in TestRegions) { foreach (CollisionRegion MyRegion in cCollisionList) { if (MGMath.TestCircleCollisions(CurrRegion.Origin, CurrRegion.Radius, MyRegion.Origin, MyRegion.Radius) == true) { return(true); } } } return(false); }
/// <summary> /// Test if this object is colliding with any object in a collection of regions /// </summary> /// <param name="TestRegions">Collection of regions to test collisions with</param> /// <returns>True if this object collides with any of the collision regions</returns> public bool TestCollision(IEnumerable <CollisionRegion> TestRegions) { foreach (CollisionRegion CurrRegion in TestRegions) //Loop through all specified regions { foreach (CollisionRegion MyRegion in cCollisionList) //Loop through all of this objects regions { if (MyRegion.Type == CollideType.Circle) { if (CurrRegion.Type == CollideType.Circle) { if (MGMath.TestCircleCollisions(CurrRegion.Origin, CurrRegion.Radius, MyRegion.Origin, MyRegion.Radius) == true) { return(true); } } else //CurrRegion is a rectangle (MyRegion is Circle) { if (MGMath.TestCircleRectangleCollision(CurrRegion.Origin, CurrRegion.RectOffsets, MyRegion.Origin, MyRegion.Radius) == true) { return(true); } } } else //My Region is a rectangle { if (CurrRegion.Type == CollideType.Circle) { if (MGMath.TestCircleRectangleCollision(MyRegion.Origin, MyRegion.RectOffsets, CurrRegion.Origin, CurrRegion.Radius) == true) { return(true); } } else //CurrRegion is a rectangle (MyRegion is Rectangle) { if (MGMath.TestRectangleCollisions(CurrRegion.Origin, CurrRegion.RectOffsets, MyRegion.Origin, MyRegion.RectOffsets) == true) { return(true); } } } } } return(false); }
private int DetermineShownCardFromCoords(Point Coord) { if ((SelectedCardIndex != -1) && (SelectedCardIsZoomed == true)) { if (MGMath.IsPointInRect(Coord, cCardsShownList[SelectedCardIndex].DrawRegion) == true) { return(SelectedCardIndex); } } for (int Ctr = cCardsShownList.Count - 1; Ctr >= 0; Ctr--) //Cards are placed on top, so start from bottom to get most visible cards { if (MGMath.IsPointInRect(Coord, cCardsShownList[Ctr].DrawRegion) == true) { return(Ctr); } } return(-1); }
public override bool Update(GameTime CurrTime) { int nCtr, nBestTargetID = -1, nBestAvoidID = -1; float nBestTargetDist = 0, nBestAvoidDist = 0, nCurrDist, nAvoidDir, nTargetDir; Vector2 vNewPos; List <PhysicalObject> aTargetList = cObjMgr[cnTargetGroupID]; List <PhysicalObject> aAvoidList = cObjMgr[cnAvoidGroupID]; base.Update(CurrTime); if (cnHealth <= 0) { //This enemy is dead :( //Should fire off some explosion particles return(false); } cTarget = null; cAvoid = null; //Find a target to track for (nCtr = 0; nCtr < aTargetList.Count; nCtr++) { nCurrDist = MGMath.SquaredDistanceBetweenPoints(CenterPoint, aTargetList[nCtr].CenterPoint); if (nBestTargetID == -1) //No target picked { nBestTargetID = nCtr; nBestTargetDist = nCurrDist; cTarget = aTargetList[nCtr]; } else if (nCurrDist < nBestTargetDist) { nBestTargetID = nCtr; nBestTargetDist = nCurrDist; cTarget = aTargetList[nCtr]; } } for (nCtr = 0; nCtr < aAvoidList.Count; nCtr += 1) { nCurrDist = MGMath.SquaredDistanceBetweenPoints(CenterPoint, aAvoidList[nCtr].CenterPoint); if (nBestAvoidID == -1) //No target picked { nBestAvoidID = nCtr; nBestAvoidDist = nCurrDist; cAvoid = aAvoidList[nCtr]; } else if (nCurrDist < nBestAvoidDist) { nBestAvoidID = nCtr; nBestAvoidDist = nCurrDist; cAvoid = aAvoidList[nCtr]; } } if (nBestAvoidDist > cnMaxStrafeDist * cnMaxStrafeDist) { //Thing to avoid is too far away to worry about nBestAvoidID = -1; cAvoid = null; } if (nBestTargetID != -1) //Found a valid target, attack it { if (nBestTargetDist > (cnMaxStrafeDist * cnMaxStrafeDist)) { if (cnLastMove != 1) { cDevConsole?.AddText("Move closer"); } cnLastMove = 1; //Too far away to strafe, move closer nTargetDir = AITools.SteerTowardTarget(CenterPoint, aTargetList[nBestTargetID].CenterPoint, ObjectRotation, cnMaxTurn); } else if (nBestTargetDist < cnMinStrafeDist * cnMinStrafeDist) { if (cnLastMove != 2) { cDevConsole?.AddText("Back away"); } cnLastMove = 2; //Too close, steer away from target nTargetDir = AITools.SteerAwayFromTarget(CenterPoint, aTargetList[nBestTargetID].CenterPoint, ObjectRotation, cnMaxTurn); } else //Close enough, strafe around the target { if (cnLastMove != 3) { cDevConsole?.AddText("Strafe"); } cnLastMove = 3; nTargetDir = AITools.SteerToCircleTarget(CenterPoint, aTargetList[nBestTargetID].CenterPoint, ObjectRotation, cnMaxTurn, cbStrafeCW); } } else { nTargetDir = 0; } if (nBestAvoidID != -1) { nAvoidDir = AITools.SteerAwayFromTarget(CenterPoint, aAvoidList[nBestAvoidID].CenterPoint, ObjectRotation, cnMaxTurn); } else { nAvoidDir = 0; } if (cnTargetGroupID != -1) { if ((nBestAvoidID != -1) && (nBestAvoidDist < nBestTargetDist)) { //Thing to avoid is closer than the target, it gets priority nCurrDist = (0.75f * nAvoidDir) + (0.25f * nTargetDir); } else { //Target is closer, go for the kill nCurrDist = nTargetDir; } } else { //No target so just avoid? nCurrDist = nAvoidDir; } //Set the new movement direction, but don't change the speed SetMovement(nCurrDist, cnMaxSpeed); //Apply current speed vNewPos = CenterPoint; vNewPos.X += cvCurrSpeed.X; vNewPos.Y += cvCurrSpeed.Y; CenterPoint = vNewPos; if (ctHitFlashUntil > CurrTime.TotalGameTime.TotalMilliseconds) { //Flash red when being hit this.TintColor = Color.Red; } else { //Normal condition is white, for no tint this.TintColor = cclrNormalColor; } //Return True to keep this alive, false to have it removed return(true); }
public override bool Update(GameTime CurrTime) { Vector2 MyCenter, TargetCenter, Speed; float Distance; MyCenter.X = TopLeft.X + (Width / 2); MyCenter.Y = TopLeft.Y + (Height / 2); TargetCenter = TargetObject.GetCenterCoordinates(); Rotation = MGMath.GetAngleFromPoints(MyCenter, TargetCenter, true); Distance = MGMath.SquaredDistanceBetweenPoints(MyCenter, TargetCenter); if ((Distance >= MaxDistanceFromTarget * MaxDistanceFromTarget) && (cStrafeDirection != 0)) //Too far away get closer { Speed = MGMath.CalculateXYMagnitude(Rotation, 3); cStrafeDirection = 0; } else if ((Distance >= (MaxDistanceFromTarget * MaxDistanceFromTarget * 0.8f * 0.8f)) && (cStrafeDirection == 0)) //If moving forward overshoot a bit { Speed = MGMath.CalculateXYMagnitude(Rotation, 3); } else if (Distance <= MinDistanceFromTarget * MinDistanceFromTarget) //Too close back away { Speed = MGMath.CalculateXYMagnitude(Rotation + 3.14f, 3); cStrafeDirection = 0; } else if ((Distance <= MinDistanceFromTarget * MinDistanceFromTarget * 1.2f * 1.2f) && (cStrafeDirection == 0)) //If backing away overshoot a bit { Speed = MGMath.CalculateXYMagnitude(Rotation + 3.14f, 3); } else //Close enough, circle { if (cStrafeDirection == 0) { if (cRand.Next(1, 3) == 1) { cStrafeDirection = -1; } else { cStrafeDirection = 1; } } Speed = MGMath.CalculateXYMagnitude(Rotation + 1.57f, 2); Speed.X *= cStrafeDirection; Speed.Y *= cStrafeDirection; } SpeedX = (SpeedX + Speed.X) / 2; SpeedY = (SpeedY + Speed.Y) / 2; TopLeft.X += SpeedX; TopLeft.Y -= SpeedY; if (cLastShot == 0) { cLastShot = CurrTime.TotalGameTime.TotalMilliseconds + 1500 + cRand.Next(1000); } if (cLastShot < CurrTime.TotalGameTime.TotalMilliseconds) { Vector2 BulletOrigin; BulletOrigin.Y = TopLeft.Y + (Height / 2); BulletOrigin.X = TopLeft.X + (Height / 2); Rotation += (float)(cRand.Next(-10, 10) * (Math.PI / 180.0)); //Randmize the direction so that it's not perfect (+/- 10 degrees) BulletManager.AddParticle(BulletTexture, BulletOrigin.Y - 10, BulletOrigin.X - 10, 20, 20, Rotation, 8, new Color(255, 75, 75, 255)); cLastShot = CurrTime.TotalGameTime.TotalMilliseconds + 1500 + cRand.Next(1000); } return(true); }
public void WriteAngle(float value, int bits) => Write((uint)MGMath.PackAngle(value, bits), bits);
protected override void UpdateContents(GameTime CurrTime, KeyboardState CurrKeys, MouseState CurrMouse, bool ProcessMOuseEvent) { Vector2 SpeedAdjust, NewPos, ShipCenter; //float RotatDegrees; if (MouseRotate == false) { if (CurrKeys.IsKeyDown(Keys.Left) == true) { cRotation += 0.03f; if (cRotation > 6.28318531f) { cRotation = 0; } HasChanged = true; } if (CurrKeys.IsKeyDown(Keys.Right) == true) { cRotation -= 0.03f; if (cRotation < 0) { cRotation = 6.28318531f; } HasChanged = true; } if (CurrKeys.IsKeyDown(Keys.Up) == true) { SpeedAdjust = MGMath.CalculateXYMagnitude(cRotation, 0.1f); cSpeedX += SpeedAdjust.X; cSpeedY -= SpeedAdjust.Y; } if (CurrKeys.IsKeyDown(Keys.Down) == true) { SpeedAdjust = MGMath.CalculateXYMagnitude(cRotation, 0.1f); cSpeedX -= SpeedAdjust.X; cSpeedY += SpeedAdjust.Y; } } else { ShipCenter.Y = Height / 2; ShipCenter.X = Width / 2; cRotation = MGMath.GetAngleFromPoints(ShipCenter, CurrMouse.Position.ToVector2(), true) + ImageInitialAngle; if (cRotation != ImageInitialAngle) //Ship rotation has changed { HasChanged = true; } if (CurrKeys.IsKeyDown(Keys.W) == true) { cSpeedY -= SPEEDSTEP; } else if (cSpeedY < 0) { cSpeedY += SPEEDSTEP / 2; } if (CurrKeys.IsKeyDown(Keys.S) == true) { cSpeedY += SPEEDSTEP; } else if (cSpeedY > 0) { cSpeedY -= SPEEDSTEP / 2; } if (cSpeedY > SPEEDMAX) { cSpeedY = SPEEDMAX; } else if (cSpeedY < SPEEDMAX * -1) { cSpeedY = SPEEDMAX * -1; } if (CurrKeys.IsKeyDown(Keys.A) == true) { cSpeedX -= SPEEDSTEP; } else if (cSpeedX < 0) { cSpeedX += SPEEDSTEP / 2; } if (CurrKeys.IsKeyDown(Keys.D) == true) { cSpeedX += SPEEDSTEP; } else if (cSpeedX > 0) { cSpeedX -= SPEEDSTEP / 2; } if (cSpeedX < SPEEDMAX * -1) { cSpeedX = SPEEDMAX * -1; } else if (cSpeedX > SPEEDMAX) { cSpeedX = SPEEDMAX; } } NewPos = TopLeft; NewPos.X += cSpeedX; NewPos.Y += cSpeedY; TopLeft = NewPos; if (Top < -1 * (cDrawRegion.Height + cDrawRegion.X)) { Top = cGraphicsDevice.Viewport.Bounds.Height; } if (Top > cGraphicsDevice.Viewport.Bounds.Height) { Top = -1 * cDrawRegion.Height; } if (Left < -1 * (cDrawRegion.Width + cDrawRegion.X)) { Left = cGraphicsDevice.Viewport.Bounds.Width; } if (Left > cGraphicsDevice.Viewport.Bounds.Width) { Left = -1 * cDrawRegion.Width; } }
public void CalculateAngleFromPoints() { Vector2 Point1, Point2; float AngleRadians; string TestName; TestName = "Horizontal line, 0 degrees = 0 radians"; Point1.X = 1; Point1.Y = 1; Point2.X = 2; Point2.Y = 1; AngleRadians = MGMath.GetAngleFromPoints(Point1, Point2); Assert.AreEqual(0.0f, AngleRadians, TestName); TestName = "Horizontal line, 180 degrees = 3.14 radians"; Point1.X = 1; Point1.Y = 1; Point2.X = 2; Point2.Y = 1; AngleRadians = MGMath.GetAngleFromPoints(Point2, Point1); Assert.AreEqual((float)Math.PI, AngleRadians, TestName); TestName = "Diagonal line, 45 degrees = 0.785 radians"; Point1.X = 1; Point1.Y = 1; Point2.X = 2; Point2.Y = 2; AngleRadians = MGMath.GetAngleFromPoints(Point1, Point2); Assert.AreEqual((float)(Math.PI / 4), AngleRadians, TestName); TestName = "Diagonal line, 135 degrees = 2.356 radians"; Point1.X = -2; Point1.Y = -2; Point2.X = -3; Point2.Y = -1; AngleRadians = MGMath.GetAngleFromPoints(Point1, Point2); Assert.AreEqual((float)(Math.PI * 3 / 4), AngleRadians, TestName); TestName = "Diagonal line, 225 degrees = 3.93 radians"; Point1.X = 1; Point1.Y = 1; Point2.X = 2; Point2.Y = 2; AngleRadians = MGMath.GetAngleFromPoints(Point2, Point1); Assert.AreEqual((float)(Math.PI + (Math.PI / 4)), AngleRadians, TestName); TestName = "Diagonal line, 225 degrees = 3.93 radians"; Point1.X = 0; Point1.Y = 0; Point2.X = -1; Point2.Y = -1; AngleRadians = MGMath.GetAngleFromPoints(Point1, Point2); Assert.AreEqual((float)(Math.PI + (Math.PI / 4)), AngleRadians, TestName); TestName = "Diagonal line, 315 degrees = 5.498 radians"; Point1.X = 0; Point1.Y = 0; Point2.X = 1; Point2.Y = -1; AngleRadians = MGMath.GetAngleFromPoints(Point1, Point2); Assert.AreEqual((float)(Math.PI + (Math.PI * 3 / 4)), AngleRadians, TestName); }
/// <summary> /// Update game state and all content. Should be called once every redraw, possibly called /// more often than the draws but only if it exceeds time between frame draws. /// </summary> /// <param name="gameTime">Current time information of the application</param> protected override void Update(GameTime gameTime) { int nVertCtr, nPolyCtr, nTestCtr; List <Vector2> VertList = new List <Vector2>(); MouseState CurrMouse = Mouse.GetState(); Vector2 MousePt; Color NewColor; MousePt.X = CurrMouse.X; MousePt.Y = CurrMouse.Y; if ((CurrMouse.LeftButton == ButtonState.Pressed) && (cPriorMouse.LeftButton == ButtonState.Released)) { //Mouse was just clicked, see if its in range of a vertex for (nPolyCtr = 0; nPolyCtr < cPolyList.Count; nPolyCtr++) { VertList.Clear(); VertList.AddRange(cPolyList[nPolyCtr].GetVertexes(false)); for (nVertCtr = 0; nVertCtr < VertList.Count; nVertCtr++) { if (MGMath.IsPointInCircle(MousePt, VertList[nVertCtr], CIRCLERADIUS) == true) { cMousePoly = cPolyList[nPolyCtr]; cMouseVertIdx = nVertCtr; break; } } } for (nPolyCtr = 0; cMousePoly == null && nPolyCtr < cPolyList.Count; nPolyCtr++) { if (MGMath.PointInConvexPolygon(MousePt, cPolyList[nPolyCtr].GetVertexes(false)) == true) { cMovePoly = cPolyList[nPolyCtr]; cMoveStart = MousePt; break; } } } else if ((CurrMouse.LeftButton == ButtonState.Released) && (CurrMouse.RightButton == ButtonState.Pressed) && (cPriorMouse.RightButton == ButtonState.Released)) { //Mouse was just clicked, see if its in range of a vertex for (nPolyCtr = 0; cMousePoly == null && nPolyCtr < cPolyList.Count; nPolyCtr++) { if (MGMath.PointInConvexPolygon(MousePt, cPolyList[nPolyCtr].GetVertexes(false)) == true) { cMousePoly = cPolyList[nPolyCtr]; cMoveStart = MousePt; break; } } } if ((CurrMouse.LeftButton == ButtonState.Released) && (CurrMouse.RightButton == ButtonState.Released) && ((cPriorMouse.LeftButton == ButtonState.Pressed) || (cPriorMouse.RightButton == ButtonState.Pressed))) { //Mouse was just released cMousePoly = null; cMovePoly = null; } if ((CurrMouse.LeftButton == ButtonState.Pressed) && (cMousePoly != null)) { //Update the position of the selected vertex cMousePoly.UpdateVertex(cMouseVertIdx, MousePt); } if ((CurrMouse.LeftButton == ButtonState.Pressed) && (cMovePoly != null)) { //Update the position of the selected polygon cMovePoly.MoveShape(MousePt - cMoveStart); cMoveStart = MousePt; } if ((CurrMouse.LeftButton == ButtonState.Released) && (CurrMouse.RightButton == ButtonState.Pressed) && (cMousePoly != null)) { //Update the rotation of the selected vertex cMousePoly.RotateShape = MGMath.GetAngleFromPoints(cMoveStart, MousePt); } cPriorMouse = CurrMouse; //Check if the polygons are colliding and make them red for (nPolyCtr = 0; nPolyCtr < cPolyList.Count; nPolyCtr++) { //Reset the background color NewColor = cPolyList[nPolyCtr].LineColor; NewColor.R = 0; cPolyList[nPolyCtr].LineColor = NewColor; for (nTestCtr = 0; nTestCtr < cPolyList.Count; nTestCtr++) { if (nPolyCtr == nTestCtr) //Can't collide with itself { continue; } if (cPolyList[nPolyCtr].TestCollision(cPolyList[nTestCtr]) == true) { NewColor = cPolyList[nPolyCtr].LineColor; NewColor.R = 255; cPolyList[nPolyCtr].LineColor = NewColor; } } } cDevConsole.Update(gameTime); //Use monogame update base.Update(gameTime); }
public override bool Update(GameTime CurrTime) { Vector2 vNewPos; int nCtr, nBestTargetID = -1; float nBestTargetDist = 0, nCurrDist; List <PhysicalObject> aTargetList = cObjMgr[cnTargetGroupID]; bool bRetVal = true; base.Update(CurrTime); if (ctCreated == -1) //Just created, mark the time { ctCreated = CurrTime.TotalGameTime.TotalMilliseconds; } switch (ceProjType) { case eProjectileType_t.Tracking: //Find a target to track for (nCtr = 0; nCtr < aTargetList.Count; nCtr++) { nCurrDist = MGMath.SquaredDistanceBetweenPoints(CenterPoint, aTargetList[nCtr].CenterPoint); if (nBestTargetID == -1) //No target picked { nBestTargetID = nCtr; nBestTargetDist = nCurrDist; } else if (nCurrDist < nBestTargetDist) { nBestTargetID = nCtr; nBestTargetDist = nCurrDist; } } if (nBestTargetID != -1) //Found a valid target, steer toward it { nCurrDist = AITools.SteerTowardTarget(CenterPoint, aTargetList[nBestTargetID].CenterPoint, ObjectRotation, cnMaxTurn); //Set the new movement direction, but don't change the speed SetMovement(nCurrDist, cnMaxSpeed); } if (CurrTime.TotalGameTime.TotalMilliseconds - ctCreated > ctTimeToLive) { //Lived its full life, time to pop bRetVal = false; } break; case eProjectileType_t.Straight: default: //No logic, just flies straight if (cGraphDev.Viewport.Width < CenterPoint.X + Width) //remove it when off screen { bRetVal = false; } if (cGraphDev.Viewport.Height < CenterPoint.Y + Height) //remove it when off screen { bRetVal = false; } if (CenterPoint.X < -1 * Width) { bRetVal = false; } if (CenterPoint.Y < -1 * Height) { bRetVal = false; } break; } //Look for collisions with all possible targets foreach (PhysicalObject CurrObj in aTargetList) { if (CurrObj.TestCollision(this) == true) { //Hit a target! (tell it that it was hit) CurrObj.ReportCollision(CurrTime, this); bRetVal = false; break; } } //Apply the current speed vNewPos = CenterPoint; vNewPos.X += Speed.X; vNewPos.Y += Speed.Y; CenterPoint = vNewPos; if ((ParticleHandler != null) && (bRetVal == false)) { //Missile is expiring, throw some particles for (nCtr = 0; nCtr < 10; nCtr++) { ParticleHandler.AddParticle(new DustParticle(CenterPoint, Rand, cGraphDev, cImgAtlas, "spaceEffects_008.png")); } } //Return True to keep this alive, false to have it removed return(bRetVal); }
public float ReadAngle(int bits) => MGMath.UnpackAngle((int)ReadUInt(bits), bits);
private void CommandSentEventHandler(object Sender, string Command) { if (Tools.RegEx.QuickTest(Command, "^(quit|exit)$") == true) { Exit(); } else if (Tools.RegEx.QuickTest(Command, "^ship *stop$") == true) { cPlayerShip.cSpeedX = 0; cPlayerShip.cSpeedY = 0; cDevConsole.AddText("Ship speed set to 0"); } else if (Tools.RegEx.QuickTest(Command, "^ship *center$") == true) { cPlayerShip.Top = (cGraphDevMgr.GraphicsDevice.Viewport.Bounds.Height) / 2 - (cPlayerShip.Height / 2); cPlayerShip.Left = (cGraphDevMgr.GraphicsDevice.Viewport.Bounds.Width) / 2 - (cPlayerShip.Width / 2); cDevConsole.AddText("Ship position set to [X=" + cPlayerShip.Left + ", Y=" + cPlayerShip.Width + "]"); } else if (Tools.RegEx.QuickTest(Command, "^ship *state$") == true) { cDevConsole.AddText("Ship Position [X=" + cPlayerShip.Top + ", Y=" + cPlayerShip.Left + "]"); cDevConsole.AddText(" Speed X=" + cPlayerShip.cSpeedX + " Y=" + cPlayerShip.cSpeedY); cDevConsole.AddText(" Size Width=" + cPlayerShip.Width + " Height=" + cPlayerShip.Height); } else if (Tools.RegEx.QuickTest(Command, "^fire *spread$") == true) { cDevConsole.AddText("Firing Spread"); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation - 0.628f, 10, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation - 0.314f, 10, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation, 10, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation + 0.314f, 10, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], cPlayerShip.Top + (cPlayerShip.Height / 2) - 10, cPlayerShip.Left + (cPlayerShip.Height / 2) - 10, 20, 20, cPlayerShip.cRotation + 0.628f, 10, Color.White); } else if (Tools.RegEx.QuickTest(Command, "^fire *multi-?(ple|shot)$") == true) { cDevConsole.AddText("Firing Multi-Shot"); Vector2 BulletOrigin, BulletOffset; BulletOrigin = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation, cPlayerShip.Width / 4); BulletOffset = MGMath.CalculateXYMagnitude(-1 * cPlayerShip.cRotation + 1.570796f, cPlayerShip.Width / 5); //Adjust it so that it's relative to the top left screen corner BulletOrigin.Y += cPlayerShip.Top + (cPlayerShip.Height / 2); BulletOrigin.X += cPlayerShip.Left + (cPlayerShip.Height / 2); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y + (BulletOffset.Y * 2) - 10, BulletOrigin.X + (BulletOffset.X * 2) - 10, 20, 20, cPlayerShip.cRotation, 15, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y + BulletOffset.Y - 10, BulletOrigin.X + BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - 10, BulletOrigin.X - 10, 20, 20, cPlayerShip.cRotation, 15, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - BulletOffset.Y - 10, BulletOrigin.X - BulletOffset.X - 10, 20, 20, cPlayerShip.cRotation, 15, Color.White); cPlayerBullets.AddParticle(cTextureDict[Textures.Bullet], BulletOrigin.Y - (BulletOffset.Y * 2) - 10, BulletOrigin.X - (BulletOffset.X * 2) - 10, 20, 20, cPlayerShip.cRotation, 15, Color.White); } else if (Tools.RegEx.QuickTest(Command, "^new *asteroid$") == true) { cDevConsole.AddText("Spawning Asteroid"); cAsteroids.AddParticle(cTextureDict[Textures.Asteroid], cPlayerShip.Top + (cPlayerShip.Height / 2) - 50, cPlayerShip.Left + (cPlayerShip.Height / 2) - 50, 100, 100, cPlayerShip.cRotation - 0.628f, 2, Color.White); } else if (Tools.RegEx.QuickTest(Command, @"^\s*ast(eroid)?\s*count\s*?$") == true) { cDevConsole.AddText("Current Asteroid Count: " + cAsteroids.ParticleList.Count); } else if (Tools.RegEx.QuickTest(Command, @"^\s*ast(eroid)?\s*clear$") == true) { cDevConsole.AddText("Destroying all asteroids"); cAsteroids.ParticleList.Clear(); } else if (Tools.RegEx.QuickTest(Command, @"^\s*mouse\s*turn\s*=\s*(on|true|enable|1)\s*$") == true) { cDevConsole.AddText("Using mouse position to rotate ship"); cPlayerShip.MouseRotate = true; } else if (Tools.RegEx.QuickTest(Command, @"^\s*mouse\s*turn\s*=\s*(off|false|disable|0)\s*$") == true) { cDevConsole.AddText("Using arrow keys to rotate ship"); cPlayerShip.MouseRotate = false; } else if (Tools.RegEx.QuickTest(Command, @"^\s*(spawn|new)\s*hunter\s*$") == true) { cDevConsole.AddText("Spawning new hunter UFO"); Vector2 StartPos = new Vector2(-50, -50); CreateNewHunter(100, StartPos); } else if (Tools.RegEx.QuickTest(Command, @"^\s*(sparkles|particles)\s*$") == true) { Particle2D NewSparkle; Vector2 Speed; cDevConsole.AddText("Particle burst on player ship"); for (int Ctr = 0; Ctr < 25; Ctr++) { NewSparkle = new Particle2D(cGraphDevMgr.GraphicsDevice); NewSparkle.AlphaFade = true; NewSparkle.TimeToLive = 100 + (cRandom.NextDouble() * 1000); NewSparkle.Height = 10; NewSparkle.Width = 10; NewSparkle.TopLeft.X = cPlayerShip.Left + (cPlayerShip.Width / 2); NewSparkle.TopLeft.Y = cPlayerShip.Top + (cPlayerShip.Height / 2); NewSparkle.Image = cTextureDict[Textures.Bullet]; NewSparkle.Rotation = (float)(cRandom.NextDouble() * 6.2f); Speed = MGMath.CalculateXYMagnitude(NewSparkle.Rotation, (float)(cRandom.NextDouble() * 5)); NewSparkle.SpeedX = Speed.X; NewSparkle.SpeedY = Speed.Y; NewSparkle.Tint = Color.White; cSparkles.AddParticle(NewSparkle); } } else if (Tools.RegEx.QuickTest(Command, @"^\s*(headlight|flashlight|dark)\s*=\s*(1|true|enable)\s*$") == true) { cDevConsole.AddText("Headlight mode enabled."); cHeadlightMode = true; } else if (Tools.RegEx.QuickTest(Command, @"^\s*(headlight|flashlight|dark)\s*=\s*(0|false|disable)\s*$") == true) { cDevConsole.AddText("Headlight mode disabled."); cHeadlightMode = false; } else if (Tools.RegEx.QuickTest(Command, @"^\s*stats\s*=\s*(1|true|enable|on)\s*$") == true) { cDevConsole.AddText("Stats enabled."); cShowStats = true; } else if (Tools.RegEx.QuickTest(Command, @"^\s*stats\s*=\s*(0|false|disable|off)\s*$") == true) { cDevConsole.AddText("Stats disabled."); cShowStats = false; } else { cDevConsole.AddText("Unrecognized command: " + Command); } }