Armageddon - a missile commad clone by Wuuff, originally for Gamebuino. Conversion by Jonne

Dependencies:   PokittoLib

Fork of Asterocks by Pokitto Community Team

Revision:
21:153974d672f8
Parent:
15:933ac6bcd8b8
--- a/ageddon.cpp	Wed May 02 06:19:23 2018 +0000
+++ b/ageddon.cpp	Sun Jul 01 06:33:02 2018 +0000
@@ -3,7 +3,30 @@
 /* USE AT YOUR OWN RISK - NO GUARANTEES GIVEN OF 100% CORRECTNESS */
 
 #include "PokittoCore.h"
-#include "PokittoEEPROM.h"
+#include "PokittoCookie.h"
+
+#define NUM_HIGHSCORES 5
+#define NAME_SIZE 10
+#define ENTRY_SIZE 15
+
+/* Writing directly to EEPROM is deprecated */
+/*
+uint32_t highscores[NUM_HIGHSCORES] = {10000,7000,5000,4000,2000};
+char names[NUM_HIGHSCORES][NAME_SIZE+1] = {"Crack Shot", "Defender", "Gunner", "We Tried", "No Hope"};
+*/
+
+/* This is how you do it with the PokittoCookie class !!!*/
+class gdata : public Pokitto::Cookie {
+    public:
+    uint32_t highscores[NUM_HIGHSCORES];
+    char names[NUM_HIGHSCORES][NAME_SIZE+1];
+    int check_number;
+    gdata() {
+    }
+};
+
+gdata gamedata;
+
 /* Auto-generated function declarations */
 void setup();
 void nextStage();
@@ -34,7 +57,10 @@
 void drawHighscores();
 void initSound();
 void playSound(uint8_t);
-Pokitto::Core gb;
+
+
+Pokitto::Core gb; // game object
+
 
 int main() {
     setup();
@@ -254,7 +280,7 @@
       if( counter%8 == 0 ){
         flash ^= 255;
       }
-      
+
       if( flash ){
         gb.display.setColor(4);
         gb.display.cursorY = HEIGHT - TEXT_HEIGHT*2;
@@ -269,17 +295,19 @@
       counter++;
       if( gb.buttons.pressed(BTN_A) ){
         gb.display.setColor(WHITE);
-        break;    
+        break;
       }
-    }    
+    }
   }
 }
 
 void setup() {
+  loadHighscores(); // do this before gb.begin !!
   gb.begin();
-  loadHighscores();
-  
-  //We need to do extra Gamebuino-formatting-specific initialization if we don't use the title screen 
+
+
+
+  //We need to do extra Gamebuino-formatting-specific initialization if we don't use the title screen
   gb.display.setFont(font5x7);
   gb.display.fontSize = 1;
   gb.display.textWrap = false;
@@ -287,7 +315,7 @@
   gb.battery.show = false;
   gb.display.setColor(BLACK);
   gb.display.setColorDepth(1);
-  
+
   doTitle(0);
   gb.pickRandomSeed();
 }
@@ -439,7 +467,7 @@
   gb.display.setColor(explosionColor+1);
   if(counter%2 == 0){
     explosionColor++;
-    explosionColor%=7;    
+    explosionColor%=7;
   }
   for(uint8_t i = 0; i < MAX_PMISSILES; i++){
     if( pDetonations[i][0] <= WIDTH ){
@@ -494,7 +522,7 @@
       for(uint8_t i = 0; i < MAX_EMISSILES; i++){
         if( eDests[i] > WIDTH ){
           etotal--;
-          eDests[i] = random(7); //Target one of the 6 cities or 2 launch sites TODO: Come back later if random's behavior changes
+          eDests[i] = random(8); //Target one of the 6 cities or 2 launch sites TODO: Come back later if random's behavior changes
           eMissiles[i][0] = random(WIDTH); //Screen width //TODO: if random is supposed to be inclusive, add a -1
           eMissiles[i][1] = 0; //Top of screen
           eMissiles[i][2] = eMissiles[i][0]; //Start and end are same
@@ -514,7 +542,7 @@
     //Check for a valid destination without a current detonation
     if( pDests[i][0] <= WIDTH && pDetonations[i][0] > WIDTH ){
       //If the missile is close enough to the destination, detonate
-      if( abs( pDests[i][0] - pMissiles[i][0] ) < PSPEED && abs( pDests[i][1] - pMissiles[i][1] ) < PSPEED ){
+      if( abs( int(pDests[i][0] - pMissiles[i][0]) ) < PSPEED && abs( int(pDests[i][1] - pMissiles[i][1]) ) < PSPEED ){
         pDetonations[i][0] = pDests[i][0];
         pDetonations[i][1] = pDests[i][1];
         pDetonations[i][2] = 0; //Start detonation at radius of 0
@@ -533,7 +561,7 @@
     //Check for a valid destination
     if( eDests[i] <= WIDTH ){
        //If enemy missile is close enough to the destination, detonate
-      if( abs( (eDests[i]*14+6) - eMissiles[i][2] ) < PSPEED && abs( HEIGHT-4 - eMissiles[i][3] ) < PSPEED ){
+      if( abs( int((eDests[i]*14+6) - eMissiles[i][2]) ) < PSPEED && abs( int(HEIGHT-4 - eMissiles[i][3]) ) < PSPEED ){
         cities[eDests[i]] = 0; //Destroy city/launcher
 
         //If launcher, remove its ammo
@@ -831,14 +859,26 @@
     counter++;
   }
 }
-#define NUM_HIGHSCORES 5
-#define NAME_SIZE 10
-#define ENTRY_SIZE 15
 
-uint32_t highscores[NUM_HIGHSCORES] = {10000,7000,5000,4000,2000};
-char names[NUM_HIGHSCORES][NAME_SIZE+1] = {"Crack Shot", "Defender", "Gunner", "We Tried", "No Hope"};
 
 void loadHighscores(){
+
+  uint32_t defdata[] = {10000,7000,5000,4000,2000};
+  char defnames[NUM_HIGHSCORES][NAME_SIZE+1] = {"Crack Shot", "Defender", "Gunner", "We Tried", "No Hope"};
+  // Initialize the cookie
+  gamedata.begin("ArmageDD",gamedata);
+
+  uint32_t allscores=0;
+  for (int i=0;i<NUM_HIGHSCORES;i++) allscores += gamedata.highscores[i];
+
+  if (gamedata.check_number != 12345) {
+        //checknumber indicates that no highscores saved yet, so put in "defaults"
+        for (int i=0;i<NUM_HIGHSCORES;i++) gamedata.highscores[i]=defdata[i];
+        for (int i=0;i<NUM_HIGHSCORES;i++) strcpy(gamedata.names[i],defnames[i]);
+        gamedata.check_number = 12345; // put the checking number
+        gamedata.saveCookie();//save the default values
+  }
+  /*
   for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
     for( uint8_t offset = 0; offset < ENTRY_SIZE; offset++ ){
       if( offset < NAME_SIZE ){
@@ -850,10 +890,12 @@
       }
     }
   }
+  */
+
 }
 
 uint8_t isHighscore(uint32_t score){
-  if( score > highscores[NUM_HIGHSCORES-1] ){
+  if( score > gamedata.highscores[NUM_HIGHSCORES-1] ){
     return 1;
   }
   return 0;
@@ -864,20 +906,22 @@
   uint32_t tmp_score = 0;
   char tmp_name[10];
   for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
-    if( score > highscores[entry] ){
+    if( score > gamedata.highscores[entry] ){
       found = 1;
     }
     if( found ){
-      tmp_score = highscores[entry];
-      strcpy(tmp_name,names[entry]);
+      tmp_score = gamedata.highscores[entry];
+      strcpy(tmp_name,gamedata.names[entry]);
 
-      highscores[entry] = score;
-      strcpy(names[entry], who);
+      gamedata.highscores[entry] = score;
+      strcpy(gamedata.names[entry], who);
 
       score = tmp_score;
       strcpy(who, tmp_name);
     }
   }
+  gamedata.saveCookie();
+/*Writing to EEPROM directly is deprecated and not necessary
   for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
     for( uint8_t offset = 0; offset < ENTRY_SIZE; offset++ ){
       if( offset < NAME_SIZE ){
@@ -889,6 +933,7 @@
       }
     }
   }
+  */
 }
 
 void drawHighscores(){
@@ -901,24 +946,24 @@
   for( uint8_t entry = 0; entry < NUM_HIGHSCORES; entry++ ){
     gb.display.cursorX = 0;
     gb.display.cursorY = TEXT_HEIGHT*3 + TEXT_HEIGHT*entry;
-    gb.display.print(names[entry]);
+    gb.display.print(gamedata.names[entry]);
     gb.display.cursorX = WIDTH - TEXT_WIDTH*6;
-    if( highscores[entry] < 100000 ){
+    if( gamedata.highscores[entry] < 100000 ){
       gb.display.print(0);
     }
-    if( highscores[entry] < 10000 ){
+    if( gamedata.highscores[entry] < 10000 ){
       gb.display.print(0);
     }
-    if( highscores[entry] < 1000 ){
+    if( gamedata.highscores[entry] < 1000 ){
       gb.display.print(0);
     }
-    if( highscores[entry] < 100 ){
+    if( gamedata.highscores[entry] < 100 ){
       gb.display.print(0);
     }
-    if( highscores[entry] < 10 ){
+    if( gamedata.highscores[entry] < 10 ){
       gb.display.print(0);
     }
-    gb.display.print(highscores[entry]);
+    gb.display.print(gamedata.highscores[entry]);
   }
 }