player now has a constructor and implements drop
This commit is contained in:
parent
674d1b55fb
commit
a6d50decc7
38
src/player.c
38
src/player.c
|
@ -6,6 +6,7 @@
|
||||||
#include "rigidbody.h"
|
#include "rigidbody.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "physics_world.h"
|
#include "physics_world.h"
|
||||||
|
#include "game_world.h"
|
||||||
|
|
||||||
static Vector _directional = ZeroVector;
|
static Vector _directional = ZeroVector;
|
||||||
static Spritesheet* spr_player_standing = NULL;
|
static Spritesheet* spr_player_standing = NULL;
|
||||||
|
@ -19,24 +20,17 @@ void player_input_v(int val) {
|
||||||
_directional.y = -val * 10.f;
|
_directional.y = -val * 10.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_spawn(Player* self, Vector at) {
|
Player* player_new() {
|
||||||
player_start(self);
|
|
||||||
rigidbody_get_transform(self->rigidbody)->position = at;
|
|
||||||
}
|
|
||||||
|
|
||||||
void player_collision_solver(Player* self, List* contacts) {
|
|
||||||
physics_entity_solve_contacts(Player_as_PhysicsEntity(self), contacts);
|
|
||||||
}
|
|
||||||
|
|
||||||
void player_start(Player* self) {
|
|
||||||
input_add_axis_action(SDL_SCANCODE_A, SDL_SCANCODE_D, &player_input_h);
|
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);
|
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});
|
spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
|
||||||
|
|
||||||
float ex_w = 0.1f;
|
float ex_w = 0.1f;
|
||||||
float h = .75f;
|
float h = .75f;
|
||||||
float r = 0.05f;
|
float r = 0.05f;
|
||||||
float rr = 0.075f;
|
float rr = 0.075f;
|
||||||
|
Player* self = malloc(sizeof(Player));
|
||||||
*self = (Player) {
|
*self = (Player) {
|
||||||
.transform = {ZeroVector, {4, 4}, 0},
|
.transform = {ZeroVector, {4, 4}, 0},
|
||||||
.sprite = sprite_from_spritesheet(spr_player_standing, 0),
|
.sprite = sprite_from_spritesheet(spr_player_standing, 0),
|
||||||
|
@ -51,9 +45,20 @@ void player_start(Player* self) {
|
||||||
rigidbody_set_mass(self->rigidbody, 10.f);
|
rigidbody_set_mass(self->rigidbody, 10.f);
|
||||||
physics_world_add_entity(Player_as_PhysicsEntity(self));
|
physics_world_add_entity(Player_as_PhysicsEntity(self));
|
||||||
sprite_set_origin(self->sprite, (Vector){0.25f, 1.f});
|
sprite_set_origin(self->sprite, (Vector){0.25f, 1.f});
|
||||||
store_asset(self, free);
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void player_spawn(Player* self, Vector at) {
|
||||||
|
rigidbody_get_transform(self->rigidbody)->position = at;
|
||||||
|
game_world_add_entity(Player_as_BehaviourEntity(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
void player_collision_solver(Player* self, List* contacts) {
|
||||||
|
physics_entity_solve_contacts(Player_as_PhysicsEntity(self), contacts);
|
||||||
|
}
|
||||||
|
|
||||||
|
void player_start(Player* self) {}
|
||||||
|
|
||||||
void player_update(Player* self, float dt) {
|
void player_update(Player* self, float dt) {
|
||||||
Vector velocity = rigidbody_get_velocity(self->rigidbody);
|
Vector velocity = rigidbody_get_velocity(self->rigidbody);
|
||||||
Vector velocity_target = {self->is_grounded ? _directional.x : velocity.x, velocity.y};
|
Vector velocity_target = {self->is_grounded ? _directional.x : velocity.x, velocity.y};
|
||||||
|
@ -67,6 +72,17 @@ void player_update(Player* self, float dt) {
|
||||||
self->is_grounded = 0;
|
self->is_grounded = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void player_draw(Player* self) {
|
||||||
|
sprite_entity_draw(Player_as_SpriteEntity(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
void player_free(Player* self) {
|
||||||
|
rigidbody_destroy(self->rigidbody);
|
||||||
|
shape_destroy(self->shape);
|
||||||
|
sprite_destroy(self->sprite);
|
||||||
|
free(self);
|
||||||
|
}
|
||||||
|
|
||||||
void player_collision(Player* self, Collision hit) {
|
void player_collision(Player* self, Collision hit) {
|
||||||
if(hit.point.y > -0.01f) {
|
if(hit.point.y > -0.01f) {
|
||||||
self->is_grounded = 1;
|
self->is_grounded = 1;
|
||||||
|
|
11
src/player.h
11
src/player.h
|
@ -1,6 +1,7 @@
|
||||||
#ifndef _fencer_player_h
|
#ifndef _fencer_player_h
|
||||||
#define _fencer_player_h
|
#define _fencer_player_h
|
||||||
|
|
||||||
|
#include "drop.h"
|
||||||
#include "physics_entity.h"
|
#include "physics_entity.h"
|
||||||
#include "behaviour_entity.h"
|
#include "behaviour_entity.h"
|
||||||
#include "transformable.h"
|
#include "transformable.h"
|
||||||
|
@ -17,9 +18,12 @@ typedef struct Player {
|
||||||
int is_grounded;
|
int is_grounded;
|
||||||
} Player;
|
} Player;
|
||||||
|
|
||||||
|
extern Player* player_new();
|
||||||
extern void player_spawn(Player* self, Vector at);
|
extern void player_spawn(Player* self, Vector at);
|
||||||
extern void player_start(Player* self);
|
extern void player_start(Player* self);
|
||||||
extern void player_update(Player* self, float dt);
|
extern void player_update(Player* self, float dt);
|
||||||
|
extern void player_draw(Player* self);
|
||||||
|
extern void player_free(Player* self);
|
||||||
|
|
||||||
extern void player_collision(Player* self, Collision hit);
|
extern void player_collision(Player* self, Collision hit);
|
||||||
|
|
||||||
|
@ -46,10 +50,15 @@ impl_PhysicsEntity_for(Player,
|
||||||
player_collision_solver
|
player_collision_solver
|
||||||
)
|
)
|
||||||
|
|
||||||
|
impl_Drop_for(Player,
|
||||||
|
player_free
|
||||||
|
)
|
||||||
|
|
||||||
impl_BehaviourEntity_for(Player,
|
impl_BehaviourEntity_for(Player,
|
||||||
player_start,
|
player_start,
|
||||||
player_update,
|
player_update,
|
||||||
player_spawn
|
player_spawn,
|
||||||
|
player_draw
|
||||||
)
|
)
|
||||||
|
|
||||||
#endif // !_fencer_player_h
|
#endif // !_fencer_player_h
|
||||||
|
|
Loading…
Reference in a new issue