//Adds an object for the sprites. //This defines the sprites, gives them an object with positions etc. public void DefineSymbolsAndPos(string world) { int indexX = 0; int indexY = 0; foreach (char c in world) { indexX += 1; switch (c) { case '@': player = new Player(); player.setPositionX(indexX); player.setPositionY(indexY); break; case 'O': Stone tempStone = new Stone(); tempStone.posX = indexX; tempStone.posY = indexY; stoneList.Add(tempStone); break; case 'P': Potion tempPotion = new Potion(); tempPotion.posX = indexX; tempPotion.posY = indexY; potionList.Add(tempPotion); break; case '?': Treasure tempTreasure = new Treasure(); tempTreasure.posX = indexX; tempTreasure.posY = indexY; treasureList.Add(tempTreasure); break; case '/': stairs = new Stairs(); stairs.posX = indexX; stairs.posY = indexY; break; case 'M': Monster tempMonster = new Monster(); tempMonster.setPositionX(indexX); tempMonster.setPositionY(indexY); monsterList.Add(tempMonster); break; case '#': Wall tempWall = new Wall(); tempWall.posX = indexX; tempWall.posY = indexY; wallList.Add(tempWall); break; default: break; } if (c == '\n') { indexX = 0; indexY += 1; } else if (c == '\r') { } else { Console.SetCursorPosition(indexX, indexY); Console.Write(c); } } AMOUNT_OF_COLS = indexX; AMOUNT_OF_ROWS = indexY; }
//The collisioncheck, this function runs after the player and monsters has changed their position. //It checks everything in the world. //ToFix: Give only moving creatures a function for the collisioncheck. Helps readability. public void CheckCollisions() { //To fix: Player and monster and collide when there is something unpassable on the same // spot they change position to. //Stairs and Player collision foreach (Monster m in monsterList) { //Monster and Player collision if (Collision.HasCollided(player, m)) { encounteredMonster = m; player.setPositionX(player.prevPosX); player.setPositionY(player.prevPosY); m.setPositionX(m.prevPosX); m.setPositionY(m.prevPosY); player.SetHealth(-m.damage); m.SetHealth(-player.damage); //gameIsRunning = false; if (m.isDead()) { monsterList.Remove(m); encounteredMonster = null; break; } else if (player.isDead()) { gameIsRunning = false; break; } } //Potion and Monster collision foreach (Potion p in potionList) { if (Collision.HasCollided(m, p)) { potionList.Remove(p); break; } } } foreach (Potion p in potionList) { //Potion and Player collision if (Collision.HasCollided(player, p)) { player.SetHealth(p.giveHP); } } foreach (Stone s in stoneList) { foreach (Wall w in wallList) { if (Collision.HasCollided(w, s)) { gameIsRunning = false; //Sätter positionen till föregående position. //s.posX = s.prevPosX; //s.posY = s.prevPosY; } } } foreach (Wall w in wallList) { if (Collision.HasCollided(player, w)) { player.posX = player.prevPosX; player.posY = player.prevPosY; } foreach (Monster m in monsterList) { if (Collision.HasCollided(m, w)) { m.posX = m.prevPosX; m.posY = m.prevPosY; } } } foreach (Treasure t in treasureList) { if (Collision.HasCollided(player, t)) { } } }