[f419b09] | 1 | #include "Game.h"
|
---|
| 2 |
|
---|
[ce2bb87] | 3 | #include "Common.h"
|
---|
| 4 |
|
---|
[f419b09] | 5 | using namespace std;
|
---|
| 6 |
|
---|
| 7 | Game::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] | 15 | Game::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 |
|
---|
| 23 | Game::~Game() {
|
---|
[b92e6a7] | 24 | delete this->worldMap;
|
---|
[f419b09] | 25 | }
|
---|
| 26 |
|
---|
[ab8fd40] | 27 | string Game::getName() {
|
---|
| 28 | return this->name;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[b92e6a7] | 31 | int Game::getNumPlayers() {
|
---|
| 32 | return this->players.size();
|
---|
[f419b09] | 33 | }
|
---|
| 34 |
|
---|
[b92e6a7] | 35 | map<unsigned int, Player*>& Game::getPlayers() {
|
---|
| 36 | return this->players;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[45734ff] | 39 | bool 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 |
|
---|
| 48 | bool Game::removePlayer(unsigned int id) {
|
---|
| 49 | if (players.erase(id) == 1)
|
---|
| 50 | return true;
|
---|
| 51 | else
|
---|
| 52 | return false;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1d96513] | 55 | map<unsigned int, Projectile>& Game::getProjectiles() {
|
---|
| 56 | return this->projectiles;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[45734ff] | 59 | bool 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 |
|
---|
| 68 | bool Game::removeProjectile(unsigned int id) {
|
---|
| 69 | if (projectiles.erase(id) == 1)
|
---|
| 70 | return true;
|
---|
| 71 | else
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[c9f6a1c] | 75 | unsigned int Game::getRedScore() {
|
---|
[b92e6a7] | 76 | return this->redScore;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[c9f6a1c] | 79 | unsigned int Game::getBlueScore() {
|
---|
[b92e6a7] | 80 | return this->blueScore;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | WorldMap* Game::getMap() {
|
---|
| 84 | return this->worldMap;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void Game::setId(unsigned int id) {
|
---|
| 88 | this->id = id;
|
---|
[2ee386d] | 89 | }
|
---|
| 90 |
|
---|
[c9f6a1c] | 91 | void Game::setRedScore(unsigned int score) {
|
---|
[1d96513] | 92 | this->redScore = score;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[c9f6a1c] | 95 | void Game::setBlueScore(unsigned int score) {
|
---|
[1d96513] | 96 | this->blueScore = score;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[0129700] | 99 | bool 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
|
---|
| 121 | bool 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
|
---|
| 145 | int 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] | 177 | void Game::assignProjectileId(Projectile* p) {
|
---|
| 178 | p->id = unusedProjectileId;
|
---|
| 179 | updateUnusedProjectileId();
|
---|
[b92e6a7] | 180 | }
|
---|
| 181 |
|
---|
[45734ff] | 182 | void Game::updateUnusedProjectileId() {
|
---|
| 183 | while (projectiles.find(unusedProjectileId) != projectiles.end())
|
---|
| 184 | unusedProjectileId++;
|
---|
[b92e6a7] | 185 | }
|
---|