]> git.mcshandy.xyz Git - assteroids/commitdiff
decent enough asteroid explosions
authorAynRandDuran <lpj1496@gmail.com>
Mon, 5 Jul 2021 03:46:27 +0000 (23:46 -0400)
committerAynRandDuran <lpj1496@gmail.com>
Mon, 5 Jul 2021 03:46:27 +0000 (23:46 -0400)
assteroids.h
game.cpp

index bae7b6a8cbc8f6201f7ea28825c297dac218dccc..22cf9455fd34590cec849cdf7e057305513b2e1f 100644 (file)
@@ -24,5 +24,9 @@ const int MAX_ASTEROIDS = 16;
 
 Vector4* dead_ship;
 const int SHIP_DEBRIS = 6;
+
+Vector4 dead_astr[MAX_ASTEROIDS][6];
+void explode_asteroid(Vector4* astr);
+void update_explosions();
 #endif //__ass
 
index 3ea4657226ed96c6b8b777a4c241f2b4cdaf27f3..3d8f693b83280d7fdd39fca225adc99e7db6a213 100644 (file)
--- a/game.cpp
+++ b/game.cpp
@@ -13,6 +13,11 @@ Vector4 translate(Vector4 v1, Vector4 v2) {
     return translation;
 }
 
+bool onscreen(Vector2 v) {
+    // Ensure v is still onscreen
+    return CheckCollisionPointRec(v, screenspace); 
+}
+
 void init_ship(){
     ship_alive = true;
     throttle = false;
@@ -29,12 +34,16 @@ 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);    
+
+    memset(dead_astr, 0, sizeof(Vector4) * SHIP_DEBRIS * MAX_ASTEROIDS);
 }
 
 void die(){
     ship_alive = false;
     for(int i = 0; i < SHIP_DEBRIS; i++) {
+        if (!onscreen(flatten(dead_ship[i])))
+            continue;
         dead_ship[i].x = center.x; dead_ship[i].y = center.y;
         dead_ship[i].z = (360/SHIP_DEBRIS) * (i + 1);
         dead_ship[i].w = 8;
@@ -55,11 +64,6 @@ void spin_ship(int az_delta){
 
 }
 
-bool onscreen(Vector2 v) {
-    // Ensure v is still onscreen
-    return CheckCollisionPointRec(v, screenspace); 
-}
-
 void update_v4(Vector4* origin, Vector4* v_body, int speed){
     Vector2 heading = v2_normal(flatten(*origin));
     v_body->x += heading.x * (speed*throttle); 
@@ -95,6 +99,7 @@ void update_bullets(){
                     asteroids[a].w = 0;
                     bullets[i].w = 0;
                     score++;
+                    explode_asteroid(&asteroids[a]);
                     break;
                 }
             } 
@@ -143,8 +148,9 @@ void update_astrs() {
             astr->y += sin(astr->z * (PI/180)) *3;
             DrawPolyLines(flatten(*astr), 7, astr->w, 0, RAYWHITE);
         }
-        if(!onscreen(flatten(*astr)))
+        if(!onscreen(flatten(*astr))) {
             astr->w = 0;
+        }
 
         if(ship_collision(astr) && ship_alive){
             die();
@@ -198,6 +204,7 @@ int main(void) {
             astr_spawner = rand() & 1000 + 1;
         } else {
             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();
         }
         if(throttle && astr_spawner > 989) {
@@ -205,6 +212,7 @@ int main(void) {
         }
         update_bullets();
         update_astrs();
+        update_explosions();
         
         DrawText(TextFormat("%d", score), 4, scrH-34, 32, WHITE);
         DrawFPS(0, 0);
@@ -218,3 +226,36 @@ int main(void) {
     
     return 0;
 }
+
+void explode_asteroid(Vector4* astr) {
+    for(int i = 0; i < MAX_ASTEROIDS; i++) {
+        if(dead_astr[i][0].w == 0) {
+            for (int a = 0; a < SHIP_DEBRIS; a++) {
+                dead_astr[i][a].x = astr->x;
+                dead_astr[i][a].y = astr->y;
+                dead_astr[i][a].z = (360/SHIP_DEBRIS) * a;
+                dead_astr[i][a].w = 1;
+            }
+            break;
+        }
+    }
+}
+
+void update_explosions() {
+    int offscreen_bubbles = 0;
+    for(int i = 0; i < MAX_ASTEROIDS; i++) {
+        if(dead_astr[i][0].w == 1) {
+            for (int a = 0; a < SHIP_DEBRIS; a++) {
+                dead_astr[i][a].x += cos(dead_astr[i][a].z * (PI/180)) * 5;
+                dead_astr[i][a].y += sin(dead_astr[i][a].z * (PI/180)) * 5;
+                DrawCircleLines(dead_astr[i][a].x, dead_astr[i][a].y, 8, RAYWHITE);
+                if(!onscreen(flatten(dead_astr[i][a])))
+                    offscreen_bubbles++;
+                if(offscreen_bubbles == SHIP_DEBRIS-1)
+                    memset(&dead_astr[i], 0, sizeof(Vector4) * SHIP_DEBRIS);
+            }
+        }
+        offscreen_bubbles=0;
+    }
+    
+}