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
|
#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;
Entity e;
e = new_entity(world);
add_components(
world,
e,
ctype_position |
ctype_sprite |
ctype_enemy |
ctype_skull
);
pos = &world->positions[e];
sprite = &world->sprites[e];
enemy = &world->enemies[e];
pos->x = x;
pos->y = y;
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;
char buf[32];
player = &world->player;
ppos = &world->positions[player->entity];
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;
dpy = (ppos->y - pos->y) >> 4;
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,
pos->y,
tpx * enemy_bullet_speed,
tpy * enemy_bullet_speed
);
enemy->shoot_timer = 0;
}
enemy->shoot_timer++;
}
}
}
|