implemented new input system
This commit is contained in:
parent
43527cd675
commit
86ba2b8c29
17 changed files with 591 additions and 47 deletions
64
src/player.c
64
src/player.c
|
|
@ -1,24 +1,65 @@
|
|||
#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
|
||||
void player_input_h(Player* self, int val) {
|
||||
self->directional.x = val * 10.f;
|
||||
void player_input_h(Player* self, InputEvent val) {
|
||||
self->directional.x = val.as_float * 5.f;
|
||||
}
|
||||
|
||||
static
|
||||
void player_input_v(Player* self, int val) {
|
||||
self->directional.y = -val * 10.f;
|
||||
void player_input_jump(Player* self, InputEvent down) {
|
||||
if(down.as_bool && self->is_grounded) {
|
||||
Vector velocity = rigidbody_get_velocity(self->rigidbody);
|
||||
self->directional.y = 0;
|
||||
velocity.y = -20.f;
|
||||
rigidbody_set_velocity(self->rigidbody, velocity);
|
||||
}
|
||||
}
|
||||
|
||||
static inline
|
||||
void _internal_player_init_input(Player* self) {
|
||||
// default to keyboard if no controllers are available
|
||||
if(input_get_device_by_id(0) != NULL)
|
||||
self->player_input = playerinput_new(self, 0);
|
||||
else
|
||||
self->player_input = playerinput_new(self, -1);
|
||||
|
||||
// KEYBOARD ------------------------------------------------
|
||||
// WALK
|
||||
playerinput_add(self->player_input, CompositeAxis1D_as_InputAxis(compositeaxis1d_new(
|
||||
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_A)),
|
||||
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_D)),
|
||||
InputEvent_Float
|
||||
)), (InputDelegateFn)player_input_h);
|
||||
// JUMP
|
||||
playerinput_add(self->player_input,
|
||||
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_W)),
|
||||
(InputDelegateFn)player_input_jump
|
||||
);
|
||||
|
||||
// CONTROLLER ------------------------------------------------
|
||||
// WALK
|
||||
playerinput_add(self->player_input,
|
||||
ControllerAxis_as_InputAxis(controlleraxis_new(0)),
|
||||
(InputDelegateFn)player_input_h
|
||||
);
|
||||
// JUMP
|
||||
playerinput_add(self->player_input,
|
||||
ControllerButton_as_InputAxis(controllerbutton_new(SDL_CONTROLLER_BUTTON_A)),
|
||||
(InputDelegateFn)player_input_jump
|
||||
);
|
||||
}
|
||||
|
||||
Player* player_new() {
|
||||
|
||||
Spritesheet* spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
|
||||
|
||||
float ex_w = 0.1f;
|
||||
|
|
@ -36,12 +77,14 @@ Player* player_new() {
|
|||
{ex_w-r, -h}, {ex_w, rr-h},
|
||||
}, 8)
|
||||
};
|
||||
|
||||
self->rigidbody = rigidbody_make(Player_as_Transformable(self));
|
||||
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});
|
||||
input_add_axis_action(self, InputDelegate(player_input_h), SDL_SCANCODE_A, SDL_SCANCODE_D);
|
||||
input_add_axis_action(self, InputDelegate(player_input_v), SDL_SCANCODE_S, SDL_SCANCODE_W);
|
||||
|
||||
_internal_player_init_input(self);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
|
@ -59,11 +102,6 @@ void player_start(Player* self) {}
|
|||
void player_update(Player* self, float dt) {
|
||||
Vector velocity = rigidbody_get_velocity(self->rigidbody);
|
||||
Vector velocity_target = {self->is_grounded ? self->directional.x : velocity.x, velocity.y};
|
||||
if(self->directional.y < 0 && self->is_grounded) {
|
||||
self->directional.y = 0;
|
||||
velocity.y = -30.f;
|
||||
rigidbody_set_velocity(self->rigidbody, velocity);
|
||||
}
|
||||
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;
|
||||
|
|
@ -77,7 +115,7 @@ void player_free(Player* self) {
|
|||
rigidbody_destroy(self->rigidbody);
|
||||
shape_destroy(self->shape);
|
||||
sprite_destroy(self->sprite);
|
||||
input_remove_actions(self);
|
||||
playerinput_drop(self->player_input);
|
||||
free(self);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue