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

Last change on this file since 1e250bf was 1e250bf, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

Client makefile is up-to-date and includes -Wall

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