PageRenderTime 3750ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 0ms

/Game/Game/Game/Model/'base'/LevelGenerator.cs

http://ap222cb.googlecode.com/
C# | 216 lines | 202 code | 13 blank | 1 comment | 81 complexity | 993cb137c28610d1dc5470555ac744b1 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace Game.Model
  7. {
  8. static class LevelGenerator
  9. {
  10. private const char LITTLE_ROUND_ENEMY = 'N';
  11. private const char JUMPING_START_ENEMY = 'J';
  12. private const char BOSS_ENEMY = 'A';
  13. private const char HUNDRED_POINTS = '1';
  14. private const char TWOHUNDRED_POINTS = '2';
  15. private const char THREEHUNDRED_POINTS = '3';
  16. private const char HEALTH_BOX = 'H';
  17. private const char TRAMPOLINE = 'T';
  18. private const char PLAYER_SPAWN_LOCATION = 'C';
  19. private const char NEXT_LEVEL_SPACE_SHIP_LOCATION = 'D';
  20. private const char BLACK_BLOCK = 'b';
  21. private const char FAKE_BLACK_BLOCK = 'B';
  22. private const char SHIFTING_BLOCK = 'E';
  23. private const char NAILTRAP = 's';
  24. private const char FAKE_WINDOW = 'w';
  25. private const char REAL_WINDOW = 'W';
  26. private const char LEFT_PORTAL_EDGE = 'I';
  27. private const char BOTTOM_PORTAL = 'O'; //(stort o)
  28. private const char RIGHT_PORTAL_EDGE = 'K';
  29. private const char PORTAL = 'o';
  30. private const char RIGHT_PORAL_WALL = 'k';
  31. private const char LEFT_PORTAL_WALL = 'i';
  32. private const char LEFT_BOTTOM_PORTAL_EDGE = 'l';
  33. private const char RIGHT_BOTTOM_PORTAL_EDGE = 'r';
  34. private const char PORTAL_TOP = 'R';
  35. private const char SKY = '+';
  36. public static ModelTileType[,] GenerateLevel(ActiveControllerType activeControllerType, ILevelDesignListener levelDesignListener, ILevelContentListener levelContentListener)
  37. {
  38. String levelTextFileName = activeControllerType + ".txt";
  39. List<String> lines = new List<string>();
  40. string line;
  41. using (System.IO.StreamReader file = new System.IO.StreamReader(levelTextFileName))
  42. {
  43. while ((line = file.ReadLine()) != null)
  44. {
  45. lines.Add(line);
  46. }
  47. }
  48. //Mĺste vända upp och ner pĺ allt som lästes in.
  49. int levelWidth = lines[0].Length;
  50. int levelHeight = lines.Count;
  51. ModelTileType[,] tiles = new ModelTileType[levelWidth, levelHeight];
  52. for (int y = lines.Count - 1; y >= 0; y--)
  53. {
  54. line = lines[lines.Count - 1 - y];
  55. for (int x = 0; x < line.Length; x++)
  56. {
  57. if (line[x] == SKY)
  58. {
  59. tiles[x, y] = ModelTileType.Empty;
  60. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  61. }
  62. else if (line[x] == BLACK_BLOCK)
  63. {
  64. tiles[x, y] = ModelTileType.Block;
  65. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
  66. }
  67. else if (line[x] == FAKE_WINDOW)
  68. {
  69. tiles[x, y] = ModelTileType.Empty;
  70. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.FakeWindow);
  71. }
  72. else if (line[x] == NAILTRAP)
  73. {
  74. tiles[x, y] = ModelTileType.Trap;
  75. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.NailTrap);
  76. }
  77. else if (line[x] == LEFT_PORTAL_EDGE)
  78. {
  79. tiles[x, y] = ModelTileType.Block;
  80. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftPortalEdge);
  81. }
  82. else if (line[x] == LEFT_PORTAL_WALL)
  83. {
  84. tiles[x, y] = ModelTileType.Block;
  85. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftPortalWall);
  86. }
  87. else if (line[x] == LEFT_BOTTOM_PORTAL_EDGE)
  88. {
  89. tiles[x, y] = ModelTileType.Block;
  90. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftBottomPortalEdge);
  91. }
  92. else if (line[x] == BOTTOM_PORTAL)
  93. {
  94. tiles[x, y] = ModelTileType.Empty;
  95. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BottomPortal);
  96. }
  97. else if (line[x] == RIGHT_BOTTOM_PORTAL_EDGE)
  98. {
  99. tiles[x, y] = ModelTileType.Block;
  100. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightBottomPortalEdge);
  101. }
  102. else if (line[x] == RIGHT_PORAL_WALL)
  103. {
  104. tiles[x, y] = ModelTileType.Block;
  105. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightPortalWall);
  106. }
  107. else if (line[x] == RIGHT_PORTAL_EDGE)
  108. {
  109. tiles[x, y] = ModelTileType.Block;
  110. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightPortalEdge);
  111. }
  112. else if (line[x] == PORTAL)
  113. {
  114. tiles[x, y] = ModelTileType.Empty;
  115. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Portal);
  116. }
  117. else if (line[x] == FAKE_BLACK_BLOCK)
  118. {
  119. tiles[x, y] = ModelTileType.Empty;
  120. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
  121. }
  122. else if (line[x] == PORTAL_TOP)
  123. {
  124. tiles[x, y] = ModelTileType.Empty;
  125. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.PortalTop);
  126. }
  127. else if (line[x] == LITTLE_ROUND_ENEMY)
  128. {
  129. levelContentListener.AddLittleRoundEnemy(new Vector2(x, y));
  130. tiles[x, y] = ModelTileType.Empty;
  131. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  132. }
  133. else if (line[x] == JUMPING_START_ENEMY)
  134. {
  135. levelContentListener.AddRotatingStarEnemy(new Vector2(x, y));
  136. tiles[x, y] = ModelTileType.Empty;
  137. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  138. }
  139. else if (line[x] == BOSS_ENEMY)
  140. {
  141. levelContentListener.AddBossEnemy(new Vector2(x, y));
  142. tiles[x, y] = ModelTileType.Empty;
  143. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  144. }
  145. else if (line[x] == HUNDRED_POINTS)
  146. {
  147. levelContentListener.AddPoints(new Vector2(x, y), 100);
  148. tiles[x, y] = ModelTileType.Empty;
  149. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  150. }
  151. else if (line[x] == TWOHUNDRED_POINTS)
  152. {
  153. levelContentListener.AddPoints(new Vector2(x, y), 200);
  154. tiles[x, y] = ModelTileType.Empty;
  155. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  156. }
  157. else if (line[x] == THREEHUNDRED_POINTS)
  158. {
  159. levelContentListener.AddPoints(new Vector2(x, y), 300);
  160. tiles[x, y] = ModelTileType.Empty;
  161. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  162. }
  163. else if (line[x] == HEALTH_BOX)
  164. {
  165. levelContentListener.AddHealthBox(new Vector2(x, y));
  166. tiles[x, y] = ModelTileType.Empty;
  167. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  168. }
  169. else if (line[x] == PLAYER_SPAWN_LOCATION)
  170. {
  171. levelContentListener.SetPlayerStartPosition(new Vector2(x, y));
  172. tiles[x, y] = ModelTileType.Empty;
  173. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  174. }
  175. else if (line[x] == NEXT_LEVEL_SPACE_SHIP_LOCATION)
  176. {
  177. levelContentListener.SetSpaceShipLocation(new Vector2(x, y));
  178. tiles[x, y] = ModelTileType.Empty;
  179. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  180. }
  181. else if (line[x] == TRAMPOLINE)
  182. {
  183. levelContentListener.AddTrampoline(new Vector2(x, y));
  184. tiles[x, y] = ModelTileType.Empty;
  185. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
  186. }
  187. else if (line[x] == SHIFTING_BLOCK)
  188. {
  189. levelContentListener.AddShiftingBlock(new Vector2(x, y));
  190. tiles[x, y] = ModelTileType.Block;
  191. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
  192. }
  193. else if (line[x] == REAL_WINDOW)
  194. {
  195. tiles[x, y] = ModelTileType.Empty;
  196. levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Window);
  197. }
  198. }
  199. }
  200. return tiles;
  201. }
  202. }
  203. }