[f419b09] | 1 | #include "Game.h"
|
---|
| 2 |
|
---|
| 3 | using namespace std;
|
---|
| 4 |
|
---|
| 5 | Game::Game() {
|
---|
| 6 | this->id = 0;
|
---|
| 7 | this->name = "";
|
---|
[b92e6a7] | 8 | this->blueScore = 0;
|
---|
| 9 | this->redScore = 0;
|
---|
| 10 | this->worldMap = NULL;
|
---|
[f419b09] | 11 | }
|
---|
| 12 |
|
---|
[233e736] | 13 | Game::Game(string name, string filepath) {
|
---|
[f419b09] | 14 | this->id = 0;
|
---|
| 15 | this->name = name;
|
---|
[b92e6a7] | 16 | this->blueScore = 0;
|
---|
| 17 | this->redScore = 0;
|
---|
[233e736] | 18 | this->worldMap = WorldMap::loadMapFromFile(filepath);
|
---|
[f419b09] | 19 | }
|
---|
| 20 |
|
---|
| 21 | Game::~Game() {
|
---|
[b92e6a7] | 22 | delete this->worldMap;
|
---|
[f419b09] | 23 | }
|
---|
| 24 |
|
---|
[ab8fd40] | 25 | string Game::getName() {
|
---|
| 26 | return this->name;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[b92e6a7] | 29 | int Game::getNumPlayers() {
|
---|
| 30 | return this->players.size();
|
---|
[f419b09] | 31 | }
|
---|
| 32 |
|
---|
[b92e6a7] | 33 | map<unsigned int, Player*>& Game::getPlayers() {
|
---|
| 34 | return this->players;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | int Game::getRedScore() {
|
---|
| 38 | return this->redScore;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | int Game::getBlueScore() {
|
---|
| 42 | return this->blueScore;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | WorldMap* Game::getMap() {
|
---|
| 46 | return this->worldMap;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | void Game::setId(unsigned int id) {
|
---|
| 50 | this->id = id;
|
---|
[2ee386d] | 51 | }
|
---|
| 52 |
|
---|
[f419b09] | 53 | bool Game::addPlayer(Player* p) {
|
---|
[b48ef09] | 54 | if (players.find(p->id) == players.end()) {
|
---|
[f419b09] | 55 | players[p->id] = p;
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 | else
|
---|
| 59 | return false;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[b92e6a7] | 62 | bool Game::removePlayer(unsigned int id) {
|
---|
[f419b09] | 63 | if (players.erase(id) == 1)
|
---|
| 64 | return true;
|
---|
| 65 | else
|
---|
| 66 | return false;
|
---|
| 67 | }
|
---|
[b92e6a7] | 68 |
|
---|
[0129700] | 69 | bool Game::startPlayerMovement(unsigned int id, int x, int y) {
|
---|
| 70 | // need to check if players actually contains the id
|
---|
| 71 | Player* p = players[id];
|
---|
| 72 |
|
---|
| 73 | // we need to make sure the player can move here
|
---|
| 74 | if (0 <= x && x < this->worldMap->width*25 &&
|
---|
| 75 | 0 <= y && y < this->worldMap->height*25 &&
|
---|
| 76 | this->worldMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
| 77 | {
|
---|
| 78 | p->target.x = x;
|
---|
| 79 | p->target.y = y;
|
---|
| 80 |
|
---|
| 81 | p->isChasing = false;
|
---|
| 82 | p->isAttacking = false;
|
---|
| 83 |
|
---|
| 84 | return true;
|
---|
| 85 | }
|
---|
| 86 | else
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[402cf86] | 90 | // returns true if the movement should be canceled
|
---|
| 91 | bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
|
---|
| 92 |
|
---|
| 93 | // check if the move needs to be canceled
|
---|
| 94 | switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
|
---|
| 95 | {
|
---|
| 96 | case WorldMap::TERRAIN_NONE:
|
---|
| 97 | case WorldMap::TERRAIN_OCEAN:
|
---|
| 98 | case WorldMap::TERRAIN_ROCK:
|
---|
| 99 | {
|
---|
| 100 | p->pos = oldPos;
|
---|
| 101 | p->target.x = p->pos.x;
|
---|
| 102 | p->target.y = p->pos.y;
|
---|
| 103 | p->isChasing = false;
|
---|
| 104 | return true;
|
---|
| 105 | break;
|
---|
| 106 | }
|
---|
| 107 | default:
|
---|
| 108 | // if there are no obstacles, don't cancel movement
|
---|
| 109 | return false;
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[b92e6a7] | 114 | void Game::setRedScore(int score) {
|
---|
| 115 | this->redScore = score;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void Game::setBlueScore(int score) {
|
---|
| 119 | this->blueScore = score;
|
---|
| 120 | }
|
---|