#include "player.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include "actor.h"
|
|
#include "game.h"
|
|
#include "sdk/assets.h"
|
|
#include "sdk/joypad.h"
|
|
|
|
#define PLAYER_SPEED 2
|
|
#define GRAVITY 0x002e //!< 0.18
|
|
#define PLAYER_INIT_JUMP_VY 0x0399 //!< 3.6
|
|
#define PLAYER_INIT_FALL_VY 0xff1a //!< -1.1015625
|
|
#define MAX_VY 0xf900 //!< -7.0
|
|
|
|
#define SWITCH_ANIM_STATE(state) \
|
|
do { \
|
|
PLAYER_ACTOR->anim = (state); \
|
|
PLAYER_ACTOR->frame_counter = 0; \
|
|
PLAYER_ACTOR->frame_idx = 0; \
|
|
} while (0)
|
|
|
|
player_t PLAYER;
|
|
|
|
ASSET(player_tiles, "player.2bpp");
|
|
ASSET(player_map, "player.map");
|
|
|
|
void player_init(void) {
|
|
PLAYER.state = PLAYER_STATE_STAND;
|
|
memcpy(VRAM_TILE_PTR(TILE_INDEX_PLAYER), &player_tiles[0],
|
|
player_tiles_end - player_tiles);
|
|
}
|
|
|
|
void player_update(void) {
|
|
if ((joypad_pressed & PAD_UP) && PLAYER.state != PLAYER_STATE_JUMP) {
|
|
SWITCH_ANIM_STATE(ANIM_JUMP);
|
|
PLAYER.state = PLAYER_STATE_JUMP;
|
|
PLAYER.vy = PLAYER_INIT_JUMP_VY;
|
|
}
|
|
|
|
if (PLAYER.state == PLAYER_STATE_JUMP) {
|
|
PLAYER.vy -= GRAVITY;
|
|
*((uint16_t*)&PLAYER.y_lo) -= PLAYER.vy;
|
|
|
|
if (player_bg_collides()) {
|
|
if (PLAYER.vy & (1 << 15)) {
|
|
PLAYER.y_hi--;
|
|
while (player_bg_collides()) {
|
|
PLAYER.y_hi--;
|
|
}
|
|
SWITCH_ANIM_STATE(ANIM_STAND);
|
|
PLAYER.state = PLAYER_STATE_STAND;
|
|
} else {
|
|
PLAYER.y_hi++;
|
|
while (player_bg_collides()) {
|
|
PLAYER.y_hi++;
|
|
}
|
|
}
|
|
PLAYER.vy = 0;
|
|
}
|
|
}
|
|
|
|
if ((joypad_state & PAD_RIGHT) && PLAYER.x + TILE_WIDTH * 2 < SCRN_X) {
|
|
PLAYER.dir = DIR_RIGHT;
|
|
PLAYER.x += PLAYER_SPEED;
|
|
if (player_bg_collides()) {
|
|
PLAYER.x -= PLAYER_SPEED;
|
|
}
|
|
} else if ((joypad_state & PAD_LEFT) && PLAYER.x > 0) {
|
|
PLAYER.dir = DIR_LEFT;
|
|
PLAYER.x -= PLAYER_SPEED;
|
|
if (player_bg_collides()) {
|
|
PLAYER.x += PLAYER_SPEED;
|
|
}
|
|
}
|
|
|
|
if (player_in_air() && PLAYER.state != PLAYER_STATE_JUMP) {
|
|
PLAYER.state = PLAYER_STATE_JUMP;
|
|
PLAYER.vy = PLAYER_INIT_FALL_VY;
|
|
}
|
|
|
|
PLAYER_ACTOR->flip = PLAYER.dir == DIR_LEFT;
|
|
PLAYER_ACTOR->x = PLAYER.x;
|
|
PLAYER_ACTOR->y = PLAYER.y_hi;
|
|
}
|