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
|
#include <stdint.h>
#include "vga.h"
#include "map.h"
#include "entities.h"
#include "player.h"
#include "bat.h"
#define DIR_UP 0
#define DIR_DOWN 1
/* limit of the animation cycle */
#define MAX_FRAME 4
static const Rect frames[2 * 4] =
{
/* right */
{ 64, 32, 144, 144 },
{ 80, 32, 144, 144 },
{ 96, 32, 144, 144 },
{ 80, 32, 144, 144 },
/* left */
{ 64, 32, 144, 144 },
{ 80, 32, 144, 144 },
{ 96, 32, 144, 144 },
{ 80, 32, 144, 144 },
};
void bat_init(Entity *e)
{
e->frames = (const Rect *)frames;
e->update = bat_update;
/* will control vertical movement */
e->flags = DIR_UP;
}
void bat_update(Entity *e)
{
if (e->delay & 1)
{
/* horizontal */
if (e->dir == DIR_RIGHT)
{
if (map_is_blocked(e->x + 16, e->y + 7))
e->dir = DIR_LEFT;
else
e->x++;
}
else
{
/* dir is LEFT */
if (map_is_blocked(e->x - 1, e->y + 7))
e->dir = DIR_RIGHT;
else
e->x--;
}
/* vertical */
if (e->flags == DIR_DOWN)
{
if (map_is_blocked(e->x + 7, e->y + 16))
e->flags = DIR_UP;
else
e->y++;
}
else
{
/* dir is UP */
if (map_is_blocked(e->x + 7, e->y - 1))
e->flags = DIR_DOWN;
else
e->y--;
}
}
if (player_collision(e))
{
player_hit();
/* change direction */
e->dir ^= 1;
e->flags ^= 1;
}
if (e->delay++ == WALK_DELAY - 2)
{
e->delay = 0;
e->frame++;
if (e->frame == MAX_FRAME)
e->frame = 0;
}
}
|