ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Revision:
13:eb60628db8bf
Parent:
11:494cc44777fe
Child:
15:3571beaaeed8
--- a/shot/shot.cpp	Mon May 11 14:43:16 2020 +0000
+++ b/shot/shot.cpp	Thu May 14 16:45:20 2020 +0000
@@ -1,10 +1,21 @@
 #include "shot.h"
 
 int shots[4][3][3] = {
-    {       {0,1,0},{1,1,1},{0,1,0},        },
-    {       {1,1,1},{0,1,1},{0,0,1},        },
-    {       {1,1,1},{0,1,0},{0,1,0},        },
-    {       {1,1,0},{1,1,0},{0,0,1},        }
+    {       {0,1,0},
+            {1,1,1},
+            {0,1,0},        },//shot type1
+            
+    {       {1,1,1},
+            {0,1,1},
+            {0,0,1},        },//shot type2
+    
+    {       {1,1,1},
+            {0,1,0},
+            {0,1,0},        },//shot type3
+    
+    {       {1,1,0},
+            {1,1,0},
+            {0,0,1},        }//shot type4
 };
 
 shot::shot()
@@ -21,6 +32,7 @@
 {
 
     _size = 10;
+    _size_f = 10.0;
     _p.resize(_size);
     srand(time(NULL));
     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
@@ -62,19 +74,7 @@
 
 }
 
-void shot::init_shot()
-{
-    _p.resize(_size);
-    for(std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-        if(((*i).x == 0)&&((*i).y == 0)) {
-            init_pos(i);
-            (*i).type = rand() % 4;//randomise initial type
-            (*i).dir = rand() % 6;//randomise initial direction
-        }
-    }
-}
-
-void shot::update()
+void shot::update(N5110 &lcd)
 {
     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
         if ((*i).dir == 0) {
@@ -97,8 +97,24 @@
             (*i).y +=1;//SW
         }
     }
+    // delete invalid shots
+    delete_shot();
+    //generate shot to keep constant number of shots
+    update_shot();
+    draw(lcd);
 }
 
+void shot::update_shot()
+{
+    _p.resize(_size);
+    for(std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
+        if(((*i).x == 0)&&((*i).y == 0)) {
+            init_pos(i);
+            (*i).type = rand() % 4;//randomise initial type
+            (*i).dir = rand() % 6;//randomise initial direction
+        }
+    }
+}
 
 void shot::draw(N5110 &lcd)
 {
@@ -114,16 +130,32 @@
         // if beyoud border, delete it and generate new one, keep total number constant
         if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)||
         //keep shots away from starting point
-            (((*i).x==3)&&((*i).y==27))   ||   (((*i).x==3)&&((*i).y==18))||
-            (((*i).x==80)&&((*i).y==17))  ||   (((*i).x==80)&&((*i).y==28)))  
+            (((*i).x==3)&&((*i).y==18))   ||   (((*i).x==4)&&((*i).y==28))||
+            (((*i).x==79)&&((*i).y==16))  ||   (((*i).x==79)&&((*i).y==29)))  
         {
-            init_pos(i);
+            init_pos(i);//generate new shots to keep the total number
             (*i).type = ((*i).type + 2)%4;
             (*i).dir = ((*i).dir + 1)%6; // increase randomness
         }
     }
 }
 
+/*  increment(number of shots is generated during 1000/6 ms)
+    and maximum number of shots
+    control degree of difficulty of the game*/
+void shot::gen_shot(int timer_flag, float increment, int max)
+{
+    if(_size < max){
+        if(timer_flag == 1){
+            timer_flag = 0;
+            _size_f = _size_f + increment;
+            int size = int(_size_f);
+            //printf("Generate\n");
+            set_size(size);
+        }
+    }
+}
+
 void shot::set_size(int size) 
 {
     _size = size;
@@ -133,16 +165,4 @@
 {
     return _size;
 }
-
-void shot::gen_shot(int timer_flag, float increment, int max)
-{
-    if(get_size() < max){
-        if(timer_flag == 1){
-            timer_flag = 0;
-            int size = get_size()+ (int)increment;
-            //printf("Generate\n");
-            set_size(size);
-        }
-    }
-}
-    
\ No newline at end of file
+ 
\ No newline at end of file