basic-game-framework/src/player.c

42 lines
1.1 KiB
C
Raw Normal View History

2023-05-05 21:20:23 +00:00
#include "player.h"
#include "layers.h"
#include "ui.h"
#include "world.h"
#include "input.h"
#include "engine.h"
#include <SDL2/SDL_scancode.h>
float player_move_x = 0;
float player_move_y = 0;
float player_xv = 0;
float player_yv = 0;
object_t* player_instance = NULL;
void player_update(object_t *object) {
float m = sqrtf(player_move_x*player_move_x + player_move_y*player_move_y);
if(m == FP_NAN) m = 1;
player_xv = player_move_x * 10.f / m,
player_yv = player_move_y * 10.f / m;
object->sprite.x += player_xv* delta_time();
object->sprite.y += player_yv* delta_time();
}
object_t* create_player() {
player_instance = make_object();
player_instance->evt_update = &player_update;
player_instance->sprite = make_sprite("player.png", 0, 0);
add_key_listener(SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, &player_axis_horizontal);
add_key_listener(SDL_SCANCODE_DOWN, SDL_SCANCODE_UP, &player_axis_vertical);
return player_instance;
}
void player_axis_horizontal(int axis) {
player_move_x = axis;
}
void player_axis_vertical(int axis) {
player_move_y = -axis;
}