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

Dependencies:   mbed MotionSensor

Revision:
27:a1b41626f57c
Parent:
26:abbc19edc5c1
Child:
28:98848e6a77a2
diff -r abbc19edc5c1 -r a1b41626f57c RoomEngine/RoomEngine.cpp
--- a/RoomEngine/RoomEngine.cpp	Mon Apr 29 02:45:17 2019 +0000
+++ b/RoomEngine/RoomEngine.cpp	Mon Apr 29 10:39:09 2019 +0000
@@ -1,11 +1,20 @@
 #include "RoomEngine.h"
-
-void GameEngine::init()
+RoomEngine::RoomEngine()
 {
-    Player player(39, 27);
+    player = new Player(39, 27);
 }
 
-void GameEngine::read_input(Gamepad &gamepad)
+RoomEngine::~RoomEngine()
+{
+    delete player;
+}
+
+void RoomEngine::load_room(Room &current_room)
+{
+    room = &current_room;
+}
+
+void RoomEngine::read_input(Gamepad &gamepad)
 {
     _L = gamepad.check_event(Gamepad::L_PRESSED);
     _R = gamepad.check_event(Gamepad::R_PRESSED);
@@ -18,27 +27,36 @@
     mapped_coord = gamepad.get_mapped_coord();
 }
 
-void GameEngine::update(N5110 &lcd)
+void RoomEngine::update()
 {
     check_damage();
-    check_death();
+    check_enemies_death();
     move();
-    minimap_detection(lcd);
-    pause_detection(lcd);
+    player->buttons(_A, _B, _Y, _X);
 }
 
-void GameEngine::draw(N5110 &lcd)
-{   
-    lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
-    draw_player(lcd);
-    draw_enemies(lcd);
-    draw_bullets(lcd);
-    draw_health(lcd);
+void RoomEngine::render(N5110 &lcd, Gamepad &gamepad)
+{
+    minimap_detection(lcd, gamepad);
+    pause_detection(lcd, gamepad);
+    lcd.clear();
+    draw(lcd);
+    lcd.refresh();
+    wait_ms(1000/40); // setting FPS
+}
+
+bool RoomEngine::check_player_death()
+{
+    // Player Death
+    if (player->get_hp() <= 0) {
+        return true;
+    }
+    return false;
 }
 
 // Private Functions
 
-bool GameEngine::entity_collision(Entity &a, Entity &b)  // returns true if the two entity hitboxes collide
+bool RoomEngine::entity_collision(Entity &a, Entity &b)  // returns true if the two entity hitboxes collide
 {
     if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) ||
             ((b.get_pos_x() <= a.get_pos_x() + a.get_hitbox_width() - 1) && (a.get_pos_x() + a.get_hitbox_width() - 1 <= b.get_pos_x() + b.get_hitbox_width() - 1))) {
@@ -51,7 +69,7 @@
 }
 
 // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction
-float GameEngine::entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[])
+float RoomEngine::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 (valid_enemies[i]) {
@@ -70,7 +88,7 @@
 }
 
 // returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction
-float GameEngine::entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[])
+float RoomEngine::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 (valid_enemies[i]) {
@@ -88,36 +106,36 @@
     return 0;
 }
 
-void GameEngine::check_damage()
+void RoomEngine::check_damage()
 {
     check_damage_player();
     check_damage_enemies();
 }
 
-void GameEngine::check_damage_player()
+void RoomEngine::check_damage_player()
 {
     for (int i = 0; i < MAX_ENEMIES; i++) {
-        if (room.valid_enemies[i]) {
-            if(entity_collision(player, *room.enemies[i])) {
-                player.take_damage(room.enemies[i]->get_attack());
+        if (room->valid_enemies[i]) {
+            if(entity_collision(*player, *room->enemies[i])) {
+                player->take_damage(room->enemies[i]->get_attack());
             }
         }
-    };
+    }
 }
 
-void GameEngine::check_damage_enemies()
+void RoomEngine::check_damage_enemies()
 {
     for (int i = 0; i < bullets_max; i++) {
-        if (player.valid_bullets[i]) {
-            if (player.bullets_array[i]->out_of_bounds_check()) {
-                player.valid_bullets[i] = false;
-                delete player.bullets_array[i];
+        if (player->valid_bullets[i]) {
+            if (player->bullets_array[i]->out_of_bounds_check(room->get_current_map_2d())) {
+                player->valid_bullets[i] = false;
+                delete player->bullets_array[i];
             } else {
                 for (int j = 0; j < MAX_ENEMIES; j++) {
-                    if (room.valid_enemies[j] && (entity_collision(*player.bullets_array[i], *room.enemies[j]))) {
-                        room.enemies[j]->take_damage(player.get_attack());
-                        player.valid_bullets[i] = false;
-                        delete player.bullets_array[i];
+                    if (room->valid_enemies[j] && (entity_collision(*player->bullets_array[i], *room->enemies[j]))) {
+                        room->enemies[j]->take_damage(player->get_attack());
+                        player->valid_bullets[i] = false;
+                        delete player->bullets_array[i];
                         break;
                     }
                 }
@@ -126,82 +144,78 @@
     }
 }
 
-void GameEngine::check_death()
+void RoomEngine::check_enemies_death()
 {
-    // Player Death
-    if (player.get_hp() <= 0) {
-    }
     // Enemy Death
     for (int i = 0; i < MAX_ENEMIES; i++) {
-        if (room.valid_enemies[i]) {
-            if(room.enemies[i]->get_hp() <= 0) {
-                room.valid_enemies[i] = false;
-                delete room.enemies[i];
+        if (room->valid_enemies[i]) {
+            if(room->enemies[i]->get_hp() <= 0) {
+                room->valid_enemies[i] = false;
+                delete room->enemies[i];
             }
         }
     }
 }
 
-void GameEngine::move()
+void RoomEngine::move()
 {
     move_player();
     move_enemies();
     move_bullets();
 }
 
-void GameEngine::move_player()
+void RoomEngine::move_player()
 {
-    player.move(mapped_coord.x, mapped_coord.y);
+    player->move(mapped_coord.x, mapped_coord.y, room->get_current_map_2d());
 }
 
-void GameEngine::move_enemies()
+void RoomEngine::move_enemies()
 {
     // Actual Movement of Enemies
     for (int i = 0; i < MAX_ENEMIES; i++) {
-        if (room.valid_enemies[i]) {
-            room.enemies[i]->update_prev_pos();
-            room.enemies[i]->move(player.get_pos_x(), player.get_pos_y());
+        if (room->valid_enemies[i]) {
+            room->enemies[i]->update_prev_pos();
+            room->enemies[i]->move(player->get_pos_x(), player->get_pos_y(), room->get_current_map_2d());
         }
-    };
+    }
     // Entity Collision Repulsion
     for (int i = 0; i < MAX_ENEMIES; i++) {
-        if (room.valid_enemies[i]) {
-            room.enemies[i]->position_add_x(entity_move_check_x(room.enemies[i], room.enemies, MAX_ENEMIES, i, room.valid_enemies));
-            room.enemies[i]->position_add_y(entity_move_check_y(room.enemies[i], room.enemies, MAX_ENEMIES, i, room.valid_enemies));
+        if (room->valid_enemies[i]) {
+            room->enemies[i]->position_add_x(entity_move_check_x(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies));
+            room->enemies[i]->position_add_y(entity_move_check_y(room->enemies[i], room->enemies, MAX_ENEMIES, i, room->valid_enemies));
         }
+    }
+}
+
+void RoomEngine::move_bullets()
+{
+    for (int i = 0; i < bullets_max; i++) {
+        if (player->valid_bullets[i]) {
+            player->bullets_array[i]->move(player->get_bullet_speed(), 0, 0);
+        }
+    }
+}
+
+void RoomEngine::minimap_detection(N5110 &lcd, Gamepad &gamepad)
+{
+    while(_BACK) {
+        _BACK = gamepad.check_event(Gamepad::BACK_PRESSED);
+        lcd.clear();
+        lcd.refresh();
+        wait_ms(1000/40);
     };
 }
 
-void GameEngine::move_bullets()
-{
-    for (int i = 0; i < bullets_max; i++) {
-        if (player.valid_bullets[i]) {
-            player.bullets_array[i]->move(player.get_bullet_speed(), 0);
-        }
-    };
-}
-
-void GameEngine::minimap_detection(N5110 &lcd)
-{
-    while(_BACK) {
-        wait(0.05);
-        lcd.clear();
-    };
-}
-
-void GameEngine::pause_detection(N5110 &lcd)
+void RoomEngine::pause_detection(N5110 &lcd, Gamepad &gamepad)
 {
     if(_START) {
-        lcd.drawSpriteTransparent(0,
-                                  0,
-                                  player.get_hearts_height(),
-                                  player.get_hearts_width(),
-                                  player.get_hearts_sprite());
+        draw_health(lcd);
         int * paused_screen = lcd.readScreen();
         int pause_timer = 2;
         lcd.drawSpriteTransparent(20, 20, 9, 45, (int *)pause_sprite);
         wait(0.05);
         while(_START) {
+            _START = gamepad.check_event(Gamepad::START_PRESSED);
             lcd.clear();
             lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen);
             if (pause_timer % 10 <= 4) {
@@ -210,10 +224,11 @@
             lcd.refresh();
             pause_timer++;
             wait_ms(1000/40);
-        };
+        }
         wait(0.05);
         pause_timer += 2;
         while(!_START) {
+            _START = gamepad.check_event(Gamepad::START_PRESSED);
             lcd.clear();
             lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen);
             if (pause_timer % 10 <= 4) {
@@ -222,10 +237,11 @@
             lcd.refresh();
             pause_timer++;
             wait_ms(1000/40);
-        };
+        }
         wait(0.05);
         pause_timer += 2;
         while(_START) {
+            _START = gamepad.check_event(Gamepad::START_PRESSED);
             lcd.clear();
             lcd.drawSprite(0, 0, HEIGHT, WIDTH, paused_screen);
             if (pause_timer % 10 <= 4) {
@@ -234,50 +250,64 @@
             lcd.refresh();
             pause_timer++;
             wait_ms(1000/40);
-        };
+        }
+    }
+}
+
+void RoomEngine::draw(N5110 &lcd)
+{   
+    room->draw_room(lcd);
+    //lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
+    draw_player(lcd);
+    draw_enemies(lcd);
+    draw_bullets(lcd);
+    if (_L) {
+        draw_health(lcd);
     }
 }
 
-void GameEngine::draw_player(N5110 &lcd)
+void RoomEngine::draw_player(N5110 &lcd)
 {
-    lcd.drawSpriteTransparent(player.get_pos_x()-player.get_offset_x(),
-                              player.get_pos_y()-player.get_offset_y(),
-                              player.get_sprite_height(),
-                              player.get_sprite_width(),
-                              player.get_frame());
+    lcd.drawSpriteTransparent(player->get_pos_x()-player->get_offset_x(),
+                              player->get_pos_y()-player->get_offset_y(),
+                              player->get_sprite_height(),
+                              player->get_sprite_width(),
+                              player->get_frame());
 }
 
-void GameEngine::draw_enemies(N5110 &lcd)
+void RoomEngine::draw_enemies(N5110 &lcd)
 {
     for (int i = 0; i < MAX_ENEMIES; i++) {
-        if (room.valid_enemies[i]) {
-            lcd.drawSpriteTransparent(room.enemies[i]->get_pos_x()-room.enemies[i]->get_offset_x(),
-                                      room.enemies[i]->get_pos_y()-room.enemies[i]->get_offset_y(),
-                                      room.enemies[i]->get_sprite_height(),
-                                      room.enemies[i]->get_sprite_width(),
-                                      room.enemies[i]->get_frame());
+        if (room->valid_enemies[i]) {
+            lcd.drawSpriteTransparent(room->enemies[i]->get_pos_x()-room->enemies[i]->get_offset_x(),
+                                      room->enemies[i]->get_pos_y()-room->enemies[i]->get_offset_y(),
+                                      room->enemies[i]->get_sprite_height(),
+                                      room->enemies[i]->get_sprite_width(),
+                                      room->enemies[i]->get_frame());
         }
-    };
+    }
 }
 
-void GameEngine::draw_bullets(N5110 &lcd)
+void RoomEngine::draw_bullets(N5110 &lcd)
 {
     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());
+        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());
         }
-    };
+    }
 }
 
-void GameEngine::draw_health(N5110 &lcd)
+void RoomEngine::draw_health(N5110 &lcd)
 {
-    lcd.drawSpriteTransparent(0,
-                              0,
-                              player.get_hearts_height(),
-                              player.get_hearts_width(),
-                              player.get_hearts_sprite());
+    for (int i = 0; i < player->get_hp(); i++){
+        lcd.drawSpriteTransparent(i*10,
+                                  0,
+                                  player->get_hearts_height(),
+                                  player->get_hearts_width(),
+                                  player->get_hearts_sprite());
+    }
 }
\ No newline at end of file