/Game/Game/Game/Model/'base'/LevelGenerator.cs
C# | 216 lines | 202 code | 13 blank | 1 comment | 81 complexity | 993cb137c28610d1dc5470555ac744b1 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
-
- namespace Game.Model
- {
- static class LevelGenerator
- {
- private const char LITTLE_ROUND_ENEMY = 'N';
- private const char JUMPING_START_ENEMY = 'J';
- private const char BOSS_ENEMY = 'A';
-
- private const char HUNDRED_POINTS = '1';
- private const char TWOHUNDRED_POINTS = '2';
- private const char THREEHUNDRED_POINTS = '3';
-
- private const char HEALTH_BOX = 'H';
- private const char TRAMPOLINE = 'T';
-
- private const char PLAYER_SPAWN_LOCATION = 'C';
- private const char NEXT_LEVEL_SPACE_SHIP_LOCATION = 'D';
-
- private const char BLACK_BLOCK = 'b';
- private const char FAKE_BLACK_BLOCK = 'B';
- private const char SHIFTING_BLOCK = 'E';
-
- private const char NAILTRAP = 's';
- private const char FAKE_WINDOW = 'w';
- private const char REAL_WINDOW = 'W';
-
- private const char LEFT_PORTAL_EDGE = 'I';
- private const char BOTTOM_PORTAL = 'O'; //(stort o)
- private const char RIGHT_PORTAL_EDGE = 'K';
- private const char PORTAL = 'o';
- private const char RIGHT_PORAL_WALL = 'k';
- private const char LEFT_PORTAL_WALL = 'i';
- private const char LEFT_BOTTOM_PORTAL_EDGE = 'l';
- private const char RIGHT_BOTTOM_PORTAL_EDGE = 'r';
- private const char PORTAL_TOP = 'R';
- private const char SKY = '+';
-
- public static ModelTileType[,] GenerateLevel(ActiveControllerType activeControllerType, ILevelDesignListener levelDesignListener, ILevelContentListener levelContentListener)
- {
- String levelTextFileName = activeControllerType + ".txt";
- List<String> lines = new List<string>();
- string line;
- using (System.IO.StreamReader file = new System.IO.StreamReader(levelTextFileName))
- {
- while ((line = file.ReadLine()) != null)
- {
- lines.Add(line);
- }
- }
- //Mĺste vända upp och ner pĺ allt som lästes in.
- int levelWidth = lines[0].Length;
- int levelHeight = lines.Count;
- ModelTileType[,] tiles = new ModelTileType[levelWidth, levelHeight];
-
- for (int y = lines.Count - 1; y >= 0; y--)
- {
- line = lines[lines.Count - 1 - y];
- for (int x = 0; x < line.Length; x++)
- {
- if (line[x] == SKY)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == BLACK_BLOCK)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
- }
- else if (line[x] == FAKE_WINDOW)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.FakeWindow);
- }
- else if (line[x] == NAILTRAP)
- {
- tiles[x, y] = ModelTileType.Trap;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.NailTrap);
- }
- else if (line[x] == LEFT_PORTAL_EDGE)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftPortalEdge);
- }
- else if (line[x] == LEFT_PORTAL_WALL)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftPortalWall);
- }
- else if (line[x] == LEFT_BOTTOM_PORTAL_EDGE)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.LeftBottomPortalEdge);
- }
- else if (line[x] == BOTTOM_PORTAL)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BottomPortal);
- }
- else if (line[x] == RIGHT_BOTTOM_PORTAL_EDGE)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightBottomPortalEdge);
- }
- else if (line[x] == RIGHT_PORAL_WALL)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightPortalWall);
- }
- else if (line[x] == RIGHT_PORTAL_EDGE)
- {
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.RightPortalEdge);
- }
- else if (line[x] == PORTAL)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Portal);
- }
- else if (line[x] == FAKE_BLACK_BLOCK)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
- }
- else if (line[x] == PORTAL_TOP)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.PortalTop);
- }
-
- else if (line[x] == LITTLE_ROUND_ENEMY)
- {
- levelContentListener.AddLittleRoundEnemy(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == JUMPING_START_ENEMY)
- {
- levelContentListener.AddRotatingStarEnemy(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
-
- }
- else if (line[x] == BOSS_ENEMY)
- {
- levelContentListener.AddBossEnemy(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == HUNDRED_POINTS)
- {
- levelContentListener.AddPoints(new Vector2(x, y), 100);
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == TWOHUNDRED_POINTS)
- {
- levelContentListener.AddPoints(new Vector2(x, y), 200);
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == THREEHUNDRED_POINTS)
- {
- levelContentListener.AddPoints(new Vector2(x, y), 300);
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == HEALTH_BOX)
- {
- levelContentListener.AddHealthBox(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
-
- else if (line[x] == PLAYER_SPAWN_LOCATION)
- {
- levelContentListener.SetPlayerStartPosition(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == NEXT_LEVEL_SPACE_SHIP_LOCATION)
- {
- levelContentListener.SetSpaceShipLocation(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == TRAMPOLINE)
- {
- levelContentListener.AddTrampoline(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Sky);
- }
- else if (line[x] == SHIFTING_BLOCK)
- {
- levelContentListener.AddShiftingBlock(new Vector2(x, y));
- tiles[x, y] = ModelTileType.Block;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.BlackBlock);
- }
- else if (line[x] == REAL_WINDOW)
- {
- tiles[x, y] = ModelTileType.Empty;
- levelDesignListener.AddViewTile(new Vector2(x, y), ViewTileType.Window);
- }
- }
- }
- return tiles;
- }
-
- }
- }