A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Revision:
13:d04a6caba40d
Parent:
12:a1c1991835ca
Child:
14:3361879490b2
--- a/main.cpp	Tue Apr 23 22:59:12 2019 +0000
+++ b/main.cpp	Wed Apr 24 02:33:33 2019 +0000
@@ -36,16 +36,18 @@
 }
 
 // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction
-bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
+bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
     for (int i = 0; i < no_of_enemies; i++){
-        if(i != current_entity){
-            if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
-            ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
-            {
-                if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
-                ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+        if (valid_enemies[i]){
+            if(i != current_entity){
+                if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
+                ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
                 {
-                    return true;
+                    if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
+                    ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+                    {
+                        return true;
+                    }
                 }
             }
         }
@@ -54,17 +56,19 @@
 }
 
 // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction
-bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
+bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
     for (int i = 0; i < no_of_enemies; i++){
-        if(i != current_entity){
-            if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
-            ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
-            {
-                if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
-                ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+        if (valid_enemies[i]){
+            if(i != current_entity){
+                if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
+                ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
                 {
-                    return true;
-                }          
+                    if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
+                    ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+                    {
+                        return true;
+                    }          
+                }
             }
         }
     }
@@ -79,21 +83,38 @@
     while(1){
         Player player(39, 27);
         int no_of_enemies = 3;
+        bool valid_enemies[no_of_enemies];
+        for (int i = 0; i < no_of_enemies; i++){valid_enemies[i] = false;}
         Entity *enemies[no_of_enemies];
-        Headless enemy1(20, 20);
-        Headless enemy2(20, 30);
-        Headless enemy3(60, 30);
-        enemies[0] = &enemy1;
-        enemies[1] = &enemy2;
-        enemies[2] = &enemy3;
+        enemies[0] = new Headless(20, 20);
+        valid_enemies[0] = true;
+        enemies[1] = new Headless(20, 30);
+        valid_enemies[1] = true;
+        enemies[2] = new Headless(60, 30);
+        valid_enemies[2] = true;
         
         while(1){
             int pos_x = player.get_pos_x();
             int pos_y = player.get_pos_y();
             // Damage action
             for (int i = 0; i < no_of_enemies; i++){
-                if(entity_collision(player, *enemies[i])){
-                    goto gameover;
+                if (valid_enemies[i]){
+                    if(entity_collision(player, *enemies[i])){
+                        goto gameover;
+                    }
+                }
+            };
+            for (int i = 0; i < bullets_max; i++){
+                if (player.valid_bullets[i]){
+                    for (int j = 0; j < no_of_enemies; j++){
+                        if (valid_enemies[j]){
+                            if(entity_collision(*player.bullets_array[i], *enemies[j])){
+                                enemies[j]->take_damage(player.get_attack());
+                                player.valid_bullets[i] = false;
+                                player.bullets_array[i]->~Bullets;
+                            }
+                        }
+                    };
                 }
             };
             
@@ -104,12 +125,16 @@
             
             // Enemy Movement
             for (int i = 0; i < no_of_enemies; i++){
-                enemies[i]->update_prev_pos();
-                enemies[i]->move(pos_x, pos_y);
+                if (valid_enemies[i]){
+                    enemies[i]->update_prev_pos();
+                    enemies[i]->move(pos_x, pos_y);
+                }
             };
             for (int i = 0; i < no_of_enemies; i++){
-                enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i));
-                enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i));
+                if (valid_enemies[i]){
+                    enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i, valid_enemies));
+                    enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i, valid_enemies));
+                }
             };
             
             // Entity Collision Detection
@@ -142,13 +167,23 @@
                                       player.get_sprite_width(),
                                       player.get_frame());
             for (int i = 0; i < no_of_enemies; i++){
-                lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
-                                          enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
-                                          enemies[i]->get_sprite_height(),
-                                          enemies[i]->get_sprite_width(),
-                                          enemies[i]->get_frame());
+                if (valid_enemies[i]){
+                    lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
+                                              enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
+                                              enemies[i]->get_sprite_height(),
+                                              enemies[i]->get_sprite_width(),
+                                              enemies[i]->get_frame());
+                }
             };
-            
+            for (int i = 0; i < bullets_max; i++){
+                if (player.valid_bullets[i]){
+                    lcd.drawSpriteTransparent(player.bullets_array[i]->get_pos_x()-player.bullets_array[i]->get_offset_x(),
+                                              player.bullets_array[i]->get_pos_y()-player.bullets_array[i]->get_offset_y(),
+                                              player.bullets_array[i]->get_sprite_height(),
+                                              player.bullets_array[i]->get_sprite_width(),
+                                              player.bullets_array[i]->get_frame());
+                }
+            };
             lcd.refresh();
             wait_ms(1000/20); // setting FPS
             counter++;