public void LoadMap() { Blocks = new List <Block>(); Ladders = new List <Ladder>(); Doors = new List <Door>(); Nodes = new List <Node>(); if (!KeyCollected) { stars = new List <Star>(); } key = null; Block Block; Ladder Ladder; Toilet Toilet; Star Star; Door Door; Key Key; Block Fence; Node Node; for (int i = 0; i < ActiveMap.Map.GetLength(0); i++) { for (int j = 0; j < ActiveMap.Map.GetLength(1); j++) { switch (ActiveMap.Map[i, j]) { case 0: break; case 1: Block = new Block(new Vector2(j * 50, i * 50)); if (i == 0) { Block.Texture = FloorTexture; } else if (ActiveMap.Map[i - 1, j] != 1) { Block.Texture = FloorTexture; Node = new Node(new Vector2(j * 50, (i - 1) * 50)); Nodes.Add(Node); } else { Block.Texture = WallTexture; } Blocks.Add(Block); break; case 2: Ladder = new Ladder(new Vector2(j * 50, i * 50)) { Texture = LadderTexture }; Ladders.Add(Ladder); Node = new Node(new Vector2(j * 50, i * 50)); Nodes.Add(Node); if (ActiveMap.Map[i - 1, j] == 0) { Node = new Node(new Vector2(j * 50, (i - 1) * 50)); Nodes.Add(Node); } break; case 3: //toilet Toilet = new Toilet(new Vector2(j * 50, i * 50)) { Texture = toiletTexture }; Goal = Toilet; break; case 4: //end break; case 5: //key if (!KeyCollected) { Key = new Key(new Vector2(j * 50, i * 50)) { Texture = keyTexture }; key = Key; } break; case 6: //star if (!KeyCollected) { Star = new Star(new Vector2(j * 50, i * 50)) { Texture = starTexture }; Stars.Add(Star); } break; case 7: //door if (!KeyCollected) { if (ActiveMap.Map[i + 1, j] == 7) { Door = new Door(new Vector2(j * 50, i * 50)) { Texture = doorTextureTop }; } else { Door = new Door(new Vector2(j * 50, i * 50)) { Texture = doorTexture }; } Doors.Add(Door); } break; case 8: Flag = new Flag(new Vector2(j * 50, i * 50)) { Texture = flagTexture }; break; case 9: Fence = new Block(new Vector2(j * 50, i * 50)) { Texture = fenceTexture }; Fences.Add(Fence); break; } } } for (int i = 0; i < Nodes.Count; i++) { Debug.WriteLine(i + ". " + Nodes[i].Position); } for (int i = 0; i < Enemies.Count; i++) { Enemies[i].Texture = EnemyTexture; } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here MouseState mouse = Mouse.GetState(); switch (gameState) { case GameState.MainMenu: if (btnAzerty.click == true) { player.Controls = new ControlsZQSD(); gameState = GameState.Game; } if (btnQwerty.click == true) { player.Controls = new ControlsWASD(); gameState = GameState.Game; } btnAzerty.Update(mouse); btnQwerty.Update(mouse); if (Keyboard.GetState().IsKeyDown(Keys.E)) { gameState = GameState.Finished; } break; case GameState.Game: IsMouseVisible = false; if (Keyboard.GetState().IsKeyDown(Keys.F2)) { lives = 5; NextLevel(0); } level.IssueCommands(); player.Update(gameTime); if (player.CheckDeath(level.Enemies)) { if (lives == 0) { gameState = GameState.GameOver; } lives--; player.Position = Respawn; } if (player.CheckItem(level.key)) { level.KeyCollected = true; Respawn = level.key.Position; level.LoadMap(); collisionManager = new CollisionManager(level.GetLevelCollision(), level.Ladders); player.CollManager = collisionManager; } for (int i = 0; i < level.Stars.Count; i++) { Star star = level.Stars[i]; if (player.CheckItem(star)) { level.Stars.Remove(star); starsCollected++; } } if (player.CheckItem(level.Goal) && !level.GoalReached) { level.GoalReached = true; soundEffects[0].CreateInstance().Play(); Respawn = level.Goal.Position; } if (player.CheckItem(level.Flag) && level.GoalReached) { if (starsCollected == starsTotal) { lives = lives + 5; } if (!levelCompleted) { NextLevel(1); levelCompleted = true; } else { gameState = GameState.Finished; } soundEffects[1].CreateInstance().Play(); } camera.Update(player.Position, level.Width, level.Height); foreach (Enemy enemy in level.Enemies) { enemy.Update(gameTime); if (level.GoalReached && ((enemy.Position.X % 2) == 0)) { enemy.WalkSpeedx = 2; } } break; case GameState.GameOver: if (Keyboard.GetState().IsKeyDown(Keys.F2)) { lives = 5; levelCompleted = false; NextLevel(0); } break; } base.Update(gameTime); }