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

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

A player can pick up an opponent's flag in an individual game

  • Property mode set to 100644
File size: 3.3 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
39int Game::getRedScore() {
40 return this->redScore;
41}
42
43int Game::getBlueScore() {
44 return this->blueScore;
45}
46
47WorldMap* Game::getMap() {
48 return this->worldMap;
49}
50
51void Game::setId(unsigned int id) {
52 this->id = id;
53}
54
55bool Game::addPlayer(Player* p) {
56 if (players.find(p->id) == players.end()) {
57 players[p->id] = p;
58 return true;
59 }
60 else
61 return false;
62}
63
64bool Game::removePlayer(unsigned int id) {
65 if (players.erase(id) == 1)
66 return true;
67 else
68 return false;
69}
70
71bool Game::startPlayerMovement(unsigned int id, int x, int y) {
72 // need to check if players actually contains the id
73 Player* p = players[id];
74
75 // we need to make sure the player can move here
76 if (0 <= x && x < this->worldMap->width*25 &&
77 0 <= y && y < this->worldMap->height*25 &&
78 this->worldMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
79 {
80 p->target.x = x;
81 p->target.y = y;
82
83 p->isChasing = false;
84 p->isAttacking = false;
85
86 return true;
87 }
88 else
89 return false;
90}
91
92// returns true if the movement should be canceled
93bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
94
95 // check if the move needs to be canceled
96 switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
97 {
98 case WorldMap::TERRAIN_NONE:
99 case WorldMap::TERRAIN_OCEAN:
100 case WorldMap::TERRAIN_ROCK:
101 {
102 p->pos = oldPos;
103 p->target.x = p->pos.x;
104 p->target.y = p->pos.y;
105 p->isChasing = false;
106 return true;
107 break;
108 }
109 default:
110 // if there are no obstacles, don't cancel movement
111 return false;
112 break;
113 }
114}
115
116// returns the id of the picked-up flag or -1 if none was picked up
117int Game::processFlagPickupRequest(Player* p) {
118 vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
119 vector<WorldMap::Object>::iterator it;
120 int playerId = -1;
121
122 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
123 if (posDistance(p->pos, it->pos.toFloat()) < 10) {
124 switch (it->type) {
125 case WorldMap::OBJECT_BLUE_FLAG:
126 if (p->team == 1) {
127 p->hasBlueFlag = true;
128 playerId = it->id;
129 }
130 break;
131 case WorldMap::OBJECT_RED_FLAG:
132 if (p->team == 0) {
133 p->hasRedFlag = true;
134 playerId = it->id;
135 }
136 break;
137 }
138
139 if (playerId > -1) {
140 vctObjects->erase(it);
141 return playerId;
142 }
143 }
144 }
145
146 return playerId;
147}
148
149void Game::setRedScore(int score) {
150 this->redScore = score;
151}
152
153void Game::setBlueScore(int score) {
154 this->blueScore = score;
155}
Note: See TracBrowser for help on using the repository browser.