added player to test world

pull/2/head
Sara 2023-05-05 23:20:23 +02:00
parent 465551e981
commit 6afac70411
2 changed files with 59 additions and 0 deletions

41
src/player.c Normal file
View File

@ -0,0 +1,41 @@
#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;
}

18
src/player.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _player_h
#define _player_h
struct object_t;
extern float player_move_x;
extern float player_move_y;
extern float player_xv;
extern float player_yv;
extern struct object_t* player_instance;
void player_update(struct object_t* object);
struct object_t* create_player();
extern void player_axis_horizontal(int axis);
extern void player_axis_vertical(int axis);
#endif /* _player_h */