-
Notifications
You must be signed in to change notification settings - Fork 1
/
tower.h
137 lines (121 loc) · 2.68 KB
/
tower.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef _TOWER_H_
#define _TOWER_H_
#include "image.h"
#include "draw.h"
#include "enemy.h"
class Tower {
public:
float x, y, cx, cy, width, height, range;
float dmg;
bool active;
int frameNo;
int level;
int id;
Image *texture;
Enemy *currEnemy;
struct timespec frameStart, currentTime;
Tower();
Tower(Image *img, float x, float y);
void setxy(int x, int y);
void setwh(int w, int h);
void draw();
void drawProjectile();
void showRange();
void setCurrEnemy(Enemy *enemy);
void attackEnemy();
} nullTower;
Tower::Tower()
{
texture = &towerBasic;
x = y = -10;
width = height = 0;
range = 0;
active = 0;
level = 0;
id = -1;
}
Tower::Tower(Image *img, float x, float y) {
texture = img;
this->x = x;
this->y = y;
cx = x+g.tile_pxSize/2;
cy = y+g.tile_pxSize/2;
width = g.tower_pxSize;
height = g.tower_pxSize;
id = (x/g.tile_pxSize)*10 + (y/g.tile_pxSize);
level = 1;
active = 1;
range = 175;
dmg = 0.20;
currEnemy = nullptr;
frameNo = 1;
clock_gettime(CLOCK_REALTIME, &frameStart);
}
void Tower::setxy(int x, int y)
{
this->x = x;
this->y = y;
}
void Tower::setwh(int w, int h)
{
this->width = h;
this->height = w;
}
void Tower::draw()
{
//offset is used to center tower to tile
//tx and ty vars are used to map the frames of the sprite sheet
static const float offset = (g.tile_pxSize - g.tower_pxSize)/2.0f;
float tx1 = 0.0f + (float)((frameNo-1) % 11) * (1.0f/11.0f);
float tx2 = tx1 + (1.0f/11.0f);
static const float ty1 = 0.0f;
static const float ty2 = 1.0f;
//timespec to control sprite frames
static double diff = 0;
static const double framerate = 0.075;
clock_gettime(CLOCK_REALTIME, ¤tTime);
diff = timeDiff(&frameStart, ¤tTime); //initial diff will always be greater than frame rate
if (diff > framerate) {
frameNo++;
timeCopy(&frameStart, ¤tTime);
}
if (frameNo >= 11)
frameNo = 1;
//draw tower
drawQuadTexAlpha(*texture, x+offset, y+offset, tx1, tx2, ty1, ty2, width, height);
}
void Tower::drawProjectile()
{
//draw attack
if (currEnemy) {
//get center of enemy texture
float e_cx = currEnemy->x+(float)g.enemy_pxSize/2.0f;
float e_cy = currEnemy->y+(float)g.enemy_pxSize/2.0f;;
//draw line
glColor4ub(255,0,255,255);
glPushMatrix();
glBegin(GL_LINES);
glVertex2f(cx, cy+18);
glVertex2f(e_cx, e_cy);
glEnd();
glPopMatrix();
}
}
void Tower::showRange()
{
const int offset = g.tile_pxSize/2;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.68, 0.85, 0.90, 0.60);
drawCircle(x+offset, y+offset, range);
glDisable(GL_BLEND);
}
void Tower::setCurrEnemy(Enemy *enemy)
{
currEnemy = enemy;
}
void Tower::attackEnemy()
{
currEnemy->health -= dmg;
}
#endif //_TOWER_H_