[6319311] | 1 | #include "GameRender.h"
|
---|
| 2 |
|
---|
[1e250bf] | 3 | #include <cmath>
|
---|
[dfc81f0] | 4 | #include <iostream>
|
---|
[1e250bf] | 5 |
|
---|
[6319311] | 6 | #include <allegro5/allegro_primitives.h>
|
---|
| 7 |
|
---|
[e5697b1] | 8 | #include "../../common/Common.h"
|
---|
| 9 |
|
---|
[dfc81f0] | 10 | using namespace std;
|
---|
| 11 |
|
---|
[6319311] | 12 | void GameRender::drawMap(WorldMap* gameMap)
|
---|
| 13 | {
|
---|
[e6c26b8] | 14 | POSITION mapPos;
|
---|
| 15 | mapPos.x = 0;
|
---|
| 16 | mapPos.y = 0;
|
---|
| 17 | mapPos = mapToScreen(mapPos);
|
---|
| 18 |
|
---|
| 19 | for (int x=0; x<gameMap->width; x++)
|
---|
| 20 | {
|
---|
| 21 | for (int y=0; y<gameMap->height; y++)
|
---|
| 22 | {
|
---|
[5c7f28d] | 23 | TerrainType terrain = gameMap->getElement(x, y);
|
---|
[f66d04f] | 24 | StructureType structure = gameMap->getStructure(x, y);
|
---|
[e6c26b8] | 25 |
|
---|
[5c7f28d] | 26 | switch(terrain) {
|
---|
| 27 | case TERRAIN_GRASS:
|
---|
[e6c26b8] | 28 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
|
---|
[5c7f28d] | 29 | break;
|
---|
| 30 | case TERRAIN_OCEAN:
|
---|
[e6c26b8] | 31 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
[5c7f28d] | 32 | break;
|
---|
| 33 | case TERRAIN_ROCK:
|
---|
[e6c26b8] | 34 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
|
---|
[5c7f28d] | 35 | break;
|
---|
| 36 | case TERRAIN_NONE:
|
---|
| 37 | break;
|
---|
| 38 | }
|
---|
[e6c26b8] | 39 |
|
---|
[5c7f28d] | 40 | switch(structure) {
|
---|
| 41 | case STRUCTURE_BLUE_FLAG:
|
---|
[e6c26b8] | 42 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
| 43 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
[5c7f28d] | 44 | break;
|
---|
| 45 | case STRUCTURE_RED_FLAG:
|
---|
[e6c26b8] | 46 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
| 47 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
[5c7f28d] | 48 | break;
|
---|
| 49 | case STRUCTURE_NONE:
|
---|
| 50 | break;
|
---|
[e6c26b8] | 51 | }
|
---|
| 52 | }
|
---|
[6319311] | 53 | }
|
---|
| 54 |
|
---|
[e6c26b8] | 55 | for (int x=0; x<gameMap->width; x++)
|
---|
| 56 | {
|
---|
| 57 | for (int y=0; y<gameMap->height; y++)
|
---|
| 58 | {
|
---|
| 59 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
| 60 |
|
---|
| 61 | vector<WorldMap::Object>::iterator it;
|
---|
| 62 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
| 63 | switch(it->type) {
|
---|
[f66d04f] | 64 | case OBJECT_BLUE_FLAG:
|
---|
[e6c26b8] | 65 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
| 66 | break;
|
---|
[f66d04f] | 67 | case OBJECT_RED_FLAG:
|
---|
[e6c26b8] | 68 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
| 69 | break;
|
---|
[1e250bf] | 70 | case OBJECT_NONE:
|
---|
| 71 | break;
|
---|
[e6c26b8] | 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[6319311] | 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void GameRender::drawPlayers(map<unsigned int, Player*>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
| 79 | {
|
---|
[e6c26b8] | 80 | map<unsigned int, Player*>::iterator it;
|
---|
| 81 |
|
---|
| 82 | Player* p;
|
---|
| 83 | POSITION pos;
|
---|
| 84 | ALLEGRO_COLOR color;
|
---|
| 85 |
|
---|
[b4c5b6a] | 86 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
[e6c26b8] | 87 | {
|
---|
| 88 | p = it->second;
|
---|
| 89 |
|
---|
| 90 | if (p->isDead)
|
---|
| 91 | continue;
|
---|
| 92 |
|
---|
| 93 | pos = mapToScreen(p->pos.toInt());
|
---|
| 94 |
|
---|
[5b92307] | 95 | if (p->getId() == curPlayerId)
|
---|
[e6c26b8] | 96 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
| 97 |
|
---|
[85da778] | 98 | if (p->team == Player::TEAM_BLUE)
|
---|
[e6c26b8] | 99 | color = al_map_rgb(0, 0, 255);
|
---|
[85da778] | 100 | else if (p->team == Player::TEAM_RED)
|
---|
[e6c26b8] | 101 | color = al_map_rgb(255, 0, 0);
|
---|
[dfc81f0] | 102 | else {
|
---|
| 103 | color = al_map_rgb(0, 0, 0);
|
---|
| 104 | cout << "Failed to determine player team when drawing player" << endl;
|
---|
| 105 | }
|
---|
[e6c26b8] | 106 |
|
---|
| 107 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
| 108 |
|
---|
| 109 | // draw player class
|
---|
| 110 | int fontHeight = al_get_font_line_height(font);
|
---|
| 111 |
|
---|
| 112 | string strClass;
|
---|
| 113 | switch (p->playerClass) {
|
---|
| 114 | case Player::CLASS_WARRIOR:
|
---|
| 115 | strClass = "W";
|
---|
| 116 | break;
|
---|
| 117 | case Player::CLASS_RANGER:
|
---|
| 118 | strClass = "R";
|
---|
| 119 | break;
|
---|
| 120 | case Player::CLASS_NONE:
|
---|
| 121 | strClass = "";
|
---|
| 122 | break;
|
---|
| 123 | }
|
---|
| 124 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
| 125 |
|
---|
| 126 | // draw player health
|
---|
| 127 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
| 128 | if (p->maxHealth != 0)
|
---|
| 129 | 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));
|
---|
| 130 |
|
---|
| 131 | if (p->hasBlueFlag)
|
---|
| 132 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
| 133 | else if (p->hasRedFlag)
|
---|
| 134 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
[6319311] | 135 | }
|
---|
[e5697b1] | 136 | }
|
---|
| 137 |
|
---|
| 138 | void GameRender::drawProjectiles(map<unsigned int, Projectile>& mapProjectiles, map<unsigned int, Player*>& mapPlayers)
|
---|
| 139 | {
|
---|
| 140 | map<unsigned int, Projectile>::iterator it;
|
---|
| 141 | for (it = mapProjectiles.begin(); it != mapProjectiles.end(); it++)
|
---|
| 142 | {
|
---|
| 143 | Projectile proj = it->second;
|
---|
| 144 |
|
---|
| 145 | FLOAT_POSITION target = mapPlayers[proj.target]->pos;
|
---|
| 146 | float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
|
---|
| 147 |
|
---|
| 148 | POSITION start, end;
|
---|
| 149 | start.x = cos(angle)*15+proj.pos.x;
|
---|
| 150 | start.y = sin(angle)*15+proj.pos.y;
|
---|
| 151 | end.x = proj.pos.x;
|
---|
| 152 | end.y = proj.pos.y;
|
---|
| 153 |
|
---|
| 154 | start = mapToScreen(start);
|
---|
| 155 | end = mapToScreen(end);
|
---|
| 156 |
|
---|
| 157 | al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
|
---|
| 158 | }
|
---|
[1e250bf] | 159 | }
|
---|