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
|
#include "bat.h"
#include "../game.h"
#include "../PHL.h"
#include "../hero.h"
#include <stdlib.h>
#include <math.h>
void batStep(Bat* b);
void batDraw(Bat* b);
void createBat(int x, int y, int type)
{
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i] == NULL) {
Enemy* result = /*(Enemy*)*/malloc(sizeof *result);
Bat* b = /*(Bat*)*/malloc(sizeof *b);
b->id = i;
b->x = b->xstart = x;
b->y = b->ystart = y;
b->type = type;
b->imageIndex = 5;
b->counter = 0;
b->timer = 0;
b->state = 0;
b->dir = 1;
result->data = b;
result->enemyStep = batStep;
result->enemyDraw = batDraw;
result->type = 1;
enemies[i] = result;
i = MAX_ENEMIES;
}
}
}
void batStep(Bat* b)
{
//Wait
if (b->state == 0)
{
//Animate
{
b->imageIndex = 5;
}
//wait for hero to get near
{
if (b->timer <= 0) {
Mask area;
area.circle = 0;
area.unused = 0;
area.x = b->xstart - 60;
area.y = b->ystart;
area.w = 160; area.h = 100;
if (checkCollisionXY(area, herox, heroy + 20)) {
PHL_PlaySound(sounds[sndPi07], CHN_ENEMIES);
b->state = 1;
b->timer = 270;
if (b->type == 1) {
b->counter = 1;
if (herox < b->x + 20) {
b->dir = -1;
}else{
b->dir = 1;
}
}
}
}else{
b->timer -= 1;
}
}
}
//Fly
else if (b->state == 1)
{
//Animate
{
b->imageIndex += 0.25;
if (b->imageIndex >= 5) {
b->imageIndex -= 5;
}
}
//Rotation angle
{
b->timer += 4;
if (b->timer >= 360) {
b->timer -= 360;
}
}
//Movement
{
b->y = b->ystart + 30 + (30 * sin(b->timer * 3.14159 / 180));
//Red bat
if (b->type == 1) {
b->x += 2 * b->dir;
}
}
//Return to perch
{
if (b->timer == 270) {
if (b->type == 1 && b->counter > 0) {
b->dir *= -1;
b->timer = 270;
b->counter -= 1;
}else{
b->state = 0;
b->timer = 70;
}
}
}
}
//Setup Mask
Mask mask;
{
mask.circle = mask.unused = 0;
mask.w = 32;
mask.h = 28;
mask.x = b->x + ((40 - mask.w) / 2);
mask.y = b->y;
}
//Hit Player
{
if (checkCollision(mask, heroMask)) {
heroHit(10, mask.x + (mask.w / 2));
}
}
//Weapon collision
{
int i;
for (i = 0; i < MAX_WEAPONS; i++) {
if (weapons[i] != NULL) {
if (checkCollision(mask, weapons[i]->weaponMask)) {
weaponHit(weapons[i]);
//Death
createEffect(2, b->x - 12, b->y - 6);
spawnCollectable(b->x + 20, b->y);
enemyDestroy(b->id);
i = MAX_WEAPONS;
}
}
}
}
}
void batDraw(Bat* b)
{
int cropX = 0,
cropY = 120;
if (b->type == 1) {
cropX = 400;
cropY = 280;
}
cropX += (int)b->imageIndex * 40;
PHL_DrawSurfacePart(b->x, b->y - 4, cropX, cropY, 40, 40, images[imgEnemies]);
}
|