From 0e6a605b9a96a71fe39917746995cc1b4dae36e0 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 28 Mar 2020 11:09:25 +0800 Subject: [PATCH] Add destroying target --- include/header.h | 1 + main.c | 18 ++++++++++++------ obj/player.c | 13 +++++++------ obj/target.c | 19 +++++++++++++++---- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/include/header.h b/include/header.h index 9202478..8735a53 100644 --- a/include/header.h +++ b/include/header.h @@ -94,6 +94,7 @@ struct target_obj { double radius; struct kinematic_obj kinematic; + bool destroyed; }; struct target_obj_node { diff --git a/main.c b/main.c index 1020d11..f1b4d01 100644 --- a/main.c +++ b/main.c @@ -45,6 +45,7 @@ int main() camera.offset = (Vector2){0,0}; camera.rotation = 0.0f; camera.zoom = 1.0f; + struct player_obj player = { .kinematic = init_kinematic_obj(PLAYER_SIZE, PLAYER_SIZE), @@ -71,6 +72,7 @@ int main() set_position(&tile7, 700, 80); struct squishy_square sqr = init_squishy_square(&player.kinematic, RED); player.image = &sqr; + //camera.target = player.kinematic.pos; add_kinematic_node(&tile, &kinematic_HEAD); add_kinematic_node(&tile2, &kinematic_HEAD); @@ -83,7 +85,7 @@ int main() struct target_obj target = init_target(50, 300); - set_position(&target.kinematic, 150, 380); + set_position(&target.kinematic, 300, 100); add_target_node(&target, &target_HEAD); SetTargetFPS(60); // Set our game to run at 60 frames-per-second @@ -104,20 +106,24 @@ int main() BeginDrawing(); ClearBackground(RAYWHITE); - draw_afterimages(&player); - draw_squishy(&sqr); struct kinematic_obj_node *current; struct target_obj_node *target_current; + // Camera target follows player + camera.target = (Vector2){ player.kinematic.pos.x -screenWidth/2, player.kinematic.pos.y -screenHeight/2, }; BeginMode2D(camera); + draw_afterimages(&player); + draw_squishy(&sqr); current = kinematic_HEAD; while(current){ - DrawRectangleLinesEx(current->obj->rect, 1, BLACK); + if (current->obj != &player.kinematic) + DrawRectangleLinesEx(current->obj->rect, 1, BLACK); current = current->next; } target_current = target_HEAD; while(target_current){ - DrawCircle(target_current->obj->kinematic.pos.x, target_current->obj->kinematic.pos.y, - target_current->obj->kinematic.ori_width, BLACK); + if (!target_current->obj->destroyed) + DrawCircle(target_current->obj->kinematic.pos.x, target_current->obj->kinematic.pos.y, + target_current->obj->kinematic.ori_width, BLACK); target_current = target_current->next; } DrawFPS(0,0); diff --git a/obj/player.c b/obj/player.c index f7660ea..5a4f96a 100644 --- a/obj/player.c +++ b/obj/player.c @@ -200,8 +200,9 @@ void player_input_check(struct player_obj *player){ struct target_obj_node *target_current = target_HEAD; while(target_current){ - if (collide_target(&player->kinematic, target_current->obj)==true) - printf("collide\n"); + if (collide_target(&player->kinematic, target_current->obj)==true){ + dash_count = 1; + } target_current = target_current->next; } @@ -242,9 +243,9 @@ void player_input_check(struct player_obj *player){ // Air friction is less than ground friction if (on_ground == true) - accel.x -= player->kinematic.velocity.x * 6.0; + accel.x -= player->kinematic.velocity.x * 8.0; else - accel.x -= player->kinematic.velocity.x * 3.0; + accel.x -= player->kinematic.velocity.x * 4.0; // Handle wall jumping // TODO: define the wall jump values @@ -312,9 +313,9 @@ void player_input_check(struct player_obj *player){ player->kinematic.set_dim_reduction[3] = 0; if (on_ground == false){ if (player->kinematic.velocity.y < 0) - player->kinematic.set_dim_reduction[3] = -player->kinematic.velocity.y / 40; + player->kinematic.set_dim_reduction[3] = - fmax(player->kinematic.velocity.y / 40, -32) ; if (player->kinematic.velocity.y > 0) - player->kinematic.set_dim_reduction[1] = player->kinematic.velocity.y / 40; + player->kinematic.set_dim_reduction[1] = fmin(player->kinematic.velocity.y / 40, 32); set_squish_target_offset(player->image, 1, player->kinematic.velocity.y / 30); set_squish_target_offset(player->image, 3, -player->kinematic.velocity.y / 30); diff --git a/obj/target.c b/obj/target.c index c517c2e..0d53eff 100644 --- a/obj/target.c +++ b/obj/target.c @@ -1,15 +1,18 @@ #include "header.h" #include #include + +#define GRID_SIZE 32 // Target is circular, thus a kinematic obj has w=h struct target_obj init_target(){ return (struct target_obj){ .radius = 12, - .kinematic = init_kinematic_obj(12, 12) + .kinematic = init_kinematic_obj(12, 12), + .destroyed = false }; } -// TODO: Implement a grid collision system for target +// SUGGESTION: Implement a grid collision system for target // This is okay since target is always constant size // Partition the screen into apropriate grid sizes // Check which grid requires check (i.e. which grid is the player overlapping) @@ -30,16 +33,22 @@ struct target_obj init_target(){ // The construction of the grid will take in a linked list all targets. // So no need to modify the existing linked list +// However, it might be possible to make it an array bool collide_target(struct kinematic_obj *obj, struct target_obj *target){ /* The method is based off SAT */ + if (target->destroyed) + return false; + Vector2 obj_center = center(obj->rect); Vector2 target_center = center(target->kinematic.rect); float dist = Vector2Distance(obj_center, target_center); - if (dist < target->radius) + if (dist < target->radius){ + target->destroyed = true; return true; + } double max_dim = fmax(obj->rect.width, obj->rect.height); if (dist > max_dim + target->radius) @@ -72,8 +81,10 @@ bool collide_target(struct kinematic_obj *obj, struct target_obj *target){ double target_proj = Vector2DotProduct(target_center, n); - if (!(max_proj < target_proj - target->radius) && !(min_proj > target_proj + target->radius)) + if (!(max_proj < target_proj - target->radius) && !(min_proj > target_proj + target->radius)){ + target->destroyed = true; return true; + } return false; }