-
Notifications
You must be signed in to change notification settings - Fork 0
/
stagemediator.cpp
145 lines (124 loc) · 4.37 KB
/
stagemediator.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "stagemediator.h"
#include "gameboard.h"
#include "boardobjectaicontext.h"
#include "gamecontroller.h"
#include "tankaistrategy.h"
#include "projectileaistrategy.h"
#include "userbaseaistrategy.h"
StageMediator::StageMediator(QObject *parent) : QObject(parent) {
_board = new GameBoard(this);
QList<BoardObjectAIStrategy*> strategyList;
try {
if (auto strategy = new TankAIStrategy(this, this, _board)) {
strategyList.append(strategy);
}
if (auto strategy = new ProjectileAIStrategy(this, this, _board)) {
strategyList.append(strategy);
}
if (auto strategy = new UserBaseAIStrategy(this, this, _board)) {
strategyList.append(strategy);
}
if (auto context = new BoardObjectAIContext(this, strategyList, this)) {
_contexts.append(context);
}
}
catch(std::bad_alloc&) {
qDebug("Not enough memory!");
throw;
}
}
StageMediator::~StageMediator() {
}
void StageMediator::sendInit(int stageNumber, int fragsCount, int lifesCount) {
_board->init(this, stageNumber);
_board->createFragObjectsByNumber(fragsCount);
_board->setLifesCount(lifesCount);
}
void StageMediator::setGameControllerDelegate(GameControllerProtocol *controllerDelegate) {
_controllerDelegate = controllerDelegate;
}
void StageMediator::sendMovingDirectionChanged(QString objectId,
AnimatedBoardObject::MovingDirectionType direction) {
for(auto context : _contexts) {
context->setMovingDirection(objectId, direction);
}
}
void StageMediator::sendSetPositionAndRotation(QString objectId, int positionX,
int positionY, int rotation) {
_board->setObjectPositionAndRotationById(objectId, positionX, positionY, rotation);
}
void StageMediator::sendControlKeyPressed(QString objectId, int key) {
for(auto context : _contexts) {
context->controlKeyPressed(objectId, key);
}
}
void StageMediator::sendObjectFired(QString objectId) {
for(auto context : _contexts) {
context->objectFired(objectId);
}
}
void StageMediator::sendHitObject(QString sourceObjectId, QString targetObjectId) {
for(auto context : _contexts) {
context->objectHitByObject(targetObjectId, sourceObjectId);
}
}
void StageMediator::sendRemoveObject(const QString& objectId) {
if (objectId.length() && _board) {
_board->removeObject(objectId);
}
}
void StageMediator::sendObjectDidCreate(QString objectId, AnimatedBoardObject::ObjectType type) {
if (_controllerDelegate) {
_controllerDelegate->objectDidCreate(objectId, type);
}
for(auto context : _contexts) {
context->objectDidCreate(objectId, type);
}
}
void StageMediator::sendObjectDidRemove(QString objectId, AnimatedBoardObject::ObjectType type) {
if (_controllerDelegate) {
_controllerDelegate->objectDidRemove(objectId, type);
}
for(auto context : _contexts) {
context->objectDidRemove(objectId, type);
}
}
void StageMediator::sendSetObjectMovingDirectionById(const QString& objectId,
AnimatedBoardObject::MovingDirectionType movingDirection) {
if (objectId.length() && _board) {
_board->setObjectMovingDirectionById(objectId, movingDirection);
}
}
void StageMediator::sendCreateAnimatedObject(AnimatedBoardObject::ObjectType type,
int positionX,
int positionY,
int rotation,
AnimatedBoardObject::MovingDirectionType movingDirection) {
if (_board) {
_board->createAnimatedObject(type, positionX,
positionY, rotation, movingDirection);
}
}
void StageMediator::sendAddOneFrag(const QString& userId) {
if (_board) {
_board->addOneFrag(userId);
}
}
void StageMediator::sendRemoveOneLife(const QString& userId) {
if (_board) {
_board->removeOneLife(userId);
}
}
GameBoard* StageMediator::getGameBoard() const {
return this->_board;
}
void StageMediator::initFrame() {
for(auto context : _contexts) {
context->init();
}
}
void StageMediator::nextFrame() {
for(auto context : _contexts) {
context->advance();
}
}