-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode_order.cpp
More file actions
271 lines (244 loc) · 8.71 KB
/
mode_order.cpp
File metadata and controls
271 lines (244 loc) · 8.71 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "mode_order.hpp"
using namespace std;
float MAP_SCROLL_SPEED = 300;
ModeOrder::ModeOrder() {
}
void ModeOrder::init() {
RequestQueues::entityRequests.push_back(entRequest::newEntRequest("testUnit", HALF_TILE_WIDTH, HALF_TILE_HEIGHT, ENT_TYPE_UNIT));
entManager->flushRequests();
test = entManager->unitManager->lastCreatedEnt;
test->skills = SKILL_MINING | SKILL_WOODCUT;
leftClicked = true;
rightClicked = true;
screenX = 0;
screenY = 0;
selectionActive = SELECT_TYPE_NONE;
curOrderType = ORDER_MODE_DIG;
digButton = new UIDrawable("./data/images/skills/BoostAtk.png", 0, WINDOW_HEIGHT-40);
woodcutButton = new UIDrawable("./data/images/skills/VoltIcon.png", 40, WINDOW_HEIGHT-40);
buildButton = new UIDrawable("./data/images/skills/HealIcon.png", 80, WINDOW_HEIGHT-40);
}
void ModeOrder::setOrderMode(int orderType) {
curOrderType = orderType;
}
void ModeOrder::createJob(int type, int requirements, Entity* targetEnt, point targetPoint) {
Job* thisJob = new Job();
point* Point = new point();
Point->realX = targetPoint.realX;
Point->realY = targetPoint.realY;
Point->tileX = targetPoint.tileX;
Point->tileY = targetPoint.tileY;
thisJob->requirements = requirements;
thisJob->suspended = false;
thisJob->repeating = false;
thisJob->assigned = NULL;
thisJob->targetEnt = targetEnt;
thisJob->targetPoint = Point;
thisJob->type = type;
JobQueue::jobQueue.push_back(thisJob);
}
// Both this function and createJob should probably be moved to a new job factory class
void ModeOrder::findTasksInArea(int type, int x0, int x1, int y0, int y1, bool doCreateJob, bool colorize, bool uncolorize) {
checkBounds(&x0, &x1);
checkBounds(&y0, &y1);
for (int y = y0; y <= y1; y++) {
int staggerFix = y%2-y0%2; // To only grab the tiles that are "inner", we need to account for tiles that are odd "sticking out"
for (int x = x0; x <= x1 - staggerFix; x++) {
switch (type) {
case JOB_TYPE_MINING:
{
// Mining jobs: any doodad with the IS_MINABLE tag is assigned a mining job
std::vector<Doodad*> doodadsHere = entManager->doodadManager->getDoodadsAtPoint(x,y);
for (Doodad* thisDoodad : doodadsHere) {
if (thisDoodad->hasTags(IS_MINABLE)) {
if (doCreateJob) {
createJob(JOB_TYPE_MINING, SKILL_WOODCUT, thisDoodad, point());
thisDoodad->tags |= DOODAD_TASKED;
}
if (colorize) thisDoodad->spr.setColor(DEFAULT_TILE_COLORS[COLOR_TASKED]);
if (uncolorize && !thisDoodad->hasTags(DOODAD_TASKED)) thisDoodad->spr.setColor(DEFAULT_TILE_COLORS[COLOR_NONE]);
}
}
break;
break;
}
case JOB_TYPE_WOODCUT:
{
// Woodcutting jobs: any doodad with the IS_TREE tag is assigned a mining job
std::vector<Doodad*> doodadsHere = entManager->doodadManager->getDoodadsAtPoint(x,y);
for (Doodad* thisDoodad : doodadsHere) {
if (thisDoodad->hasTags(IS_TREE)) {
if (doCreateJob) {
createJob(JOB_TYPE_WOODCUT, SKILL_WOODCUT, thisDoodad, point());
thisDoodad->tags |= DOODAD_TASKED;
}
if (colorize) thisDoodad->spr.setColor(DEFAULT_TILE_COLORS[COLOR_TASKED]);
if (uncolorize && !thisDoodad->hasTags(DOODAD_TASKED)) thisDoodad->spr.setColor(DEFAULT_TILE_COLORS[COLOR_NONE]);
}
}
break;
}
case JOB_TYPE_BUILD:
{
// Mining jobs: any tile with the IS_WALKABLE tag is currently able to be turned into a room
if (curMap->inBounds(x,y) && curMap->isWalkable(x,y)) {
bool mustUncolorize = false;
// need to rethink this: where should the color for a tile actually be determined? In map?
if (colorize) curMap->setColor(x, y, COLOR_TASKED);
if (doCreateJob) {
point thisSpot = TileXYToTexXY(x, y);
Item* item = entManager->itemManager->getItemWithTags(IS_WOOD);
if (item != NULL) {
item->tasked = true;
createJob(JOB_TYPE_BUILD, SKILL_MINING, item, thisSpot);
curMap->setTasked(x,y,true);
} else {
mustUncolorize = true;
}
}
if ((uncolorize || mustUncolorize) && !curMap->getTasked(x,y)) curMap->setColor(x, y, COLOR_NONE);
}
break;
}
default:
{
cerr << "Unknown jobtype in findTasksInArea: " << type << endl;
break;
}
}
}
}
}
sf::Vector2i ModeOrder::getMousePos(sf::RenderWindow* screen) {
return sf::Mouse::getPosition(*screen) + sf::Vector2i(screenX,screenY);
}
void ModeOrder::handleKeyboard(float dt) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
screenX -= MAP_SCROLL_SPEED*dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
screenX += MAP_SCROLL_SPEED*dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
screenY -= MAP_SCROLL_SPEED*dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
screenY += MAP_SCROLL_SPEED*dt;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
setOrderMode(ORDER_MODE_DIG);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
setOrderMode(ORDER_MODE_CUTTREE);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B)) {
setOrderMode(ORDER_MODE_BUILD);
}
};
void ModeOrder::checkUIForClick(float x, float y) {
if (digButton->isClicked(x,y)) {
setOrderMode(ORDER_MODE_DIG);
} else if (woodcutButton->isClicked(x,y)) {
setOrderMode(ORDER_MODE_CUTTREE);
} else if (buildButton->isClicked(x,y)) {
setOrderMode(ORDER_MODE_BUILD);
}
}
void ModeOrder::handleMouse(sf::RenderWindow* screen) {
sf::Vector2i mousePos = getMousePos(screen);
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
if (!leftClicked && selectionActive == SELECT_TYPE_NONE) { // Initiate clicking
selectionActive = SELECT_TYPE_LEFT;
point startPoint = TexXYToTileXY(mousePos.x, mousePos.y);
selectStartX = startPoint.tileX;
selectStartY = startPoint.tileY;
selectLastX = selectStartX;
selectLastY = selectStartY;
leftClicked = true;
checkUIForClick(mousePos.x, mousePos.y);
} else if (selectionActive == SELECT_TYPE_LEFT) {
point thisPoint = TexXYToTileXY(mousePos.x, mousePos.y);
if (thisPoint.tileX != selectLastX || thisPoint.tileY != selectLastY) {
// redraw
int jobType;
if (curOrderType == ORDER_MODE_DIG) {
jobType = JOB_TYPE_MINING;
} else if (curOrderType == ORDER_MODE_CUTTREE) {
jobType = JOB_TYPE_WOODCUT;
} else if (curOrderType == ORDER_MODE_BUILD) {
jobType = JOB_TYPE_BUILD;
}
findTasksInArea(jobType, selectStartX, selectLastX, selectStartY, selectLastY, false, false, true);
findTasksInArea(jobType, selectStartX, thisPoint.tileX, selectStartY, thisPoint.tileY, false, true, false);
selectLastX = thisPoint.tileX;
selectLastY = thisPoint.tileY;
}
}
} else {
if (leftClicked && selectionActive == SELECT_TYPE_LEFT) {
selectionActive = SELECT_TYPE_NONE;
point endPoint = TexXYToTileXY(mousePos.x, mousePos.y);
int jobType;
if (curOrderType == ORDER_MODE_DIG) {
jobType = JOB_TYPE_MINING;
} else if (curOrderType == ORDER_MODE_CUTTREE) {
jobType = JOB_TYPE_WOODCUT;
} else if (curOrderType == ORDER_MODE_BUILD) {
jobType = JOB_TYPE_BUILD;
}
findTasksInArea(jobType, selectStartX, endPoint.tileX, selectStartY, endPoint.tileY, true, true, false);
}
leftClicked = false;
}
if (!rightClicked && sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
selectionActive = SELECT_TYPE_RIGHT;
rightClicked = true;
point clicked = TexXYToTileXY(mousePos.x, mousePos.y);
point unitPoint = TexXYToTileXY(test->realX, test->realY);
std::vector<point> route = AStarSearch(curMap, unitPoint.tileX, unitPoint.tileY, clicked.tileX, clicked.tileY);
if (route.size() != 0) {
// Remove the last point in the route, since it is the current position
route.pop_back();
// Walk to the target location
test->walkTo(route);
}
} else if (!sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
if (rightClicked)
selectionActive = SELECT_TYPE_NONE;
rightClicked = false;
}
};
void ModeOrder::flushRequests() {
/*while (!RequestQueues::uiRequests.empty()) {
// parse this request
uiRequest request = RequestQueues::uiRequests.back();
// TODO: Handle this request
RequestQueues::uiRequests.pop_back();
}*/
}
void ModeOrder::update(float dt, sf::RenderWindow* screen) {
sf::Event e;
while(screen->pollEvent(e)){
if(e.type == sf::Event::Closed){
screen->close();
}
}
entManager->update(dt);
handleKeyboard(dt);
handleMouse(screen);
}
// To be moved into a UI container class
void ModeOrder::drawUI(sf::RenderTarget* screen) {
digButton->render(screen);
woodcutButton->render(screen);
buildButton->render(screen);
}
void ModeOrder::render(sf::RenderTarget* screen) {
sf::Vector2u size = screen->getSize();
sf::View view = screen->getView();
view.setCenter(screenX+float(size.x)/2, screenY+float(size.y)/2);
screen->setView(view);
curMap->render(screen);
entManager->render(screen);
drawUI(screen);
}