-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.cpp
126 lines (113 loc) · 3.06 KB
/
enemy.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
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
#include "enemy.h"
#include "waypoint.h"
#include <cmath>
#include <iostream>
#include <SDL.h>
#include <cmath>
//bool watch;
bool Enemy::overline(State state) {
circle c = Circles[circle_num];
double temp;
switch (state) {
case IN:
temp = c.Fa * (X - c.centerX) + c.Fb * (Y - c.centerY);
if (judgement && judgement * temp <= 0) {
if (c.over_flat && !flat_overed){
flat_overed = true;
break;
}
exiting_circle = true;
judgement = 0;
return true;
}
break;
case OUT:
temp = c.Sa * (X - c.centerX) + c.Sb * (Y - c.centerY);
if (judgement && judgement * temp <= 0) {
in_circle = true;
flat_overed = false;
judgement = 0;
return true;
}
break;
}
judgement = temp;
return false;
}
Enemy::Enemy(int n) {
//Initialize the offsets
Pos = WayPoint[0];
Pos.y += n;
//Initialize the velocity
PicOrder = 0;
}
void Enemy::move_control() {
if (exiting_circle) {
circle_num++;
in_circle = false;
exiting_circle = false;
}
move(Circles[circle_num]);
}
void Enemy::move(struct circle Circle) {
Distance_Covered += VEL;
// Pos.x += VelX;
if (in_circle) {
double Vxtoy;
int temp;
temp = X - Circle.centerX > 0 ? -1 : 1;
Vxtoy = -(Y - Circle.centerY) / (X - Circle.centerX + Circle.conter * 0.0000000001);
VelY = temp * Circle.conter * sqrt(VEL / (1 + Vxtoy * Vxtoy));
VelX = VelY * Vxtoy;
overline(IN);
} else overline(OUT);
X += VelX;
Y += VelY;
Pos.x = (int) X;
Pos.y = (int) Y; //这里是为了记录具体位置。
}
void Enemy::render() {
if (Health <= 0 || Pos.y > 870) {
PicOrder < (Picture_max - 2) * 2 ? PicOrder = (Picture_max - 2) * 2 : 0; //设定死亡动画只有两帧。
dying = true;
smogpos.x=Pos.x-20;
smogpos.y=Pos.y-10;
SDL_RenderCopy(gRenderer, smog[4], nullptr, &smogpos);
death();
return;
}
move_control();
SDL_RenderCopy(gRenderer, Texture_Set[PicOrder / 2], nullptr, &Pos);
PicOrder++;
PicOrder = PicOrder % ((Picture_max - 2) * 2);
//Update screen
}
void Enemy::death() {
game->Enemy_Array[game->Enemy_Num] = nullptr;
game->money += 5;
delete this;
}
Enemy1::Enemy1(int n) : Enemy(n) {
Texture_Set = DevilTexture;
Picture_max = Enemy1_pic;
SDL_QueryTexture(Texture_Set[0], nullptr, nullptr, &(Center.x), &(Center.y));
Center.x /= 2;
Center.y /= 2;
Health = 30;
VEL = 30;
VelX = -std::sqrt(VEL);
X = Pos.x;
Y = Pos.y;
life_cost = 1;
}
void Enemy1::death() {
if (PicOrder < Picture_max * 2) {
SDL_RenderCopy(gRenderer, Texture_Set[PicOrder++ / 2], nullptr, &Pos);
return;
}
if (Pos.y > 870) {
game->life -= life_cost;
game->money -= 5;
}
Enemy::death();
}