ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18rg

Dependencies:   mbed Gamepad2 ELEC2645_Project_el18rg

Dependents:   ELEC2645_Project_el18rg

Engine/Engine.cpp

Committer:
el18rg
Date:
2020-05-28
Revision:
9:e7dce4de0910
Parent:
8:c5a8576b2e8d
Child:
10:b6e45e4acde7

File content as of revision 9:e7dce4de0910:

#include "Engine.h"
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"

Engine::Engine(){}
Engine::~Engine(){}

void Engine::init(int paddle_width,int paddle_height,int bug_size,int speed)
{
    width = paddle_width;
    height = paddle_height;
    _speed = speed;
    x = GAP;
    _cup.init(x,height,width);
    _bug.init(_speed);
}

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

void Engine::draw(N5110 &lcd)
{
    _bug.draw(lcd);
    _cup.draw(lcd);
}

void Engine::update(N5110 & lcd, Gamepad & pad)
{
    _cup.update(_d,_mag);
    _bug.update();
    check_wall_collision(pad);
    check_paddle_collisions(lcd, pad);
}
void Engine::check_wall_collision(Gamepad &pad)
{
    Vector2D bug_pos = _bug.get_pos();
    Vector2D bug_velocity = _bug.get_velocity();

    if (bug_pos.y <= 1) {  
        bug_pos.y = 1;  
        bug_velocity.y = -bug_velocity.y;
        bug_velocity.x = rand() % 5;   
                   
        pad.tone(750.0,0.1);
    }
    else if (bug_pos.y + 10 >= (HEIGHT-1) ) {
        bug_pos.y = (HEIGHT-1) - 10;
        bug_velocity.y = -bug_velocity.y ;
        bug_velocity.x = rand() % 5;   
        pad.tone(750.0,0.1);
    }
    if (bug_pos.x <= 1) {  
        bug_pos.x = 1;  
        bug_velocity.x = -bug_velocity.x;
        bug_velocity.y = rand() % 5;                            
        pad.tone(750.0,0.1);
    }
    else if (bug_pos.x + 10 >= (WIDTH-1) ) {
        bug_pos.x = (WIDTH-1) - 10;
        bug_velocity.x = -bug_velocity.x ;
        bug_velocity.y = rand() % 5; 
        pad.tone(750.0,0.1);
    }

    _bug.set_velocity(bug_velocity);
    _bug.set_pos(bug_pos);
}
void Engine::check_paddle_collisions(N5110 & lcd, Gamepad & pad)
{
    Vector2D bug_pos = _bug.get_pos();
    Vector2D p_pos = _cup.get_pos();
    Vector2D bug_velocity = _bug.get_velocity();
    
    if ((bug_pos.x <= (p_pos.x + 5)) && (bug_pos.x +5 >= p_pos.x))
    {
        collisionX=true;
    }
    else
    {
        collisionX=false;
    }
    
    if ((bug_pos.y <= (p_pos.y+5)) && (bug_pos.y+5 >= p_pos.y ))
    {
        collisionY=true;
    }
    else
    {
        collisionY=false;
    }
        
    if (collisionX && collisionY)
    {
        ending(lcd, pad);
        }
}
void Engine::ending(N5110 & lcd, Gamepad & pad)
{
    lcd.clear();
    pad.leds_on();
    for (double i = 0; i < 100000; i += 0.1)
    {
    lcd.refresh();
    _splat.draw(lcd);
    }
}