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

Last change on this file since aee0634 was c666518, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Move handleGameEvents and handlePlayers events from the Game class to the server

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[f419b09]1#include "Game.h"
2
[d05c484]3#include <iostream>
4#include <cstring>
[64a1f4e]5#include <cstdlib>
[d05c484]6
[ce2bb87]7#include "Common.h"
8
[f419b09]9using namespace std;
10
11Game::Game() {
12 this->id = 0;
13 this->name = "";
[b92e6a7]14 this->blueScore = 0;
15 this->redScore = 0;
16 this->worldMap = NULL;
[d05c484]17 this->msgProcessor = NULL;
[f419b09]18}
19
[d05c484]20Game::Game(string name, string filepath, MessageProcessor* msgProcessor) {
[f419b09]21 this->id = 0;
22 this->name = name;
[b92e6a7]23 this->blueScore = 0;
24 this->redScore = 0;
[233e736]25 this->worldMap = WorldMap::loadMapFromFile(filepath);
[d05c484]26 this->msgProcessor = msgProcessor;
[f419b09]27}
28
29Game::~Game() {
[b92e6a7]30 delete this->worldMap;
[f419b09]31}
32
[ab8fd40]33string Game::getName() {
34 return this->name;
35}
36
[b92e6a7]37int Game::getNumPlayers() {
38 return this->players.size();
[f419b09]39}
40
[b92e6a7]41map<unsigned int, Player*>& Game::getPlayers() {
42 return this->players;
43}
44
[ea17281]45bool Game::addPlayer(Player* p, bool serverSide) {
[5b92307]46 if (players.find(p->getId()) == players.end()) {
47 players[p->getId()] = p;
[64a1f4e]48
49 // reset player stats, location, etc.
50 p->pos.x = p->target.x = 200;
51 p->pos.y = p->target.y = 200;
52 p->setTargetPlayer(0);
53 p->isChasing = false;
54 p->isAttacking = false;
55 p->isDead = false;
56 p->health = p->maxHealth;
57 p->hasBlueFlag = false;
58 p->hasRedFlag = false;
59
[ea17281]60 if (serverSide) {
61 // choose a random team (either 0 or 1)
62 p->team = rand() % 2;
63 }
[64a1f4e]64
65 p->currentGame = this;
66
[45734ff]67 return true;
68 }
69 else
70 return false;
71}
72
73bool Game::removePlayer(unsigned int id) {
74 if (players.erase(id) == 1)
75 return true;
76 else
77 return false;
78}
79
[1d96513]80map<unsigned int, Projectile>& Game::getProjectiles() {
81 return this->projectiles;
82}
83
[45734ff]84bool Game::addProjectile(Projectile p) {
85 if (projectiles.find(p.id) == projectiles.end()) {
86 projectiles[p.id] = p;
87 return true;
88 }
89 else
90 return false;
91}
92
93bool Game::removeProjectile(unsigned int id) {
94 if (projectiles.erase(id) == 1)
95 return true;
96 else
97 return false;
98}
99
[c9f6a1c]100unsigned int Game::getRedScore() {
[b92e6a7]101 return this->redScore;
102}
103
[c9f6a1c]104unsigned int Game::getBlueScore() {
[b92e6a7]105 return this->blueScore;
106}
107
108WorldMap* Game::getMap() {
109 return this->worldMap;
110}
111
112void Game::setId(unsigned int id) {
113 this->id = id;
[2ee386d]114}
115
[c9f6a1c]116void Game::setRedScore(unsigned int score) {
[1d96513]117 this->redScore = score;
118}
119
[c9f6a1c]120void Game::setBlueScore(unsigned int score) {
[1d96513]121 this->blueScore = score;
122}
123
[7f884ea]124void Game::addObjectToMap(ObjectType objectType, int x, int y) {
[d05c484]125 NETWORK_MSG serverMsg;
126
127 this->getMap()->addObject(objectType, x, y);
128
129 serverMsg.type = MSG_TYPE_OBJECT;
130 this->worldMap->getObjects()->back().serialize(serverMsg.buffer);
131
132 this->msgProcessor->broadcastMessage(serverMsg, this->players);
133}
134
[0129700]135bool Game::startPlayerMovement(unsigned int id, int x, int y) {
136 // need to check if players actually contains the id
137 Player* p = players[id];
138
139 // we need to make sure the player can move here
140 if (0 <= x && x < this->worldMap->width*25 &&
141 0 <= y && y < this->worldMap->height*25 &&
[7f884ea]142 this->worldMap->getElement(x/25, y/25) == TERRAIN_GRASS)
[0129700]143 {
144 p->target.x = x;
145 p->target.y = y;
146
147 p->isChasing = false;
148 p->isAttacking = false;
[99cf349]149 p->setTargetPlayer(0);
[0129700]150
151 return true;
152 }
153 else
154 return false;
155}
156
[402cf86]157// returns true if the movement should be canceled
158bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
159
160 // check if the move needs to be canceled
161 switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
162 {
[7f884ea]163 case TERRAIN_NONE:
164 case TERRAIN_OCEAN:
165 case TERRAIN_ROCK:
[402cf86]166 {
167 p->pos = oldPos;
168 p->target.x = p->pos.x;
169 p->target.y = p->pos.y;
170 p->isChasing = false;
171 return true;
172 break;
173 }
174 default:
175 // if there are no obstacles, don't cancel movement
176 return false;
177 break;
178 }
179}
180
[ce2bb87]181// returns the id of the picked-up flag or -1 if none was picked up
182int Game::processFlagPickupRequest(Player* p) {
183 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
184 vector<WorldMap::Object>::iterator it;
[0678d60]185 int itemId = -1;
[ce2bb87]186
187 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
188 if (posDistance(p->pos, it->pos.toFloat()) < 10) {
189 switch (it->type) {
[7f884ea]190 case OBJECT_BLUE_FLAG:
[ce2bb87]191 if (p->team == 1) {
192 p->hasBlueFlag = true;
[0678d60]193 itemId = it->id;
[ce2bb87]194 }
195 break;
[7f884ea]196 case OBJECT_RED_FLAG:
[ce2bb87]197 if (p->team == 0) {
198 p->hasRedFlag = true;
[0678d60]199 itemId = it->id;
[ce2bb87]200 }
201 break;
[7f884ea]202 case OBJECT_NONE:
[0678d60]203 break;
[ce2bb87]204 }
205
[0678d60]206 if (itemId > -1) {
[ce2bb87]207 vctObjects->erase(it);
[0678d60]208 return itemId;
[ce2bb87]209 }
210 }
211 }
212
[0678d60]213 return itemId;
[ce2bb87]214}
215
[d05c484]216void Game::dealDamageToPlayer(Player* p, int damage) {
217 p->takeDamage(damage);
218
[53643ca]219 if (p->isDead) {
[7f884ea]220 ObjectType flagType = OBJECT_NONE;
[d05c484]221 if (p->hasBlueFlag)
[7f884ea]222 flagType = OBJECT_BLUE_FLAG;
[d05c484]223 else if (p->hasRedFlag)
[7f884ea]224 flagType = OBJECT_RED_FLAG;
[d05c484]225
[7f884ea]226 if (flagType != OBJECT_NONE)
[d05c484]227 this->addObjectToMap(flagType, p->pos.x, p->pos.y);
228 }
229
230 // send a PLAYER message after dealing damage
231 NETWORK_MSG serverMsg;
232 serverMsg.type = MSG_TYPE_PLAYER;
233 p->serialize(serverMsg.buffer);
234 msgProcessor->broadcastMessage(serverMsg, this->players);
235}
236
[45734ff]237void Game::assignProjectileId(Projectile* p) {
238 p->id = unusedProjectileId;
239 updateUnusedProjectileId();
[b92e6a7]240}
241
[45734ff]242void Game::updateUnusedProjectileId() {
243 while (projectiles.find(unusedProjectileId) != projectiles.end())
244 unusedProjectileId++;
[b92e6a7]245}
Note: See TracBrowser for help on using the repository browser.