LevelManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Tile_Engine;
  9. namespace Gemstone_Hunter
  10. {
  11. public static class LevelManager
  12. {
  13. private static ContentManager Content;
  14. private static Player player;
  15. private static int currentLevel;
  16. private static Vector2 respawnLocation;
  17. private static List<Gemstone> gemstones = new List<Gemstone>();
  18. private static List<Enemy> enemies = new List<Enemy>();
  19. public static int CurrentLevel
  20. {
  21. get { return currentLevel; }
  22. }
  23. public static Vector2 RespawnLocation
  24. {
  25. get { return respawnLocation; }
  26. set { respawnLocation = value; }
  27. }
  28. public static void Initialize(
  29. ContentManager content,
  30. Player gamePlayer)
  31. {
  32. Content = content;
  33. player = gamePlayer;
  34. }
  35. private static void checkCurrentCellCode()
  36. {
  37. string code = TileMap.CellCodeValue(
  38. TileMap.GetCellByPixel(player.WorldCenter));
  39. if (code == "DEAD")
  40. {
  41. player.Kill();
  42. }
  43. }
  44. public static void LoadLevel(int levelNumber)
  45. {
  46. using (var stream = TitleContainer.OpenStream(
  47. "Content/Maps/MAP" +
  48. levelNumber.ToString().PadLeft(3, '0') + ".MAP"))
  49. {
  50. TileMap.LoadMapFromStream(stream);
  51. }
  52. gemstones.Clear();
  53. enemies.Clear();
  54. for (int x = 0; x < TileMap.MapWidth; x++)
  55. {
  56. for (int y = 0; y < TileMap.MapHeight; y++)
  57. {
  58. if (TileMap.CellCodeValue(x, y) == "START")
  59. {
  60. player.WorldLocation = new Vector2(
  61. x * TileMap.TileWidth,
  62. y * TileMap.TileHeight);
  63. }
  64. if (TileMap.CellCodeValue(x, y) == "GEM")
  65. {
  66. gemstones.Add(new Gemstone(Content, x, y));
  67. }
  68. if (TileMap.CellCodeValue(x, y) == "ENEMY")
  69. {
  70. enemies.Add(new Enemy(Content, x, y));
  71. }
  72. }
  73. }
  74. currentLevel = levelNumber;
  75. respawnLocation = player.WorldLocation;
  76. }
  77. public static void ReloadLevel()
  78. {
  79. Vector2 saveRespawn = respawnLocation;
  80. LoadLevel(currentLevel);
  81. respawnLocation = saveRespawn;
  82. player.WorldLocation = respawnLocation;
  83. }
  84. public static void Update(GameTime gameTime)
  85. {
  86. if (!player.Dead)
  87. {
  88. checkCurrentCellCode();
  89. for (int x = gemstones.Count - 1; x >= 0; x--)
  90. {
  91. gemstones[x].Update(gameTime);
  92. if (player.CollisionRectangle.Intersects(
  93. gemstones[x].CollisionRectangle))
  94. {
  95. gemstones.RemoveAt(x);
  96. player.Score += 10;
  97. }
  98. }
  99. for (int x = enemies.Count - 1; x >= 0; x--)
  100. {
  101. enemies[x].Update(gameTime);
  102. if (!enemies[x].Dead)
  103. {
  104. if (player.CollisionRectangle.Intersects(
  105. enemies[x].CollisionRectangle))
  106. {
  107. if (player.WorldCenter.Y < enemies[x].WorldLocation.Y)
  108. {
  109. player.Jump();
  110. player.Score += 5;
  111. enemies[x].PlayAnimation("die");
  112. enemies[x].Dead = true; ;
  113. }
  114. else
  115. {
  116. player.Kill();
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if (!enemies[x].Enabled)
  123. {
  124. enemies.RemoveAt(x);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. public static void Draw(SpriteBatch spriteBatch)
  131. {
  132. foreach (Gemstone gem in gemstones)
  133. gem.Draw(spriteBatch);
  134. foreach (Enemy enemy in enemies)
  135. enemy.Draw(spriteBatch);
  136. }
  137. }
  138. }