ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_ll17lrc_v2

Dependencies:   mbed

Imposs/ImpossEngine.cpp

Committer:
ll17lrc
Date:
2020-05-25
Revision:
11:7a4abe731f9c
Parent:
10:df6a496270be
Child:
12:299479b6bb59

File content as of revision 11:7a4abe731f9c:

 #include "ImpossEngine.h"


ImpossEngine::ImpossEngine()
{

}

ImpossEngine::~ImpossEngine()
{

}

StartMenu _start;

void ImpossEngine::complete(Gamepad &pad,N5110 &lcd)
{
    level = 0;
    _start.complete(pad,lcd); 
    int x = 0;
    int y = 21;
    _ball.init(x,y);
}
 
void ImpossEngine::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void ImpossEngine::draw(N5110 &lcd,Gamepad &pad)
{
    // draw the elements in the LCD buffer
    // pitch   
    
    
    if (level == 0){
        _zero.draw(lcd);
    }

    if (level == 1){
        _one.draw(lcd);
    } 
    
    if (level == 2){
        _two.draw(lcd);
    }
    
    if (level == 3){
         _three.draw(lcd);
    }
    
    if (level == 4){
         _four.draw(lcd);
    }
    
    if (level == 5){
         _five.draw(lcd);
    }
         
    _ball.update(_d,pad);
    
    _ball.draw(lcd);

}

void ImpossEngine::update(Gamepad &pad,N5110 &lcd)
{
    check_collision(pad,lcd);
    
    check_finish(pad);
}

void ImpossEngine::set_level_zero(){
    level = 0;
    }
    
void ImpossEngine::set_level_one(){
    level = 1;
    }
    
void ImpossEngine::set_level_two(){
    level = 2;
    }

void ImpossEngine::set_level_three(){
    level = 3;
    }
    
void ImpossEngine::set_level_four(){
    level = 4;
    }
    

void ImpossEngine::check_collision(Gamepad &pad,N5110 &lcd)
{
    int _x = 0;
    int _y = 0;
    int i = 0;
    int x_pos = _ball.get_ball_x_pos() - 1;
    int y_pos = _ball.get_ball_y_pos() - 1;
    bool collision = false;
    //check around ball to see if it has made contact with anything
    
    while(i < 3){
        
        i++;
        
        if(lcd.getPixel(x_pos + _x,y_pos + _y) == 1){
            collision = true;
            }
            
        _x ++;
        
    }
    
    i = 0;
     
    while(i < 3){
        
        i++;
        
        if(lcd.getPixel(x_pos + _x,y_pos + _y) == 1){
            collision = true;
            }
            
        _y ++;
        
    }
    
    i = 0;
    
    while(i < 3){
        
        i++;
        
        if(lcd.getPixel(x_pos + _x,y_pos + _y) == 1){
            collision = true;
            }
            
        _x --;
        
    }
    
    i = 0;
    
    while(i < 3){
        
        i++;
        
        if(lcd.getPixel(x_pos + _x,y_pos + _y) == 1){
            collision = true;
            }
            
        _y --;
        
    if(collision == true){
        lcd.clear();
        lcd.printString("  You died! ",0,2);
        lcd.refresh();
        wait(0.1); 
        pad.leds_on();
        wait(0.2);
        pad.leds_off();
        wait(0.1);
        pad.leds_on();
        wait(0.2);
        pad.leds_off();
        
        _ball.level_finish();
        }
    }
}

void ImpossEngine::check_finish(Gamepad &pad)
{
    
    int pos = _ball.get_ball_x_pos();
    
    if(pos >= 82){
        level++;
        _ball.level_finish();
        }
    
    if(pad.Y_pressed() == true){
        level++;
        }
    if(pad.X_pressed() == true){
        level--;
        }
        
}