HaoZhang SID: 201199702

Dependencies:   mbed

Bullet/Bullet.cpp

Committer:
zh870524589
Date:
2020-05-14
Revision:
2:867fdea920c1
Parent:
0:45ce241d316b

File content as of revision 2:867fdea920c1:

#include "Bullet.h"
#include "iostream"

 Bullet::Bullet() {}

Bullet::Bullet(Gamepad &pad,Vector2D p)
{   
     this->m_d = pad.get_direction();
     this->_x = p.x;
     this->_y = p.y;
     
}

void Bullet::update(N5110 &lcd,Direction prem_d)
{
    v1.push_back(prem_d);
    if(m_d == CENTRE)
    {
         if (v1[0] == S) {
              this->_y+=2;
        lcd.setPixel(_x+2,_y+4,true);
      

    }else if (v1[0] == N) {
        this->_y-=2;
        lcd.setPixel(_x+2,_y,true);
               
    } else if (v1[0] == E || v1[0] == SE ||v1[0] == NE) {
        this->_x+=2;
        lcd.setPixel(_x+4,_y+2,true);
        
        
         
    } else if (v1[0] == W || v1[0] == SW || v1[0] == NW) {
        this->_x-=2;
        lcd.setPixel(_x,_y+2,true);              
    }
    } else if (m_d == S) {
        this->_y+=2;
        lcd.setPixel(_x+2,_y+4,true);

    } else if (m_d == N) {
       this->_y-=2;
        lcd.setPixel(_x+2,_y,true);
   
    } else if (m_d == E || m_d == SE || m_d == NE) {
        this->_x+=2;
        lcd.setPixel(_x+4,_y+2,true);
        
    } else if (m_d == W || m_d == SW || m_d == NW) {
         this->_x-=2;
        lcd.setPixel(_x,_y+2,true);       
    }
}

void Bullet::et_update(N5110 &lcd,Direction e_d)
{
    v2.push_back(e_d);
    if (v2[0]== S) {
        this->_y+=2;
        lcd.setPixel(_x+2,_y+4,true);

    } else if (v2[0]== N) {
       this->_y-=2;
        lcd.setPixel(_x+2,_y,true);
   
    } else if (v2[0] == E || v2[0] == SE || v2[0] == NE) {
        this->_x+=2;
        lcd.setPixel(_x+4,_y+2,true);
        
    } else if (v2[0] == W || v2[0] == SW || v2[0] == NW) {
         this->_x-=2;
        lcd.setPixel(_x,_y+2,true);       
    }
}


Vector2D Bullet::get_pos()
{
    Vector2D p = {_x,_y};
    return p;
}

void Bullet::set_pos(int x,int y)
{
    this->_x = x;
    this->_y = y;

}


bool Bullet::destroy()
{
    //check if the bullet go out of the screen 
    if(this->_x<0||this->_x>84||this->_y>48||this->_y<0)
        return true;
   
    else
        return false;
} 
 
bool Bullet::destroyT(Vector2D t_pos)
{
        if((this->_x >= t_pos.x && this->_x<= t_pos.x+2) && (this->_y >= t_pos.y &&this->_y <=t_pos.y+2)
        ||(this->_x +2 >= t_pos.x && this->_x <= t_pos.x) && (this->_y >= t_pos.y && this->_y <=t_pos.y+2) 
        || (this->_x >= t_pos.x && this->_x <= t_pos.x+2) && (this->_y +2>= t_pos.y && this->_y <=t_pos.y)
        ||(this->_x +2 >= t_pos.x && this->_x<= t_pos.x) &&(this->_y +2>= t_pos.y && this->_y <=t_pos.y))
         return true;
         
         else 
           return false;   
}

void Bullet::bullet_collison(list<Bullet*> &lstBullets,list<Bullet*>&mtbullet)
{
     
    for(list<Bullet*>::iterator it = lstBullets.begin(); it != lstBullets.end();it++){
       Vector2D other_pos = (*it)->get_pos();
    for(list<Bullet*>::iterator mit = mtbullet.begin(); mit != mtbullet.end();mit++){
          Vector2D m_pos = (*mit)->get_pos();
          
    if(m_pos.x ==other_pos.x && m_pos.y ==other_pos.y)
    {
        (*it)->set_pos(-12,-12);
        (*mit)->set_pos(-12,-12);   
            
    }else if ((v2[0]== S || v2[0]== N)&&(m_pos.x ==other_pos.x )){
    
      if(m_pos.y ==other_pos.y+1|| m_pos.y ==other_pos.y+2)  
    {
        (*it)->set_pos(-12,-12);
        (*mit)->set_pos(-12,-12);

    } else if ( m_pos.y ==other_pos.y-1||m_pos.y ==other_pos.y-2) {

        (*it)->set_pos(-12,-12);
        (*mit)->set_pos(-12,-12); 
    }
    } else if ((v2[0]== W ||v2[0]== E)&& (m_pos.y ==other_pos.y)){
        if(m_pos.x ==other_pos.x+1|| m_pos.x ==other_pos.x+2) {
          (*it)->set_pos(-12,-12);
          (*mit)->set_pos(-12,-12);
         } else if ( m_pos.x ==other_pos.x-1||m_pos.x ==other_pos.x-2){

          (*it)->set_pos(-12,-12);
          (*mit)->set_pos(-12,-12);
         }
    }
   }
  }
}