Tengo un problema con las colisiones, me detecta paredes donde lo hay nada, y las paredes no las asigna tan bien.
main.cpp
#include <iostream>
#include "Player.h"
#include "MapCell.h"
#include "GameMap.h"
using namespace std;
int main()
{
bool isGameOver = false;
GameMap Map;
Player Hero;
while(isGameOver == false)
{
//Aqui es el loop de nuestro juego
Hero.CallInput();
//Actualizado de información
if(Map.SetPlayerCell(Hero.x,Hero.y))
{
//Aqui dibujamos el mapa
Map.Draw();
}else{
Hero.ResetToSafePosition();
cout<<"Reseteado"<<endl;
Map.Draw();
}
}
return 0;
}
Player.cpp
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player()
{
//ctor
}
void Player::CallInput()
{
char UserInput = ' ';
cin>>UserInput;
lastY = y;
lastX =x;
switch(UserInput)
{
case 'a':
y--;
break;
case 's':
x++;
break;
case 'w':
x--;
break;
case 'd':
y++;
break;
}
}
void Player::ResetToSafePosition()
{
x = lastX;
y = lastY;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
public:
Player();
void CallInput();
void ResetToSafePosition();
int x = 1,y = 1;
int lastX, lastY;
protected:
private:
};
#endif // PLAYER_H
GameMap.cpp
#include "GameMap.h"
#include <iostream>
#include <fstream>
using namespace std;
GameMap::GameMap()
{
//ctor
PlayerCell = NULL;
LoadMapFromFile();
}
void GameMap::Draw()
{
for(int i = 0; i < 15 ; i++)
{
for(int p = 0;p < 10; p++)
{
cout<<cells[i][p].id;
}
cout<<endl;
}
}
bool GameMap::SetPlayerCell(int PlayerX, int PlayerY)
{
if(cells[PlayerY][PlayerX].IsBlocked() == false)
{
if(PlayerCell != NULL)
{
PlayerCell->id = ' ';
}
PlayerCell = &cells[PlayerX][PlayerY];
PlayerCell->id = 'S';
return true;
}else{
return false;
}
//cout<<"Las coordenadas del jugador son: "<<PlayerX<<","<<PlayerY<<endl;
}
void GameMap::LoadMapFromFile()
{
/*ofstream FileCreated("Map.txt");
if(FileCreated.is_open()){
}else{
cout<<"ERROR : MAP FILE COULD NOT BE CREATED"<<endl;
}
*/
string line;
int row = 0;
ifstream MyFile("Map.txt");
if(MyFile.is_open())
{
while(getline(MyFile,line))
{
for(int p = 0;p<line.length();p++)
{
if(line[p] == '0')
{
cells[row][p].id =' ';
}else
{
cells[row][p].id = line[p];
}
}
row++;
}
}else{
cout<<"ERROR : MAP FILE COULD NOT BE LOADED"<<endl;
}
}