public bool CheckCollisionWith(CModel model) { foreach (BoundingBox box in boundingBoxes) { foreach (BoundingBox check in model.boundingBoxes) { if(box.Intersects(check)) return true; } } return false; }
/// <summary> /// Allows the game to run logic. /// </summary> protected override void Update(GameTime gameTime) { lastKeyboardState = currentKeyboardState; lastGamePadState = currentGamePadState; currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); if (ship.thrustAmount > 1.5f) currentflame = flame1; else if (ship.thrustAmount > 1.0f) currentflame = flame2; else if (ship.thrustAmount > 0.5f) currentflame = flame3; #region If State = New Game if (_gameState == State.NewGame) { if (currentKeyboardState.IsKeyUp(Keys.Enter) && lastKeyboardState.IsKeyDown(Keys.Enter)) { _gameState = State.Playing; return; } } if (_gameState == State.Gameover) { if (currentKeyboardState.IsKeyUp(Keys.Enter) && lastKeyboardState.IsKeyDown(Keys.Enter)) { timeLeft = TimeSpan.FromSeconds(60); man.Model = manStanding; camera.DesiredPositionOffset = new Vector3(0.0f, 2000.0f, 3500.0f); gameOver = false; _gameState = State.Playing; ship.Reset(); camera.Reset(); lives = 3; _objects.Clear(); SpawnObjects(); NewManPos(); return; } if (currentKeyboardState.IsKeyUp(Keys.Escape) && lastKeyboardState.IsKeyDown(Keys.Escape)) { _gameState = State.Credits; } return; } #endregion If State = New Game #region If State = Paused if (_gameState == State.Paused) { if (currentKeyboardState.IsKeyUp(Keys.P) && lastKeyboardState.IsKeyDown(Keys.P)) _gameState = State.Playing; return; } #endregion If State = Paused #region if state = playing if (_gameState == State.Playing) { if (lastKeyboardState.IsKeyUp(Keys.I) && currentKeyboardState.IsKeyDown(Keys.I)) ship.inverted = !ship.inverted; timeLeft -= gameTime.ElapsedGameTime; controlTimer -= gameTime.ElapsedGameTime; if (timeLeft.TotalMilliseconds <= 0) { gameOver = true; } if (currentKeyboardState.IsKeyDown(Keys.G)) gameOver = true; if (currentKeyboardState.IsKeyUp(Keys.P) && lastKeyboardState.IsKeyDown(Keys.P)) _gameState = State.Paused; if (currentKeyboardState.IsKeyUp(Keys.Enter) && lastKeyboardState.IsKeyDown(Keys.Enter)) _gameState = State.Paused; if (gameOver) { UpdateCameraChaseTarget(); camera.Reset(); camera.DesiredPositionOffset = new Vector3(0.0f, 40000.0f, 70000.0f); if (man.World.Translation.Z + (man.Width / 2) <= _buildings[rI].World.Translation.Z + (_buildings[rI].Width / 2)) { man.Model = manRunning; man.World *= Matrix.CreateTranslation(0.0f, 0.0f, 50.0f); } else if (man.World.Translation.Y > 0) { man.Model = manFalling; man.World *= Matrix.CreateTranslation(0.0f, -100.0f, 0.0f); } else _gameState = State.Gameover; base.Update(gameTime); return; } if ((currentKeyboardState.IsKeyDown(Keys.F1) && lastKeyboardState.IsKeyUp(Keys.F1)) || (currentGamePadState.IsButtonDown(Buttons.Back) && lastGamePadState.IsButtonUp(Buttons.Back))) { showCollisions = !showCollisions; } // Exit when the Escape key or Back button is pressed if (currentKeyboardState.IsKeyDown(Keys.Escape) || currentGamePadState.IsButtonDown(Buttons.Start)) { Exit(); } bool touchTopLeft = currentMouseState.LeftButton == ButtonState.Pressed && lastMousState.LeftButton != ButtonState.Pressed && currentMouseState.X < GraphicsDevice.Viewport.Width / 10 && currentMouseState.Y < GraphicsDevice.Viewport.Height / 10; if (lastKeyboardState.IsKeyUp(Keys.E) && (currentKeyboardState.IsKeyDown(Keys.E))) NewManPos(); // Reset the ship on R key or right thumb stick clicked if (currentKeyboardState.IsKeyDown(Keys.R)) { ship.Reset(); camera.Reset(); } // Update the ship ship.Update(gameTime); // Update the camera to chase the new target UpdateCameraChaseTarget(); // Update the business man's objects UpdateBusObjects(); // The chase camera's update behavior is the springs camera.Update(gameTime); if (_hudTextStuff.Count > 0) { for (int i = _hudTextStuff.Count - 1; i >= 0; i--) { _hudTextStuff[i].Update(gameTime); if (_hudTextStuff[i].DoneDrawing) _hudTextStuff.RemoveAt(i); } } foreach (Buildings check in _buildings) { int lol; if (check == _buildings[rI]) lol = 1; check.UpdateCollisions(); if (ship.CheckCollisionWith(check)) { _hudTextStuff.Add(new HudText((Content.Load<Texture2D>("Textures/Hud/crashed")), TimeSpan.FromSeconds(3), new Vector2(400, 300))); lives--; if (lives <= 0) gameOver = true; ship.Reset(); } } if (ship.CheckCollisionWith(man)) if (_objects.Count == 0) { score += 100; timeLeft += TimeSpan.FromSeconds(40); Console.WriteLine("New Score : " + score); // Spawn the objects again at NEW random locations // Make a new Business man at a new random location SpawnObjects(); NewManPos(); } skyBox.World = Matrix.CreateTranslation(camera.Position - new Vector3(0.0f, 100.0f, 0.0f)); base.Update(gameTime); } #endregion if state = playing if (_gameState == State.Credits) { creditTime -= gameTime.ElapsedGameTime; if (creditTime.TotalSeconds <= 0) Exit(); } }
/// <summary> /// Load graphics content. /// </summary> protected override void LoadContent() { flame1 = new CModel(Content.Load<Model>("Models/Flame_1"), GraphicsDevice, Matrix.Identity, boxEffect); flame2 = new CModel(Content.Load<Model>("Models/Flame_2"), GraphicsDevice, Matrix.Identity, boxEffect); flame3 = new CModel(Content.Load<Model>("Models/Flame_3"), GraphicsDevice, Matrix.Identity, boxEffect); currentflame = flame3; boxEffect = new BasicEffect(GraphicsDevice); life = Content.Load<Texture2D>("Textures/Hud/life"); briefcase = Content.Load<Texture2D>("Textures/Hud/Breifcase_UI"); speed = Content.Load<Texture2D>("Textures/Hud/speed"); minimap = Content.Load<Texture2D>("Textures/Hud/minimap"); dude = Content.Load<Texture2D>("Textures/Hud/dude"); controls = Content.Load<Texture2D>("Textures/Hud/Controls"); newGameTex = Content.Load<Texture2D>("Textures/Screens/NewGame"); paused = Content.Load<Texture2D>("Textures/Screens/paused"); deathScreen = Content.Load<Texture2D>("Textures/Screens/Death_Screen"); creditsTex = Content.Load<Texture2D>("Textures/Screens/Credits"); spriteBatch = new SpriteBatch(graphics.GraphicsDevice); spriteFont = Content.Load<SpriteFont>("gameFont"); ship = new Ship(Content.Load<Model>("Models/Actors/Pidgeon_1"), GraphicsDevice, boxEffect); ground = new CModel(Content.Load<Model>("ground"), GraphicsDevice, Matrix.CreateScale(500.0f, 500.0f, 500.0f), boxEffect); manStanding = Content.Load<Model>("Dude_Standing"); manRunning = Content.Load<Model>("Dude_Running"); manFalling = Content.Load<Model>("Dude_Falling"); Model load; for (int count = 1; count < 5; count++) load = Content.Load<Model>("Models/object_" + count.ToString()); man = new BuisnessMan(manStanding, GraphicsDevice, Matrix.Identity, boxEffect); man.collision = new CModel(Content.Load<Model>("Collison_Cylinder"), GraphicsDevice, Matrix.Identity, boxEffect); skyBox = new CModel(Content.Load<Model>("skybox"), GraphicsDevice, Matrix.Identity, boxEffect); #region Init Buildings // Add the buildings to the list with a random type // (i.e, the random building could be a small one with no windows, or a really tall one with loads of windows! for (int i = 0; i < 100; i++) { Buildings building = null; bool collision = true; while (collision == true) { collision = false; var randomB = _random.Next(2, 9); building = new Buildings(Content.Load<Model>("Models/Buildings/Building_" + randomB), GraphicsDevice, Matrix.CreateScale(20f) * Matrix.CreateTranslation(_random.Next(-buildingDistance, buildingDistance), 0, _random.Next(-buildingDistance, buildingDistance)), boxEffect); building.UpdateCollisions(); // Need to check if the spawning model is intersecting with a previously spawned model and chuck it if it is foreach (var b in _buildings) { if (building.CheckCollisionWith(b)) { collision = true; } } } if (building.boundingBoxes.Max().Max.Y > maxHeight) maxHeight = building.boundingBoxes.Max().Max.Y; _buildings.Add(building); Console.WriteLine("Spawning building at new random position"); } ship.SpawnHeight = maxHeight + 1000; ship.Reset(); #endregion Init Buildings }