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
|
#include "bullet.h"
#include "components.h"
#include "game_config.h"
#include "sprite.h"
#include "standard.h"
#include "world.h"
Entity new_skull(World* world, int x, int y) {
CSprite* sprite;
CPosition* pos;
CEnemy* enemy;
CCollider* col;
Entity e;
e = new_entity(world);
add_components(
world,
e,
ctype_position |
ctype_sprite |
ctype_enemy |
ctype_skull |
ctype_collider |
ctype_moveable
);
pos = &world->positions[e];
sprite = &world->sprites[e];
enemy = &world->enemies[e];
col = &world->colliders[e];
pos->x = x;
pos->y = y;
col->x = 2 << fbits;
col->y = 2 << fbits;
col->w = 14 << fbits;
col->h = 14 << fbits;
init_csprite(sprite, sprite_skull_right);
enemy->hp = skull_hp;
enemy->shoot_timer = 0;
return e;
}
void enemy_system(World* world) {
/* Skulls. */
int i;
unsigned bits;
CPosition* pos;
CEnemy* enemy;
Player* player;
CPosition* ppos;
int dpx, dpy, tpx, tpy, d;
player = &world->player;
ppos = &world->positions[player->entity];
for (i = 0; i < world->entity_count; i++) {
bits = world->bitmask[i];
if (bits & ctype_enemy) {
enemy = &world->enemies[i];
if (enemy->hp <= 0) {
destroy_entity(world, i);
}
}
}
for (i = 0; i < world->entity_count; i++) {
bits = world->bitmask[i];
if (
(bits & ctype_position) &&
(bits & ctype_enemy) &&
(bits & ctype_skull)
) {
pos = &world->positions[i];
enemy = &world->enemies[i];
dpx = ((ppos->x - pos->x) >> 4) + 256;
dpy = ((ppos->y - pos->y) >> 4) + 256;
tpx = dpx;
tpy = dpy;
d = ((dpx * dpx) >> fbits) + ((dpy * dpy) >> fbits);
if (d < 10 << fbits) {
dpx = -dpx;
dpy = -dpy;
enemy->backpedal = 1;
} else if (enemy->backpedal) {
if (d > 30 << fbits) {
enemy->backpedal = 0;
} else {
dpx = -dpx;
dpy = -dpy;
}
}
dpx = (dpx < 0 ? -1 : 1);
dpy = (dpy < 0 ? -1 : 1);
pos->x += dpx * skull_speed;
pos->y += dpy * skull_speed;
if (enemy->shoot_timer > skull_shoot_cooldown) {
vec_nrmise(&tpx, &tpy);
new_enemy_bullet(
world,
pos->x + 2560,
pos->y + 2560,
tpx * enemy_bullet_speed,
tpy * enemy_bullet_speed
);
enemy->shoot_timer = 0;
}
enemy->shoot_timer++;
}
}
}
|