simplified player spawn function

This commit is contained in:
Sara 2023-10-25 12:17:45 +02:00
parent b487b2582e
commit 32e6068f2a

View file

@ -7,16 +7,18 @@
#include "input.h"
#include "physics_world.h"
static Vector directional = ZeroVector;
static Vector _directional = ZeroVector;
static Spritesheet* spr_player_standing = NULL;
static const int _ground_face = 7;
static
void player_input_h(int val) {
directional.x = val * 10.f;
_directional.x = val * 10.f;
}
static
void player_input_v(int val) {
directional.y = -val * 10.f;
_directional.y = -val * 10.f;
}
void player_spawn(Player* self, Vector at) {
@ -32,47 +34,45 @@ void player_start(Player* self) {
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);
self->transform = IdentityTransform;
self->transform.scale = (Vector){4.f, 4.f};
spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
store_asset(self, free);
self->sprite = sprite_from_spritesheet(spr_player_standing, 0);
sprite_set_origin(self->sprite, (Vector){0.25f, 1.f});
self->rigidbody = rigidbody_make(Player_as_Transformable(self));
rigidbody_set_mass(self->rigidbody, 10.f);
float ex_w = 0.1f;
float h = .75f;
float r = 0.05f;
float rr = r;
self->shape = shape_new((Vector[]){
{r-ex_w, 0.f}, {-ex_w, -rr},
{-ex_w, rr-h}, {r-ex_w, -h},
{ex_w-r, -h}, {ex_w, rr-h},
{ex_w, -rr}, {ex_w-r, 0.f},
}, 8);
*self = (Player) {
.transform = {ZeroVector, {4, 4}, 0},
.sprite = sprite_from_spritesheet(spr_player_standing, 0),
.shape = shape_new((Vector[]){
{r-ex_w, 0.f}, {-ex_w, -rr},
{-ex_w, rr-h}, {r-ex_w, -h},
{ex_w-r, -h}, {ex_w, rr-h},
{ex_w, -rr}, {ex_w-r, 0.f},
}, 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});
store_asset(self, free);
}
void player_update(Player* self, float dt) {
Vector velocity = rigidbody_get_velocity(self->rigidbody);
Vector velocity_target = {directional.x, velocity.y};
if(directional.y < 0) {
directional.y = 0;
Vector velocity_target = {_directional.x, velocity.y};
if(_directional.y < 0 && self->is_grounded) {
_directional.y = 0;
velocity.y = -20.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;
}
void player_collision(Player* self, Collision hit) {
if(hit.point.y > -0.01f) {
self->is_grounded = 1;
}
}
Sprite* player_get_sprite(Player* self) {