Add hitbox reduction

master
En Yi 2020-01-15 20:29:45 +08:00
parent 3480c4cdd3
commit 7474d2ebbe
4 changed files with 51 additions and 19 deletions

View File

@ -12,10 +12,11 @@ struct kinematic_obj
{
Rectangle rect;
Vector2 velocity;
double scale;
double set_scale;
Vector2 pos;
int ori_width;
int ori_height;
double dim_reduction[4];
double set_dim_reduction[4];
};
struct kinematic_obj_node
@ -46,6 +47,8 @@ struct player_obj
enum PLAYER_STATE state;
};
extern unsigned int PLAYER_SIZE;
struct squishy_square
{
struct kinematic_obj *parent;
@ -68,6 +71,7 @@ struct kinematic_obj init_kinematic_obj(int width, int height);
void move(struct kinematic_obj *obj, Vector2 acceleration);
void set_position(struct kinematic_obj *obj, int x, int y);
bool place_meeting(struct kinematic_obj *obj, Vector2 dir);
void scale_rect(struct kinematic_obj *obj);
// Math functions, math.c
long mag(Vector2 vec);

2
main.c
View File

@ -40,7 +40,7 @@ int main()
camera.zoom = 1.0f;
struct player_obj player = {
.kinematic = init_kinematic_obj(40, 40),
.kinematic = init_kinematic_obj(PLAYER_SIZE, PLAYER_SIZE),
.state = IDLE
};

View File

@ -4,18 +4,20 @@ extern struct kinematic_obj_node *kinematic_HEAD;
struct kinematic_obj init_kinematic_obj(int width, int height){
struct kinematic_obj obj = {
.velocity = {0.0f,0.0f},
.pos = {0.0f,0.0f},
.rect = {0,0,width,height},
.scale = 1.0,
.set_scale = 1.0,
.ori_width = width,
.ori_height = height
.ori_height = height,
.dim_reduction = {0,0,0,0}
};
return obj;
};
void set_position(struct kinematic_obj *obj, int x, int y){
obj->pos.x = x;
obj->rect.x = x;
obj->pos.y = y;
obj->rect.y = y;
};
@ -27,7 +29,10 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
struct kinematic_obj_node *current;
//Simplistic Collision Handling for AABB, Could add coeff of restitution?
obj->velocity.x += acceleration.x * delta;
obj->rect.x += obj->velocity.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];
current = kinematic_HEAD;
while(current != NULL){
if(current->obj != obj){
@ -36,15 +41,19 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
if(collide_rect.width < collide_rect.height){
if (!place_meeting(obj, (Vector2){-collide_rect.width,0})){
obj->rect.x -= collide_rect.width;
obj->pos.x -= collide_rect.width;
}else{
obj->rect.x += collide_rect.width;
obj->pos.x += collide_rect.width;
}
obj->velocity.x = 0;
}else{
if (!place_meeting(obj, (Vector2){0,-collide_rect.height})){
obj->rect.y -= collide_rect.height;
obj->pos.y -= collide_rect.height;
}else{
obj->rect.y += collide_rect.height;
obj->pos.y += collide_rect.height;
}
obj->velocity.y = 0;
}
@ -55,7 +64,10 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
// Repeat for y
obj->velocity.y += acceleration.y * delta;
obj->rect.y += obj->velocity.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];
current = kinematic_HEAD;
while(current != NULL){
if(current->obj != obj){
@ -63,9 +75,11 @@ void move(struct kinematic_obj *obj, Vector2 acceleration){
collide_rect = GetCollisionRec(obj->rect, current->obj->rect);
if(collide_rect.width < collide_rect.height){
obj->rect.x -= sign(obj->velocity.x) * collide_rect.width;
obj->pos.x -= sign(obj->velocity.x) * collide_rect.width;
obj->velocity.x = 0;
}else{
obj->rect.y -= sign(obj->velocity.y) * collide_rect.height;
obj->pos.y -= sign(obj->velocity.y) * collide_rect.height;
obj->velocity.y = 0;
}
}

View File

@ -20,6 +20,8 @@ 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;
// The player FSM
void player_input_check(struct player_obj *player){
Vector2 accel = (Vector2){
@ -140,17 +142,29 @@ void player_input_check(struct player_obj *player){
}
// TODO: Add a key to resize the rect and see what happens?
if (IsKeyDown(KEY_P)){
player->kinematic.set_scale = 1.2;
}
else if (IsKeyDown(KEY_O)){
player->kinematic.set_scale = 0.85;
}else{
player->kinematic.set_scale = 1;
}
approach(&player->kinematic.scale, player->kinematic.set_scale, 0.5);
player->kinematic.rect.width = player->kinematic.scale * player->kinematic.ori_width;
player->kinematic.rect.height = player->kinematic.scale * player->kinematic.ori_height;
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);
//scale_rect(&player->kinematic);
if (IsKeyPressed(JUMP) && jumps > 0){
player->state = JUMP_SQUAT;