source: network-game/client/Client/GameRender.cpp@ f15d6a9

Last change on this file since f15d6a9 was c991530, checked in by dportnoy <dmp1488@…>, 11 years ago

The playerClass instance variable of the Player class is now an enum instead of an int

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