//reloads the current level private void startLevel(RawLevel level) { gameState = GameState.PLAYING; loadedLevel = false; Player.isDead = false; Player.PosX = Block.BLOCK_WIDTH; Player.PosY = Block.BLOCK_WIDTH; Player.XVel = 0; Player.YVel = 0; Player.XAcc = 0; Player.YAcc = 0; Engine.xOffset = 0; Engine.yOffset = 0; //Entity.State = EntityState.GOOD; loadedLevel = _currentLevel.LoadLevel(this, level, tileMap); }
/* * Parse a level from a tile, the level must have two states in the current implementation, line numbers are hard coded */ public static RawLevel ParseTextFile(string filename) { try { using (StreamReader reader = new StreamReader(filename)) { string state1Line = null; string state2Line = null; string characterLine = null; string widthLine = null; string heightLine = null; int lineNum = 1; while (!reader.EndOfStream) { /* * Pull relevant lines from the file */ string line = reader.ReadLine(); if (lineNum == 4) state1Line = line; if (lineNum == 15) state2Line = line; if (lineNum == 5) heightLine = line; if (lineNum == 10) widthLine = line; if(lineNum == 26) characterLine = line; lineNum++; } Debug.WriteLine("State1:" +state1Line); Debug.WriteLine("State2:" + state2Line); Debug.WriteLine("Character:" + characterLine); Debug.WriteLine("Width:" + widthLine); Debug.WriteLine("Height:" + heightLine); /* * Extract values from lines * */ string[] state1Data = state1Line.Split( new[] { '[', ']' })[1].Split(new[] { ',' }); string[] state2Data = state2Line.Split( new[] { '[', ']' })[1].Split(new[] { ',' }); string[] characterData = characterLine.Split(new[] { '[', ']' })[1].Split(new[] { ',' }); int height = int.Parse(heightLine.Split(new[] { ':', ',' })[1]); int width = int.Parse(widthLine.Split(new[] { ':',',' })[1]); //foreach (string s in state1Data) //Debug.WriteLine(s); int[] state1Values = new int[width * height]; int[] state2Values = new int[width * height]; int[] characterValues = new int[width * height]; for( int i = 0 ; i < width * height ; i ++) { state1Values[i] = int.Parse(state1Data[i]); state2Values[i] = int.Parse(state2Data[i]); characterValues[i] = int.Parse(characterData[i]); } RawLevel rawlevel = new RawLevel(); rawlevel.height = height; rawlevel.width = width; rawlevel.State1 = state1Values; rawlevel.State2 = state2Values; rawlevel.Characters = characterValues; return rawlevel; } } catch (Exception e) { Debug.WriteLine("Could not read file:" +e); } return null; }
public Boolean LoadLevel(Engine game, RawLevel rawLevel, TileMap tileMap) { _blocks = new LinkedList<Block>(); _characters = new LinkedList<Character>(); _entities = new LinkedList<Entity>(); _raw = rawLevel; CharacterLibrary characterLibrary = new CharacterLibrary( tileMap, tileMap.Width / tileMap.TileSize, tileMap.Height / tileMap.TileSize ); TileLibrary tileLibrary = new TileLibrary( tileMap.Width / tileMap.TileSize , tileMap.Height / tileMap.TileSize ); for (int y = 0; y < rawLevel.height; y++) { for (int x = 0; x < rawLevel.width; x++) { /* * Check for blocks */ int blockIDGood = rawLevel.State1[y * rawLevel.width + x]; int blockIDBad = rawLevel.State2[y * rawLevel.width + x]; Block b = new Block(tileMap, blockIDGood, blockIDBad) { GridX = x, GridY = y }; b.AssignBehaviour( new Dictionary<EntityState, List<Behaviour>>() { {EntityState.GOOD, tileLibrary.get(blockIDGood)}, {EntityState.BAD, tileLibrary.get(blockIDBad)} }); _blocks.AddLast(b); if (!(b.Behaviours[EntityState.GOOD].Count == 0 && b.Behaviours[EntityState.BAD].Count == 0)) _entities.AddLast(b); /* * Check for characters */ int characterId = rawLevel.Characters[y * rawLevel.width + x]; if(characterId != 0){ Character c = characterLibrary.get(characterId); c.PosX = x * Block.BLOCK_WIDTH; c.PosY = y * Block.BLOCK_WIDTH; if (c is Boss) Debug.WriteLine(c.PosX + " , " + c.PosY); _characters.AddFirst(c); _entities.AddFirst(c); } } } _characters.AddFirst(Engine.Player); _entities.AddFirst(Engine.Player); foreach (Entity e in _entities) e.SetLevel(this); return true; }