[f419b09] | 1 | #include "Game.h"
|
---|
| 2 |
|
---|
[0693e25] | 3 | #include <allegro5/allegro_primitives.h>
|
---|
| 4 |
|
---|
| 5 | #include "Common.h"
|
---|
| 6 |
|
---|
[f419b09] | 7 | using namespace std;
|
---|
| 8 |
|
---|
| 9 | Game::Game() {
|
---|
| 10 | this->id = 0;
|
---|
| 11 | this->name = "";
|
---|
[b92e6a7] | 12 | this->blueScore = 0;
|
---|
| 13 | this->redScore = 0;
|
---|
| 14 | this->worldMap = NULL;
|
---|
[f419b09] | 15 | }
|
---|
| 16 |
|
---|
[233e736] | 17 | Game::Game(string name, string filepath) {
|
---|
[f419b09] | 18 | this->id = 0;
|
---|
| 19 | this->name = name;
|
---|
[b92e6a7] | 20 | this->blueScore = 0;
|
---|
| 21 | this->redScore = 0;
|
---|
[233e736] | 22 | this->worldMap = WorldMap::loadMapFromFile(filepath);
|
---|
[f419b09] | 23 | }
|
---|
| 24 |
|
---|
| 25 | Game::~Game() {
|
---|
[b92e6a7] | 26 | delete this->worldMap;
|
---|
[f419b09] | 27 | }
|
---|
| 28 |
|
---|
[ab8fd40] | 29 | string Game::getName() {
|
---|
| 30 | return this->name;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[b92e6a7] | 33 | int Game::getNumPlayers() {
|
---|
| 34 | return this->players.size();
|
---|
[f419b09] | 35 | }
|
---|
| 36 |
|
---|
[b92e6a7] | 37 | map<unsigned int, Player*>& Game::getPlayers() {
|
---|
| 38 | return this->players;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | int Game::getRedScore() {
|
---|
| 42 | return this->redScore;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | int Game::getBlueScore() {
|
---|
| 46 | return this->blueScore;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | WorldMap* Game::getMap() {
|
---|
| 50 | return this->worldMap;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void Game::setId(unsigned int id) {
|
---|
| 54 | this->id = id;
|
---|
[2ee386d] | 55 | }
|
---|
| 56 |
|
---|
[f419b09] | 57 | bool Game::addPlayer(Player* p) {
|
---|
[b48ef09] | 58 | if (players.find(p->id) == players.end()) {
|
---|
[f419b09] | 59 | players[p->id] = p;
|
---|
| 60 | return true;
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[b92e6a7] | 66 | bool Game::removePlayer(unsigned int id) {
|
---|
[f419b09] | 67 | if (players.erase(id) == 1)
|
---|
| 68 | return true;
|
---|
| 69 | else
|
---|
| 70 | return false;
|
---|
| 71 | }
|
---|
[b92e6a7] | 72 |
|
---|
| 73 | void Game::setRedScore(int score) {
|
---|
| 74 | this->redScore = score;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void Game::setBlueScore(int score) {
|
---|
| 78 | this->blueScore = score;
|
---|
| 79 | }
|
---|
[0693e25] | 80 |
|
---|
| 81 | void Game::drawPlayers(ALLEGRO_FONT* font, unsigned int curPlayerId) {
|
---|
| 82 | map<unsigned int, Player*>::iterator it;
|
---|
| 83 |
|
---|
| 84 | Player* p;
|
---|
| 85 | POSITION pos;
|
---|
| 86 | ALLEGRO_COLOR color;
|
---|
| 87 |
|
---|
| 88 | for(it = players.begin(); it != players.end(); it++)
|
---|
| 89 | {
|
---|
| 90 | p = it->second;
|
---|
| 91 |
|
---|
| 92 | if (p->isDead)
|
---|
| 93 | continue;
|
---|
| 94 |
|
---|
| 95 | pos = mapToScreen(p->pos.toInt());
|
---|
| 96 |
|
---|
| 97 | if (p->id == curPlayerId)
|
---|
| 98 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
| 99 |
|
---|
| 100 | if (p->team == 0)
|
---|
| 101 | color = al_map_rgb(0, 0, 255);
|
---|
| 102 | else if (p->team == 1)
|
---|
| 103 | color = al_map_rgb(255, 0, 0);
|
---|
| 104 |
|
---|
| 105 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
| 106 |
|
---|
| 107 | // draw player class
|
---|
| 108 | int fontHeight = al_get_font_line_height(font);
|
---|
| 109 |
|
---|
| 110 | string strClass;
|
---|
| 111 | switch (p->playerClass) {
|
---|
| 112 | case Player::CLASS_WARRIOR:
|
---|
| 113 | strClass = "W";
|
---|
| 114 | break;
|
---|
| 115 | case Player::CLASS_RANGER:
|
---|
| 116 | strClass = "R";
|
---|
| 117 | break;
|
---|
| 118 | case Player::CLASS_NONE:
|
---|
| 119 | strClass = "";
|
---|
| 120 | break;
|
---|
| 121 | default:
|
---|
| 122 | strClass = "";
|
---|
| 123 | break;
|
---|
| 124 | }
|
---|
| 125 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
| 126 |
|
---|
| 127 | // draw player health
|
---|
| 128 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
| 129 | if (p->maxHealth != 0)
|
---|
| 130 | al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*p->health)/p->maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
|
---|
| 131 |
|
---|
| 132 | if (p->hasBlueFlag)
|
---|
| 133 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
| 134 | else if (p->hasRedFlag)
|
---|
| 135 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
| 136 | }
|
---|
| 137 | }
|
---|