aboutsummaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-21 16:12:16 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-21 16:12:16 +0100
commitcaeb26e90ef09fdad85af72f460644e09d3b2c7c (patch)
treefa24199fac41284ddb5bcf6855a0b094de7582a2 /src/game.cpp
parent0cebd4ae751e0554f948389654fe4ad7e92943f0 (diff)
downloadmario_sokoban-caeb26e90ef09fdad85af72f460644e09d3b2c7c.tar.gz
mario_sokoban-caeb26e90ef09fdad85af72f460644e09d3b2c7c.tar.bz2
mario_sokoban-caeb26e90ef09fdad85af72f460644e09d3b2c7c.zip
Player sprite direction, read map file
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp134
1 files changed, 88 insertions, 46 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 1ab4ab8..cdc8937 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1,52 +1,29 @@
#include "game.hpp"
-#define EMPTY_CHAR ' '
-#define WALL_CHAR '#'
-#define CRATE_CHAR 'U'
-#define PAYLOAD_CHAR '*'
-#define MARIO_CHAR 'm'
+#define EMPTY_CHAR ' '
+#define WALL_CHAR '#'
+#define CRATE_CHAR '$'
+#define CRATE_SOLVED_CHAR '*'
+#define PAYLOAD_CHAR '.'
+#define MARIO_CHAR '@'
Game::Game(std::string fmt)
: m_playerDirection(DirectionDown)
{
- size_t p;
+ construct(fmt);
+}
- m_height = std::count(fmt.begin(), fmt.end(), '\n');
- m_grid = new Cell*[m_height];
- m_width = fmt.find("\n");
- for (int i = 0; (p = fmt.find("\n")) != std::string::npos; i++)
- {
- std::string token = fmt.substr(0, p);
- m_grid[i] = new Cell[m_width];
- for (size_t j = 0; j < token.size(); j++)
- {
- switch (token[j])
- {
- case EMPTY_CHAR:
- m_grid[i][j] = CellEmpty;
- break;
- case WALL_CHAR:
- m_grid[i][j] = CellWall;
- break;
- case CRATE_CHAR:
- m_grid[i][j] = CellEmpty;
- m_cratePos.push_back(makePos(i, j));
- break;
- case PAYLOAD_CHAR:
- m_grid[i][j] = CellEmpty;
- m_payloadPos.push_back(makePos(i, j));
- break;
- case MARIO_CHAR:
- m_grid[i][j] = CellEmpty;
- m_playerPos.y = i;
- m_playerPos.x = j;
- break;
- default:
- exit(1);
- }
- }
- fmt.erase(0, p + 1);
- }
+Game::Game(std::ifstream &file)
+ : m_playerDirection(DirectionDown)
+{
+ std::string fmt;
+ std::string line;
+
+ if (!file)
+ exit(1);
+ while (getline(file, line))
+ fmt += line + "\n";
+ construct(fmt);
}
Game::~Game()
@@ -131,7 +108,73 @@ Game::Position const &Game::getPlayer() const
return m_playerPos;
}
-#include <iostream>
+Game::Direction Game::getPlayerDirection() const
+{
+ return m_playerDirection;
+}
+
+void Game::construct(std::string fmt)
+{
+ size_t p;
+
+ m_height = std::count(fmt.begin(), fmt.end(), '\n');
+ m_grid = new Cell*[m_height];
+ findWidth(fmt);
+ for (int i = 0; (p = fmt.find("\n")) != std::string::npos; i++)
+ {
+ std::string token = fmt.substr(0, p);
+ m_grid[i] = new Cell[m_width];
+ for (size_t j = 0; j < m_width; j++)
+ m_grid[i][j] = CellEmpty;
+ for (size_t j = 0; j < token.size(); j++)
+ {
+ switch (token[j])
+ {
+ case EMPTY_CHAR:
+ m_grid[i][j] = CellEmpty;
+ break;
+ case WALL_CHAR:
+ m_grid[i][j] = CellWall;
+ break;
+ case CRATE_CHAR:
+ m_grid[i][j] = CellEmpty;
+ m_cratePos.push_back(makePos(i, j));
+ break;
+ case CRATE_SOLVED_CHAR:
+ m_grid[i][j] = CellEmpty;
+ m_cratePos.push_back(makePos(i, j));
+ m_payloadPos.push_back(makePos(i, j));
+ break;
+ case PAYLOAD_CHAR:
+ m_grid[i][j] = CellEmpty;
+ m_payloadPos.push_back(makePos(i, j));
+ break;
+ case MARIO_CHAR:
+ m_grid[i][j] = CellEmpty;
+ m_playerPos.y = i;
+ m_playerPos.x = j;
+ break;
+ default:
+ std::cerr << "Cannot parse map" << std::endl;
+ exit(1);
+ }
+ }
+ fmt.erase(0, p + 1);
+ }
+}
+
+void Game::findWidth(std::string fmt)
+{
+ size_t p;
+ m_width = 0;
+
+ for (int i = 0; (p = fmt.find("\n")) != std::string::npos; i++)
+ {
+ m_width = std::max(m_width, p);
+ fmt.erase(0, p + 1);
+ }
+}
+
bool Game::tryMoveCrate(Position &pos, Direction direction)
{
Position clone = pos;
@@ -150,9 +193,8 @@ bool Game::tryMoveCrate(Position &pos, Direction direction)
case DirectionRight:
clone.x++;
}
- // std::cout << pos.y << ", " << pos.x << "\n";
- // std::cout << get(pos) << std::endl;
- if (!validPosition(clone) || get(clone) == CellCrate || get(clone) == CellWall)
+ if (!validPosition(clone) || get(clone) == CellCrate
+ || get(clone) == CellCrateSolved || get(clone) == CellWall)
return false;
pos = clone;
return true;