Faizan and Pages fun little game

Dependencies:   4DGL-uLCD-SE mbed SDFileSystem wave_player

Revision:
3:058e10b8ecf6
Parent:
2:4c5f409d6bb8
Child:
4:c00da0b158c4
--- a/main.cpp	Mon Oct 31 00:59:12 2016 +0000
+++ b/main.cpp	Mon Oct 31 02:53:59 2016 +0000
@@ -4,6 +4,7 @@
 #include "stack.h"
 #include <ctime>
 #include <cstdlib>
+#include <cmath>  
 #include <vector>
 
 #define BREAD 1
@@ -16,13 +17,16 @@
 
 uLCD_4DGL lcd(p9,p10,p11);
 vector<Food> foods;
-Food collided(&lcd);
+Food * collided;
+
+using namespace std;
 Stack sandwich(64,&lcd);
 DigitalIn right(p19);
 DigitalIn left(p20);
 int score, lives;
 float clk, clk2;
 Timer t;
+int indexToRemove;
 
 bool collisionCheck();
 
@@ -36,7 +40,7 @@
     srand(time(NULL));
     clk = clk2 = 0;
     
-    lcd.line(1, 112, 126, 112, WHITE);
+    lcd.line(120, 1, 120, 128, WHITE);
     
     while(lives) {
         if (!left) {
@@ -45,12 +49,20 @@
             sandwich.move(+1);
         }
         if (collisionCheck()) {
-            // if the collided object is bad 
-                // loss of life
-            // if the collided object is bread
-                // close up the sandwich, add the points to the overall score, re draw
-            // else
-                // add to stack, remove from falling object list, redraw
+            collided->erase();
+            if (collided->isBad) {
+                lives--;
+                // lifeLost()
+                // make a noise!1111
+            } else if (collided->typeOfFood == BREAD) {
+                // updateScore(sandwich.size()); // add the points to the overall score
+                sandwich.clear(); // should redraw
+                // make a noise!!11
+            } else {
+                // remove from falling object list, erase
+                foods.erase(foods.begin() + indexToRemove);
+                sandwich.add(collided);
+            }
         }
         // every 3 seconds add new food! use timer.
         float curr = t.read();
@@ -79,15 +91,25 @@
 }
 
 bool collisionCheck() {
-    
-    
-    // remove foods from vector that are off screen
+    if (!foods.empty()) {
+        Food * f = &foods.front();
+        if (f->y > 128) foods.erase(foods.begin()); // removed if offscreen!
+    }
+    for (int i = 0; i < foods.size(); i++) {
+        if (foods[i].y + 6 == sandwich.top) {
+            if (abs(foods[i].x - sandwich.x) < 8) {
+                collided = &foods[i];
+                indexToRemove = i;
+                return true;
+            } else break;
+        }
+    }
+    return false;
     // find foods that have an (x,y) that is within the range of the top of the stack
     // if nothing found w/i the range
         // return false
     // if something found
         // collidedFood = that food item
         // return true
-    return NULL;
 }