Add squishyness

master
En Yi 2020-01-01 17:44:23 +08:00
parent c30e3efcae
commit 19e61744ce
4 changed files with 89 additions and 29 deletions

View File

@ -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);

12
main.c
View File

@ -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);

View File

@ -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){

View File

@ -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;
}