From 19e61744cedcddff4b0e7e1b36b095a3615a15f3 Mon Sep 17 00:00:00 2001 From: En Yi Date: Wed, 1 Jan 2020 17:44:23 +0800 Subject: [PATCH] Add squishyness --- include/header.h | 5 ++- main.c | 12 ++---- obj/squishy.c | 97 ++++++++++++++++++++++++++++++++++++++---------- utilities/math.c | 4 ++ 4 files changed, 89 insertions(+), 29 deletions(-) diff --git a/include/header.h b/include/header.h index 27f3db5..a1a9dfd 100644 --- a/include/header.h +++ b/include/header.h @@ -44,7 +44,7 @@ struct player_obj struct squishy_square { - Rectangle *rect; + struct kinematic_obj *parent; Color color; Vector2 topleft; Vector2 topright; @@ -70,6 +70,7 @@ bool place_meeting(struct kinematic_obj *obj, Vector2 dir); long mag(Vector2 vec); int sign(float val); Vector2 dir(Vector2 vec); +void approach(double *val, double target, float f); // Linked list, linked_list.c extern struct kinematic_obj_node *kinematic_HEAD; @@ -79,7 +80,7 @@ void add_node(struct kinematic_obj *obj); void free_list(void); // Squishy Square functions, squishy.c -struct squishy_square init_squishy_square(Rectangle *rect, Color color); +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); diff --git a/main.c b/main.c index 70030a8..3025ace 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,5 @@ /******************************************************************************************* * -* raylib [core] example - Basic 3d example -* * Welcome to raylib! * * To compile example, just press F5. @@ -49,14 +47,12 @@ int main() struct kinematic_obj tile = init_kinematic_obj(900, 100); struct kinematic_obj tile2 = init_kinematic_obj(100, 40); struct kinematic_obj tile3 = init_kinematic_obj(100, 40); - set_position(&player.kinematic, 400, 300); + set_position(&player.kinematic, 400, 100); set_position(&tile, -50, 380); - set_position(&tile2, 350, 280); - set_position(&tile3, 250, 167); - struct squishy_square sqr = init_squishy_square(&player.kinematic.rect, RED); + set_position(&tile2, 350, 330); + set_position(&tile3, 250, 270); + struct squishy_square sqr = init_squishy_square(&player.kinematic, RED); - // TODO: get a linked list implementation - //create_list(); add_node(&tile); add_node(&tile2); add_node(&tile3); diff --git a/obj/squishy.c b/obj/squishy.c index dc6aba6..c97a37b 100644 --- a/obj/squishy.c +++ b/obj/squishy.c @@ -1,10 +1,14 @@ #include "header.h" -void three_point_beizerfan(Vector2 start, Vector2 mid, Vector2 end, Vector2* arr); +#define INTERP_FACTOR 0.5 +#define OFFSET_VALUE 20 -struct squishy_square init_squishy_square(Rectangle *rect, Color color){ +void three_point_beizerfan(Vector2 start, Vector2 mid, Vector2 end, Vector2* arr); +void calc_offsets(struct squishy_square *square); + +struct squishy_square init_squishy_square(struct kinematic_obj *parent, Color color){ struct squishy_square sqr = { - .rect = rect, + .parent = parent, .color = color, .topleft = (Vector2){0,0}, .topright= (Vector2){0,0}, @@ -23,23 +27,78 @@ struct squishy_square init_squishy_square(Rectangle *rect, Color color){ } void update_squishy(struct squishy_square *square){ - square->topleft.x = square->rect->x; - square->topleft.y = square->rect->y; - square->topright.x = square->rect->x + square->rect->width; - square->topright.y = square->rect->y; - square->bottomleft.x = square->rect->x; - square->bottomleft.y = square->rect->y + square->rect->height; - square->bottomright.x = square->rect->x + square->rect->width; - square->bottomright.y = square->rect->y + square->rect->height; - square->top_handle.x = square->rect->x + square->rect->width / 2; - square->top_handle.y = square->rect->y + square->top_offset; - square->bottom_handle.x = square->rect->x + square->rect->width / 2; - square->bottom_handle.y = square->rect->y + square->rect->height - square->bottom_offset; - square->left_handle.x = square->rect->x + square->left_offset; - square->left_handle.y = square->rect->y + square->rect->height / 2; - square->right_handle.x = square->rect->x + square->rect->width - square->right_offset; - square->right_handle.y = square->rect->y+ square->rect->height / 2; + calc_offsets(square); + + // Update to follow the player + square->topleft.x = square->parent->rect.x; + square->topleft.y = square->parent->rect.y; + square->topright.x = square->parent->rect.x + square->parent->rect.width; + square->topright.y = square->parent->rect.y; + square->bottomleft.x = square->parent->rect.x; + square->bottomleft.y = square->parent->rect.y + square->parent->rect.height; + square->bottomright.x = square->parent->rect.x + square->parent->rect.width; + square->bottomright.y = square->parent->rect.y + square->parent->rect.height; + + square->top_handle.x = square->parent->rect.x + square->parent->rect.width / 2; + square->top_handle.y = square->parent->rect.y + square->top_offset; + square->bottom_handle.x = square->parent->rect.x + square->parent->rect.width / 2; + square->bottom_handle.y = square->parent->rect.y + square->parent->rect.height - square->bottom_offset; + square->left_handle.x = square->parent->rect.x + square->left_offset; + square->left_handle.y = square->parent->rect.y + square->parent->rect.height / 2; + square->right_handle.x = square->parent->rect.x + square->parent->rect.width - square->right_offset; + square->right_handle.y = square->parent->rect.y + square->parent->rect.height / 2; + +} + +void calc_offsets(struct squishy_square *square){ + // TODO: Normalise the offsets + double left_target_offset = 0; + double right_target_offset = 0; + double top_target_offset = 0; + double bottom_target_offset = 0; + + if (IsKeyDown(KEY_A)){ + left_target_offset = OFFSET_VALUE; + if (place_meeting(square->parent, (Vector2){1, 0})){ + top_target_offset = -OFFSET_VALUE / 2; + bottom_target_offset = -OFFSET_VALUE / 2; + }else{ + right_target_offset = -OFFSET_VALUE * 0.8; + } + } + if (IsKeyDown(KEY_D)){ + right_target_offset = OFFSET_VALUE; + if (place_meeting(square->parent, (Vector2){-1, 0})){ + top_target_offset = -OFFSET_VALUE / 2; + bottom_target_offset = -OFFSET_VALUE / 2; + }else{ + left_target_offset = -OFFSET_VALUE * 0.8; + } + } + if (IsKeyDown(KEY_W)){ + top_target_offset = OFFSET_VALUE; + if (place_meeting(square->parent, (Vector2){0, 1})){ + right_target_offset = -OFFSET_VALUE / 2; + left_target_offset = -OFFSET_VALUE / 2; + }else{ + bottom_target_offset = -OFFSET_VALUE * 0.8; + } + } + if (IsKeyDown(KEY_S)){ + bottom_target_offset = OFFSET_VALUE; + if (place_meeting(square->parent, (Vector2){0, -1})){ + right_target_offset = -OFFSET_VALUE / 2; + left_target_offset = -OFFSET_VALUE / 2; + }else{ + top_target_offset = -OFFSET_VALUE * 0.8; + } + } + + approach(&square->left_offset, left_target_offset, INTERP_FACTOR); + approach(&square->right_offset, right_target_offset, INTERP_FACTOR); + approach(&square->top_offset, top_target_offset, INTERP_FACTOR); + approach(&square->bottom_offset, bottom_target_offset, INTERP_FACTOR); } void draw_squishy(struct squishy_square *square){ diff --git a/utilities/math.c b/utilities/math.c index cf49076..1361e58 100644 --- a/utilities/math.c +++ b/utilities/math.c @@ -12,4 +12,8 @@ Vector2 dir(Vector2 vec){ int sign(float val){ return (val > 0)?1:-1; +} + +void approach(double *val, double target, float f){ + *val += (target - *val) * f; } \ No newline at end of file