ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_ll17lrc_v2

Dependencies:   mbed

Imposs/ImpossEngine.cpp

Committer:
ll17lrc
Date:
2020-05-16
Revision:
3:4494e6928194
Parent:
2:823dea76ff2e
Child:
4:a9d5fca3b7ba

File content as of revision 3:4494e6928194:

#include "ImpossEngine.h"
#include "Zero.h"
#include "One.h"
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Ball.h"
#include "StartMenu.h"

ImpossEngine::ImpossEngine()
{

}

ImpossEngine::~ImpossEngine()
{

}

void ImpossEngine::complete(Gamepad &pad,N5110 &lcd,int level)
{
    _start.complete(pad,lcd,level);   
}

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

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

    if (level == 1){
        _one.draw(lcd);
    }
    

}

void ImpossEngine::update(Gamepad &pad,N5110 &lcd, int ball_x_pos, int ball_y_pos)
{
    _ball.update(_d);

    check_collision(pad,lcd,ball_x_pos,ball_y_pos);
    
    check_finish(pad,ball_x_pos,ball_y_pos,level);
}

void check_collision(Gamepad &pad,N5110 &lcd, int ball_x_pos, int ball_y_pos)
{
    int _x = 0;
    int _y = 0;
    int i = 0;
    bool collision = false;
    //check around ball to see if it has made contact with anything
    
    while(i < 4){
        
        i++;
        
        if(lcd.getPixel(ball_x_pos + _x,ball_y_pos + _y) == 1){
            collision = true;
            }
            
        _x ++;
        
    }
    
    i = 0;
     
    while(i < 4){
        
        i++;
        
        if(lcd.getPixel(ball_x_pos + _x,ball_y_pos + _y) == 1){
            collision = true;
            }
            
        _y ++;
        
    }
    
    i = 0;
    
    while(i < 4){
        
        i++;
        
        if(lcd.getPixel(ball_x_pos + _x,ball_y_pos + _y) == 1){
            collision = true;
            }
            
        _x --;
        
    }
    
    i = 0;
    
    while(i < 4){
        
        i++;
        
        if(lcd.getPixel(ball_x_pos + _x,ball_y_pos + _y) == 1){
            collision = true;
            }
            
        _y --;
        
    if(collision == true){
        ball_x_pos = 0;
        ball_y_pos = 21;
        }
    }
}

void check_finish(Gamepad &pad, int ball_x_pos, int ball_y_pos, int level)
{
    
    if(ball_x_pos == 82){
        level++;
        ball_x_pos = 0;
        ball_y_pos = 21;
        }
        
}