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