ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Revision:
30:91038c2afec7
Parent:
29:4a02f0bae202
Child:
31:e681177037ef
--- a/Game/Game.cpp	Thu Apr 04 22:04:10 2019 +0000
+++ b/Game/Game.cpp	Fri Apr 05 13:54:34 2019 +0000
@@ -1,4 +1,6 @@
 #include "Game.h"
+#include "SDFileSystem.h"
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
 
 Serial pc(USBTX, USBRX); // tx, rx
 
@@ -8,9 +10,14 @@
     gamepad.init();
     renderer.init();
     resetScene();
-    help = -1;
-    input.x = 0;
-    input.bCooldown = input.yCooldown = input.aCooldown = false;
+    help = -1; //Set help screen to inactive (-1)
+    input.x = 0; //Set joystick input to 0
+    input.bCooldown = input.yCooldown = input.aCooldown = false; //Allow buttons to be pressed
+    deathMenuSelection = true; //Set death menu to highlight restart option
+    //filePointer = fopen("/sd/topscore.txt", "w");
+    //fprintf(filePointer, "%i", score);
+    pc.printf("a");
+    highScore = readHighScore();
 }
 
 void Game::resetScene(){
@@ -19,37 +26,36 @@
     }
 }
 
-int Game::readHighScore(){
-//    filePointer = fopen("/sd/highscore.txt", "r");
+int Game::readHighScore(){    
+    int highScore = 0;
+    filePointer = fopen("/sd/el17cdHighScore.txt", "r");
+
     if (filePointer != NULL) {
-        highScore = fscanf(filePointer, "%i");
+        fscanf(filePointer, "%d", &highScore); //read integer from sd card file
+        fclose(filePointer);
     }
-    else {
-        highScore = 0;
-    }
-    return highScore;
-    pc.printf("%i", highScore);
+    return highScore;;
 }
 
 void Game::writeHighScore(int score){
- //   filePointer = fopen("/sd/highscore.txt", "w");
+    filePointer = fopen("/sd/el17cdHighScore.txt", "w");
+
     if (filePointer != NULL) {
-        fprintf(filePointer, "%i", score);
+        fprintf(filePointer, "%d", score); //write integer to sd card file
+        fclose(filePointer);
     }
 }
 
 
 void Game::run(){
-    pc.printf("in");
-    inHomeMenu = true;
+    inHomeMenu = true; //Start at home screen
     score = 0;
-    deathMenuSelection = true;
     playing = true; //set game state to playing and death screen selection to 'restart'
     while(true) {
-        processInput();
+        processInput(); //process user input and store values in input struct
         renderer.clear();
         if(help > -1){
-            helpScreen();
+            helpScreen(); //Show the help screen if the help screen number is above -1
         }
         else if(inHomeMenu){
             homeScreen();
@@ -100,7 +106,7 @@
 
 void Game::moveCubes(Cube *cube){ //animate cubes
     if(playing){
-        if(score < 2000)
+        if(score < 6000)
             cube->translate(-input.x*1.4f,0,-1-(float)score/1000); //move cubes closer to user and in x axis depending on joystick pos
         else{
             cube->translate(-input.x*1.4f,0,-3); //once max speed reached maintain constant z speed
@@ -161,6 +167,7 @@
         }
         if (input.bButton && homeSelection == 0){ //if top highlighted and b pressed then start game
             inHomeMenu = false;
+            playing = true;
         }
         else if(input.bButton && homeSelection == 1){ //if bottom highlighted and b pressed then exit game
             help = 0;
@@ -171,43 +178,37 @@
         }
 }
 
-void Game::homeScreen(){
+void Game::homeScreen(){//draw home screen
     homeButtonSelections(); //determine selection on home screen
-    if(help == -1) {
-        renderer.drawHomeScreen(homeSelection); //draw home screen
-    }
-    else{
-        helpScreen();
-    }
+    renderer.drawHomeScreen(homeSelection);
 }
 
-void Game::helpScreen(){
+void Game::helpScreen(){ //Show help screen depending on help screen number
     if (help < 2) {
         renderer.drawHelpScreen1();
         checkNextHelpScreen();
     }
     else if (help == 2) {
-        renderer.drawHelpScreen2();   //execution starts at this case label
+        renderer.drawHelpScreen2();
         checkNextHelpScreen();
     }
     else if (help == 3) {
         renderer.drawHelpScreen3();
         checkNextHelpScreen();
     }
-    else if (help == 4) {
+    else if (help == 4) { //if past last screen then set help screen number to disabled (-1)
         help = -1;
     }
 }
 
-void Game::checkNextHelpScreen(){
+void Game::checkNextHelpScreen(){ //if a button pressed then advance help screen
     if(input.bButton){
         help ++;
-                pc.printf("%i\r\n", help);
     }
 }
 
-void Game::processInput(){
-    input.x = gamepad.get_coord().x;
+void Game::processInput(){ //Obtain user inputs and store in input struct
+    input.x = gamepad.get_coord().x; //Get value of joystick x axis
     bool y = gamepad.check_event(Gamepad::Y_PRESSED);
     bool a = gamepad.check_event(Gamepad::A_PRESSED);
     bool b = gamepad.check_event(Gamepad::B_PRESSED);
@@ -217,10 +218,10 @@
     disableBButton(b);
 }
 
-void Game::disableYButton(bool y){
+void Game::disableYButton(bool y){ //Set y button to disabled, call function to reenable in 0.3 seconds
     if(!input.yCooldown && y){
         input.yCooldown = true;
-        disableY.attach(this, &Game::enableY, 0.3);
+        disableY.attach(callback(this, &Game::enableY), 0.3); //attach function to ticker to renable y button
         input.yButton = true;
     }
     else{
@@ -228,10 +229,10 @@
     }
 }
 
-void Game::disableAButton(bool a){
+void Game::disableAButton(bool a){ //Set a button to disabled, call function to reenable in 0.3 seconds
     if(!input.aCooldown && a){
         input.aCooldown = true;
-        disableA.attach(this, &Game::enableA, 0.3);
+        disableA.attach(callback(this, &Game::enableA), 0.3); //attach function to ticker to renable a button
         input.aButton = true;
     }
     else{
@@ -239,10 +240,10 @@
     }    
 }
 
-void Game::disableBButton(bool b){
+void Game::disableBButton(bool b){ //Set b button to disabled, call function to reenable in 0.3 seconds
     if(!input.bCooldown && b){
         input.bCooldown = true;
-        disableB.attach(this, &Game::enableB, 0.3);
+        disableB.attach(callback(this, &Game::enableB), 0.3); //attach function to ticker to renable b button
         input.bButton = true;
     }
     else{
@@ -250,17 +251,17 @@
     }
 }
 
-void Game::enableA(){
-    disableA.detach();
+void Game::enableY(){ //reenable y button
+    disableY.detach(); //detach ticker so function only runs once
+    input.yCooldown = false;
+}
+
+void Game::enableA(){ //reenable a button
+    disableA.detach(); //detach ticker so function only runs once
     input.aCooldown = false;
 }
 
-void Game::enableB(){
-    disableB.detach();
+void Game::enableB(){ //reenable b button
+    disableB.detach(); //detach ticker so function only runs once
     input.bCooldown = false;
 }
-
-void Game::enableY(){
-    disableY.detach();
-    input.yCooldown = false;
-}
\ No newline at end of file