1 | #include "Game.h"
|
---|
2 |
|
---|
3 | #include "Common.h"
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | Game::Game() {
|
---|
8 | this->id = 0;
|
---|
9 | this->name = "";
|
---|
10 | this->blueScore = 0;
|
---|
11 | this->redScore = 0;
|
---|
12 | this->worldMap = NULL;
|
---|
13 | }
|
---|
14 |
|
---|
15 | Game::Game(string name, string filepath) {
|
---|
16 | this->id = 0;
|
---|
17 | this->name = name;
|
---|
18 | this->blueScore = 0;
|
---|
19 | this->redScore = 0;
|
---|
20 | this->worldMap = WorldMap::loadMapFromFile(filepath);
|
---|
21 | }
|
---|
22 |
|
---|
23 | Game::~Game() {
|
---|
24 | delete this->worldMap;
|
---|
25 | }
|
---|
26 |
|
---|
27 | string Game::getName() {
|
---|
28 | return this->name;
|
---|
29 | }
|
---|
30 |
|
---|
31 | int Game::getNumPlayers() {
|
---|
32 | return this->players.size();
|
---|
33 | }
|
---|
34 |
|
---|
35 | map<unsigned int, Player*>& Game::getPlayers() {
|
---|
36 | return this->players;
|
---|
37 | }
|
---|
38 |
|
---|
39 | bool Game::addPlayer(Player* p) {
|
---|
40 | if (players.find(p->id) == players.end()) {
|
---|
41 | players[p->id] = p;
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 | else
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 |
|
---|
48 | bool Game::removePlayer(unsigned int id) {
|
---|
49 | if (players.erase(id) == 1)
|
---|
50 | return true;
|
---|
51 | else
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | map<unsigned int, Projectile>& Game::getProjectiles() {
|
---|
56 | return this->projectiles;
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool Game::addProjectile(Projectile p) {
|
---|
60 | if (projectiles.find(p.id) == projectiles.end()) {
|
---|
61 | projectiles[p.id] = p;
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 | else
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool Game::removeProjectile(unsigned int id) {
|
---|
69 | if (projectiles.erase(id) == 1)
|
---|
70 | return true;
|
---|
71 | else
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int Game::getRedScore() {
|
---|
76 | return this->redScore;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int Game::getBlueScore() {
|
---|
80 | return this->blueScore;
|
---|
81 | }
|
---|
82 |
|
---|
83 | WorldMap* Game::getMap() {
|
---|
84 | return this->worldMap;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Game::setId(unsigned int id) {
|
---|
88 | this->id = id;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void Game::setRedScore(int score) {
|
---|
92 | this->redScore = score;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void Game::setBlueScore(int score) {
|
---|
96 | this->blueScore = score;
|
---|
97 | }
|
---|
98 |
|
---|
99 | bool Game::startPlayerMovement(unsigned int id, int x, int y) {
|
---|
100 | // need to check if players actually contains the id
|
---|
101 | Player* p = players[id];
|
---|
102 |
|
---|
103 | // we need to make sure the player can move here
|
---|
104 | if (0 <= x && x < this->worldMap->width*25 &&
|
---|
105 | 0 <= y && y < this->worldMap->height*25 &&
|
---|
106 | this->worldMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
107 | {
|
---|
108 | p->target.x = x;
|
---|
109 | p->target.y = y;
|
---|
110 |
|
---|
111 | p->isChasing = false;
|
---|
112 | p->isAttacking = false;
|
---|
113 |
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | // returns true if the movement should be canceled
|
---|
121 | bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
|
---|
122 |
|
---|
123 | // check if the move needs to be canceled
|
---|
124 | switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
|
---|
125 | {
|
---|
126 | case WorldMap::TERRAIN_NONE:
|
---|
127 | case WorldMap::TERRAIN_OCEAN:
|
---|
128 | case WorldMap::TERRAIN_ROCK:
|
---|
129 | {
|
---|
130 | p->pos = oldPos;
|
---|
131 | p->target.x = p->pos.x;
|
---|
132 | p->target.y = p->pos.y;
|
---|
133 | p->isChasing = false;
|
---|
134 | return true;
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | default:
|
---|
138 | // if there are no obstacles, don't cancel movement
|
---|
139 | return false;
|
---|
140 | break;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // returns the id of the picked-up flag or -1 if none was picked up
|
---|
145 | int Game::processFlagPickupRequest(Player* p) {
|
---|
146 | vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
|
---|
147 | vector<WorldMap::Object>::iterator it;
|
---|
148 | int playerId = -1;
|
---|
149 |
|
---|
150 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
151 | if (posDistance(p->pos, it->pos.toFloat()) < 10) {
|
---|
152 | switch (it->type) {
|
---|
153 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
154 | if (p->team == 1) {
|
---|
155 | p->hasBlueFlag = true;
|
---|
156 | playerId = it->id;
|
---|
157 | }
|
---|
158 | break;
|
---|
159 | case WorldMap::OBJECT_RED_FLAG:
|
---|
160 | if (p->team == 0) {
|
---|
161 | p->hasRedFlag = true;
|
---|
162 | playerId = it->id;
|
---|
163 | }
|
---|
164 | break;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (playerId > -1) {
|
---|
168 | vctObjects->erase(it);
|
---|
169 | return playerId;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | return playerId;
|
---|
175 | }
|
---|
176 |
|
---|
177 | void Game::assignProjectileId(Projectile* p) {
|
---|
178 | p->id = unusedProjectileId;
|
---|
179 | updateUnusedProjectileId();
|
---|
180 | }
|
---|
181 |
|
---|
182 | void Game::updateUnusedProjectileId() {
|
---|
183 | while (projectiles.find(unusedProjectileId) != projectiles.end())
|
---|
184 | unusedProjectileId++;
|
---|
185 | }
|
---|