-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtower.cpp
100 lines (88 loc) · 2.87 KB
/
tower.cpp
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
#include "tower.h"
#include <cmath>
#define TowerWidth 90
Tower::Tower(int n) {
self_rect = TowerPosition[n];
}
#define DILUTE 2
void Tower::attack() { //attack 由render掌管
if (type == Magic) {
PicOrder / DILUTE < 5 ? SDL_RenderCopy(gRenderer, MtowerTexture[(int) (PicOrder / DILUTE)], nullptr, &self_rect)
:SDL_RenderCopy(gRenderer, Texture_Now, nullptr, (&self_rect));
SDL_RenderCopy(gRenderer, MtowerManTexture[(int) (PicOrder / DILUTE)], nullptr, &Man_rect);
if ((float) PicOrder / DILUTE == 5) {
game->Bullet_Array[game->Bullet_point++] = new Bullet(target, self_rect.x + 38, self_rect.y + 0);
}
} else if (type == Cannon){
PicOrder / DILUTE < 5 ? SDL_RenderCopy(gRenderer, CtowerTexture[(int) (PicOrder / DILUTE)], nullptr, &self_rect) :
SDL_RenderCopy(gRenderer, Texture_Now, nullptr, &self_rect);
if ((float) PicOrder / DILUTE == 5) {
game->Shell_Array[game->Shell_point++] = new Shell(target, self_rect.x + 38, self_rect.y + 0);
}
}
PicOrder / DILUTE < 9 ? PicOrder++ : PicOrder = 0, target = nullptr;
}
void Tower::buildTower(TowerType tower_type) {
switch (tower_type) {
case Arrow:
break;
case Barracks:
break;
case Cannon:
Ctower_Init();
break;
case Magic:
Mtower_Init();
break;
}
Update_Rect(&self_rect);
tower_level = true;
}
void Tower::Update_Rect(SDL_Rect *r) {
int w, h;
SDL_QueryTexture(Texture_Now, nullptr, nullptr, &w, &h);
HtoW = (float) h / w;
h = TowerWidth * HtoW;
r->y = r->y + r->h - h + 5;
r->h = h;
}
void Tower::render() {
if (target == nullptr || CDstart > 0) {
CDstart--;
SDL_RenderCopy(gRenderer, Texture_Now, nullptr, &self_rect);
if (type == Magic) {
SDL_RenderCopy(gRenderer, Man_in_Tower, nullptr, &Man_rect);
}
} else {
attack();
}
}
bool Tower::in_range(SDL_Rect target) {
return sqrt((target.x - attackX) * (target.x - attackX) + (target.y - attackY) * (target.y - attackY)) < 166;
}
void Tower::Mtower_Init() {
game->money -= 100;
type = Magic;
Texture_Now = TowerStaticTexture[1];
SDL_QueryTexture(Texture_Now, nullptr, nullptr, &(Center.x), &(Center.y));
Center.x /= 2;
Center.y /= 2;
Man_rect = self_rect;
Man_rect.x += 30;
Man_rect.y -= 18;
Man_rect.h = 30;
Man_rect.w = 30;
attackX = self_rect.x + 44;
attackY = self_rect.y + 12;
Man_in_Tower = MtowerManTexture[0];
}
void Tower::Ctower_Init() {
game->money -= 125;
type = Cannon;
Texture_Now = TowerStaticTexture[2];
SDL_QueryTexture(Texture_Now, nullptr, nullptr, &(Center.x), &(Center.y));
Center.x /= 2;
Center.y /= 2;
attackX = self_rect.x + 44;
attackY = self_rect.y + 12;
}