[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 |
|
---|
[1d96513] | 39 | map<unsigned int, Projectile>& Game::getProjectiles() {
|
---|
| 40 | return this->projectiles;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[b92e6a7] | 43 | int Game::getRedScore() {
|
---|
| 44 | return this->redScore;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | int Game::getBlueScore() {
|
---|
| 48 | return this->blueScore;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | WorldMap* Game::getMap() {
|
---|
| 52 | return this->worldMap;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void Game::setId(unsigned int id) {
|
---|
| 56 | this->id = id;
|
---|
[2ee386d] | 57 | }
|
---|
| 58 |
|
---|
[1d96513] | 59 | void Game::setRedScore(int score) {
|
---|
| 60 | this->redScore = score;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void Game::setBlueScore(int score) {
|
---|
| 64 | this->blueScore = score;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[f419b09] | 67 | bool Game::addPlayer(Player* p) {
|
---|
[b48ef09] | 68 | if (players.find(p->id) == players.end()) {
|
---|
[f419b09] | 69 | players[p->id] = p;
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 | else
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[b92e6a7] | 76 | bool Game::removePlayer(unsigned int id) {
|
---|
[f419b09] | 77 | if (players.erase(id) == 1)
|
---|
| 78 | return true;
|
---|
| 79 | else
|
---|
| 80 | return false;
|
---|
| 81 | }
|
---|
[b92e6a7] | 82 |
|
---|
[0129700] | 83 | bool 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 |
|
---|
[402cf86] | 104 | // returns true if the movement should be canceled
|
---|
| 105 | bool 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 |
|
---|
[ce2bb87] | 128 | // returns the id of the picked-up flag or -1 if none was picked up
|
---|
| 129 | int 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 |
|
---|
[1d96513] | 161 | bool 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;
|
---|
[b92e6a7] | 168 | }
|
---|
| 169 |
|
---|
[1d96513] | 170 | bool Game::removeProjectile(unsigned int id) {
|
---|
| 171 | if (projectiles.erase(id) == 1)
|
---|
| 172 | return true;
|
---|
| 173 | else
|
---|
| 174 | return false;
|
---|
[b92e6a7] | 175 | }
|
---|