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
|
#include <stdint.h>
#include "keyb.h"
#include "vga.h"
#include "map.h"
#include "data.h"
#include "player.h"
#define DIR_RIGHT 0
#define DIR_LEFT 1
#define FRAME_STANDING 0
#define FRAME_JUMPING 4
#define FRAME_DYING 5
#define WALK_CYCLE_FRAMES 4
#define WALK_DELAY 8
#define GRAVITY_OFF 0
/* XXX: subtract 1 to get the value from gravity_seq */
#define GRAVITY_DOWN 14
#define GRAVITY_UP 1
#define GRAVITY_SEQ_LEN 24
#define IS_GOING_DOWN(x) ((x)>=GRAVITY_DOWN)
static uint16_t x, y;
static uint8_t dir, frame, delay, gravity, jump;
const uint8_t gravity_seq[GRAVITY_SEQ_LEN] = {
6, 4, 4, 2, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 4
};
static uint8_t bg[256];
static const Rect frames[2][6] =
{
{
/* walk cycle */
{ 0, 0, 144, 144 },
{ 16, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 32, 0, 144, 144 },
/* jump */
{ 48, 0, 144, 144 },
/* dying */
{ 128, 0, 144, 144 },
},
{
/* walk cycle */
{ 64, 0, 144, 144 },
{ 80, 0, 144, 144 },
{ 64, 0, 144, 144 },
{ 96, 0, 144, 144 },
/* jump */
{ 112, 0, 144, 144 },
/* dying */
{ 128, 0, 144, 144 },
},
};
void player_init(uint16_t start_x, uint8_t start_y)
{
dir = DIR_RIGHT;
frame = FRAME_STANDING;
delay = 0;
jump = 0;
x = start_x;
y = start_y;
gravity = GRAVITY_OFF;
}
void player_update()
{
uint8_t moved = 0;
if (gravity == GRAVITY_OFF
&& !map_is_blocked(x + 4, y + 16)
&& !map_is_blocked(x + 11, y + 16))
{
gravity = GRAVITY_DOWN;
frame = FRAME_JUMPING;
moved = 1;
}
if (keys[KEY_UP])
{
if (gravity == GRAVITY_OFF && !jump)
{
jump = 1;
frame = FRAME_JUMPING;
gravity = GRAVITY_UP;
}
}
else
jump = 0;
if (keys[KEY_RIGHT] && !keys[KEY_LEFT])
{
moved = 1;
dir = DIR_RIGHT;
/* XXX: off limits */
if (x < MAP_W * MAP_TILE_W - 16)
{
if ((IS_GOING_DOWN(gravity) && !map_is_blocked(x + 16, y + 15))
|| (!IS_GOING_DOWN(gravity)
&& !(map_is_blocked(x + 16, y + 15)
&& map_is_blocked(x + 16, y + 7))))
x++;
}
}
if (keys[KEY_LEFT] && !keys[KEY_RIGHT])
{
moved = 1;
dir = DIR_LEFT;
/* XXX: off limits */
if (x)
{
if ((IS_GOING_DOWN(gravity) && !map_is_blocked(x - 1, y + 15))
|| (!IS_GOING_DOWN(gravity)
&& !(map_is_blocked(x - 1, y + 15)
&& map_is_blocked(x - 1, y + 7))))
x--;
}
}
if (gravity != GRAVITY_OFF)
{
uint8_t steps = gravity_seq[gravity - 1];
moved = 1;
if (gravity > GRAVITY_DOWN)
{
/* going down! */
for (uint8_t i = 0; i < steps; i++)
{
/* hit the floor */
if ((map_is_blocked(x + 11, y + 16)
|| map_is_blocked(x + 4, y + 16))
&& !map_is_blocked(x + 4, y + 15)
&& !map_is_blocked(x + 11, y + 15))
{
gravity = GRAVITY_OFF;
frame = FRAME_STANDING;
break;
}
y++;
}
}
else
{
/* going up! */
if (y < steps)
y = 0;
else
y -= steps;
}
if (gravity != GRAVITY_OFF && gravity != GRAVITY_SEQ_LEN)
gravity++;
}
if (moved)
{
if (delay++ == WALK_DELAY)
{
delay = 0;
if (frame != FRAME_JUMPING)
{
frame++;
if (frame == WALK_CYCLE_FRAMES)
frame = FRAME_STANDING;
}
}
}
else
{
delay = 0;
frame = FRAME_STANDING;
}
}
void player_erase()
{
Rect dst = { x, y + MAP_OFFS_Y, 16, 16 };
blit(bg, &dst);
}
void player_draw()
{
Rect dst = { x, y + MAP_OFFS_Y, 16, 16 };
read_buffer(bg, &dst);
blitrc(binary_sprites_start, &frames[dir][frame], &dst);
}
|