ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers shot.cpp Source File

shot.cpp

00001 #include "shot.h"
00002 
00003 int shots[4][3][3] = {
00004     {       {0,1,0},
00005             {1,1,1},
00006             {0,1,0},        },//shot type1
00007             
00008     {       {1,1,1},
00009             {0,1,1},
00010             {0,0,1},        },//shot type2
00011     
00012     {       {1,1,1},
00013             {0,1,0},
00014             {0,1,0},        },//shot type3
00015     
00016     {       {1,1,0},
00017             {1,1,0},
00018             {0,0,1},        }//shot type4
00019 };
00020 
00021 shot::shot()
00022 {
00023 
00024 }
00025 
00026 shot::~shot()
00027 {
00028 
00029 }
00030 
00031 void shot::init()
00032 {
00033 
00034     _size = 7;
00035     _size_f = 7.0;
00036     _p.resize(_size);//initalises 7 shots vector
00037     srand(time(NULL));
00038     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
00039         init_pos(i);
00040         (*i).type = rand() % 4;//randomise initial type
00041         (*i).dir = rand() % 6;//randomise initial direction
00042     }
00043 
00044 }
00045 
00046 void shot::init_pos(shot_posandtype* i)
00047 {
00048 
00049     int num_pos = rand() % 6;//randomise initial position
00050     if (num_pos == 0) {
00051         (*i).x = WIDTH/4;
00052         (*i).y = 0;
00053     }//top
00054     if (num_pos == 1) {
00055         (*i).x = WIDTH/2;
00056         (*i).y = 0;
00057     }//top
00058     if (num_pos == 2) {
00059         (*i).x = 3*WIDTH/4;
00060         (*i).y = 0;
00061     }//top
00062     if (num_pos == 3) {
00063         (*i).x = WIDTH/4;
00064         (*i).y = HEIGHT-3;
00065     }//bottom
00066     if (num_pos == 4) {
00067         (*i).x = WIDTH/2;
00068         (*i).y = HEIGHT-3;
00069     }//bottom
00070     if (num_pos == 5) {
00071         (*i).x = 3*WIDTH/4;
00072         (*i).y = HEIGHT-3;
00073     }//bottom
00074 
00075 }
00076 
00077 void shot::update()
00078 {
00079     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
00080         if ((*i).dir == 0) {
00081             (*i).x +=1;
00082             (*i).y +=1;//SE
00083         } else if ((*i).dir == 1) {
00084             (*i).x +=0;
00085             (*i).y +=1;//S
00086         } else if ((*i).dir == 2) {
00087             (*i).x +=1;
00088             (*i).y -=1;//NE
00089         } else if ((*i).dir == 3) {
00090             (*i).x -=1;
00091             (*i).y -=1;//NW
00092         } else if ((*i).dir == 4) {
00093             (*i).x -=0;
00094             (*i).y -=1;//N
00095         } else if ((*i).dir == 5) {
00096             (*i).x -=1;    
00097             (*i).y +=1;//SW
00098         }
00099     }
00100 }
00101 
00102 void shot::update_shot()
00103 {
00104     _p.resize(_size);
00105     for(std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
00106         if(((*i).x == 0)&&((*i).y == 0)) {
00107             init_pos(i);
00108             (*i).type = rand() % 4;//randomise initial type
00109             (*i).dir = rand() % 6;//randomise initial direction
00110         }
00111     }
00112 }
00113 
00114 void shot::draw(N5110 &lcd)
00115 {
00116     update();
00117     // delete invalid shots
00118     delete_shot();
00119     //generate shot to keep constant number of shots
00120     update_shot();
00121     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
00122         lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]);
00123         //printf("coordinate = %d,%d\n",(*i).x,(*i).y);
00124     }
00125 }
00126 
00127 void shot::delete_shot()
00128 {
00129     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
00130         // if beyoud border, delete it and generate new one, keep total number constant
00131         if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)||
00132         //keep shots away from starting point
00133             (((*i).x==3)&&((*i).y==18))   ||   (((*i).x==4)&&((*i).y==28))||
00134             (((*i).x==79)&&((*i).y==16))  ||   (((*i).x==79)&&((*i).y==29)))  
00135         {
00136             init_pos(i);//generate new shots instead of those shots which beyoud border 
00137             (*i).type = ((*i).type + 2)%4;
00138             (*i).dir = ((*i).dir + 1)%6; // reset their type and dir (complex calculate to increase randomness)
00139         }
00140     }
00141 }
00142 
00143 /*  increment(number of shots is generated during 1000/6 ms)
00144     and maximum number of shots
00145     control degree of difficulty of the game*/
00146 void shot::gen_shot(int timer_flag, float increment, int max)
00147 {
00148     if(_size < max){
00149         if(timer_flag == 1){
00150             timer_flag = 0;
00151             _size_f = _size_f + increment;
00152             int size = int(_size_f);
00153             //printf("Generate\n");
00154             set_size(size);
00155         }
00156     }
00157 }
00158 
00159 void shot::set_size(int size) 
00160 {
00161     _size = size;
00162 }
00163 
00164 void shot::set_shot(int x, int y, int type, int Direction)
00165 {
00166     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
00167         (*i).x = x;
00168         (*i).y = y;
00169         (*i).type = type;
00170         (*i).dir = Direction;
00171     }
00172 }
00173 int shot::get_size() 
00174 {
00175     return _size;
00176 }
00177 
00178 Vector2D shot::get_shot()
00179 {
00180     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
00181         _shot_pos.x =(*i).x;
00182         _shot_pos.y =(*i).y;
00183     }
00184     return _shot_pos;
00185 }
00186         
00187 
00188