1 /*
2     Copyright (C) 2009 Andrew Caudwell (acaudwell@gmail.com)
3 
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License
6     as published by the Free Software Foundation; either version
7     3 of the License, or (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "pawn.h"
19 
20 float gGourceShadowStrength = 0.5;
21 
Pawn(const std::string & name,vec2 pos,int tagid)22 Pawn::Pawn(const std::string& name, vec2 pos, int tagid) {
23     this->name  = name;
24     this->pos   = pos;
25     this->tagid = tagid;
26     this->hidden = false;
27     this->speed = 1.0;
28 
29     selected = false;
30     mouseover = false;
31 
32     shadow = false;
33 
34     namewidth = 0;
35 
36     this->shadowOffset = vec2(2.0, 2.0);
37     this->elapsed = 0.0;
38     this->fadetime = 1.0;
39     this->nametime = 5.0;
40     this->name_interval = 0.0;
41     this->namecol = vec3(1.0, 1.0, 1.0);
42 
43     this->graphic = 0;
44     this->graphic_ratio = 1.0;
45 }
46 
getSize()47 float Pawn::getSize() {
48     return size;
49 }
50 
setPos(vec2 pos)51 void Pawn::setPos(vec2 pos) {
52     this->pos = pos;
53 }
54 
getTagID()55 int Pawn::getTagID() {
56     return tagid;
57 }
58 
showName()59 void Pawn::showName() {
60     if(name_interval <= 0.0) name_interval = nametime;
61 }
62 
updateQuadItemBounds()63 void Pawn::updateQuadItemBounds() {
64 
65     float halfsize_x = size * 0.5f;
66 
67     vec2 halfsize ( halfsize_x, halfsize_x * graphic_ratio );
68 
69     //set bounds
70     quadItemBounds.set(pos - halfsize, pos + halfsize);
71 }
72 
logic(float dt)73 void Pawn::logic(float dt) {
74     elapsed += dt;
75 
76     if(!isHidden()) {
77         if(name_interval>0.0) name_interval -= dt;
78     }
79 }
80 
setGraphic(TextureResource * graphic)81 void Pawn::setGraphic(TextureResource* graphic) {
82 
83     if(graphic) {
84         graphic_ratio = graphic->h / (float) graphic->w;
85     } else {
86         graphic_ratio = 1.0f;
87     }
88 
89     this->graphic = graphic;
90     this->dims = vec2(size, size*graphic_ratio);
91 }
92 
93 
setMouseOver(bool over)94 void Pawn::setMouseOver(bool over) {
95     //showName();
96     this->mouseover = over;
97 }
98 
setSelected(bool selected)99 void Pawn::setSelected(bool selected) {
100     this->selected = selected;
101 }
102 
getNameColour() const103 const vec3& Pawn::getNameColour() const {
104     return namecol;
105 }
106 
calcScreenPos(const vec2 & offset)107 void Pawn::calcScreenPos(const vec2& offset) {
108     screenpos = display.project(vec3(pos.x+offset.x, pos.y+offset.y, 0.0f));
109 }
110 
nameVisible() const111 bool Pawn::nameVisible() const {
112     return ((!selected && name_interval < 0.0) || isHidden()) ? false : true;
113 }
114 
drawName()115 void Pawn::drawName() {
116     if(!nameVisible()) return;
117 
118     float done = nametime - name_interval;
119 
120     if(done < 1.0) {
121         drawNameText(done);
122     } else if(done > 1.0 && done < nametime - 1.0) {
123         drawNameText(1.0);
124     } else {
125         drawNameText((nametime - done));
126     }
127 }
128 
drawShadow(float dt)129 void Pawn::drawShadow(float dt) {
130     if(isHidden() || !shadow) return;
131 
132     float halfsize = size * 0.5f;
133     vec2 offsetpos = pos - vec2(halfsize, halfsize*graphic_ratio) + shadowOffset;
134 
135     float alpha = getAlpha();
136 
137     glBindTexture(GL_TEXTURE_2D, graphic->textureid);
138 
139     glColor4f(0.0, 0.0, 0.0, alpha * gGourceShadowStrength);
140 
141     glPushMatrix();
142         glTranslatef(offsetpos.x, offsetpos.y, 0.0f);
143 
144         glBegin(GL_QUADS);
145             glTexCoord2f(0.0f,0.0f);
146             glVertex2f(0.0f, 0.0f);
147 
148             glTexCoord2f(1.0f,0.0f);
149             glVertex2f(size, 0.0f);
150 
151             glTexCoord2f(1.0f,1.0f);
152             glVertex2f(size, size*graphic_ratio);
153 
154             glTexCoord2f(0.0f,1.0f);
155             glVertex2f(0.0f, size*graphic_ratio);
156         glEnd();
157     glPopMatrix();
158 }
159 
draw(float dt)160 void Pawn::draw(float dt) {
161     if(hidden) return;
162 
163     float halfsize = size * 0.5f;
164     vec2 offsetpos = pos - vec2(halfsize, halfsize*graphic_ratio);
165 
166     float alpha = getAlpha();
167 
168     vec3 col = getColour();
169 
170     glBindTexture(GL_TEXTURE_2D, graphic->textureid);
171 
172     glPushMatrix();
173         glTranslatef(offsetpos.x, offsetpos.y, 0.0f);
174 
175         glColor4f(col.x, col.y, col.z, alpha);
176 
177         glBegin(GL_QUADS);
178             glTexCoord2f(0.0f,0.0f);
179             glVertex2f(0.0f, 0.0f);
180 
181             glTexCoord2f(1.0f,0.0f);
182             glVertex2f(size, 0.0f);
183 
184             glTexCoord2f(1.0f,1.0f);
185             glVertex2f(size, size*graphic_ratio);
186 
187             glTexCoord2f(0.0f,1.0f);
188             glVertex2f(0.0f, size*graphic_ratio);
189         glEnd();
190 
191     glPopMatrix();
192 }
193