momento/src/player/init.c

58 lines
1.3 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "level.h"
#include "particles.h"
#include "player.h"
#include "tiles.h"
#include <libimg.h>
extern struct Level level;
extern img_t img_player_blink;
extern img_t img_player_idle;
extern img_t img_player_jump;
extern img_t img_player_walk;
struct Player
player_init(void)
{
int x = 0;
int y = 0;
struct Player player = {
.spd_x = 0.0,
.spd_y = 0.0,
.rem_x = 0.0,
.rem_y = 0.0,
.jump_buffer = 0,
.jump_grace = 0,
.jumps_left = 0,
.air_state = AirNeutral,
};
/* initialize animations */
player.blink_anim = particle_init(&img_player_blink, 0, 0, 10, 4, 0, 0);
player.idle_anim = particle_init(&img_player_idle, 0, 0, 10, 1, 1, 1);
player.jump_anim = particle_init(&img_player_jump, 0, 0, 8, 6, 0, 0);
player.walk_anim = particle_init(&img_player_walk, 0, 0, 12, 6, 1, 0);
player.anim = player.idle_anim;
/* find spawn position in level */
x = level.width;
while (x-- > 0) {
y = level.height;
while (y-- > 0) {
if (level.data[x + y * level.width] == TILE_START) {
/* set player position */
player.x = x * TILE_WIDTH +
(TILE_WIDTH - PLAYER_WIDTH) / 2;
player.y = y * TILE_HEIGHT + TILE_HEIGHT -
PLAYER_HEIGHT;
return player;
}
}
}
return player;
}