player now animates
This commit is contained in:
parent
f162a29b3c
commit
c86904dd45
17
src/player.c
17
src/player.c
|
@ -1,18 +1,22 @@
|
|||
#include "player.h"
|
||||
#include "assets.h"
|
||||
#include "debug.h"
|
||||
#include "input_axis.h"
|
||||
#include "physics_entity.h"
|
||||
#include "program.h"
|
||||
#include "rigidbody.h"
|
||||
#include "input.h"
|
||||
#include "physics_world.h"
|
||||
#include "game_world.h"
|
||||
#include <SDL2/SDL_gamecontroller.h>
|
||||
|
||||
static const float _anim_speed = 1.0;
|
||||
|
||||
static
|
||||
void player_input_h(Player* self, InputEvent val) {
|
||||
self->directional.x = val.as_float * 5.f;
|
||||
if(val.as_float > 0.0) {
|
||||
sprite_flip_horizontal(self->sprite, 0);
|
||||
} else if(val.as_float < 0.0) {
|
||||
sprite_flip_horizontal(self->sprite, 1);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
|
@ -79,6 +83,7 @@ Player* player_new() {
|
|||
rigidbody_set_mass(self->rigidbody, 10.f);
|
||||
physics_world_add_entity(Player_as_PhysicsEntity(self));
|
||||
sprite_set_origin(self->sprite, (Vector){0.25f, 1.f});
|
||||
rigidbody_set_bounce(self->rigidbody, 0.0);
|
||||
|
||||
_internal_player_init_input(self);
|
||||
|
||||
|
@ -102,6 +107,12 @@ void player_update(Player* self, float dt) {
|
|||
rigidbody_accelerate(self->rigidbody, vmulff(vsubf(velocity_target, velocity), 50.f), 0);
|
||||
rigidbody_accelerate(self->rigidbody, (Vector){0.0f, 100.f}, 0);
|
||||
self->is_grounded = 0;
|
||||
|
||||
self->frame_timer -= dt;
|
||||
if(self->frame_timer <= 0.0) {
|
||||
sprite_set_tile(self->sprite, sprite_get_tile(self->sprite) + 1);
|
||||
self->frame_timer = _anim_speed;
|
||||
}
|
||||
}
|
||||
|
||||
void player_draw(Player* self) {
|
||||
|
|
|
@ -20,6 +20,8 @@ typedef struct Player {
|
|||
Sprite* sprite;
|
||||
int is_grounded;
|
||||
PlayerInput* player_input;
|
||||
|
||||
float frame_timer;
|
||||
} Player;
|
||||
|
||||
extern Player* player_new();
|
||||
|
|
Loading…
Reference in a new issue