-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cpp
86 lines (62 loc) · 1.99 KB
/
GameController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <algorithm>
#include "GameController.hpp"
#include "GameCommands.hpp"
#include "GameObject.hpp"
#include "GameClass.hpp"
#include "Level.hpp"
static ObjectRef getPlayer (const Level& level) {
for (const auto& object : level) {
if (object != nullptr && object->getGameClass ().getId () == 'h') {
return object;
}
}
return nullptr;
}
static Coords operator+ (const Coords& coords, Direction dir) {
switch (dir) {
case Direction::Left :
return {coords.x - 1, coords.y};
case Direction::Right :
return {coords.x + 1, coords.y};
case Direction::Up :
return {coords.x, coords.y - 1};
case Direction::Down :
return {coords.x, coords.y + 1};
default:
return {-1, -1};
}
}
GameController::GameController (const Level& level, const DisplayFunction& displayFunction) :
level (level),
player (getPlayer (level)),
displayFunction (displayFunction) {
displayFunction (data, level); //displaying the game initially
}
bool GameController::isFinished () const {
return data.state != GameState::Running;
}
GameState GameController::getState () const {
return data.state;
}
void GameController::tryPlayerMove (Direction direction) {
if (isFinished ()) {
return;
}
Coords playerCoords = level.getObjectCoords (player);
Coords playerDestination = playerCoords + direction;
if (level.isValidCoord (playerDestination)) {
if (!level.isEmpty (playerDestination)) {
ObjectRef targetObject = level.getObject (playerDestination);
auto interaction = targetObject->getGameClass ().interactionFunction;
GameCommands commands (*this, targetObject);
interaction (commands);
} else {
level.setObject (playerDestination, player);
level.clear (playerCoords);
}
}
displayFunction (data, level);
}
void GameController::solve () {
//todo
}