forked from Kiritouse/TouHouInc--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
294 lines (278 loc) · 7.69 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "Enemy.h"
#include "FrameManager.h"
#include "Operation.h"
#include "Map.h"
#include "Draw.h"
#include "LoadResources.h"
#include "MathAndPhysics.h"
#include <vector>
#include <iostream>
#include <math.h>
#include <random>
const double MOVE_FACTOR = 0.1;
Enemy::EnemyNode* createEnemy(int FireSwitch, int fire_on, int x0, int y0, int moveMode, double radian, double speed, Enemy::EnemyName name, int health, int weaponLevel, Frame frame) {
Enemy::EnemyNode* pNew = new Enemy::EnemyNode;
switch (name)
{
case Enemy::EnemyName::ENEMY0:
pNew->type_enemy0 = 1;
break;
case Enemy::EnemyName::ENEMY1:
pNew->type_enemy1 = 1;
break;
}
//std::cout << "asdasd" << std::endl;
pNew->x = x0;
pNew->y = y0;
pNew->x0 = x0;
pNew->y0 = y0;
pNew->moveMode = moveMode;
pNew->radian = radian;
pNew->speed = speed;
pNew->health = health;
pNew->weaponLevel = weaponLevel;
pNew->f_create = frame.f_total - frame.f_pause;
pNew->isExist = 1;
pNew->FireSwitch = FireSwitch;
pNew->fire_on = fire_on;
pNew->pnext = NULL;
return pNew;
}
Enemy::EnemyNode* createEnemy(int FireSwitch, int fire_on, int x0, int y0, int moveMode, double speed, Enemy::EnemyName name, int health, int weaponLevel, Frame frame) {
Enemy::EnemyNode* pNew = new Enemy::EnemyNode;
switch (name)
{
case Enemy::EnemyName::ENEMY0:
pNew->type_enemy0 = 1;
break;
case Enemy::EnemyName::ENEMY1:
pNew->type_enemy1 = 1;
break;
}
pNew->FireSwitch = FireSwitch;
pNew->fire_on = fire_on;
pNew->x = x0;
pNew->y = y0;
pNew->x0 = x0;
pNew->y0 = y0;
pNew->moveMode = moveMode;
pNew->speed = speed;
pNew->health = health;
pNew->weaponLevel = weaponLevel;
pNew->f_create = frame.f_total - frame.f_pause;
pNew->isExist = 1;
pNew->pnext = NULL;
return pNew;
}
Enemy::EnemyNode* createEnemy(int FireSwitch, int fire_on, int x0, int y0, int moveMode, int xo, int yo, double radian, double speed, Enemy::EnemyName name, int health, int weaponLevel, Frame frame) {
Enemy::EnemyNode* pNew = new Enemy::EnemyNode;
switch (name)
{
case Enemy::EnemyName::ENEMY0:
pNew->type_enemy0 = 1;
break;
case Enemy::EnemyName::ENEMY1:
pNew->type_enemy1 = 1;
break;
}
pNew->FireSwitch = FireSwitch;
pNew->fire_on = fire_on;
pNew->x = x0;
pNew->y = y0;
pNew->x0 = x0;
pNew->y0 = y0;
pNew->moveMode = moveMode;
pNew->radian = radian;
pNew->xo = xo;
pNew->yo = yo;
pNew->r = y0 - yo;
pNew->speed = speed;
pNew->health = health;
pNew->weaponLevel = weaponLevel;
pNew->f_create = frame.f_total - frame.f_pause;
pNew->isExist = 1;
pNew->pnext = NULL;
return pNew;
}
void Enemy::Enemy_ListPushHead(Enemy::EnemyNode** pp_Enemy_List_Node_Head, Enemy::EnemyNode* newNode) {
if (*pp_Enemy_List_Node_Head == NULL)//如果链表为空,那么新增的节点就是第一个,头节点
{
*pp_Enemy_List_Node_Head = newNode;
return;
}
newNode->pnext = (*pp_Enemy_List_Node_Head)->pnext;//让新增节点指向原先头节点指向的数据
(*pp_Enemy_List_Node_Head)->pnext = newNode;//让原头节点指向新增节点
}
void Enemy::update_EnemyImage(Enemy::EnemyNode** p_Enemy_List) {
for (EnemyNode* cur = *p_Enemy_List; cur != NULL; cur = cur->pnext) {
if (cur->type_enemy0 == 1) {
transparentimage(NULL, cur->x, cur->y, WIDTH_ENEMY0, HEIGHT_ENEMY0,
0, 0, WIDTH_ENEMY0, HEIGHT_ENEMY0, &enemy0);
}
else if (cur->type_enemy1 == 1) {
transparentimage(NULL, cur->x, cur->y, WIDTH_ENEMY1, HEIGHT_ENEMY1,
0, 0, WIDTH_ENEMY1, HEIGHT_ENEMY1, &enemy1);
}
else if (cur->type_boss == 1) {
transparentimage(NULL, cur->x, cur->y, WIDTH_BOSS, HEIGHT_BOSS,
0, 0, WIDTH_BOSS, HEIGHT_BOSS, &enemy1);
}
}
}
void moveLine(Enemy::EnemyNode* cur) {
int xnext = cur->x + cur->speed * cos(cur->radian);
int ynext = cur->y + cur->speed * sin(cur->radian);
if (cur->type_enemy0 == 1)
{
if (xnext < WIDTH_MAP - WIDTH_ENEMY0 && ynext < HEIGHT_MAP - HEIGHT_ENEMY0 && xnext > 0 && ynext > 0) {
cur->x = xnext;
cur->y = ynext;
}
else cur->isExist = 0;
return;
}
else if (cur->type_enemy1 == 1) {
if (xnext < WIDTH_MAP - WIDTH_ENEMY1 && ynext < HEIGHT_MAP - HEIGHT_ENEMY1 && xnext >0 && ynext > 0) {
cur->x = xnext;
cur->y = ynext;
}
else cur->isExist = 0;
return;
}
}
void moveCircle(Enemy::EnemyNode* cur, int framebuffer, int type) {
int xnext, ynext;
if (type == 0) {
xnext = cur->x0 + cur->r * sin(cur->radian);
ynext = cur->y0 - (cur->r - cur->r * cos(cur->radian));
cur->radian = cur->radian + cur->speed;
}
else if (type == 1) {
xnext = cur->x0 - cur->r * sin(cur->radian);
ynext = cur->y0 - (cur->r - cur->r * cos(cur->radian));
cur->radian = cur->radian + cur->speed;
}
if (cur->type_enemy0 == 1) {
if (xnext < WIDTH_MAP - WIDTH_ENEMY0 && ynext < HEIGHT_MAP - HEIGHT_ENEMY0 && xnext > 1 && ynext > 1) {
cur->x = xnext;
cur->y = ynext;
}
else {
cur->isExist = 0;
return;
}
}
else if (cur->type_enemy1 == 1) {
if (xnext < WIDTH_MAP - WIDTH_ENEMY1 && ynext < HEIGHT_MAP - HEIGHT_ENEMY1 && xnext > 1 && ynext > 1) {
cur->x = xnext;
cur->y = ynext;
}
else {
cur->isExist = 0;
return;
}
}
}
void moveRand(Enemy::EnemyNode* cur, int framebuffer) {
srand((unsigned int)time(NULL));
int xnext = cur->x + rand() % ((int)cur->speed + 1) - (cur->speed) / 2;
int ynext = cur->y + rand() % ((int)cur->speed + 1) - (cur->speed) / 20;
if (cur->type_enemy0 == 1) {
if (xnext < WIDTH_MAP - WIDTH_ENEMY0 && ynext < HEIGHT_MAP - HEIGHT_ENEMY0 && xnext > 0 && ynext >0) {
cur->x = xnext;
cur->y = ynext;
}
else {
cur->isExist = 0;
return;
}
}
else if (cur->type_enemy1 == 1) {
if (xnext < WIDTH_MAP - WIDTH_ENEMY1 && ynext < HEIGHT_MAP - HEIGHT_ENEMY1 && xnext > 0 && ynext >0) {
cur->x = xnext;
cur->y = ynext;
}
else {
cur->isExist = 0;
return;
}
}
}
void moveBoss(Enemy::EnemyNode* cur) {
if (cur->type_boss == 1) {
if (cur->y <= HEIGHT_MAP / 3) {
cur->y += cur->speed;
}
else {
srand(time(0));
int xnext = cur->x + rand() % (int)cur->speed - (cur->speed / 2);//左右随机移动
if (xnext < 2 || xnext >= WIDTH_MAP - WIDTH_BOSS) {
cur->x = xnext;//只把符合要求的x的位移值给cur->x
}
}
}
}
void update_EnemyPosition(Enemy::EnemyNode** pp_Enemy_List_Node_Head, Frame frame) {//用指针遍历每个飞机节点,根据飞机的初始位置,初始方向,选择不同的更新方式
if (*pp_Enemy_List_Node_Head == NULL) return;
Enemy::EnemyNode* cur = *pp_Enemy_List_Node_Head;
int framebuffer = frame.f_total - frame.f_pause;
while (cur != NULL)
{
if (cur->isExist == 0) return;//如果已经被删除了就不需要再移动了
if (cur->type_boss == 1) moveBoss(cur);
else {
switch (cur->moveMode)
{
case DEF_MOVE_LINE:
moveLine(cur);
break;
case DEF_MOVE_CIRCLE_RIGHT:
moveCircle(cur, framebuffer, 0);
break;
case DEF_MOVE_CIRCLE_LEFT:
moveCircle(cur, framebuffer, 1);
break;
case DEF_MOVE_RAND:
moveRand(cur, framebuffer);
break;
default:
break;
}
}
cur = cur->pnext;
}
}
void listRemoveNode_Enemy(Enemy::EnemyNode** pp_Enemy_List_Node_Head)
{
if (*pp_Enemy_List_Node_Head == NULL)//如果链表为空,就没有可删除的节点了
return;
Enemy::EnemyNode* curP_Enemy = *pp_Enemy_List_Node_Head;//curret先指向第一个节点
Enemy::EnemyNode* prevP_Enemy = NULL; //previous指向上一个节点的指针
while (curP_Enemy != NULL)//遍历链表
{
if (curP_Enemy->isExist == 0)
{
if (*pp_Enemy_List_Node_Head == curP_Enemy)
{
*pp_Enemy_List_Node_Head = curP_Enemy->pnext;
free(curP_Enemy);
curP_Enemy = *pp_Enemy_List_Node_Head;
}
else
{
prevP_Enemy->pnext = curP_Enemy->pnext;
free(curP_Enemy);
curP_Enemy = prevP_Enemy;
}
}
else
{
prevP_Enemy = curP_Enemy;
curP_Enemy = curP_Enemy->pnext;
}
}
}
void Enemy::update_Enemy(Enemy::EnemyNode** pp_Enemy_List_Node_Head, Frame frame) {
update_EnemyPosition(pp_Enemy_List_Node_Head, frame);
listRemoveNode_Enemy(pp_Enemy_List_Node_Head);
}