[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 |
|
---|
| 39 | int Game::getRedScore() {
|
---|
| 40 | return this->redScore;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | int Game::getBlueScore() {
|
---|
| 44 | return this->blueScore;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | WorldMap* Game::getMap() {
|
---|
| 48 | return this->worldMap;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void Game::setId(unsigned int id) {
|
---|
| 52 | this->id = id;
|
---|
[2ee386d] | 53 | }
|
---|
| 54 |
|
---|
[f419b09] | 55 | bool Game::addPlayer(Player* p) {
|
---|
[b48ef09] | 56 | if (players.find(p->id) == players.end()) {
|
---|
[f419b09] | 57 | players[p->id] = p;
|
---|
| 58 | return true;
|
---|
| 59 | }
|
---|
| 60 | else
|
---|
| 61 | return false;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[b92e6a7] | 64 | bool Game::removePlayer(unsigned int id) {
|
---|
[f419b09] | 65 | if (players.erase(id) == 1)
|
---|
| 66 | return true;
|
---|
| 67 | else
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
[b92e6a7] | 70 |
|
---|
[0129700] | 71 | bool 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 |
|
---|
[402cf86] | 92 | // returns true if the movement should be canceled
|
---|
| 93 | bool 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 |
|
---|
[ce2bb87] | 116 | // returns the id of the picked-up flag or -1 if none was picked up
|
---|
| 117 | int 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 |
|
---|
[b92e6a7] | 149 | void Game::setRedScore(int score) {
|
---|
| 150 | this->redScore = score;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | void Game::setBlueScore(int score) {
|
---|
| 154 | this->blueScore = score;
|
---|
| 155 | }
|
---|