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

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

Projectile drawing code moved to the GameRender class

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