ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19tb

Dependencies:   mbed

Revision:
19:6d9721ffc078
Parent:
18:6be4c3c94a3d
Child:
20:077f845f09f2
diff -r 6be4c3c94a3d -r 6d9721ffc078 CrossyChicken/CrossyChicken.cpp
--- a/CrossyChicken/CrossyChicken.cpp	Thu May 14 03:53:16 2020 +0000
+++ b/CrossyChicken/CrossyChicken.cpp	Thu May 14 05:52:50 2020 +0000
@@ -207,9 +207,44 @@
     } 
 }
 
-
+void CrossyChicken::createMultipleSafetyLane()
+{
+    int min = 0; // start from the top, and draw till 0 
+    int max = grid_width; // draw 21 objects to the first lane
+    int row = 1;
+    int x_fac = 1;
+    
+    for(int x = 0; x < 2; x++){
+        creatSafetyObject(min, max, row, x_fac);
+        row += 6; // increment the rows to the next lane
+        min +=  22; // fill in the vector with 22 grid units
+        max += 22;
+    }      
+}
 
+void CrossyChicken::createSafetyObject(int min, int max, int row, int x_fac)
+{
+    int state = 1;
+    
+    for(int z = 0; z < 22; z++){
+        switch(state)
+        {
+            case 1:
+                safety_lane.push_back(z/x_fac, row, 'K'); // safety lane so row 0
+                state++;
+                break;
+            case 2: 
+                safety_lane.push_back(z/x_fac, row, 'P'); // safety lane so row 0
+                state--; // back to inital state
+                break;
+        }
+    }    
+}
 
+void CrossyChicken::drawSafety()
+{
+    graphics.drawSafetyLanes(safety_lane);   
+}
 
 
 void CrossyChicken::createMultipleRoadLane()
@@ -242,85 +277,79 @@
     graphics.getRoadObjects(roads);
 }
 
-void CrossyChicken::createTwoSafetyLanes()
+void CrossyChicken::createMultipleLanesWater()
+{ 
+    int min = 0; // start from the top, and draw till 0 
+    int max = grid_width; // draw 21 objects to the first lane
+    int row = 7;
+    int x_fac = 1;
+    
+    for(int x = 0; x < 3; x++){
+        if(x == 1){ 
+            createWaterLane(min, max, row, -0.6); // second row goes left
+        } else {
+            createWaterLane(min, max, row, 0.4); // rest of the rows go right
+        }
+        
+        row++; // increment the rows to the next lane
+        min += grid_width; // fill in the vector with 22 grid units
+        max += grid_width;
+    }    
+}
+
+void CrossyChicken::createWaterLane(int min, int max, int row, float speed, int x_fac){
+    int state = 1;    
+    for(it = min; it < max; it++)
+    {
+        switch(state)
+        {
+            case 1: // initial state
+                water[it].push_back(it/x_fac, row, speed, 'F'); // first type of water
+                state++; // transition to next state
+                break;
+            case 2: 
+                water[it].push_back(it/x_fac, row, speed, 'S'); // first type of water
+                state++; // transition to state 3
+                break;       
+            case 3:
+                water[it].push_back(it/x_fac, row, speed, 'T'); // first type of water
+                state = 1; // back to start state
+                break;
+        } 
+    }   
+}
+
+// moves the water based on the velocity
+void CrossyChicken::moveWater(std::vector<Water>& water_lanes)
 {
-    int min = 0;
-    int max = grid_width;
-    int row = 0; // first starting lane is at row 0
+    for(unsigned int i = 0; i < water_lanes.size(); i++)
+    {
+        if(water_lanes(i).speed > 0){
+            water_lanes(i).x += speed;   
+        } else {
+            water_lanes(i).x -= speed;   
+        }
+    }
     
-    // there are going to be two lanes
-    for(int i = 0; i < 2; i++){
-        drawSafetyLanes(min+1, max+1, row);
+    loopWater(water_lanes); 
+}
+
+
+void CrossyChicken::loopWater(std::vector<Water>& water_lanes)
+{ 
+    for(unsigned int i = 0; i < water_lanes.size(); i++)
+    {
+        if(water_lanes(i).x > 84+grid){
+            water_lanes(i).x = -4;   
+        } else if(water_lanes[i].x < -4){
+            water_lanes(i).x = 84 + grid;   
+        }
     }
 }
 
-// we are going to have two safety lanes
-void CrossyChicken::drawSafetyLanes(int min, int max, int row)
+void CrossyChicken::drawWater()
 {
-    int state = 1;
-    SafetyLane lane;
-    
-    for(int z = 0; z < 22; z++){
-        switch(state)
-        {
-            case 1:
-                safety_lane.push_back(std::tr1::make_shared<Background>(z, row)); // safety lane so row 0
-                safety_lane.push_back(std::tr1::make_shared<SafetyLane>('K')); // safety lane so row 0
-                state++;
-                break;
-            case 2: 
-                graphics.drawSecondSafetyBackground(x, 0); // safety lane so row 0
-                graphics.drawSecondSafetyBackground(x, 6); // middle lane so row 6
-                state -= 1; // back to inital state
-                break;
-        } 
-    }      
-}
-
-// the water sprites are going to 'move' to make it realistic
-void CrossyChicken::createWater(int row, float speed){   
-    int state = 1;   
-    
-    for(it = 0; it != 21; it++)
-    {
-        // fsm for drawing safety lane background
-        switch(state)
-        {
-            case 1: // initial state
-                row_water_one[it].push_back('F', row, speed, it); // first type of water
-                state++; // transition to next state
-                break;
-            case 2: 
-                row_water_one[it].push_back('S', row, speed, it); // second type of water
-                state++; // transition to state 3
-                break;       
-            case 3:
-                row_water_one[it].push_back('T', row, speed, it); // third type of water
-                state = 1; // back to start state
-                break;
-        } 
-    }
-}
-
-void CrossyChicken::moveWater(Water *water, int dir)
-{
-    switch(dir)
-        case 1:
-    for(it = 0; it != row_water_one.size(); it++){
-        
-    }
-}
-
-// if sprite goes out of bound then show again
-void CrossyChicken::loopWater(Water *sprite)
-{
-    // check if car goes out of bounds
-    if(sprite->x > 84+grid){
-        sprite->x = -4;
-        
-    } else if(sprite->x < -4){ 
-        sprite->x = 84 + grid;
-    } 
+    graphics.drawWater(water);         
 }
 
 // make the frog move same speed as log