From 7a2d214f38b020f7559e65ee240f37f7c6fbb017 Mon Sep 17 00:00:00 2001 From: AynRandDuran Date: Mon, 5 Jul 2021 23:07:24 -0400 Subject: [PATCH] Shotgun and shielding looking okay --- assteroids.h | 26 ++++++++++ game.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++----- powerups.hpp | 12 +++++ 3 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 powerups.hpp diff --git a/assteroids.h b/assteroids.h index f9c3547..096e8b0 100644 --- a/assteroids.h +++ b/assteroids.h @@ -29,5 +29,31 @@ const int SHIP_DEBRIS = 6; Vector4 dead_astr[MAX_ASTEROIDS][6]; void explode_asteroid(Vector4* astr); void update_explosions(); + +// bitfield +const int SHOTGUN = 1; +const int BOMB = 2; +const int GOD = 4; +char active_powerups = 0; +int shotgun_blasts = 0; +int bullet_offsets[5] = {0, 6, 12, -6, -12}; + +Vector4 shotgun_box = {center.x, center.y, 45, 0}; +Vector2 master_s_top_box[4] = {{-10, -2}, {-10, -10}, {10, -10}, {10, -2}}; +Vector2 master_s_bot_box[4] = {{-10, 2}, {-10, 10}, {10, 10}, {10, 2}}; +Vector2 s_top_box[4]; +Vector2 s_bot_box[4]; +void init_shotgun(); +void draw_shotgun(); +int enable_shotgun(); +int disable_shotgun(); + +Vector4 shield_pickup = {0, 0, 0, 0}; +int shield_hp = 9; +time_t shield_spawn_time = 0; +void init_shield(); +void draw_shield_pickup(); //explicitly, the powerup +void enable_shield(); +void disable_shield(); #endif //__ass diff --git a/game.cpp b/game.cpp index ed77026..3b7d488 100644 --- a/game.cpp +++ b/game.cpp @@ -1,6 +1,7 @@ -#include "assteroids.h" #include #include +#include "assteroids.h" +#include "powerups.hpp" #include "vectormath.hpp" Vector2 flatten(Vector4 pV){ @@ -8,6 +9,8 @@ Vector2 flatten(Vector4 pV){ return stan; } + + Vector4 translate(Vector4 v1, Vector4 v2) { Vector4 translation = {v1.x+v2.x, v1.y+v2.y, v1.y+v2.y, v1.z+v2.z}; return translation; @@ -35,12 +38,15 @@ void init_ship(){ memset(bullets, 0, sizeof(Vector4) * MAX_BULLETS); memset(asteroids, 0, sizeof(Vector4) * MAX_ASTEROIDS); memset(dead_ship, 0, sizeof(Vector4) * SHIP_DEBRIS); - + init_shotgun(); + memset(&shotgun_box, 0, sizeof(Vector4)); memset(dead_astr, 0, sizeof(Vector4) * SHIP_DEBRIS * MAX_ASTEROIDS); + memset(&shield_pickup, 0, sizeof(Vector4)); } void die(){ ship_alive = false; + disable_shield(); for(int i = 0; i < SHIP_DEBRIS; i++) { if (!onscreen(flatten(dead_ship[i]))) continue; @@ -48,6 +54,7 @@ void die(){ dead_ship[i].z = (360/SHIP_DEBRIS) * (i + 1); dead_ship[i].w = 6; } + disable_shotgun(); } @@ -71,17 +78,25 @@ void update_v4(Vector4* origin, Vector4* v_body, int speed){ } void shoot(){ - for(int i = 0; i < MAX_BULLETS; i++){ - // find available bullet - if(bullets[i].w == 0 && time(NULL) > last_fire) { - bullets[i].x = center.x; - bullets[i].y = center.y; - bullets[i].z = nose.z; - bullets[i].w = 1; - time(&last_fire); - break; + if(time(NULL) <= last_fire) + return; + for(int s = 0; s < 1 + (4 * (active_powerups & SHOTGUN)); s++) { + for(int i = 0; i < MAX_BULLETS; i++){ + // find available bullet + if(bullets[i].w == 0) { + bullets[i].x = center.x; + bullets[i].y = center.y; + bullets[i].z = nose.z + bullet_offsets[s]; + bullets[i].w = 1; + break; + } } } + if(active_powerups & SHOTGUN) + shotgun_blasts--; + if(shotgun_blasts == 0 && active_powerups & SHOTGUN) + disable_shotgun(); + time(&last_fire); } void update_bullets(){ @@ -154,9 +169,11 @@ void update_astrs() { } if(ship_collision(astr) && ship_alive){ + if((active_powerups & GOD)) { + continue; + } die(); } - } } @@ -203,7 +220,17 @@ int main(void) { flatten(translate(starboard, center)), RAYWHITE); astr_spawner = rand() & 1000 + 1; - } else { + if(active_powerups & GOD) { + DrawCircleSectorLines(flatten(center), 3*shield_hp, 0, 360, 6, SKYBLUE); + DrawCircleSectorLines(flatten(center), 2*shield_hp, 0, 360, 6, SKYBLUE); + if(time(NULL) > shield_spawn_time) { + time(&shield_spawn_time); + shield_hp--; + } + if(shield_hp <= 0) + disable_shield(); + } + } else if(!(active_powerups & GOD)) { DrawText("YOU DIED", (scrW/2)-(MeasureText("YOU DIED", 64)/2), (scrH/2)-64, 64, RED); DrawText("FUCK YOU", (scrW/2)-(MeasureText("FUCK YOU", 16)/2), (scrH/2)-8, 16, RED); explode_ship(); @@ -214,6 +241,12 @@ int main(void) { update_bullets(); update_astrs(); update_explosions(); + if(throttle && shotgun_box.w == 0 && (rand() % 10000) < 8 && !active_powerups & SHOTGUN) + init_shotgun(); //a little treat + if(shotgun_box.w == 1) + draw_shotgun(); + if(shield_pickup.w > 0) + draw_shield_pickup(); DrawText(TextFormat("%d", score), 4, scrH-34, 32, WHITE); DrawFPS(0, 0); @@ -228,6 +261,35 @@ int main(void) { return 0; } +//spawn a shield pickup at a dead asteroid +void init_shield(Vector4* v) { + shield_pickup.x = v->x; + shield_pickup.y = v->y; + shield_pickup.w = 16; +} +void draw_shield_pickup() { + DrawRingLines(flatten(shield_pickup), shield_pickup.w, shield_pickup.w * 1.3, 0, 360, 6, SKYBLUE); + DrawRingLines(flatten(shield_pickup), shield_pickup.w/3, shield_pickup.w * 0.66, 0, 360, 6, SKYBLUE); + if(CheckCollisionPointCircle(flatten(center), flatten(shield_pickup), shield_pickup.w)) { + shield_pickup.w = 0; + enable_shield(); + } +} + +void enable_shield() { + memset(&shield_pickup, 0, sizeof(Vector4)); + shield_hp = 9; + active_powerups |= GOD; + time(&shield_spawn_time); + printf("powerups: %d\n", active_powerups); +} + +void disable_shield() { + active_powerups ^= GOD; + shield_hp = 9; + printf("powerups: %d\n", active_powerups); +} + void explode_asteroid(Vector4* astr) { for(int i = 0; i < MAX_ASTEROIDS; i++) { if(dead_astr[i][0].w == 0) { @@ -237,6 +299,8 @@ void explode_asteroid(Vector4* astr) { dead_astr[i][a].z = ((360/SHIP_DEBRIS) * a) + (360/(SHIP_DEBRIS*3)); dead_astr[i][a].w = 1; } + if(shield_pickup.w == 0 && rand() % 20 < 3) + init_shield(astr); break; } } @@ -260,3 +324,48 @@ void update_explosions() { } } + +void init_shotgun() { + int heading = rand() % 360; + shotgun_box.z = heading; + shotgun_box.w = 1; + shotgun_box.x = (heading > 90 && heading < 270) ? scrW : 0; + shotgun_box.y = (heading > 180 && heading < 360) ? scrH : 0; + + memcpy(s_top_box, master_s_top_box, sizeof(Vector2)*4); + memcpy(s_bot_box, master_s_bot_box, sizeof(Vector2)*4); + for(int i = 0; i < 4; i++) { + s_top_box[i].x += shotgun_box.x; + s_top_box[i].y += shotgun_box.y; + s_bot_box[i].x += shotgun_box.x; + s_bot_box[i].y += shotgun_box.y; + } +} + +void update_shotgun() { + float xd = cos(shotgun_box.z * (PI/180)) *2; + float yd = sin(shotgun_box.z * (PI/180)) *2; + shotgun_box.x += xd; + shotgun_box.y += yd; + + for(int i = 0; i < 4; i++) { + s_top_box[i].x += xd; + s_top_box[i].y += yd; + s_bot_box[i].x += xd; + s_bot_box[i].y += yd; + } + + if(!onscreen(flatten(shotgun_box))) + shotgun_box.w = 0; + if(CheckCollisionPointCircle(flatten(center), flatten(shotgun_box), 32)) { + enable_shotgun(); + shotgun_box.w = 0; + } +} + +void draw_shotgun() { + update_shotgun(); + DrawLineStrip(s_top_box, 4, WHITE); + DrawLineStrip(s_bot_box, 4, WHITE); + DrawText("S", shotgun_box.x-6, shotgun_box.y-8, 20, GOLD); +} diff --git a/powerups.hpp b/powerups.hpp new file mode 100644 index 0000000..8283c9a --- /dev/null +++ b/powerups.hpp @@ -0,0 +1,12 @@ +int enable_shotgun() { + shotgun_blasts = 6; + active_powerups |= 1; + return 1; +} + +int disable_shotgun() { + active_powerups &= 0; + shotgun_box.w = 0; + shotgun_blasts = 0; + return 0; +} -- 2.49.0