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