diff --git a/src/engine/goblin_engine.cpp b/src/engine/goblin_engine.cpp index bef1eee..75a31f7 100644 --- a/src/engine/goblin_engine.cpp +++ b/src/engine/goblin_engine.cpp @@ -3,10 +3,9 @@ const auto frame_duration = std::chrono::milliseconds(250); -Command::Command(Direction direction, int speed) : direction(Direction::NONE), speed(0) {} +Command::Command(Direction direction) : direction(Direction::NONE) {} -goblin_engine::goblin_engine(unsigned int size_x, unsigned int size_y) : state(size_x, size_y), current_command(Direction::NONE, 0) { -} +goblin_engine::goblin_engine(unsigned int size_x, unsigned int size_y) : state(size_x, size_y), current_command(Direction::NONE) {} goblin_engine::~goblin_engine() = default; @@ -24,12 +23,12 @@ void goblin_engine::start() { if(std::chrono::duration_cast(current_time - last_frame) > frame_duration) { auto command = current_command; state.current_frame++; - if (command.direction != Direction::NONE && command.speed > 0) { - // Try to move the goblin speed spots in the specified direction - move_goblin(command.direction, command.speed); + if (command.direction != Direction::NONE) { + // Try to move the goblin in the specified direction + move_goblin(command.direction); } last_frame = current_time; - current_command = Command(Direction::NONE, 0); + current_command = Command(Direction::NONE); } } }).detach(); @@ -39,10 +38,37 @@ Cave_State goblin_engine::get_state() { return state; } -void goblin_engine::command_goblin(Direction direction, unsigned short speed) { - current_command = Command(direction, speed); +void goblin_engine::command_goblin(Direction direction) { + current_command = Command(direction); } -void goblin_engine::move_goblin(Direction direction, unsigned short speed) { +void goblin_engine::move_goblin(Direction direction) { + short x = state.goblin.x; + short y = state.goblin.y; + if (direction == Direction::NORTH) { + y = state.goblin.y > 0 ? --state.goblin.y: state.goblin.y; + } else if (direction == Direction::SOUTH) { + y = state.goblin.y < state.maze.size() ? ++state.goblin.y: state.goblin.y; + } else if (direction == Direction::WEST) { + x = state.goblin.x > 0 ? --state.goblin.x: state.goblin.x; + } else if (direction == Direction::EAST) { + x = state.goblin.x > state.maze.size() ? ++state.goblin.x: state.goblin.x; + } + + if (x >= 0 && x < state.maze[0].size() && y >= 0 && y < state.maze.size()) { + if (state.maze[y][x] != MazeValue::Wall) { + state.goblin.x = x; + state.goblin.y = y; + + if (state.maze[y][x] == MazeValue::Chest) { + state.goblin.gold = 10; + } + + if (state.maze[y][x] == MazeValue::Cart) { + state.cart_gold += state.goblin.gold; + state.goblin.gold = 0; + } + } + } } diff --git a/src/engine/goblin_engine.h b/src/engine/goblin_engine.h index 2ac3747..ec0be04 100644 --- a/src/engine/goblin_engine.h +++ b/src/engine/goblin_engine.h @@ -8,10 +8,9 @@ enum class Direction {NORTH, EAST, SOUTH, WEST, NONE}; struct Command { - Command(Direction direction, int speed); + Command(Direction direction); Direction direction; - unsigned short speed{}; }; // Class that manages the game engine for the Goblin Maze game. @@ -43,12 +42,11 @@ public: /** * This function allows to command the goblin character in the game. * @param direction The cardinal direction to move the goblin. - * @param speed The speed at which the goblin should move. */ - void command_goblin(Direction direction, unsigned short speed); + void command_goblin(Direction direction); private: - void move_goblin(Direction direction, unsigned short speed); + void move_goblin(Direction direction); // The current state of the Cave in the maze. Cave_State state; Command current_command;