input listeners now take object arguments

This commit is contained in:
Sara 2023-11-02 21:00:02 +01:00
parent c4438c86de
commit 78e4558786
5 changed files with 48 additions and 80 deletions

View file

@ -8,23 +8,18 @@
#include "physics_world.h"
#include "game_world.h"
static Vector _directional = ZeroVector;
static Spritesheet* spr_player_standing = NULL;
static
void player_input_h(int val) {
_directional.x = val * 10.f;
void player_input_h(Player* self, int val) {
self->directional.x = val * 10.f;
}
static
void player_input_v(int val) {
_directional.y = -val * 10.f;
void player_input_v(Player* self, int val) {
self->directional.y = -val * 10.f;
}
Player* player_new() {
input_add_axis_action(SDL_SCANCODE_A, SDL_SCANCODE_D, &player_input_h);
input_add_axis_action(SDL_SCANCODE_S, SDL_SCANCODE_W, &player_input_v);
spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
Spritesheet* spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
float ex_w = 0.1f;
float h = .75f;
@ -45,6 +40,8 @@ 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});
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);
return self;
}
@ -61,9 +58,9 @@ 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 ? _directional.x : velocity.x, velocity.y};
if(_directional.y < 0 && self->is_grounded) {
_directional.y = 0;
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);
}
@ -80,6 +77,7 @@ void player_free(Player* self) {
rigidbody_destroy(self->rigidbody);
shape_destroy(self->shape);
sprite_destroy(self->sprite);
input_remove_actions(self);
free(self);
}