Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#include "map.h"
void Map::load_map(std::string map_file, Player &player, std::vector<Enemy>& enemies){
std::ifstream file(map_file);
std::string input;
while(getline(file, input)){
map_data.push_back(input);
}
for(int i = 0; i < map_data.size(); i++){
for(int j = 0; j < map_data[i].size(); j++){
char tile = map_data[i][j];
switch(tile){
case '@':
{
player.set_position(j, i);
break;
}
case 'o':
{
Orc newOrc;
newOrc.set_enemypos(j,i);
enemies.push_back(newOrc);
break;
}
case 'u':
{
Undead newUndead;
newUndead.set_enemypos(j,i);
enemies.push_back(newUndead);
break;
}
case 'l':
{
Lizardman newLizardman;
newLizardman.set_enemypos(j,i);
enemies.push_back(newLizardman);
break;
}
}
}
}
}
void Map::print_map(){
for(std::string i: map_data){
std::cout << i << std::endl;
}
}
void Map::move_player(Player &player){
char input = player.player_input();
int x = player.get_x();
int y = player.get_y();
switch(input){
case 'w':
if(map_data[y-1][x] == '#'){
std::cout << "you bumped into a wall" << std::endl;
} else {
map_data[y][x] = '.';
map_data[y-1][x] = '@';
player.set_position(x, y-1);
}
break;
case 's':
if(map_data[y + 1][x] == '#'){
std::cout << "you bumped into a wall" << std::endl;
} else {
map_data[y][x] = '.';
map_data[y + 1][x] = '@';
player.set_position(x, y + 1);
}
break;
case 'a':
if(map_data[y][x - 1] == '#'){
std::cout << "you bumped into a wall" << std::endl;
} else {
map_data[y][x] = '.';
map_data[y][x - 1] = '@';
player.set_position(x - 1, y);
}
break;
case 'd':
if(map_data[y][x + 1] == '#'){
std::cout << "you bumped into a wall" << std::endl;
} else {
map_data[y][x] = '.';
map_data[y][x + 1] = '@';
player.set_position(x + 1, y);
}
break;
}
}
void Map::generate_graph(Player &player){
graph.resize(50, std::vector<int>(50, 0));
for(int i = 0; i < map_data.size(); i++){
for(int j = 0; j < map_data[i].size(); j++){
int tilex = j;
int tiley = i;
int playerx = player.get_x();
int playery = player.get_y();
if(map_data[i][j] == '#'){
graph[i][j] = 999;
} else{
int distance = round(sqrt(pow((tiley - playery), 2) + pow((tilex - playerx), 2)));
graph[i][j] = distance;
}
}
}
}
void Map::move_enemy(std::vector<Enemy>& enemies){
for(int i = 0; i < enemies.size(); i++){
char character = enemies[i].get_char();
int x = enemies[i].get_enemy_x();
int y = enemies[i].get_enemy_y();
std::vector<int> queue;
queue.push_back(graph[y][x + 1]);
queue.push_back(graph[y][x - 1]);
queue.push_back(graph[y+1][x]);
queue.push_back(graph[y-1][x]);
queue.push_back(graph[y + 1][x + 1]);
queue.push_back(graph[y - 1][x - 1]);
queue.push_back(graph[y - 1][x + 1]);
queue.push_back(graph[y + 1][x - 1]);
int min = *min_element(queue.begin(), queue.end());
if(graph[y][x+1] == min){
if(map_data[y][x + 1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y][x + 1] = character;
enemies[i].set_enemypos(x + 1, y);
}
}
else if(graph[y][x - 1] == min){
if(map_data[y][x - 1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y][x - 1] = character;
enemies[i].set_enemypos(x - 1, y);
}
}
else if(graph[y + 1][x] == min){
if(map_data[y + 1][x] != '.'){
} else {
map_data[y][x] = '.';
map_data[y + 1][x] = character;
enemies[i].set_enemypos(x, y + 1);
}
}
else if(graph[y - 1][x] == min){
if(map_data[y-1][x] != '.'){
} else {
map_data[y][x] = '.';
map_data[y-1][x] = character;
enemies[i].set_enemypos(x, y-1);
}
}
else if(graph[y + 1][x + 1] == min){
if(map_data[y+1][x+1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y + 1][x + 1] = character;
enemies[i].set_enemypos(x + 1, y + 1);
}
}
else if(graph[y - 1][x - 1] == min){
if(map_data[y-1][x - 1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y - 1][x - 1] = character;
enemies[i].set_enemypos(x - 1, y - 1);
}
}
else if(graph[y - 1][x + 1] == min){
if(map_data[y-1][x + 1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y - 1][x + 1] = character;
enemies[i].set_enemypos(x + 1, y - 1);
}
}
else if (graph[y + 1][x - 1] == min){
if(map_data[y+1][x-1] != '.'){
} else {
map_data[y][x] = '.';
map_data[y + 1][x - 1] = character;
enemies[i].set_enemypos(x - 1, y + 1);
}
}
}
}