2
0

LevelManager.cs 5.1 KB

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