95 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "player.h"
 | |
| #include "assets.h"
 | |
| #include "debug.h"
 | |
| #include "physics_entity.h"
 | |
| #include "program.h"
 | |
| #include "rigidbody.h"
 | |
| #include "input.h"
 | |
| #include "physics_world.h"
 | |
| 
 | |
| static Vector _directional = ZeroVector;
 | |
| static Spritesheet* spr_player_standing = NULL;
 | |
| 
 | |
| static
 | |
| void player_input_h(int val) {
 | |
|     _directional.x = val * 10.f;
 | |
| }
 | |
| static
 | |
| void player_input_v(int val) {
 | |
|     _directional.y = -val * 10.f;
 | |
| }
 | |
| 
 | |
| void player_spawn(Player* self, Vector at) {
 | |
|     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_S, SDL_SCANCODE_W, &player_input_v);
 | |
| 
 | |
|     spr_player_standing = spritesheet_load("assets/sprites/player.png", (IVector){128, 128});
 | |
|     float ex_w = 0.1f;
 | |
|     float h = .75f;
 | |
|     float r = 0.05f;
 | |
|     float rr = 0.075f;
 | |
|     *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 = {self->is_grounded ? _directional.x : velocity.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) {
 | |
|     return self->sprite;
 | |
| }
 | |
| 
 | |
| Transform* player_get_transform(Player* self) {
 | |
|     return &self->transform;
 | |
| }
 | |
| 
 | |
| Transform* player_get_rigidbody_transform(Player* self) {
 | |
|     return rigidbody_get_transform(self->rigidbody);
 | |
| }
 | |
| 
 | |
| RigidBody* player_get_rigidbody(Player* self) {
 | |
|     return self->rigidbody;
 | |
| }
 | |
| 
 | |
| Shape* player_get_shape(Player* self) {
 | |
|     return self->shape;
 | |
| }
 | 
