Add squishyness in vertical movement

master
En Yi 2020-01-19 15:12:56 +08:00
parent 7474d2ebbe
commit a93ac9deaa
5 changed files with 64 additions and 54 deletions

View File

@ -45,6 +45,7 @@ struct player_obj
{
struct kinematic_obj kinematic;
enum PLAYER_STATE state;
struct squishy_square *image;
};
extern unsigned int PLAYER_SIZE;
@ -60,6 +61,8 @@ struct squishy_square
double left_offset;
double right_offset;
double target_offsets[4];
Vector2 top_vertices[BEZIER_POINTS+1];
Vector2 bottom_vertices[BEZIER_POINTS+1];
Vector2 left_vertices[BEZIER_POINTS+1];
@ -90,6 +93,7 @@ void free_list(void);
struct squishy_square init_squishy_square(struct kinematic_obj *parent, Color color);
void update_squishy(struct squishy_square *square);
void draw_squishy(struct squishy_square *square);
void set_squish_target_offset(struct squishy_square *square, unsigned int dir, int val);
//Player stuff, player.c
struct player_obj init_player_obj();

1
main.c
View File

@ -52,6 +52,7 @@ int main()
set_position(&tile2, 350, 330);
set_position(&tile3, 250, 270);
struct squishy_square sqr = init_squishy_square(&player.kinematic, RED);
player.image = &sqr;
add_node(&tile);
add_node(&tile2);

View File

@ -28,10 +28,11 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
Rectangle collide_rect;
struct kinematic_obj_node *current;
//Simplistic Collision Handling for AABB, Could add coeff of restitution?
// TODO: Dont reset velocity if clipping vector is not the same dir as the movement dir
obj->velocity.x += acceleration.x * delta;
obj->pos.x += obj->velocity.x * delta;
obj->rect.x = obj->pos.x + obj->dim_reduction[0];
obj->rect.width = obj->ori_width - obj->dim_reduction[0] - obj->dim_reduction[1];
obj->rect.width = obj->ori_width - obj->dim_reduction[0] - obj->dim_reduction[2];
current = kinematic_HEAD;
while(current != NULL){
@ -65,8 +66,8 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
// Repeat for y
obj->velocity.y += acceleration.y * delta;
obj->pos.y += obj->velocity.y * delta;
obj->rect.y = obj->pos.y + obj->dim_reduction[2];
obj->rect.height = obj->ori_height - obj->dim_reduction[2] - obj->dim_reduction[3];
obj->rect.y = obj->pos.y + obj->dim_reduction[1];
obj->rect.height = obj->ori_height - obj->dim_reduction[1] - obj->dim_reduction[3];
current = kinematic_HEAD;
while(current != NULL){

View File

@ -20,7 +20,7 @@ const unsigned int run_start_frames = 10;
const unsigned int jump_squat_frames = 4;
const unsigned int land_lag_frames = 6;
extern unsigned int PLAYER_SIZE = 40;
unsigned int PLAYER_SIZE = 40;
// The player FSM
void player_input_check(struct player_obj *player){
@ -80,7 +80,9 @@ void player_input_check(struct player_obj *player){
case TURN_AROUND:
break;
case JUMP_SQUAT:
case JUMP_SQUAT:
player->kinematic.set_dim_reduction[1] = 10;
set_squish_target_offset(player->image, 1, 20);
if(frame_counter<jump_squat_frames){
++frame_counter;
if (short_hop != true && !IsKeyDown(JUMP)){
@ -90,28 +92,39 @@ void player_input_check(struct player_obj *player){
else{
frame_counter = 0;
if (short_hop == true)
player->kinematic.velocity.y = -JUMP_SPD/2;
player->kinematic.velocity.y = -JUMP_SPD * 0.6;
else
player->kinematic.velocity.y = -JUMP_SPD;
player->state = JUMPING;
on_ground = false;
short_hop = false;
player->kinematic.set_dim_reduction[1] = 0;
player->kinematic.set_dim_reduction[3] = 10;
}
break;
case JUMPING:
set_squish_target_offset(player->image, 3, 15);
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->kinematic.set_dim_reduction[3] = 0;
}
break;
case FALLING:
//player->kinematic.set_dim_reduction[3] = -player->kinematic.velocity.y/20;
set_squish_target_offset(player->image, 1, 15);
accel.x = AIR_ACCEL*(IsKeyDown(KEY_RIGHT)-IsKeyDown(KEY_LEFT));
if (place_meeting(&player->kinematic, (Vector2){0,1})){
player->state = LANDING;
player->kinematic.dim_reduction[3] = 0;
player->kinematic.set_dim_reduction[3] = 0;
player->kinematic.dim_reduction[1] = 40;
on_ground = true;
state_buffer = IDLE;
}
break;
case LANDING:
set_squish_target_offset(player->image, 1, 0);
if(frame_counter<land_lag_frames){
++frame_counter;
if (IsKeyDown(JUMP))
@ -141,28 +154,11 @@ void player_input_check(struct player_obj *player){
break;
}
// TODO: Add a key to resize the rect and see what happens?
Vector2 offset = (Vector2){0,0};
player->kinematic.set_dim_reduction[0] = 0;
if (IsKeyDown(KEY_J))
player->kinematic.set_dim_reduction[0] = 10;
player->kinematic.set_dim_reduction[1] = 0;
if (IsKeyDown(KEY_L))
player->kinematic.set_dim_reduction[1] = 10;
player->kinematic.set_dim_reduction[2] = 0;
if (IsKeyDown(KEY_I))
player->kinematic.set_dim_reduction[2] = 10;
player->kinematic.set_dim_reduction[3] = 0;
if (IsKeyDown(KEY_K))
player->kinematic.set_dim_reduction[3] = 10;
approach(&player->kinematic.dim_reduction[0], player->kinematic.set_dim_reduction[0], 0.5);
approach(&player->kinematic.dim_reduction[1], player->kinematic.set_dim_reduction[1], 0.5);
approach(&player->kinematic.dim_reduction[2], player->kinematic.set_dim_reduction[2], 0.5);
approach(&player->kinematic.dim_reduction[3], player->kinematic.set_dim_reduction[3], 0.5);
// Set the hitbox reductions
approach(&player->kinematic.dim_reduction[0], player->kinematic.set_dim_reduction[0], 0.2);
approach(&player->kinematic.dim_reduction[1], player->kinematic.set_dim_reduction[1], 0.2);
approach(&player->kinematic.dim_reduction[2], player->kinematic.set_dim_reduction[2], 0.2);
approach(&player->kinematic.dim_reduction[3], player->kinematic.set_dim_reduction[3], 0.2);
//scale_rect(&player->kinematic);

View File

@ -46,28 +46,36 @@ void update_squishy(struct squishy_square *square){
}
void calc_offsets(struct squishy_square *square){
void set_squish_target_offset(struct squishy_square *square, unsigned int dir, int val){
/* 0 - left, 1 - top, 2 - right, 3 - bottom
*/
square->target_offsets[0] = 0;
square->target_offsets[1] = 0;
square->target_offsets[2] = 0;
square->target_offsets[3] = 0;
switch(dir){
case 0:
square->target_offsets[0] += val;
square->target_offsets[2] += -val * 0.5;
break;
case 1:
square->target_offsets[1] += val;
square->target_offsets[3] += -val * 0.5;
break;
case 2:
square->target_offsets[2] += val;
square->target_offsets[0] += -val * 0.5;
break;
case 3:
square->target_offsets[3] += val;
square->target_offsets[1] += -val * 0.5;
break;
}
}
void calc_offsets(struct squishy_square *square){
// TODO: Normalise the offsets
double target_offsets[4] = {0,0,0,0};
if (IsKeyDown(KEY_A)){
target_offsets[0] += OFFSET_VALUE;
target_offsets[2] += -OFFSET_VALUE * 0.5;
}
if (IsKeyDown(KEY_D)){
target_offsets[2] += OFFSET_VALUE;
target_offsets[0] += -OFFSET_VALUE * 0.5;
}
if (IsKeyDown(KEY_W)){
target_offsets[1] += OFFSET_VALUE;
target_offsets[3] += -OFFSET_VALUE * 0.5;
}
if (IsKeyDown(KEY_S)){
target_offsets[3] += OFFSET_VALUE;
target_offsets[1] += -OFFSET_VALUE * 0.5;
}
bool contacts[4];
int n_contacts = 0;
contacts[0] = place_meeting(square->parent, (Vector2){-1, 0});
@ -77,7 +85,7 @@ void calc_offsets(struct squishy_square *square){
// Redistribute the offset on contact
for (int i=0; i < 4; ++i){
if (contacts[i] == true && target_offsets[i] < 0){
if (contacts[i] == true && square->target_offsets[i] < 0){
unsigned int n = 0;
unsigned int j;
unsigned int ind;
@ -90,16 +98,16 @@ void calc_offsets(struct squishy_square *square){
for (j=0; j < 3; ++j){
ind = (i+1+j) % 4;
if (contacts[ind] == false)
target_offsets[ind] += target_offsets[i] / n;
square->target_offsets[ind] += square->target_offsets[i] / n;
}
}
target_offsets[i] = 0;
square->target_offsets[i] = 0;
}
}
approach(&square->left_offset, target_offsets[0], INTERP_FACTOR);
approach(&square->top_offset, target_offsets[1], INTERP_FACTOR);
approach(&square->right_offset, target_offsets[2], INTERP_FACTOR);
approach(&square->bottom_offset, target_offsets[3], INTERP_FACTOR);
approach(&square->left_offset, square->target_offsets[0], INTERP_FACTOR);
approach(&square->top_offset, square->target_offsets[1], INTERP_FACTOR);
approach(&square->right_offset, square->target_offsets[2], INTERP_FACTOR);
approach(&square->bottom_offset, square->target_offsets[3], INTERP_FACTOR);
}
void draw_squishy(struct squishy_square *square){