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 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); }