]> git.mcshandy.xyz Git - assteroids/commitdiff
tasty death exlosion. thanks helen for the help
authorAynRandDuran <lpj1496@gmail.com>
Sun, 4 Jul 2021 00:37:22 +0000 (20:37 -0400)
committerAynRandDuran <lpj1496@gmail.com>
Sun, 4 Jul 2021 00:37:22 +0000 (20:37 -0400)
assteroids.h
game.cpp

index 88adae635b58985f745acf5b6adf83eb29bcf128..bae7b6a8cbc8f6201f7ea28825c297dac218dccc 100644 (file)
@@ -22,6 +22,7 @@ Rectangle screenspace = {-200, -200, scrW+200, scrH+200};
 Vector4* asteroids;
 const int MAX_ASTEROIDS = 16;
 
-Vector4** astr_dirs;
+Vector4* dead_ship;
+const int SHIP_DEBRIS = 6;
 #endif //__ass
 
index f403494b51bb540482297f5dc06a08a55db1902c..3ea4657226ed96c6b8b777a4c241f2b4cdaf27f3 100644 (file)
--- a/game.cpp
+++ b/game.cpp
@@ -15,6 +15,7 @@ Vector4 translate(Vector4 v1, Vector4 v2) {
 
 void init_ship(){
     ship_alive = true;
+    throttle = false;
     score = 0;
     nose.z = 270;
     port.z = 55;
@@ -26,15 +27,19 @@ void init_ship(){
     center.x = scrW/2; center.y = scrH/2;
     ship_bearing = 0;
     
-    bullets = (Vector4*)malloc(sizeof(Vector4)*MAX_BULLETS);
     memset(bullets, 0, sizeof(Vector4) * MAX_BULLETS);
-
-    asteroids = (Vector4*)malloc(sizeof(Vector4)*MAX_ASTEROIDS);
     memset(asteroids, 0, sizeof(Vector4) * MAX_ASTEROIDS);
+    
 }
 
 void die(){
     ship_alive = false;
+    for(int i = 0; i < SHIP_DEBRIS; i++) {
+        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;
+    }
+    
 }
 
 void spin_ship(int az_delta){
@@ -112,6 +117,14 @@ void spawn_astr() {
     }
 }
 
+void explode_ship() {
+    for (int i = 0; i < SHIP_DEBRIS; i++) {
+        DrawCircleLines(dead_ship[i].x, dead_ship[i].y, dead_ship[i].w, RED);
+        dead_ship[i].x += cos(dead_ship[i].z * (PI/180)) *3;
+        dead_ship[i].y += sin(dead_ship[i].z * (PI/180)) *3;
+    }
+}
+
 bool ship_collision(Vector4* body) {
     return (
             CheckCollisionPointCircle(flatten(translate(nose, center)), flatten(*body), body->w * 0.9f) ||
@@ -133,7 +146,7 @@ void update_astrs() {
         if(!onscreen(flatten(*astr)))
             astr->w = 0;
 
-        if(ship_collision(astr)){
+        if(ship_collision(astr) && ship_alive){
             die();
         }
         
@@ -142,6 +155,10 @@ void update_astrs() {
 }
 
 int main(void) {
+    asteroids = (Vector4*)malloc(sizeof(Vector4)*MAX_ASTEROIDS);
+    bullets = (Vector4*)malloc(sizeof(Vector4)*MAX_BULLETS);
+    dead_ship = (Vector4*)malloc(sizeof(Vector4) * SHIP_DEBRIS);
+
     srand(time(NULL));
     InitWindow(scrW, scrH, "Assteroids Raylib");
     SetTargetFPS(50);
@@ -151,27 +168,24 @@ int main(void) {
         BeginDrawing();
         ClearBackground(Space);
 
-        if(IsKeyDown('J')) {
+        if(IsKeyDown('J') && ship_alive) {
             spin_ship(-4);
         }
-        if(IsKeyDown('L')) {
+        if(IsKeyDown('L') && ship_alive) {
             spin_ship(4);
         }
-        if(IsKeyPressed('I')) {
+        if(IsKeyPressed('I') && ship_alive) {
             throttle = true;
         }
-        if(IsKeyPressed('S')) {
+        if(IsKeyPressed('S') && ship_alive) {
             shoot();
         }
-        if(IsKeyPressed('K')) {
-            spawn_astr();
-        }
         if(IsKeyPressed('R') && !ship_alive) {
             init_ship();
             spin_ship(0);
         }
 
-        if(!onscreen(flatten(center)))
+        if(!onscreen(flatten(center)) && ship_alive)
             die();
 
         if(ship_alive) {
@@ -182,16 +196,16 @@ int main(void) {
                 flatten(translate(starboard, center)),
                 RAYWHITE);
             astr_spawner = rand() & 1000 + 1;
-            if(throttle && astr_spawner > 989) {
-                spawn_astr();
-            }
-            update_bullets();
-            update_astrs();
         } else {
             DrawText("YOU DIED", (scrW/2)-(MeasureText("YOU DIED", 64)/2), (scrH/2)-64, 64, RED);
-            memset(bullets, 0, sizeof(Vector4) * MAX_BULLETS);
-            memset(asteroids, 0, sizeof(Vector4) * MAX_ASTEROIDS);
+            explode_ship();
+        }
+        if(throttle && astr_spawner > 989) {
+            spawn_astr();
         }
+        update_bullets();
+        update_astrs();
+        
         DrawText(TextFormat("%d", score), 4, scrH-34, 32, WHITE);
         DrawFPS(0, 0);
         EndDrawing();
@@ -200,6 +214,7 @@ int main(void) {
     CloseWindow();
     free(bullets);
     free(asteroids);
+    free(dead_ship);
     
     return 0;
 }