source: network-game/common/Game.cpp@ 1d96513

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

Game class includes projectile list

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