ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18gs

Dependencies:   mbed

Revision:
16:3b298bea3a70
Parent:
13:3b2a4e14937b
diff -r 598baed15751 -r 3b298bea3a70 Inventory/Inventory.cpp
--- a/Inventory/Inventory.cpp	Wed May 13 10:19:10 2020 +0000
+++ b/Inventory/Inventory.cpp	Tue May 26 12:17:52 2020 +0000
@@ -1,51 +1,56 @@
 #include "Inventory.h"
 
-Inventory::Inventory(SDFileSystem &sd){
+Inventory::Inventory(SDFileSystem &sd)
+{
     printf("creating inventory\n");
     stringvec ghost_list = list_ghosts("/sd/ghosts", sd);
-    
-    for(int i = 0; i < ghost_list.size(); i++){
-        
+
+    for(int i = 0; i < ghost_list.size(); i++) {
+
         std::string path = "/sd/ghosts/";
         path.append(ghost_list[i]);
         printf("Path to import '%s'\n", path.c_str());
-        
+
         Ghost temp(path, "/sd/ghosts/", sd);
         _ghosts.push_back(temp);
     }
+    _gold = 0;
 }
 
-void Inventory::regen(SDFileSystem &sd){
+void Inventory::regen(SDFileSystem &sd)
+{
     printf("Regenerating ghost list\n");
     _ghosts.clear();
-    
+
     stringvec ghost_list = list_ghosts("/sd/ghosts", sd);
-    
-    for(int i = 0; i < ghost_list.size(); i++){
-        
+
+    for(int i = 0; i < ghost_list.size(); i++) {
+
         std::string path = "/sd/ghosts/";
         path.append(ghost_list[i]);
         printf("Path to import '%s'\n", path.c_str());
-        
+
         Ghost temp(path, "/sd/ghosts/", sd);
         _ghosts.push_back(temp);
     }
-    
+
 }
 
-std::vector<int> Inventory::list_ghost_uids(void){
+std::vector<int> Inventory::list_ghost_uids(void)
+{
     std::vector<int> uids;
-    for(int i = 0; i < _ghosts.size(); i++){
+    for(int i = 0; i < _ghosts.size(); i++) {
         uids.push_back(_ghosts[i].get_uid());
         //printf("Added UID %i to list\n", _ghosts[i].get_uid());
     }
-    
+
     return uids;
 }
 
-Ghost Inventory::get_ghost_by_uid(int uid){
-    for(int i = 0; i < _ghosts.size(); i++){
-        if(_ghosts[i].get_uid() == uid){
+Ghost Inventory::get_ghost_by_uid(int uid)
+{
+    for(int i = 0; i < _ghosts.size(); i++) {
+        if(_ghosts[i].get_uid() == uid) {
             return _ghosts[i];
         }
     }
@@ -72,45 +77,67 @@
         printf("Could not open directory!\n");
     }
     closedir(d);
-    
+
     stringvec correct_files;
-    
-    for(int i = 0; i < files.size(); i++){
-        if(hasEnding(files[i], ".ghost")){
+
+    for(int i = 0; i < files.size(); i++) {
+        if(hasEnding(files[i], ".ghost")) {
             correct_files.push_back(files[i]);
         }
     }
-    
+
     return correct_files;
 }
 
-bool Inventory::hasEnding (std::string const &fullString, std::string const &ending) {
-    if (fullString.length() >= ending.length()) {
-        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
-    } else {
-        return false;
-    }
-}
-
-void Inventory::sell_ghost_by_uid(int uid, SDFileSystem &sd){
+void Inventory::sell_ghost_by_uid(int uid, SDFileSystem &sd)
+{
     printf("Inside sell function\n");
-    for(int i = 0; i < _ghosts.size(); i++){
-        if(_ghosts[i].get_uid() == uid){
+    for(int i = 0; i < _ghosts.size(); i++) {
+        if(_ghosts[i].get_uid() == uid) {
             printf("Sale target found\n");
             int value = _ghosts[i].sell(sd);
             _ghosts.erase(_ghosts.begin() + i);
             _gold = _gold + value;
+            if(_gold > 99) {
+                _gold = 99;
+            }
             break;
         }
-        
+
     }
 }
 
-void Inventory::display_inventory(SDFileSystem &sd, 
-                                    N5110 &lcd, 
-                                    Gamepad &pad,
-                                    volatile int &g_buttonX_flag,
-                                    volatile int &g_buttonStart_flag)
+void Inventory::feed_ghost_by_uid(int uid, SDFileSystem &sd)
+{
+    printf("Inside feed function\n");
+    if(_gold < 10) { return; }
+    for(int i = 0; i < _ghosts.size(); i++) {
+        if(_ghosts[i].get_uid() == uid) {
+            printf("Feed target found\n");
+            _ghosts[i].feed(10, sd);
+            _gold = _gold - 10;
+            break;
+        }
+
+    }
+}
+
+void Inventory::print_coin(SDFileSystem &sd, N5110 &lcd)
+{
+    int** ghost = import_sprite("/sd/assets/coin.sprite", sd);
+    for(int i = 0; i < 10; i++) { // Iterate Columns: x
+        for(int j = 0; j < 10; j++) { // Iterate Rows: y
+            lcd.setPixel(i + 74, j, ghost[j][i]);
+        }
+    }
+}
+
+void Inventory::display_inventory(SDFileSystem &sd,
+                                  N5110 &lcd,
+                                  Gamepad &pad,
+                                  volatile int &g_buttonX_flag,
+                                  volatile int &g_buttonStart_flag,
+                                  volatile int &g_buttonA_flag)
 {
     int which_ghost_state = 0;
     int which_view_state = 0;
@@ -140,9 +167,11 @@
             }
 
             lcd.clear();
+            char buffer [64];
+            sprintf(buffer, "Inventory:%d", _gold);
+            lcd.printString(buffer, 0, 0);
 
-            lcd.printString("Inventory", 0, 0);
-            char buffer [64];
+            print_coin(sd, lcd);
 
             if(ghost_fsm.size() == 0) {
                 lcd.printString("No ghosts", 0, 1);
@@ -264,7 +293,6 @@
                 if(g_buttonX_flag) {
                     printf("button X pressed\n");
                     g_buttonX_flag = 0;
-                    //wait_ms(50);
                     sell = true;
                 }
 
@@ -276,7 +304,6 @@
                     printf("Exiting to sell\n");
                     break;
                 }
-
             }
 
             if(sell) {
@@ -284,12 +311,17 @@
                 sell_ghost_by_uid(ghost_fsm[which_ghost_state].uid, sd);
                 regen_inven = true;
                 update = true;
-                //wait_ms(500);
             }
 
+
         } else if(g_buttonStart_flag) {
             g_buttonStart_flag = 0;
             return;
+        } else if(g_buttonA_flag == 1) {
+            g_buttonA_flag = 0;
+            feed_ghost_by_uid(ghost_fsm[which_ghost_state].uid, sd);
+            regen_inven = true;
+            update = true;
         }
 
     }