Adjust player physics

master
En Yi 2019-11-30 18:33:13 +08:00
parent 6540e27b0e
commit c48194ae12
2 changed files with 57 additions and 24 deletions

7
main.c
View File

@ -47,13 +47,16 @@ int main()
}; };
struct kinematic_obj tile = init_kinematic_obj(900, 100); struct kinematic_obj tile = init_kinematic_obj(900, 100);
set_position(&player, 400, 300); struct kinematic_obj tile2 = init_kinematic_obj(100, 40);
set_position(&player.kinematic, 400, 300);
set_position(&tile, -50, 380); set_position(&tile, -50, 380);
set_position(&tile2, 350, 280);
struct squishy_square sqr = init_squishy_square(&player.kinematic.rect, RED); struct squishy_square sqr = init_squishy_square(&player.kinematic.rect, RED);
// TODO: get a linked list implementation // TODO: get a linked list implementation
struct obj_node tile_node = {.obj=&tile, .next=NULL}; struct obj_node tile_node = {.obj=&tile, .next=NULL};
struct obj_node player_node = {.obj=&player.kinematic, .next=&tile_node}; struct obj_node tile2_node = {.obj=&tile2, .next=&tile_node};
struct obj_node player_node = {.obj=&player.kinematic, .next=&tile2_node};
HEAD = &player_node; HEAD = &player_node;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second

View File

@ -1,19 +1,24 @@
#include "header.h" #include "header.h"
#define PLAYER_ACCEL 1500 #define PLAYER_ACCEL 1600
#define RUN_INIT_SPD 200 #define AIR_ACCEL 400
#define RUN_INIT_SPD 250
#define JUMP_SPD 500 #define JUMP_SPD 500
#define GRAV 1000 #define GRAV 1000
static bool allow_move = true; static bool allow_move = true;
static int jumps = 1;
static frame_counter = 0;
static int run_dir = 1;
static bool allow_friction = true; static bool allow_friction = true;
static bool on_ground = true;
static bool short_hop = false;
static unsigned int jumps = 1;
static unsigned int frame_counter = 0;
static int run_dir = 1;
static enum PLAYER_STATE state_buffer = IDLE;
const unsigned int run_start_frames = 8; const unsigned int run_start_frames = 8;
const unsigned int jump_squat_frames = 6; const unsigned int jump_squat_frames = 5;
const unsigned int land_lag_frames = 5; const unsigned int land_lag_frames = 4;
// The player FSM // The player FSM
void player_input_check(struct player_obj *player){ void player_input_check(struct player_obj *player){
@ -71,39 +76,53 @@ void player_input_check(struct player_obj *player){
break; break;
case JUMP_SQUAT: case JUMP_SQUAT:
if(frame_counter<jump_squat_frames) if(frame_counter<jump_squat_frames){
++frame_counter; ++frame_counter;
if (short_hop != true && !IsKeyDown(JUMP)){
short_hop = true;
}
}
else{ else{
frame_counter = 0; frame_counter = 0;
if (IsKeyDown(JUMP)) if (short_hop == true)
player->kinematic.velocity.y = -JUMP_SPD;
else
player->kinematic.velocity.y = -JUMP_SPD/2; player->kinematic.velocity.y = -JUMP_SPD/2;
else
player->kinematic.velocity.y = -JUMP_SPD;
player->state = JUMPING; player->state = JUMPING;
on_ground = false;
short_hop = false;
} }
break; break;
case JUMPING: case JUMPING:
accel.x = PLAYER_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT)); accel.x = AIR_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT));
if (player->kinematic.velocity.y > 0) if (player->kinematic.velocity.y >= 0)
player->state = FALLING; player->state = FALLING;
break; break;
case FALLING: case FALLING:
accel.x = PLAYER_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT)); accel.x = AIR_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT));
if (place_meeting(&player->kinematic, (Vector2){0,1})){ if (place_meeting(&player->kinematic, (Vector2){0,1})){
player->state = LANDING; player->state = LANDING;
on_ground = true;
state_buffer = IDLE;
} }
break; break;
case LANDING: case LANDING:
if(frame_counter<land_lag_frames) if(frame_counter<land_lag_frames){
++frame_counter; ++frame_counter;
if (IsKeyDown(JUMP))
state_buffer = JUMP_SQUAT;
}
else{ else{
jumps = 1; jumps = 1;
frame_counter = 0; frame_counter = 0;
if (IsKeyDown(LEFT) || IsKeyDown(RIGHT)){ if (state_buffer == JUMP_SQUAT){
player->state = RUNNING; player->state = state_buffer;
}else{ --jumps;
player->state = IDLE;
} }
else if (IsKeyDown(LEFT) || IsKeyDown(RIGHT))
player->state = RUNNING;
else
player->state = IDLE;
} }
break; break;
case DASH_START: case DASH_START:
@ -120,13 +139,24 @@ void player_input_check(struct player_obj *player){
if (IsKeyPressed(JUMP) && jumps > 0){ if (IsKeyPressed(JUMP) && jumps > 0){
player->state = JUMP_SQUAT; player->state = JUMP_SQUAT;
allow_friction = true; allow_friction = true;
short_hop = false;
--jumps; --jumps;
} }
if (allow_friction == true) if (on_ground == true && !place_meeting(&player->kinematic, (Vector2){0,1})){
accel.x -= player->kinematic.velocity.x * 8; jumps = 0;
on_ground = false;
allow_friction = true;
player->state = FALLING;
}
if (!place_meeting(&player, (Vector2){0,1})){ if (allow_friction == true)
if (on_ground)
accel.x -= player->kinematic.velocity.x * 7.0;
else
accel.x -= player->kinematic.velocity.x * 1.0;
if (!place_meeting(&player->kinematic, (Vector2){0,1})){
accel.y = GRAV; accel.y = GRAV;
} }