parent
ce8cf1a0ea
commit
dfcc030f4d
41
src/player.c
41
src/player.c
|
@ -1,41 +0,0 @@
|
|||
#include "player.h"
|
||||
#include "layers.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;
|
||||
m = 1.f/m;
|
||||
player_xv = player_move_x * 3.f * m,
|
||||
player_yv = player_move_y * 3.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_A, SDL_SCANCODE_D, &player_axis_horizontal);
|
||||
add_key_listener(SDL_SCANCODE_S, SDL_SCANCODE_W, &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;
|
||||
}
|
16
src/player.h
16
src/player.h
|
@ -1,16 +0,0 @@
|
|||
#ifndef _player_h
|
||||
#define _player_h
|
||||
|
||||
struct object_t;
|
||||
|
||||
extern float player_move_x, player_move_y,
|
||||
player_xv, 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 */
|
Loading…
Reference in New Issue