ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Revision:
6:dce38fe4e092
Parent:
5:6774b7272e85
Child:
7:c49f3d3b672f
--- a/shot/shot.cpp	Tue Apr 14 06:39:11 2020 +0000
+++ b/shot/shot.cpp	Tue Apr 28 15:54:41 2020 +0000
@@ -1,5 +1,14 @@
 #include "shot.h"
 
+
+shot::shot() {
+    
+}
+
+shot::~shot() {
+    
+}
+
 const int shots[4][3][3] = {
 {       
         {1,1,0},
@@ -19,42 +28,23 @@
         {0,1,0},        }
 };
 
-
-shot::shot() {
-    
-}
-
-shot::~shot() {
-    
+void shot::init() {
+        
+        _size = 10;
+        _p.resize(_size);
+        srand(time(NULL));
+        for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
+            init_pos(i);
+            (*i).type = rand_type();//randomise initial type 
+            (*i).dir = rand() % 8;//randomise initial direction
+        }
+        
 }
 
-void shot::init(N5110 &lcd) {
-        _p.resize(50);
-        int index = rand_type();
-        for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-            int num_dir = rand()%4;
-            if (num_dir == 0) {  (*i).x = (WIDTH/2-1);
-                                 (*i).y = 0;           }
-            if (num_dir == 1) {  (*i).x = (WIDTH/2-1);
-                                 (*i).y = (HEIGHT-3);  }
-            if (num_dir == 2) {  (*i).x = 0;
-                                 (*i).y = (HEIGHT/2-1);}
-            if (num_dir == 3) {  (*i).x = (WIDTH-3);
-                                 (*i).y = (HEIGHT/2-1);}
-            printf("pos = %d,%d\n",(*i).x,(*i).y);
-        
-    }
-    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { 
-        (*i).type = rand() % 4;
-        (*i).dir = rand() % 8;
-        lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]);
-}
-}
-
-int shot::rand_dir() {
+/*Direction shot::rand_dir() {
     
-    //srand(time(NULL));
-    /*int d_num = rand() % 8; //randomise initial direction
+    srand(time(NULL));
+    int d_num = rand() % 8; //randomise initial direction
     if (d_num == 0) { d = N; } else
     if (d_num == 1) { d = NE; } else
     if (d_num == 2) { d = E; } else
@@ -62,73 +52,70 @@
     if (d_num == 4) { d = S; } else
     if (d_num == 5) { d = SW; } else
     if (d_num == 6) { d = W; } else
-                    { d = NW;}*/
-    //int d = rand() % 4;//randomise initial direction
-    //return d;
-}
+                    { d = NW;}   
+    return d;
+}*/
 
 int shot::rand_type() {
 
-    srand(time(NULL));
     int type = rand() % 4;//set 4 type of shot
     return type; 
 }
 
+void shot::init_pos(shot_posandtype* i) {
+    
+    int num_pos = rand_type();
+    if (num_pos == 0) {  (*i).x = (WIDTH/2-1);
+                         (*i).y = 0;           }//up
+    if (num_pos == 1) {  (*i).x = (WIDTH/2-1);
+                         (*i).y = (HEIGHT-3);  }//down
+    if (num_pos == 2) {  (*i).x = 0;
+                         (*i).y = (HEIGHT/2-1);}//left
+    if (num_pos == 3) {  (*i).x = (WIDTH-3);
+                         (*i).y = (HEIGHT/2-1);}//right
+}
 
 void shot::gen_shot() {
-    
-        /*int num_dir = rand_type();//up down left right
-        if (num_dir == 0) { _p.x = (WIDTH/2-1); _p.y = 0;     //up
-                
-                //if(dir == 1) {_p.x -= 1; _p.y +=1; }else
-                //if(dir == 2) {_p.x += 0; _p.y +=1; }else
-                             //{_p.x += 1; _p.y +=1; }    }
-        
-        if (num_dir == 1) { _p.x = (WIDTH/2-1); _p.y = (HEIGHT-3);} //down  
-        if (num_dir == 2) { _p.x = 0; _p.y = (HEIGHT/2-1);}    //left 
-        if (num_dir == 3) { _p.x = (WIDTH-3); _p.y =(HEIGHT/2-1);}  //right
-        //printf("random num = %d\n",num_dir);*/
-        _p.resize(10);
-        int index = rand_type();
-        for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-            int num_dir = rand()%4;
-            if (num_dir == 0) {  (*i).x = (WIDTH/2-1);
-                                 (*i).y = 0;           }
-            if (num_dir == 1) {  (*i).x = (WIDTH/2-1);
-                                 (*i).y = (HEIGHT-3);  }
-            if (num_dir == 2) {  (*i).x = 0;
-                                 (*i).y = (HEIGHT/2-1);}
-            if (num_dir == 3) {  (*i).x = (WIDTH-3);
-                                 (*i).y = (HEIGHT/2-1);}
-            printf("pos = %d,%d\n",(*i).x,(*i).y);
-        
-    }
+    _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_type();//randomise initial type 
+            (*i).dir = rand() % 8;//randomise initial direction
+        }
+    }    
 }
 
 
 
-void shot::update(N5110 &lcd) {
+void shot::update() {
     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-        if ((*i).dir == 0) {     (*i).x +=0; (*i).y +=1; } else
+        if ((*i).dir == 0) {     (*i).x +=0; (*i).y +=2; } else
         if ((*i).dir == 1) {     (*i).x +=1; (*i).y +=1; } else
-        if ((*i).dir == 2) {     (*i).x +=1; (*i).y +=0; } else
+        if ((*i).dir == 2) {     (*i).x +=2; (*i).y +=0; } else
         if ((*i).dir == 3) {     (*i).x +=1; (*i).y -=1; } else
-        if ((*i).dir == 4) {     (*i).x +=0; (*i).y -=1; } else
+        if ((*i).dir == 4) {     (*i).x +=0; (*i).y -=2; } else
         if ((*i).dir == 5) {     (*i).x -=1; (*i).y -=1; } else
-        if ((*i).dir == 6) {     (*i).x -=1; (*i).y -=0; } else
-        if ((*i).dir == 7) {     (*i).x -=1; (*i).y +=1; }
-        lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]);    
+        if ((*i).dir == 6) {     (*i).x -=2; (*i).y -=0; } else
+        if ((*i).dir == 7) {     (*i).x -=1; (*i).y +=1; }  
     }  
 }
 
 
 void shot::draw(N5110 &lcd){
-    /*for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { 
-        (*i).type = rand() % 4;
-        (*i).dir = rand() % 8;
+    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
         lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]);
-        //if ((*i).type == 1) {    lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot2);}
-        //if ((*i).type == 2) {    lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot3);}
-        //if ((*i).type == 3) {    lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot4);}  
-    }*/   
-}    
+        printf("coordinate = %d,%d\n",(*i).x,(*i).y);
+    }
+}
+
+void shot::delete_shot() {
+    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
+        if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)){
+            init_pos(i);
+            (*i).type = ((*i).type + 2)%4;
+            (*i).dir = ((*i).dir + 1)%4;
+        }
+    }
+}
+            
\ No newline at end of file