publish my project

Dependencies:   mbed

Start/Start.cpp

Committer:
dongyuhu
Date:
2020-04-28
Revision:
5:c31ba165e49c
Parent:
4:fd47d9c11263

File content as of revision 5:c31ba165e49c:

#include "Start.h"
bool ifnotstop = true;

Start::Start()
{

}

Start::~Start()
{

}

void Start::init(int me_width,int me_height,int attacker_size,int speed)
{
    // initialise the game parameters
    _me_width = me_width;            //3
    _me_height = me_height;          //3  
    _attacker_size = attacker_size;  //2
    _speed = speed;                  //1



    _me.init(_mex,_mey,_me_height,_me_width);

    _attacker1.init1(_attacker_size,_speed);
    _attacker2.init2(_attacker_size,_speed);
    _attacker3.init3(_attacker_size,_speed);
    _attacker4.init4(_attacker_size,_speed);
    _attacker5.init5(_attacker_size,_speed);
    _attacker6.init6(_attacker_size,_speed);
    _attacker7.init7(_attacker_size,_speed);
    _attacker8.init8(_attacker_size,_speed);
    
}

void Start::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void Start::draw(N5110 &lcd)
{

    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);    //draw the turrets
    lcd.drawRect(10,10,64,28,FILL_TRANSPARENT);    //map
    lcd.drawCircle(5,17,4,FILL_TRANSPARENT);
    lcd.drawCircle(5,31,4,FILL_TRANSPARENT);      //the two in left
    lcd.drawCircle(78,17,4,FILL_TRANSPARENT);
    lcd.drawCircle(78,31,4,FILL_TRANSPARENT);     //right
    lcd.drawCircle(30,5,4,FILL_TRANSPARENT);
    lcd.drawCircle(54,5,4,FILL_TRANSPARENT);      //up
    lcd.drawCircle(30,42,4,FILL_TRANSPARENT);
    lcd.drawCircle(54,42,4,FILL_TRANSPARENT);     //down

    _me.draw(lcd);
    _attacker1.draw(lcd);
    _attacker2.draw(lcd);
    _attacker3.draw(lcd);
    _attacker4.draw(lcd);
    _attacker5.draw(lcd);
    _attacker6.draw(lcd);
    _attacker7.draw(lcd);
    _attacker8.draw(lcd);
    
}

void Start::update(Gamepad &pad)
{
    _me.update(_d);
    _attacker1.update();
    _attacker2.update();
    _attacker3.update();
    _attacker4.update();
    _attacker5.update();
    _attacker6.update();
    _attacker7.update();
    _attacker8.update();

    check_wall_collision(pad);         
    check_body_collisions(pad);

}
void setpos(Vector2D &attacker_pos,int x,int y)
{
     attacker_pos.x = x;        
     attacker_pos.y = y;
}


void JUDGE(Vector2D &attacker11_pos,Vector2D &attacker22_pos,Vector2D &attacker33_pos,Vector2D &attacker44_pos,Vector2D &attacker55_pos,Vector2D &attacker66_pos,Vector2D &attacker77_pos,Vector2D &attacker88_pos)
{
    if(attacker11_pos.y < 10||attacker11_pos.y > 36||attacker11_pos.x < 10||attacker11_pos.x > 72) 
        setpos(attacker11_pos,30,11);  
    else if(attacker22_pos.y < 10||attacker22_pos.y > 36||attacker22_pos.x < 10||attacker22_pos.x > 72)
        setpos(attacker22_pos,54,10); 
    else if(attacker33_pos.y < 10||attacker33_pos.y > 36||attacker33_pos.x < 10||attacker33_pos.x > 72)
        setpos(attacker33_pos,10,17); 
    else if(attacker44_pos.y < 10||attacker44_pos.y > 36||attacker11_pos.x < 10||attacker44_pos.x > 72)
        setpos(attacker44_pos,10,31);  
    else if(attacker55_pos.y < 10||attacker55_pos.y > 36||attacker55_pos.x < 10||attacker55_pos.x > 72)
        setpos(attacker55_pos,30,36); 
    else if(attacker66_pos.y < 10||attacker66_pos.y > 36||attacker66_pos.x < 10||attacker66_pos.x >72)
        setpos(attacker66_pos,54,36);
    else if(attacker77_pos.y < 10||attacker77_pos.y > 36||attacker77_pos.x < 10||attacker77_pos.x >72)
        setpos(attacker77_pos,71,31);
    else if(attacker88_pos.y < 10||attacker88_pos.y > 36||attacker88_pos.x < 10||attacker88_pos.x >72)
        setpos(attacker88_pos,71,17);
}

void Start::check_wall_collision(Gamepad &pad)
{
    Vector2D attacker11_pos = _attacker1.get_pos();
    Vector2D attacker22_pos = _attacker2.get_pos();
    Vector2D attacker33_pos = _attacker3.get_pos();
    Vector2D attacker44_pos = _attacker4.get_pos();
    Vector2D attacker55_pos = _attacker5.get_pos();
    Vector2D attacker66_pos = _attacker6.get_pos();
    Vector2D attacker77_pos = _attacker7.get_pos();
    Vector2D attacker88_pos = _attacker8.get_pos();
    JUDGE(attacker11_pos,attacker22_pos,attacker33_pos,attacker44_pos,attacker55_pos,attacker66_pos,attacker77_pos,attacker88_pos);   
    _attacker1.set_pos(attacker11_pos);
    _attacker2.set_pos(attacker22_pos);
    _attacker3.set_pos(attacker33_pos);
    _attacker4.set_pos(attacker44_pos);
    _attacker5.set_pos(attacker55_pos);
    _attacker6.set_pos(attacker66_pos);
    _attacker7.set_pos(attacker77_pos);
    _attacker8.set_pos(attacker88_pos);

}
void set_stop(Vector2D &attacker_pos,int x,int y)
{
    ifnotstop = false;
    attacker_pos.x = x;
    attacker_pos.y = y;
}
bool Judge_for_each_situation(Vector2D &me_pos,Vector2D &attacker_pos)
{
      if ((attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y+1 == me_pos.y)||
          (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y   == me_pos.y)||
          (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y-1 == me_pos.y)||  
          (attacker_pos.x+1 == me_pos.x) &&(attacker_pos.y-2 == me_pos.y)|| 
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y+1 == me_pos.y)||
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y   == me_pos.y)||
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y-1 == me_pos.y)||  
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y-2 == me_pos.y)||
          (attacker_pos.x == me_pos.x-1) &&(attacker_pos.y+2 == me_pos.y+1)||
          (attacker_pos.x == me_pos.x  ) &&(attacker_pos.y+2 == me_pos.y+1)||
          (attacker_pos.x == me_pos.x+1) &&(attacker_pos.y+2 == me_pos.y+1)||  
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y+2 == me_pos.y+1)||
          (attacker_pos.x == me_pos.x-1) &&(attacker_pos.y == me_pos.y+2)||
          (attacker_pos.x == me_pos.x  ) &&(attacker_pos.y == me_pos.y+2)||
          (attacker_pos.x == me_pos.x+1) &&(attacker_pos.y == me_pos.y+2)||  
          (attacker_pos.x == me_pos.x+2) &&(attacker_pos.y == me_pos.y+2))
          return true;
      else
          return false;
}
void Judge_ifstop(Vector2D &me_pos,Vector2D &attacker11_pos,Vector2D &attacker22_pos,Vector2D &attacker33_pos,Vector2D &attacker44_pos,Vector2D &attacker55_pos,Vector2D &attacker66_pos,Vector2D &attacker77_pos,Vector2D &attacker88_pos)
{
    if(Judge_for_each_situation(me_pos,attacker11_pos))
        set_stop(attacker11_pos,30,11);
    else if(Judge_for_each_situation(me_pos,attacker22_pos))
        set_stop(attacker22_pos,54,10);
    else if(Judge_for_each_situation(me_pos,attacker33_pos))
        set_stop(attacker33_pos,10,17);
    else if(Judge_for_each_situation(me_pos,attacker44_pos))
        set_stop(attacker44_pos,10,31);
    else if(Judge_for_each_situation(me_pos,attacker55_pos))
        set_stop(attacker55_pos,30,38);
    else if(Judge_for_each_situation(me_pos,attacker66_pos))
        set_stop(attacker66_pos,54,38);
    else if(Judge_for_each_situation(me_pos,attacker77_pos))
        set_stop(attacker77_pos,74,31);
    else if(Judge_for_each_situation(me_pos,attacker88_pos))
        set_stop(attacker88_pos,74,17);
}


void Start::check_body_collisions(Gamepad &pad)
{
    Vector2D attacker11_pos = _attacker1.get_pos();
    Vector2D attacker22_pos = _attacker2.get_pos();
    Vector2D attacker33_pos = _attacker3.get_pos();
    Vector2D attacker44_pos = _attacker4.get_pos();
    Vector2D attacker55_pos = _attacker5.get_pos();
    Vector2D attacker66_pos = _attacker6.get_pos();
    Vector2D attacker77_pos = _attacker7.get_pos();
    Vector2D attacker88_pos = _attacker8.get_pos();
    Vector2D me_pos = _me.get_pos();
    Judge_ifstop(me_pos,attacker11_pos,attacker22_pos,attacker33_pos,attacker44_pos,attacker55_pos,attacker66_pos,attacker77_pos,attacker88_pos);
    _attacker1.set_pos(attacker11_pos);
    _attacker2.set_pos(attacker22_pos);
    _attacker3.set_pos(attacker33_pos);
    _attacker4.set_pos(attacker44_pos);
    _attacker5.set_pos(attacker55_pos);
    _attacker6.set_pos(attacker66_pos);
    _attacker7.set_pos(attacker77_pos);
    _attacker8.set_pos(attacker88_pos);    
}