Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
RogueLegends/Level.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
246 lines (201 sloc)
5.59 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Level.h" | |
#include <fstream> | |
#include <iostream> | |
#include <cstdio> | |
#include "Monster.h" | |
#include "windows.h" | |
#include "GamePanel.h" | |
//constructor | |
Level::Level() { | |
} | |
//loads the level from the file fileName | |
void Level::load(string fileName, Player &player) { | |
//Loads the level | |
ifstream file; | |
//open the file and quit if it fails to open | |
file.open(fileName); | |
if (file.fail()) { | |
cout << "There was an error openning this file." << endl; | |
exit(1); | |
} | |
string line; | |
while (getline(file, line)) { | |
//adds the current line to _levelData | |
_levelData.push_back(line); | |
} | |
//close the file | |
file.close(); | |
char tile; | |
//process the level, .size() returns the number of elements | |
for (int i = 0; i < _levelData.size(); i++) { | |
//loop through each element in row i | |
for (int j = 0; j < _levelData[i].size(); j++) { | |
//current tile | |
tile = _levelData[i][j]; | |
switch (tile) { | |
case '@': //player is represented by a @ symbol | |
player.setPosition(j, i); //set the position of the player | |
break; | |
case 'G': //goblin | |
_monsters.push_back(Monster("Goblin", tile, 30, 15, 10, 15)); | |
_monsters.back().setPosition(j, i); | |
break; | |
case 'D': //dragon | |
_monsters.push_back(Monster("Dragon", tile, 45, 28, 15, 18)); | |
_monsters.back().setPosition(j, i); | |
break; | |
case 'T': //troll | |
_monsters.push_back(Monster("Troll", tile, 35, 25, 10, 15)); | |
_monsters.back().setPosition(j, i); | |
break; | |
case 'O': //ogre | |
_monsters.push_back(Monster("Ogre", tile, 40, 20, 10, 10)); | |
_monsters.back().setPosition(j, i); | |
case '#': | |
case '.': | |
break; | |
default: //exception, unknown tile | |
cout << "Warning unknown title" << endl; | |
system("Pause"); | |
break; | |
} | |
} | |
} | |
} | |
//print the level | |
void Level::print() { | |
//CLEAR SCREEN! | |
cout << string(100, '\n') << endl; | |
//print each row, .size() returns the numb of rows | |
for (int i = 0; i < _levelData.size(); i++) { | |
cout << _levelData[i] << "\n"; | |
} | |
} | |
//player movement with input w,a,s,d | |
void Level::movePlayer(char input, Player &player) { | |
int playerX; | |
int playerY; | |
//get player position using the reference parameters playerX and playerY | |
player.getPosition(playerX, playerY); | |
//deal with input | |
switch (input) { | |
case 'w': //up | |
case 'W': | |
processPlayerMove(player, playerX, playerY - 1); | |
break; | |
case 's': //down | |
case 'S': | |
processPlayerMove(player, playerX, playerY + 1); | |
break; | |
case 'a': //left | |
case 'A': | |
processPlayerMove(player, playerX - 1, playerY); | |
break; | |
case 'd': //right | |
case 'D': | |
processPlayerMove(player, playerX + 1, playerY); | |
break; | |
default: //invalid input | |
cout << ("INVALID INPUT!\n") << endl; | |
system("PAUSE"); | |
break; | |
} | |
} | |
//get tile | |
char Level::getTile(int x, int y) { | |
return _levelData[y][x]; | |
} | |
//set tile | |
void Level::setTile(int x, int y, char tile) { | |
_levelData[y][x] = tile; | |
} | |
//process the player movement | |
void Level::processPlayerMove(Player &player, int targetX, int targetY) { | |
int playerX; | |
int playerY; | |
player.getPosition(playerX, playerY); | |
//get the tile where the player is moving to | |
char moveTile = getTile(targetX, targetY); | |
//process tile | |
switch (moveTile) { | |
case '.': //if its a '.' symbol, player can move | |
player.setPosition(targetX, targetY); | |
setTile(playerX, playerY, '.'); | |
setTile(targetX, targetY, '@'); | |
break; | |
case '#': //wall | |
break; | |
default: | |
fightMonster(player, targetX, targetY); | |
break; | |
} | |
} | |
void Level::fightMonster(Player &player, int targetX, int targetY) { | |
int monsterX; | |
int monsterY; | |
int playerX; | |
int playerY; | |
int attackNumber; | |
int fightResult; | |
string monsterName; | |
player.getPosition(playerX, playerY); | |
//find which monster to fight | |
for (int i = 0; i < _monsters.size(); i++) { | |
_monsters[i].getPosition(monsterX, monsterY); | |
//If monster coord == target , we found monster | |
if (targetX == monsterX && targetY == monsterY) { | |
//Get the name of the monster | |
monsterName = _monsters[i].getName(); | |
print(); //reprint the board | |
//Player attack | |
attackNumber = player.attack(); | |
cout << "Player attacked " << monsterName << " with: " << attackNumber << "!\n" << endl; | |
fightResult = _monsters[i].dmgTaken(attackNumber); | |
//If fight result is not zero, then the monster died | |
if (fightResult != 0) { | |
setTile(targetX, targetY, '.'); | |
print(); //reprint the board | |
cout << monsterName << " died!\n" << endl; | |
//remove the monster | |
_monsters[i] = _monsters.back(); | |
_monsters.pop_back(); | |
i--; | |
system("PAUSE"); | |
player.addExPoints(fightResult); | |
return; | |
} | |
system("PAUSE"); | |
print(); //reprint the board | |
//Monster attack | |
attackNumber = _monsters[i].attack(); | |
cout << monsterName << " attacked player with: " << attackNumber << "!\n"; | |
fightResult = player.dmgTaken(attackNumber); | |
//If fight result != 0, the player died | |
if (fightResult != 0) { | |
setTile(playerX, playerY, 'x'); | |
print(); | |
printf("You died !\n"); | |
system("PAUSE"); | |
cout << "\nPlease choose the following option:" << endl; | |
cout << "[1] - Play again" << endl; | |
cout << "[2] - Quit the game" << endl; | |
int choice; | |
cin >> choice; | |
if (choice == 1) { | |
cout << " " << endl; | |
Sleep(1000); | |
cout << "...ADVENTURE-LOADING..." << endl; | |
Sleep(1000); | |
cout << " " << endl; | |
GamePanel gamePanel("maplevel1.txt"); | |
gamePanel.playGame(); | |
} | |
else { | |
exit(1); //quit game | |
} | |
} | |
system("PAUSE"); | |
return; | |
} | |
} | |
} |