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

Last change on this file since fccd588 was 85da778, checked in by dportnoy <dmp1488@…>, 10 years ago

Convert the client to use the PlayerTeam enum

  • Property mode set to 100644
File size: 5.4 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
[306758e]45bool Game::addPlayer(Player* p) {
[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
60 p->currentGame = this;
61
[45734ff]62 return true;
63 }
64 else
65 return false;
66}
67
68bool Game::removePlayer(unsigned int id) {
69 if (players.erase(id) == 1)
70 return true;
71 else
72 return false;
73}
74
[1d96513]75map<unsigned int, Projectile>& Game::getProjectiles() {
76 return this->projectiles;
77}
78
[45734ff]79bool Game::addProjectile(Projectile p) {
80 if (projectiles.find(p.id) == projectiles.end()) {
81 projectiles[p.id] = p;
82 return true;
83 }
84 else
85 return false;
86}
87
88bool Game::removeProjectile(unsigned int id) {
89 if (projectiles.erase(id) == 1)
90 return true;
91 else
92 return false;
93}
94
[c9f6a1c]95unsigned int Game::getRedScore() {
[b92e6a7]96 return this->redScore;
97}
98
[c9f6a1c]99unsigned int Game::getBlueScore() {
[b92e6a7]100 return this->blueScore;
101}
102
103WorldMap* Game::getMap() {
104 return this->worldMap;
105}
106
107void Game::setId(unsigned int id) {
108 this->id = id;
[2ee386d]109}
110
[c9f6a1c]111void Game::setRedScore(unsigned int score) {
[1d96513]112 this->redScore = score;
113}
114
[c9f6a1c]115void Game::setBlueScore(unsigned int score) {
[1d96513]116 this->blueScore = score;
117}
118
[7f884ea]119void Game::addObjectToMap(ObjectType objectType, int x, int y) {
[d05c484]120 NETWORK_MSG serverMsg;
121
122 this->getMap()->addObject(objectType, x, y);
123
124 serverMsg.type = MSG_TYPE_OBJECT;
125 this->worldMap->getObjects()->back().serialize(serverMsg.buffer);
126
127 this->msgProcessor->broadcastMessage(serverMsg, this->players);
128}
129
[0129700]130bool Game::startPlayerMovement(unsigned int id, int x, int y) {
131 // need to check if players actually contains the id
132 Player* p = players[id];
133
134 // we need to make sure the player can move here
135 if (0 <= x && x < this->worldMap->width*25 &&
136 0 <= y && y < this->worldMap->height*25 &&
[7f884ea]137 this->worldMap->getElement(x/25, y/25) == TERRAIN_GRASS)
[0129700]138 {
139 p->target.x = x;
140 p->target.y = y;
141
142 p->isChasing = false;
143 p->isAttacking = false;
[99cf349]144 p->setTargetPlayer(0);
[0129700]145
146 return true;
147 }
148 else
149 return false;
150}
151
[402cf86]152// returns true if the movement should be canceled
153bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
154
155 // check if the move needs to be canceled
156 switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
157 {
[7f884ea]158 case TERRAIN_NONE:
159 case TERRAIN_OCEAN:
160 case TERRAIN_ROCK:
[402cf86]161 {
162 p->pos = oldPos;
163 p->target.x = p->pos.x;
164 p->target.y = p->pos.y;
165 p->isChasing = false;
166 return true;
167 break;
168 }
169 default:
170 // if there are no obstacles, don't cancel movement
171 return false;
172 break;
173 }
174}
175
[ce2bb87]176// returns the id of the picked-up flag or -1 if none was picked up
177int Game::processFlagPickupRequest(Player* p) {
178 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
179 vector<WorldMap::Object>::iterator it;
[0678d60]180 int itemId = -1;
[ce2bb87]181
182 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
183 if (posDistance(p->pos, it->pos.toFloat()) < 10) {
184 switch (it->type) {
[7f884ea]185 case OBJECT_BLUE_FLAG:
[85da778]186 if (p->team == Player::TEAM_RED) {
[ce2bb87]187 p->hasBlueFlag = true;
[0678d60]188 itemId = it->id;
[ce2bb87]189 }
190 break;
[7f884ea]191 case OBJECT_RED_FLAG:
[85da778]192 if (p->team == Player::TEAM_BLUE) {
[ce2bb87]193 p->hasRedFlag = true;
[0678d60]194 itemId = it->id;
[ce2bb87]195 }
196 break;
[7f884ea]197 case OBJECT_NONE:
[0678d60]198 break;
[ce2bb87]199 }
200
[0678d60]201 if (itemId > -1) {
[ce2bb87]202 vctObjects->erase(it);
[0678d60]203 return itemId;
[ce2bb87]204 }
205 }
206 }
207
[0678d60]208 return itemId;
[ce2bb87]209}
210
[d05c484]211void Game::dealDamageToPlayer(Player* p, int damage) {
212 p->takeDamage(damage);
213
[53643ca]214 if (p->isDead) {
[7f884ea]215 ObjectType flagType = OBJECT_NONE;
[d05c484]216 if (p->hasBlueFlag)
[7f884ea]217 flagType = OBJECT_BLUE_FLAG;
[d05c484]218 else if (p->hasRedFlag)
[7f884ea]219 flagType = OBJECT_RED_FLAG;
[d05c484]220
[7f884ea]221 if (flagType != OBJECT_NONE)
[d05c484]222 this->addObjectToMap(flagType, p->pos.x, p->pos.y);
223 }
224
225 // send a PLAYER message after dealing damage
226 NETWORK_MSG serverMsg;
227 serverMsg.type = MSG_TYPE_PLAYER;
228 p->serialize(serverMsg.buffer);
229 msgProcessor->broadcastMessage(serverMsg, this->players);
230}
231
[45734ff]232void Game::assignProjectileId(Projectile* p) {
233 p->id = unusedProjectileId;
234 updateUnusedProjectileId();
[b92e6a7]235}
236
[45734ff]237void Game::updateUnusedProjectileId() {
238 while (projectiles.find(unusedProjectileId) != projectiles.end())
239 unusedProjectileId++;
[b92e6a7]240}
Note: See TracBrowser for help on using the repository browser.