ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

shot/shot.cpp

Committer:
el19zf
Date:
2020-04-29
Revision:
7:c49f3d3b672f
Parent:
6:dce38fe4e092
Child:
8:8287d2ef965d

File content as of revision 7:c49f3d3b672f:

#include "shot.h"

int shots[4][3][3] = {
    {       {1,1,0},{1,1,0},{0,0,1},        },
    {       {1,0,0},{1,1,1},{1,0,0},        },
    {       {1,1,1},{0,1,0},{0,1,0},        },
    {       {0,1,0},{1,1,1},{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() % 4;//randomise initial type
        (*i).dir = rand() % 6;//randomise initial direction
    }

}

void shot::init_pos(shot_posandtype* i)
{

    int num_pos = rand() % 6;//randomise initial position
    if (num_pos == 0) {
        (*i).x = 0;
        (*i).y = HEIGHT/4;
    }//left
    if (num_pos == 1) {
        (*i).x = 0;
        (*i).y = HEIGHT/2;
    }//left
    if (num_pos == 2) {
        (*i).x = 0;
        (*i).y = 3*HEIGHT/4;
    }//left
    if (num_pos == 3) {
        (*i).x = WIDTH-3;
        (*i).y = HEIGHT/4;
    }//right
    if (num_pos == 4) {
        (*i).x = WIDTH-3;
        (*i).y = HEIGHT/2;
    }//right
    if (num_pos == 5) {
        (*i).x = WIDTH-3;
        (*i).y = 3*HEIGHT/4;
    }//right

}

void shot::gen_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()
{
    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
        if ((*i).dir == 0) {
            (*i).x +=1;
            (*i).y +=1;//SE
        } else if ((*i).dir == 1) {
            (*i).x +=1;
            (*i).y +=0;//E
        } else if ((*i).dir == 2) {
            (*i).x +=1;
            (*i).y -=1;//NE
        } else if ((*i).dir == 3) {
            (*i).x -=1;
            (*i).y -=1;//NW
        } else if ((*i).dir == 4) {
            (*i).x -=1;
            (*i).y -=0;//W
        } else if ((*i).dir == 5) {
            (*i).x -=1;    
            (*i).y +=1;//SW
        }
    }
}


void shot::draw(N5110 &lcd)
{
    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]);
        //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 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)) {
            init_pos(i);
            (*i).type = ((*i).type + 2)%4;
            (*i).dir = ((*i).dir + 1)%6; // increase randomness
        }
    }
}