forked from stefanhendriks/Dune-II---The-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
223 lines (164 loc) · 6.41 KB
/
Game.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
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
#include "Houses.hpp"
#include <boost/cast.hpp>
CreateUnitMessage::CreateUnitMessage(Unit::Type type, const Player &player, const sf::Vector2f &position):
Message(Messages::createUnit),
type(type),
player(player),
position(position)
{
}
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game():
playing(true),
screen(),
map(nullptr),
messages(),
console(messages, players),
actions(screen, console),
unitRepository(messages)
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
screen.create(sf::VideoMode(800, 600), "Dune 2 - The Maker", sf::Style::Close, settings);
screen.setFramerateLimit(IDEAL_FPS);
screen.setMouseCursorVisible(false);
messages.connect(Messages::createUnit, [this](const Message& message){
const CreateUnitMessage* received = boost::polymorphic_downcast<const CreateUnitMessage*>(&message);
units.push_back(std::move(unitRepository.create(received->type, received->player, received->position)));
});
if (!init()){
std::cerr << "Failed to initialized game.";
playing = false;
}
}
int Game::execute() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while(playing) {
dt = clock.restart();
timeSinceLastUpdate += dt;
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
updateState(TimePerFrame);
}
render();
}
return 0;
}
bool Game::init() {
if (!terrain.loadFromFile("graphics/terrain.png")) {
std::cout << "Failed to read graphics/terrain.png data" << std::endl;
return false;
}
sf::Image temp;
if (!temp.loadFromFile("graphics/shroud_edges.bmp")) {
std::cout << "Failed to read graphics/shroud_edges.bmp data" << std::endl;
return false;
}
temp.createMaskFromColor(sf::Color(255, 0, 255));
shroudEdges.loadFromImage(temp);
map.reset(new Map(terrain, shroudEdges, messages));
map->load("maps/4PL_Mountains.ini");
camera.reset({0,0,800,600});
screen.setView(camera);
//init two players
int idCount = 0;
players.emplace_back(House::Sardaukar, idCount++);
players.emplace_back(House::Harkonnen, idCount++);
messages.triggerEvent(CreateUnitMessage(Unit::Type::Trike , players[0], {256, 256}));
messages.triggerEvent(CreateUnitMessage(Unit::Type::Quad , players[1], {300, 300}));
messages.triggerEvent(CreateUnitMessage(Unit::Type::Soldier , players[0], {400, 500}));
messages.triggerEvent(CreateUnitMessage(Unit::Type::Soldier , players[0], {220, 500}));
messages.triggerEvent(CreateUnitMessage(Unit::Type::Devastator, players[1], {500, 200}));
//register listeners
typedef thor::ActionContext<std::string> actionContext;
actions.connect("close", [this](actionContext){playing = false;});
actions.connect("boxRelease", [this](actionContext){
for (auto& unit : units){
if (box.intersects(unit.getBounds())){
selectUnit(unit);
}
}
box.clear();
});
actions.connect("boxStart", [this](actionContext context) {
if (mouse.getType() != Mouse::Type::Default) {
return;
}
sf::Vector2f toSet = screen.mapPixelToCoords(mouse.getHotspot(*context.event), camera);
box.setTopLeft(toSet);
});
actions.connect("singleSelect", [this](actionContext context){
sf::Vector2f toCheck = screen.mapPixelToCoords(mouse.getHotspot(*context.event), camera);
for (auto& unit : units){
if (unit.getBounds().contains(toCheck))
selectUnit(unit);
}
});
actions.connect("deselectAll", [this](actionContext){
actions.disconnect("orderMove");
mouse.setType(Mouse::Type::Default);
for (auto& unit : units)
unit.unselect();
});
actions.connect("boxDrag", [this](actionContext){
box.setBottomRight(screen.mapPixelToCoords(sf::Mouse::getPosition(screen), camera));
});
const float cameraSpeed = 15.f;
actions.connect("cameraLeft", [this, cameraSpeed](actionContext) {camera.move(-cameraSpeed, 0.f);});
actions.connect("cameraRight", [this, cameraSpeed](actionContext){camera.move(cameraSpeed, 0.f); });
actions.connect("cameraUp", [this, cameraSpeed](actionContext) {camera.move(0.f, -cameraSpeed);});
actions.connect("cameraDown", [this, cameraSpeed](actionContext) {camera.move(0.f, cameraSpeed); });
actions.connect("toggleConsole", std::bind(&Console::toggle, &console));
return true;
}
void Game::render() {
screen.clear();
screen.setView(camera);
screen.draw(*map);
for (const auto& unit : units)
screen.draw(unit);
map->drawShrouded(screen, sf::RenderStates::Default);
screen.draw(box);
screen.draw(fpsCounter);
screen.setView(camera);
console.display(screen);
screen.draw(mouse);
screen.display();
}
void Game::updateState(sf::Time dt) {
actions.update();
console.update(dt);
sf::Vector2i mousePosition = sf::Mouse::getPosition(screen);
if (mousePosition.x < 50 ) actions.trigger("cameraLeft");
else if (mousePosition.y < 50 ) actions.trigger("cameraUp");
else if (mousePosition.x > 750) actions.trigger("cameraRight");
else if (mousePosition.y > 550) actions.trigger("cameraDown");
sf::Vector2f half_of_camera = camera.getSize() / 2.f;
sf::Vector2f topLeft = camera.getCenter() - (half_of_camera);
sf::Vector2f downRight = camera.getCenter() + (half_of_camera);
// Camera constraints take into account an invisible border of 1 cell
if (topLeft.x <= Cell::TILE_SIZE) camera.setCenter(half_of_camera.x + Cell::TILE_SIZE, camera.getCenter().y);
if (topLeft.y <= Cell::TILE_SIZE) camera.setCenter(camera.getCenter().x, half_of_camera.y + Cell::TILE_SIZE);
int max_width = (map->getMaxWidth() -1) * Cell::TILE_SIZE;
int max_height = (map->getMaxHeight() -1) * Cell::TILE_SIZE;
if (downRight.x >= max_width) camera.setCenter(max_width - half_of_camera.x, camera.getCenter().y);
if (downRight.y >= max_height) camera.setCenter(camera.getCenter().x, max_height - half_of_camera.y);
mouse.setPosition(screen.mapPixelToCoords(mousePosition,camera));
for (auto& unit: units)
unit.updateState(units, dt);
fpsCounter.update(dt);
map->prepare(screen.mapPixelToCoords(sf::Vector2i(0,0)));
}
void Game::selectUnit(Unit &unit)
{
unit.select();
actions.connect("orderMove", [this, &unit](thor::ActionContext<std::string> context){
unit.orderMove(screen.mapPixelToCoords(mouse.getHotspot(*context.event), camera));
});
mouse.setType(Mouse::Type::Move); //at least one unit selected...
}