1 | #include "GameRender.h"
|
---|
2 |
|
---|
3 | #include <cmath>
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | #include <allegro5/allegro_primitives.h>
|
---|
7 |
|
---|
8 | #include "../../common/Common.h"
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | void GameRender::drawMap(WorldMap* gameMap)
|
---|
13 | {
|
---|
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 | {
|
---|
23 | TerrainType terrain = gameMap->getElement(x, y);
|
---|
24 | StructureType structure = gameMap->getStructure(x, y);
|
---|
25 |
|
---|
26 | switch(terrain) {
|
---|
27 | case TERRAIN_GRASS:
|
---|
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));
|
---|
29 | break;
|
---|
30 | case TERRAIN_OCEAN:
|
---|
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));
|
---|
32 | break;
|
---|
33 | case TERRAIN_ROCK:
|
---|
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));
|
---|
35 | break;
|
---|
36 | case TERRAIN_NONE:
|
---|
37 | break;
|
---|
38 | }
|
---|
39 |
|
---|
40 | switch(structure) {
|
---|
41 | case STRUCTURE_BLUE_FLAG:
|
---|
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));
|
---|
44 | break;
|
---|
45 | case STRUCTURE_RED_FLAG:
|
---|
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));
|
---|
48 | break;
|
---|
49 | case STRUCTURE_NONE:
|
---|
50 | break;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
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) {
|
---|
64 | case OBJECT_BLUE_FLAG:
|
---|
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;
|
---|
67 | case OBJECT_RED_FLAG:
|
---|
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;
|
---|
70 | case OBJECT_NONE:
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void GameRender::drawPlayers(map<unsigned int, Player*>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
79 | {
|
---|
80 | map<unsigned int, Player*>::iterator it;
|
---|
81 |
|
---|
82 | Player* p;
|
---|
83 | POSITION pos;
|
---|
84 | ALLEGRO_COLOR color;
|
---|
85 |
|
---|
86 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
87 | {
|
---|
88 | p = it->second;
|
---|
89 |
|
---|
90 | if (p->isDead)
|
---|
91 | continue;
|
---|
92 |
|
---|
93 | pos = mapToScreen(p->pos.toInt());
|
---|
94 |
|
---|
95 | if (p->getId() == curPlayerId)
|
---|
96 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
97 |
|
---|
98 | if (p->team == 0)
|
---|
99 | color = al_map_rgb(0, 0, 255);
|
---|
100 | else if (p->team == 1)
|
---|
101 | color = al_map_rgb(255, 0, 0);
|
---|
102 | else {
|
---|
103 | color = al_map_rgb(0, 0, 0);
|
---|
104 | cout << "Failed to determine player team when drawing player" << endl;
|
---|
105 | }
|
---|
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));
|
---|
135 | }
|
---|
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 | }
|
---|
159 | }
|
---|