Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Files at this revision

API Documentation at this revision

Comitter:
el17sm
Date:
Thu May 09 02:46:44 2019 +0000
Parent:
47:6e31b195ce3c
Child:
49:3f83ed62d123
Commit message:
Cleaned Main and title

Changed in this revision

Title/Title.cpp Show annotated file Show diff for this revision Revisions of this file
Title/Title.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/Title/Title.cpp	Thu May 09 01:57:49 2019 +0000
+++ b/Title/Title.cpp	Thu May 09 02:46:44 2019 +0000
@@ -21,21 +21,16 @@
             lcd.clear();
             draw_title_screen(lcd);
             lcd.refresh();
-            
             title_options_joystick(gamepad);
-            
             title_count++;
             wait_ms(1000/40); // 1s/framerate
         }
         while(gamepad.check_event(Gamepad::A_PRESSED)){}
-        if(title_option == 0) {
-            break;
-        } else if (title_option == 1) {
-            title_option_option(lcd, gamepad, player, global_contrast);
-        } else if (title_option == 2) {
-            title_option_credit(lcd, gamepad);
-        } else if (title_option == 3) {
-            title_option_tutorial(lcd, gamepad);
+        
+        if(title_option == 0) { break;  // Start game, exit title loop
+        } else if (title_option == 1) { title_option_option(lcd, gamepad, player, global_contrast);
+        } else if (title_option == 2) { title_option_credit(lcd, gamepad);
+        } else if (title_option == 3) { title_option_tutorial(lcd, gamepad);
         }
     }
     player.~Player();
@@ -87,15 +82,14 @@
         player.draw(lcd);
         player.draw_bullets(lcd, player.get_pos_y() + 2);
         lcd.refresh();
-        player.move(1, 0, (char *)level_map[0][0], (bool *)sprite_transparent_player);
-        player.undo_move_x(true);
-        player.buttons(false, true, false, false);
-        player.update_bullets((char *)level_map[0][0], (bool *)sprite_transparent_player);
+        player.move(1, 0, (char *)level_map[0][0], (bool *)sprite_transparent_player);  // Adding animation of walking
+        player.undo_move_x(true);   // Keeping the player in place
+        player.buttons(false, true, false, false);  // Instructing player to shoot right
+        player.update_bullets((char *)level_map[0][0], (bool *)sprite_transparent_player);  // Move the bullets and delete those out of the screen
         wait_ms(1000/40);
     }
     wait(0.05);
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {}
 }
 
 void Title::title_option_credit(N5110 &lcd, Gamepad &gamepad)
@@ -115,6 +109,25 @@
 
 void Title::title_option_tutorial(N5110 &lcd, Gamepad &gamepad)
 {
+    print_tutorial_page_0(lcd);                         // print page 0 and wait for button toggle
+    wait(0.05);
+    while(!gamepad.check_event(Gamepad::A_PRESSED)) {}
+    wait(0.05);
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {}
+    print_tutorial_page_1(lcd);                         // print page 1 and wait for button toggle
+    wait(0.05);
+    while(!gamepad.check_event(Gamepad::A_PRESSED)) {}
+    wait(0.05);
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {}
+    print_tutorial_page_2(lcd);                         // print page 2 and wait for button toggle
+    wait(0.05);
+    while(!gamepad.check_event(Gamepad::A_PRESSED)) {}
+    wait(0.05);
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {}
+}
+
+void Title::print_tutorial_page_0(N5110 &lcd)
+{
     lcd.clear();
     lcd.printString("Use the joypad", 0, 0);
     lcd.printString("to move the", 0, 1);
@@ -123,12 +136,10 @@
     lcd.printString("ABXY to shoot", 0, 4);
     lcd.printString("directionally", 0, 5);
     lcd.refresh();
-    wait(0.05);
-    while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-    wait(0.05);
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
+}
+
+void Title::print_tutorial_page_1(N5110 &lcd)
+{
     lcd.clear();
     lcd.printString("Hold L to", 0, 0);
     lcd.printString("show player", 0, 1);
@@ -137,21 +148,13 @@
     lcd.printString("to view a", 0, 4);
     lcd.printString("minimap", 0, 5);
     lcd.refresh();
-    wait(0.05);
-    while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-    wait(0.05);
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
+}
+
+void Title::print_tutorial_page_2(N5110 &lcd)
+{
     lcd.clear();
     lcd.printString("Press Start", 0, 0);
     lcd.printString("to pause", 0, 1);
     lcd.printString("the game", 0, 2);
     lcd.refresh();
-    wait(0.05);
-    while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-    wait(0.05);
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-}
+}
\ No newline at end of file
--- a/Title/Title.h	Thu May 09 01:57:49 2019 +0000
+++ b/Title/Title.h	Thu May 09 02:46:44 2019 +0000
@@ -21,6 +21,10 @@
     void title_option_credit(N5110 &lcd, Gamepad &gamepad);
     void title_option_tutorial(N5110 &lcd, Gamepad &gamepad);
     
+    void print_tutorial_page_0(N5110 &lcd);
+    void print_tutorial_page_1(N5110 &lcd);
+    void print_tutorial_page_2(N5110 &lcd);
+    
 public:
     // Constructor
     Title();
--- a/main.cpp	Thu May 09 01:57:49 2019 +0000
+++ b/main.cpp	Thu May 09 02:46:44 2019 +0000
@@ -25,17 +25,7 @@
         room_engine = new RoomEngine(global_contrast);
         
         game_loop();
-        game_over();
-        for (int i = 0; i < MAX_ROOMS_MAP_X; i++) { 
-            for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
-                if (valid_rooms[j][j]){
-                    delete rooms[j][i];
-                    valid_rooms[i][j] = false;
-                }
-            }
-        }
-        delete room_engine;
-        delete player;
+        game_unload();
     }
 }
 
@@ -57,42 +47,38 @@
         boss_room_number = 5 + rand() % 4; // Boss room appears after travelling 5-8 rooms
         boss_room_counter = 0;
         while(1) {  // Room Loop
-            update_room_coords();
-            if (!valid_rooms[room_y][room_x]){
-                generate_room();
-            }
-            room_engine->load(player, rooms[room_y][room_x]);
-            room_engine->entrance_scene(lcd, gamepad);
-            while(room_engine->check_player_room_position() == INSIDE) {  // Room actions
+            room_entrance();
+            while(room_engine->check_player_room_position() == INSIDE) {  // actions inside the Room
                 room_engine->read_input(gamepad);
                 room_engine->update(number_of_enemies_killed);
                 room_engine->render(lcd, gamepad);
                 minimap_detection();
                 total_time++;
-                if  (player->get_hp() <= 0) {
-                    goto gameover;
-                }
-                if ((rooms[room_y][room_x]->get_room_type() == 10) && !(rooms[room_y][room_x]->enemies_exist())){
-                    goto winner;
-                }
+                if  (player->get_hp() <= 0) {goto gameover;}    // gameover if player health depleted
+                if ((rooms[room_y][room_x]->get_room_type() == 10) && !(rooms[room_y][room_x]->enemies_exist())){goto winner;}  // wins game if room is a boss room, and the boss is dead
             }
-            room_engine->exit_scene(lcd, gamepad);
-            rooms[room_y][room_x]->unload();
-            player->delete_bullets();
-            room_engine->update_current_room();
+            room_exit();
         }
     }
-    gameover : {
-        game_over();
-        goto displaystats;
-    }
-    winner : {
-        win();
-        goto displaystats;
-    }
-    displaystats : {
-        display_stats();
-    }
+    gameover : { game_over(); goto displaystats;}
+    winner : { win(); goto displaystats;}
+    displaystats : { display_stats();}
+}
+
+void room_entrance()
+{
+    update_room_coords();
+    if (!valid_rooms[room_y][room_x]){generate_room();} // generate a new room if player enters a nonexistent room
+    room_engine->load(player, rooms[room_y][room_x]);
+    room_engine->entrance_scene(lcd, gamepad);
+}
+
+void room_exit()
+{
+    room_engine->exit_scene(lcd, gamepad);
+    rooms[room_y][room_x]->unload();
+    player->delete_bullets();
+    room_engine->update_current_room();
 }
 
 void generate_room()
@@ -129,6 +115,124 @@
     room_x = room_engine->get_room_x();
 }
 
+void minimap_detection()
+{
+    while(gamepad.check_event(Gamepad::BACK_PRESSED)) {
+        lcd.clear();
+        for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+            for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
+                if (valid_rooms[j][i]) {
+                    lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]);
+                    if (rooms[j][i]->get_room_type() == 10) {
+                        lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]);
+                    } else if (rooms[j][i]->enemies_exist()) {
+                        lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]);
+                    }
+                    if (rooms[j][i]->get_doorway(0)) {
+                        lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
+                    }
+                    if (rooms[j][i]->get_doorway(1)) {
+                        lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+                    }
+                    if (rooms[j][i]->get_doorway(2)) {
+                        lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
+                    }
+                    if (rooms[j][i]->get_doorway(3)) {
+                        lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
+                    }
+                }
+            }
+        }
+        lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]);
+        lcd.refresh();
+        wait_ms(1000/40);
+    };
+}
+
+void game_over()
+{
+    lcd.clear();
+    lcd.setContrast(global_contrast);
+    lcd.printString("Game Over", 0, 0);
+    lcd.printString("Retry?", 0, 1);        
+}
+
+void win()
+{
+    lcd.clear();
+    lcd.setContrast(global_contrast);
+    lcd.printString("You won!", 0, 0);
+}
+
+void display_stats()
+{
+    lcd.printString("Enemies Killed:", 0, 2);
+    char kills[10];
+    sprintf(kills, "%d enemies", number_of_enemies_killed);
+    lcd.printString(kills, 0, 3);
+    lcd.printString("Time:", 0, 4);
+    char total_duration[10];
+    sprintf(total_duration, "%d seconds", (total_time/40));
+    lcd.printString(total_duration, 0, 5);
+    lcd.refresh();
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {
+    }
+    wait(0.05);
+    while(!gamepad.check_event(Gamepad::A_PRESSED)) {
+    }
+    wait(0.05);
+    while(gamepad.check_event(Gamepad::A_PRESSED)) {
+    }
+}
+
+int available_boss_room()
+{
+    if (!valid_rooms[room_y - 1][room_x]){
+        rooms[room_y][room_x]->set_doorway(0, true);
+        valid_rooms[room_y - 1][room_x] = true;
+        rooms[room_y - 1][room_x] = new Room(0, 10);
+        rooms[room_y - 1][room_x]->set_doorway(2, true);
+        return 0;
+    } else if (!valid_rooms[room_y][room_x + 1]){
+        rooms[room_y][room_x]->set_doorway(1, true);
+        valid_rooms[room_y][room_x + 1] = true;
+        rooms[room_y][room_x + 1] = new Room(0, 10);
+        rooms[room_y][room_x + 1]->set_doorway(3, true);
+        return 1;
+    } else if (!valid_rooms[room_y + 1][room_x]){
+        rooms[room_y][room_x]->set_doorway(2, true);
+        valid_rooms[room_y + 1][room_x] = true;
+        rooms[room_y + 1][room_x] = new Room(0, 10);
+        rooms[room_y + 1][room_x]->set_doorway(0, true);
+        return 2;
+    } else if (!valid_rooms[room_y][room_x - 1]){
+        rooms[room_y][room_x]->set_doorway(3, true);
+        valid_rooms[room_y][room_x - 1] = true;
+        rooms[room_y][room_x - 1] = new Room(0, 10);
+        rooms[room_y][room_x - 1]->set_doorway(1, true);
+        return 3;
+    }
+    delete rooms[room_y - 1][room_x];
+    rooms[room_y - 1][room_x] = new Room(0, 10);
+    rooms[room_y - 1][room_x]->set_doorway(2, true);
+    return 0;
+}
+
+void game_unload()
+{
+    for (int i = 0; i < MAX_ROOMS_MAP_X; i++) { 
+            for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+                if (valid_rooms[j][j]){
+                    delete rooms[j][i];
+                    valid_rooms[i][j] = false;
+                }
+            }
+        }
+        delete room_engine;
+        delete player;
+}
+
+// Functions
 int opposite(int value)
 {
     if (value <= 1) {
@@ -218,107 +322,4 @@
         have_to[3] = false;
         cannot[3] = false;
     }
-}
-
-int available_boss_room()
-{
-    if (!valid_rooms[room_y - 1][room_x]){
-        rooms[room_y][room_x]->set_doorway(0, true);
-        valid_rooms[room_y - 1][room_x] = true;
-        rooms[room_y - 1][room_x] = new Room(0, 10);
-        rooms[room_y - 1][room_x]->set_doorway(2, true);
-        return 0;
-    } else if (!valid_rooms[room_y][room_x + 1]){
-        rooms[room_y][room_x]->set_doorway(1, true);
-        valid_rooms[room_y][room_x + 1] = true;
-        rooms[room_y][room_x + 1] = new Room(0, 10);
-        rooms[room_y][room_x + 1]->set_doorway(3, true);
-        return 1;
-    } else if (!valid_rooms[room_y + 1][room_x]){
-        rooms[room_y][room_x]->set_doorway(2, true);
-        valid_rooms[room_y + 1][room_x] = true;
-        rooms[room_y + 1][room_x] = new Room(0, 10);
-        rooms[room_y + 1][room_x]->set_doorway(0, true);
-        return 2;
-    } else if (!valid_rooms[room_y][room_x - 1]){
-        rooms[room_y][room_x]->set_doorway(3, true);
-        valid_rooms[room_y][room_x - 1] = true;
-        rooms[room_y][room_x - 1] = new Room(0, 10);
-        rooms[room_y][room_x - 1]->set_doorway(1, true);
-        return 3;
-    }
-    delete rooms[room_y - 1][room_x];
-    rooms[room_y - 1][room_x] = new Room(0, 10);
-    rooms[room_y - 1][room_x]->set_doorway(2, true);
-    return 0;
-}
-
-void minimap_detection()
-{
-    while(gamepad.check_event(Gamepad::BACK_PRESSED)) {
-        lcd.clear();
-        for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
-            for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
-                if (valid_rooms[j][i]) {
-                    lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[0]);
-                    if (rooms[j][i]->get_room_type() == 10) {
-                        lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[2]);
-                    } else if (rooms[j][i]->enemies_exist()) {
-                        lcd.drawSprite(33 + (i-room_x)*20, 17 + (j-room_y)*15, 15, 20, (char *)minimap_sprite[1]);
-                    }
-                    if (rooms[j][i]->get_doorway(0)) {
-                        lcd.drawLine(42 + (i-room_x)*20, 17 + (j-room_y)*15, 43 + (i-room_x)*20, 17 + (j-room_y)*15, 1);
-                    }
-                    if (rooms[j][i]->get_doorway(1)) {
-                        lcd.drawLine(52 + (i-room_x)*20, 23 + (j-room_y)*15, 52 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
-                    }
-                    if (rooms[j][i]->get_doorway(2)) {
-                        lcd.drawLine(42 + (i-room_x)*20, 31 + (j-room_y)*15, 43 + (i-room_x)*20, 31 + (j-room_y)*15, 1);
-                    }
-                    if (rooms[j][i]->get_doorway(3)) {
-                        lcd.drawLine(33 + (i-room_x)*20, 23 + (j-room_y)*15, 33 + (i-room_x)*20, 24 + (j-room_y)*15, 1);
-                    }
-                }
-            }
-        }
-        lcd.drawSpriteTransparent(33, 17, 15, 20, (char *)minimap_sprite[3]);
-        lcd.refresh();
-        wait_ms(1000/40);
-    };
-}
-
-void game_over()
-{
-    lcd.clear();
-    lcd.setContrast(global_contrast);
-    lcd.printString("Game Over", 0, 0);
-    lcd.printString("Retry?", 0, 1);        
-}
-
-void win()
-{
-    lcd.clear();
-    lcd.setContrast(global_contrast);
-    lcd.printString("You won!", 0, 0);
-}
-
-void display_stats()
-{
-    lcd.printString("Enemies Killed:", 0, 2);
-    char kills[10];
-    sprintf(kills, "%d enemies", number_of_enemies_killed);
-    lcd.printString(kills, 0, 3);
-    lcd.printString("Time:", 0, 4);
-    char total_duration[10];
-    sprintf(total_duration, "%d seconds", (total_time/40));
-    lcd.printString(total_duration, 0, 5);
-    lcd.refresh();
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-    wait(0.05);
-    while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-    wait(0.05);
-    while(gamepad.check_event(Gamepad::A_PRESSED)) {
-    }
-}
+}
\ No newline at end of file
--- a/main.h	Thu May 09 01:57:49 2019 +0000
+++ b/main.h	Thu May 09 02:46:44 2019 +0000
@@ -43,16 +43,21 @@
 // Prototypes
 void init();
 void game_loop();
+
+void room_entrance();
+void room_exit();
+void generate_room();
+void update_room_coords();
+void minimap_detection();
+
 void game_over();
 void win();
 void display_stats();
-void generate_room();
-void update_room_coords();
+void game_unload();
 
 int opposite(int value);
 int count_doorways();
 void update_definite_doorways();
 int available_boss_room();
-void minimap_detection();
 
 #endif
\ No newline at end of file