source: network-game/common/Game.cpp@ e1af80c

Last change on this file since e1af80c was c9f6a1c, checked in by dportnoy <dmp1488@…>, 11 years ago

New GameSummary class for storing game results

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[f419b09]1#include "Game.h"
2
[ce2bb87]3#include "Common.h"
4
[f419b09]5using namespace std;
6
7Game::Game() {
8 this->id = 0;
9 this->name = "";
[b92e6a7]10 this->blueScore = 0;
11 this->redScore = 0;
12 this->worldMap = NULL;
[f419b09]13}
14
[233e736]15Game::Game(string name, string filepath) {
[f419b09]16 this->id = 0;
17 this->name = name;
[b92e6a7]18 this->blueScore = 0;
19 this->redScore = 0;
[233e736]20 this->worldMap = WorldMap::loadMapFromFile(filepath);
[f419b09]21}
22
23Game::~Game() {
[b92e6a7]24 delete this->worldMap;
[f419b09]25}
26
[ab8fd40]27string Game::getName() {
28 return this->name;
29}
30
[b92e6a7]31int Game::getNumPlayers() {
32 return this->players.size();
[f419b09]33}
34
[b92e6a7]35map<unsigned int, Player*>& Game::getPlayers() {
36 return this->players;
37}
38
[45734ff]39bool 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
48bool Game::removePlayer(unsigned int id) {
49 if (players.erase(id) == 1)
50 return true;
51 else
52 return false;
53}
54
[1d96513]55map<unsigned int, Projectile>& Game::getProjectiles() {
56 return this->projectiles;
57}
58
[45734ff]59bool 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
68bool Game::removeProjectile(unsigned int id) {
69 if (projectiles.erase(id) == 1)
70 return true;
71 else
72 return false;
73}
74
[c9f6a1c]75unsigned int Game::getRedScore() {
[b92e6a7]76 return this->redScore;
77}
78
[c9f6a1c]79unsigned int Game::getBlueScore() {
[b92e6a7]80 return this->blueScore;
81}
82
83WorldMap* Game::getMap() {
84 return this->worldMap;
85}
86
87void Game::setId(unsigned int id) {
88 this->id = id;
[2ee386d]89}
90
[c9f6a1c]91void Game::setRedScore(unsigned int score) {
[1d96513]92 this->redScore = score;
93}
94
[c9f6a1c]95void Game::setBlueScore(unsigned int score) {
[1d96513]96 this->blueScore = score;
97}
98
[0129700]99bool 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
[402cf86]120// returns true if the movement should be canceled
121bool 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
[ce2bb87]144// returns the id of the picked-up flag or -1 if none was picked up
145int 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
[45734ff]177void Game::assignProjectileId(Projectile* p) {
178 p->id = unusedProjectileId;
179 updateUnusedProjectileId();
[b92e6a7]180}
181
[45734ff]182void Game::updateUnusedProjectileId() {
183 while (projectiles.find(unusedProjectileId) != projectiles.end())
184 unusedProjectileId++;
[b92e6a7]185}
Note: See TracBrowser for help on using the repository browser.