diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..6c87fc4 --- /dev/null +++ b/src/player.c @@ -0,0 +1,41 @@ +#include "player.h" +#include "layers.h" +#include "ui.h" +#include "world.h" +#include "input.h" +#include "engine.h" +#include + +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; +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..52c65a7 --- /dev/null +++ b/src/player.h @@ -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 */