/// <summary> /// Game logic function. Update world, handle input, etc /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { int Seconds = gameTime.TotalGameTime.Seconds; KeyboardState KeyState = Keyboard.GetState(); MouseState CurrMouse = Mouse.GetState(); if ((PriorKeyState.IsKeyDown(Keys.OemTilde) == false) && (KeyState.IsKeyDown(Keys.OemTilde) == true)) { DevConsole.ToggleVisible(); } DevConsole.Update(gameTime, Keyboard.GetState(), Mouse.GetState()); if ((PriorMouseState.LeftButton == ButtonState.Released) && (CurrMouse.LeftButton == ButtonState.Pressed)) { DevConsole.AddText("Left mouse clicked, X=" + CurrMouse.X + " Y=" + CurrMouse.Y); } if (new Rectangle(10, 10, CardBack.Width, CardBack.Height).Contains(CurrMouse.X, CurrMouse.Y)) { if (cMouseOverCard == false) { DevConsole.AddText("Mouse entered card"); cMouseEnterTime = gameTime.TotalGameTime.TotalMilliseconds; cMouseOverCard = true; TestCont.Visible = true; } } else { if (cMouseOverCard == true) { DevConsole.AddText("Mouse exited card"); cMouseOverCard = false; TestCont.Visible = false; } } if (cMouseEnterTime != -1) { if (gameTime.TotalGameTime.TotalMilliseconds - cMouseEnterTime >= 500) { DevConsole.AddText("Mouse over 500 ms elapsed"); cMouseEnterTime = -1; } } TestCont.Update(gameTime); PriorMouseState = CurrMouse; PriorKeyState = KeyState; FrameNum = (Seconds % 6) + 1; if (gameTime.TotalGameTime.Milliseconds <= 500) { Transparency = gameTime.TotalGameTime.Milliseconds / 2; } else { Transparency = 255 - ((gameTime.TotalGameTime.Milliseconds - 500) / 2); } if (cShipUseKeys == true) { if (KeyState.IsKeyDown(Keys.Left) == true) { cShipDir = ShipDirection.Left; } else if (KeyState.IsKeyDown(Keys.Right) == true) { cShipDir = ShipDirection.Right; } else { cShipDir = ShipDirection.Straight; } } else { if (CurrMouse.Position.X < 300) { cShipDir = ShipDirection.Left; } else if (CurrMouse.Position.X > 500) { cShipDir = ShipDirection.Right; } else { cShipDir = ShipDirection.Straight; } } TestCard.Update(gameTime); NestedCont.Update(gameTime); cTextTest.Update(gameTime); //Use monogame update base.Update(gameTime); }
protected override void Update(GameTime gameTime) { KeyboardState CurrKeys = Keyboard.GetState(); MouseState CurrMouse = Mouse.GetState(); Particle2D BulletInfo, EnemyInfo; List <int> ParticlesToRemove = new List <int>(); int Ctr, Cnt; if (cAliveSince == 0) { cAliveSince = (uint)gameTime.TotalGameTime.TotalSeconds; } if (cLastAsteroid < gameTime.TotalGameTime.TotalMilliseconds) //Create new asteroid { if ((cSpawnNum % 3 != 0) && (cEnemyKills >= cAsteroids.ParticleList.Count)) { for (Ctr = 0; Ctr < 1 + (cEnemyKills / 20); Ctr++) { CreateNewAsteroid(100, new Vector2(-1, -1)); } } else { Vector2 StartPos; StartPos.X = (float)(cRandom.NextDouble() * cGraphDevMgr.GraphicsDevice.Viewport.Width); StartPos.Y = cRandom.Next(-2, 1) * 60; if (StartPos.Y == 0) { StartPos.Y = cGraphDevMgr.GraphicsDevice.Viewport.Height; } CreateNewHunter(60, StartPos); } cSpawnNum++; cLastAsteroid = gameTime.TotalGameTime.TotalMilliseconds + 2000; } //Check for player input Ctr = (int)(500 - cEnemyKillsMax); //Calculate the minimum time between shots if (Ctr > 400) { Ctr = 400; } else if (Ctr < 100) { Ctr = 50; } if (((CurrKeys.IsKeyDown(Keys.Space) == true) || (CurrMouse.LeftButton == ButtonState.Pressed)) && (cLastShot < gameTime.TotalGameTime.TotalMilliseconds - Ctr)) { PlayerFireBullet(); cLastShot = gameTime.TotalGameTime.TotalMilliseconds; } if ((CurrKeys.IsKeyDown(Keys.OemTilde) == true) && (cPriorKeyState.IsKeyDown(Keys.OemTilde) == false)) { cDevConsole.ToggleVisible(); } cUFOs.Update(gameTime); cAsteroids.Update(gameTime); cEnemyBullets.Update(gameTime); cPlayerBullets.Update(gameTime); cPlayerShip.Update(gameTime, CurrKeys, CurrMouse); cSparkles.Update(gameTime); cDevConsole.Update(gameTime, CurrKeys, CurrMouse); //Collision detection cPlayerShip.ImageTint = Color.White; for (Cnt = 0; Cnt < cAsteroids.ParticleList.Count; Cnt++) { EnemyInfo = (Particle2D)cAsteroids.ParticleList[Cnt]; //Are bullts hitting the asteroid? for (Ctr = 0; Ctr < cPlayerBullets.ParticleList.Count; Ctr++) { BulletInfo = (Particle2D)cPlayerBullets.ParticleList[Ctr]; if (BulletInfo.TestCollision(EnemyInfo.GetCollisionRegions()) == true) { CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 25 * EnemyInfo.Height / 6, EnemyInfo.Height / 3, Color.SaddleBrown, cTextureDict[Textures.Dust]); //Spawn little asteroids if (EnemyInfo.Height > 50) { Vector2 TopLeft; TopLeft.X = EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2) - (EnemyInfo.Width * 0.35f); TopLeft.Y = EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2) - (EnemyInfo.Height * 0.35f); CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft); CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft); } //Destroy shot and large asteroid ParticlesToRemove.Add(Cnt); cPlayerBullets.ParticleList.RemoveAt(Ctr); cEnemyKills++; break; //Exit inner loop so each bullet ony gets 1 asteroid } } //Is the asteroid hitting the player? if (EnemyInfo.TestCollision(cPlayerShip) == true) { cPlayerShip.ImageTint = Color.Red; CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 25 * EnemyInfo.Height / 6, EnemyInfo.Height / 3, Color.SaddleBrown, cTextureDict[Textures.Dust]); //Spawn little asteroids if (EnemyInfo.Height > 50) { Vector2 TopLeft; TopLeft.X = EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2) - (EnemyInfo.Width * 0.35f); TopLeft.Y = EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2) - (EnemyInfo.Height * 0.35f); CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft); CreateNewAsteroid((int)(EnemyInfo.Width * 0.7f), TopLeft); } //Destroy asteroid ParticlesToRemove.Add(Cnt); PlayerHit(gameTime); } } for (Cnt = ParticlesToRemove.Count - 1; Cnt >= 0; Cnt--) { cAsteroids.ParticleList.RemoveAt(ParticlesToRemove[Cnt]); } ParticlesToRemove.Clear(); for (Cnt = 0; Cnt < cUFOs.ParticleList.Count; Cnt++) { EnemyInfo = (Particle2D)cUFOs.ParticleList[Cnt]; //Are bullts hitting the UFO? for (Ctr = 0; Ctr < cPlayerBullets.ParticleList.Count; Ctr++) { BulletInfo = (Particle2D)cPlayerBullets.ParticleList[Ctr]; if (BulletInfo.TestCollision(EnemyInfo.GetCollisionRegions()) == true) { //Destroy shot and UFO cPlayerBullets.ParticleList.RemoveAt(Ctr); ParticlesToRemove.Add(Cnt); cEnemyKills++; CreateParticleBurst(new Vector2(EnemyInfo.TopLeft.X + (EnemyInfo.Width / 2), EnemyInfo.TopLeft.Y + (EnemyInfo.Height / 2)), 200, Color.OrangeRed); break; //Exit inner loop so each bullet ony gets 1 enemy } } //Is the UFO hitting the player? if (EnemyInfo.TestCollision(cPlayerShip) == true) { cPlayerShip.ImageTint = Color.Red; PlayerHit(gameTime); } } for (Cnt = ParticlesToRemove.Count - 1; Cnt >= 0; Cnt--) { cUFOs.ParticleList.RemoveAt(ParticlesToRemove[Cnt]); } for (Cnt = 0; Cnt < cEnemyBullets.ParticleList.Count; Cnt++) { BulletInfo = (Particle2D)cEnemyBullets.ParticleList[Cnt]; //Is the bullet hitting the player? if (BulletInfo.TestCollision(cPlayerShip) == true) { cEnemyBullets.ParticleList.RemoveAt(Cnt); PlayerHit(gameTime); } } cPriorKeyState = CurrKeys; //Use monogame update base.Update(gameTime); }