5 #include "../ui/display.h"
6 #include "../ui/event.h"
7 #include "../ui/sprite.h"
8 #include "../util/util.h"
15 static vec2d
const gravity
= { FIXED_POINT_I(0, 0), FIXED_POINT_I(0, 56) };
16 static vec2d
const move_max
= { FIXED_POINT_I(0, 600), FIXED_POINT_I(1, 300) };
17 static fixed_point
const accel_horiz
= FIXED_POINT_I(0, 50);
18 static fixed_point
const accel_vert
= FIXED_POINT_I(0, 167);
19 static fixed_point
const drag_factor
= FIXED_POINT_I(0, 854);
20 static fixed_point
const speed_jump_x
= FIXED_POINT_I(0, 600);
22 vec2d
hacker_extents(void) { return (vec2d
) { FIXED_INT_I(5), FIXED_INT_I(8) }; }
23 static vec2d
const shot_spawn_inertia
= { FIXED_POINT_I(0, 800), FIXED_POINT_I(0, -800) };
25 static badge_sprite
const anim_hacker
[] = {
26 { 5, 8, (uint8_t const *) "\x1c\xff\xfd\x04\x04" },
27 { 5, 8, (uint8_t const *) "\x1c\xff\x3d\xc4\x04" },
28 { 5, 8, (uint8_t const *) "\xdc\x3f\x1d\x24\xc4" },
29 { 5, 8, (uint8_t const *) "\x1c\xff\x3d\xc4\x04" }
31 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x31" },
32 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x52" },
33 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x94\x8c" },
34 { 5, 8, (uint8_t const *) "\x46\xfc\x73\x8c\x52" }
37 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\xc3\x30" },
38 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x43\x51" },
39 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x35\x82" },
40 { 6, 8, (uint8_t const *) "\x0c\xe1\x3b\x0e\x43\x51" }
43 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
44 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
45 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" },
46 { 6, 8, (uint8_t const *) "\xff\xff\xff\xff\xff\xff" }
50 static badge_sprite
const anim_sickle
[] = {
51 { 3, 3, (uint8_t const *) "\xab\x01" },
52 { 3, 3, (uint8_t const *) "\xee\x00" }
54 { 3, 3, (uint8_t const *) "\x8a\x01" },
55 { 3, 3, (uint8_t const *) "\x6a" },
56 { 3, 3, (uint8_t const *) "\xa3" },
57 { 3, 3, (uint8_t const *) "\xac" }
62 JUMPNRUN_SHOT_EXTENT
= 3,
63 JUMPNRUN_SHOT_TICKS_PER_FRAME
= 36
66 static void jumpnrun_shot_spawn(jumpnrun_shot
*shot
, jumpnrun_game_state
const *state
) {
68 shot
->inertia
= shot_spawn_inertia
;
70 if(state
->player
.anim_direction
== BADGE_BLT_MIRRORED
) {
71 shot
->current_box
= rectangle_new((vec2d
) { fixed_point_sub(rectangle_left(&state
->player
.current_box
), FIXED_INT(JUMPNRUN_SHOT_EXTENT
)), rectangle_top(&state
->player
.current_box
) },
72 (vec2d
) { FIXED_INT(JUMPNRUN_SHOT_EXTENT
), FIXED_INT(JUMPNRUN_SHOT_EXTENT
) });
73 shot
->inertia
.x
= fixed_point_neg(shot
->inertia
.x
);
75 shot
->current_box
= rectangle_new((vec2d
) { rectangle_right(&state
->player
.current_box
), rectangle_top(&state
->player
.current_box
) },
76 (vec2d
) { FIXED_INT(JUMPNRUN_SHOT_EXTENT
), FIXED_INT(JUMPNRUN_SHOT_EXTENT
) });
79 shot
->old_box
= shot
->current_box
;
80 shot
->inertia
= vec2d_add(shot
->inertia
, state
->player
.inertia
);
83 static inline int imax(int x
, int y
) {
87 static inline fixed_point
hacker_left (vec2d
const *pos
, jumpnrun_game_state
const *state
) { (void) state
; return pos
->x
; }
88 static inline fixed_point
hacker_top (vec2d
const *pos
, jumpnrun_game_state
const *state
) { (void) state
; return pos
->y
; }
89 static inline fixed_point
hacker_right (vec2d
const *pos
, jumpnrun_game_state
const *state
) { return fixed_point_add(hacker_left(pos
, state
), hacker_extents().x
); }
90 static inline fixed_point
hacker_bottom(vec2d
const *pos
, jumpnrun_game_state
const *state
) { return fixed_point_add(hacker_top (pos
, state
), hacker_extents().y
); }
92 int jumpnrun_level_assert_left_side(jumpnrun_game_state
const *state
) {
93 static int const lmargin
= 20;
94 static int const rmargin
= 50;
96 int pos_cur
= fixed_point_cast_int(state
->player
.current_box
.pos
.x
);
97 int pos_rel
= pos_cur
- state
->left
;
99 if(pos_rel
< lmargin
) {
100 return imax(0, pos_cur
- lmargin
);
101 } else if(pos_rel
> BADGE_DISPLAY_WIDTH
- rmargin
) {
102 return pos_cur
- (BADGE_DISPLAY_WIDTH
- rmargin
);
108 static int jumpnrun_bsearch_tile(jumpnrun_level
const *lv
, jumpnrun_game_state
const *state
) {
110 int len
= lv
->header
.tile_count
;
113 int mid
= front
+ len
/ 2;
115 if(fixed_point_lt(tile_right(&lv
->tiles
[mid
]), FIXED_INT(state
->left
- JUMPNRUN_MAX_SPAWN_MARGIN
))) {
126 jumpnrun_tile_range
jumpnrun_visible_tiles(jumpnrun_level
const *lv
,
127 jumpnrun_game_state
const *state
) {
128 jumpnrun_tile_range r
;
130 r
.first
= jumpnrun_bsearch_tile(lv
, state
);
132 for(r
.last
= r
.first
;
133 r
.last
< lv
->header
.tile_count
&& lv
->tiles
[r
.last
].pos
.x
* JUMPNRUN_TILE_PIXEL_WIDTH
< state
->left
+ BADGE_DISPLAY_WIDTH
+ JUMPNRUN_MAX_SPAWN_MARGIN
;
140 void jumpnrun_passive_movement(vec2d
*inertia
)
142 *inertia
= vec2d_add(*inertia
, gravity
);
144 inertia
->x
= fixed_point_min(fixed_point_max(fixed_point_neg(move_max
.x
), inertia
->x
), move_max
.x
);
145 inertia
->y
= fixed_point_min(fixed_point_max(fixed_point_neg(move_max
.y
), inertia
->y
), move_max
.y
);
148 static void jumpnrun_apply_movement(jumpnrun_level
const *lv
,
149 jumpnrun_tile_range
const *tilerange
,
150 jumpnrun_game_state
*state
,
151 vec2d
*inertia_mod
) {
152 switch(badge_event_current_input_state() &
153 (BADGE_EVENT_KEY_LEFT
|
154 BADGE_EVENT_KEY_RIGHT
)) {
155 case BADGE_EVENT_KEY_LEFT
:
156 // state->player.inertia.x = state->player.touching_ground ? fixed_point_sub(state->player.inertia.x, accel_horiz) : fixed_point_neg(speed_jump_x);
157 state
->player
.inertia
.x
= fixed_point_sub(state
->player
.inertia
.x
, accel_horiz
);
158 state
->player
.anim_direction
= BADGE_BLT_MIRRORED
;
160 case BADGE_EVENT_KEY_RIGHT
:
161 // state->player.inertia.x = state->player.touching_ground ? fixed_point_add(state->player.inertia.x, accel_horiz) : speed_jump_x;
162 state
->player
.inertia
.x
= fixed_point_add(state
->player
.inertia
.x
, accel_horiz
);
163 state
->player
.anim_direction
= 0;
166 if(state
->player
.touching_ground
) {
167 state
->player
.inertia
.x
= fixed_point_mul(state
->player
.inertia
.x
, drag_factor
);
169 //state->player.inertia.x = FIXED_INT(0);
175 if(state
->player
.jumpable_frames
== 0) {
176 // intentionally left blank.
177 } else if(badge_event_current_input_state() & BADGE_EVENT_KEY_BTN_A
) {
178 state
->player
.inertia
.y
= fixed_point_sub(state
->player
.inertia
.y
, accel_vert
);
179 --state
->player
.jumpable_frames
;
181 state
->player
.jumpable_frames
= 0;
184 jumpnrun_passive_movement(&state
->player
.inertia
);
186 vec2d new_pos
= vec2d_add(state
->player
.current_box
.pos
, state
->player
.inertia
);
188 if(fixed_point_lt(new_pos
.x
, FIXED_INT(state
->left
))) {
189 new_pos
.x
= FIXED_INT(state
->left
);
190 state
->player
.inertia
.x
= FIXED_INT(0);
193 *inertia_mod
= state
->player
.inertia
;
194 bool killed
= collisions_tiles_displace(&new_pos
, &state
->player
, lv
, tilerange
, inertia_mod
);
195 state
->player
.inertia
= *inertia_mod
;
197 if(killed
|| fixed_point_gt(state
->player
.current_box
.pos
.y
, FIXED_INT(BADGE_DISPLAY_HEIGHT
))) {
198 state
->status
= JUMPNRUN_DEAD
;
202 void jumpnrun_level_tick(jumpnrun_level
*lv
,
203 jumpnrun_game_state
*state
)
205 jumpnrun_tile_range tilerange
= jumpnrun_visible_tiles(lv
, state
);
206 vec2d inertia_mod
= state
->player
.inertia
;
208 jumpnrun_apply_movement(lv
, &tilerange
, state
, &inertia_mod
);
209 state
->left
= jumpnrun_level_assert_left_side(state
);
211 if(state
->player
.tick_minor
== 0) {
212 badge_framebuffer fb
;
213 badge_framebuffer_clear(&fb
);
215 for(size_t tile
= tilerange
.first
; tile
< tilerange
.last
; ++tile
) {
216 badge_framebuffer_blt(&fb
,
217 fixed_point_cast_int(tile_left(&lv
->tiles
[tile
])) - state
->left
,
218 fixed_point_cast_int(tile_top (&lv
->tiles
[tile
])),
219 &tile_type(&lv
->tiles
[tile
])->sprite
,
223 for(size_t item
= 0; item
< lv
->header
.item_count
; ++item
) {
224 jumpnrun_item
*item_obj
= &lv
->items
[item
];
226 if(item_obj
->flags
& JUMPNRUN_ITEM_COLLECTED
) {
230 int screenpos
= fixed_point_cast_int(item_obj
->pos
.x
) - state
->left
;
231 if(screenpos
> -item_obj
->type
->sprite
.width
&&
232 screenpos
< BADGE_DISPLAY_WIDTH
) {
233 rectangle item_rect
= rect_from_item(item_obj
);
235 if(rectangle_intersect(&state
->player
.current_box
, &item_rect
)) {
236 item_obj
->type
->on_collect(item_obj
, state
, lv
);
239 badge_framebuffer_blt(&fb
,
241 fixed_point_cast_int(item_obj
->pos
.y
),
242 &item_obj
->type
->sprite
,
247 for(size_t shot_ix
= 0; shot_ix
< JUMPNRUN_MAX_SHOTS
; ++shot_ix
) {
248 jumpnrun_shot
*shot
= &state
->shots
[shot_ix
];
250 if(jumpnrun_shot_spawned(shot
)) {
251 rectangle_move_rel(&shot
->current_box
, shot
->inertia
);
252 shot
->inertia
= vec2d_add(shot
->inertia
, gravity
);
254 if(fixed_point_gt(rectangle_top(&shot
->current_box
), FIXED_INT(BADGE_DISPLAY_HEIGHT
))) {
255 jumpnrun_shot_despawn(shot
);
258 /* show every position twice, because LCD switching time. This makes the shots more
259 * visible on the nokia lcds.
261 badge_framebuffer_blt(&fb
,
262 fixed_point_cast_int(shot
->old_box
.pos
.x
) - state
->left
,
263 fixed_point_cast_int(shot
->old_box
.pos
.y
),
264 &anim_sickle
[shot
->tick
/ JUMPNRUN_SHOT_TICKS_PER_FRAME
],
265 fixed_point_lt(shot
->inertia
.x
, FIXED_INT(0)) ? BADGE_BLT_MIRRORED
: 0);
266 badge_framebuffer_blt(&fb
,
267 fixed_point_cast_int(shot
->current_box
.pos
.x
) - state
->left
,
268 fixed_point_cast_int(shot
->current_box
.pos
.y
),
269 &anim_sickle
[shot
->tick
/ JUMPNRUN_SHOT_TICKS_PER_FRAME
],
270 fixed_point_lt(shot
->inertia
.x
, FIXED_INT(0)) ? BADGE_BLT_MIRRORED
: 0);
272 shot
->old_box
= shot
->current_box
;
275 if(shot
->tick
== ARRAY_SIZE(anim_sickle
) * JUMPNRUN_SHOT_TICKS_PER_FRAME
) {
281 for(size_t enemy_ix
= 0; enemy_ix
< lv
->header
.enemy_count
; ++enemy_ix
) {
282 jumpnrun_enemy
*enemy
= &lv
->enemies
[enemy_ix
];
283 jumpnrun_process_enemy(enemy
, &fb
, state
, lv
, &tilerange
, &inertia_mod
);
286 badge_framebuffer_blt(&fb
,
287 fixed_point_cast_int(state
->player
.current_box
.pos
.x
) - state
->left
,
288 fixed_point_cast_int(state
->player
.current_box
.pos
.y
),
289 &anim_hacker
[state
->player
.anim_frame
],
290 state
->player
.anim_direction
);
292 badge_framebuffer_flush(&fb
);
294 if(!state
->player
.touching_ground
) {
295 state
->player
.anim_frame
= 2;
296 } else if(fixed_point_gt(fixed_point_abs(state
->player
.inertia
.x
), FIXED_POINT(0, 200))) {
297 state
->player
.anim_frame
= (state
->player
.anim_frame
+ 1) % ARRAY_SIZE(anim_hacker
);
299 state
->player
.anim_frame
= 0;
302 for(size_t shot_ix
= 0; shot_ix
< JUMPNRUN_MAX_SHOTS
; ++shot_ix
) {
303 jumpnrun_shot
*shot
= &state
->shots
[shot_ix
];
305 if(jumpnrun_shot_spawned(shot
)) {
306 rectangle_move_rel(&shot
->current_box
, shot
->inertia
);
307 shot
->inertia
= vec2d_add(shot
->inertia
, gravity
);
309 if(fixed_point_gt(rectangle_top(&shot
->current_box
), FIXED_INT(BADGE_DISPLAY_HEIGHT
))) {
310 jumpnrun_shot_despawn(shot
);
314 if(shot
->tick
== ARRAY_SIZE(anim_sickle
) * JUMPNRUN_SHOT_TICKS_PER_FRAME
) {
320 for(size_t enemy_ix
= 0; enemy_ix
< lv
->header
.enemy_count
; ++enemy_ix
) {
321 jumpnrun_enemy
*enemy
= &lv
->enemies
[enemy_ix
];
322 jumpnrun_process_enemy(enemy
, NULL
, state
, lv
, &tilerange
, &inertia_mod
);
326 state
->player
.inertia
= inertia_mod
;
327 ++state
->player
.tick_minor
;
328 if(state
->player
.tick_minor
== 3) {
329 state
->player
.tick_minor
= 0;
333 uint8_t jumpnrun_play(char const *lvname
) {
336 JUMPNRUN_LEVEL_LOAD(lv
, lvname
);
338 jumpnrun_game_state gs
;
339 memset(&gs
, 0, sizeof(gs
));
341 for(gs
.lives
= 99; gs
.status
!= JUMPNRUN_WON
&& gs
.lives
!= 0; --gs
.lives
) {
342 gs
.status
= JUMPNRUN_PLAYING
;
344 memset(&gs
.player
, 0, sizeof(gs
.player
));
345 gs
.player
.current_box
= rectangle_new(lv
.start_pos
,
348 for(size_t i
= 0; i
< lv
.header
.enemy_count
; ++i
) {
349 lv
.enemies
[i
].flags
= 0;
352 while(gs
.status
== JUMPNRUN_PLAYING
) {
353 badge_event_t ev
= badge_event_wait();
355 switch(badge_event_type(ev
)) {
356 case BADGE_EVENT_USER_INPUT
:
358 uint8_t old_state
= badge_event_old_input_state(ev
);
359 uint8_t new_state
= badge_event_new_input_state(ev
);
360 uint8_t new_buttons
= new_state
& (old_state
^ new_state
);
362 if((new_buttons
& BADGE_EVENT_KEY_BTN_A
) && gs
.player
.touching_ground
) {
363 gs
.player
.jumpable_frames
= 12;
366 if((new_buttons
& BADGE_EVENT_KEY_BTN_B
)) {
368 for(i
= 0; i
< JUMPNRUN_MAX_SHOTS
&& jumpnrun_shot_spawned(&gs
.shots
[i
]); ++i
)
371 if(i
< JUMPNRUN_MAX_SHOTS
) {
372 jumpnrun_shot_spawn(gs
.shots
+ i
, &gs
);
378 case BADGE_EVENT_GAME_TICK
:
380 jumpnrun_level_tick(&lv
, &gs
);