ELEC2645 (2018/19) / Mbed 2 deprecated fy14lkaa

Dependencies:   mbed

SpaceInvadersEngine/SpaceInvadersEngine.cpp

Committer:
fy14lkaa
Date:
2019-04-27
Revision:
68:175190a03cbd
Parent:
67:4299b64ad5fc
Child:
69:19f1ad7b548e

File content as of revision 68:175190a03cbd:

#include "SpaceInvadersEngine.h"

SpaceInvadersEngine::SpaceInvadersEngine()
{

}

SpaceInvadersEngine::~SpaceInvadersEngine()
{


}




void SpaceInvadersEngine::init(int space_ship_width,int space_ship_height,int bullet_size, int Alien_size, int speed)
{
    // initialise the game parameters

    _space_ship_width= space_ship_width;
    _space_ship_height= space_ship_height;
    _bullet_size= bullet_size;
    _Alien_size= Alien_size;
    _speed= speed;


}

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

void  SpaceInvadersEngine::draw(N5110 &lcd)
{
    // draw the elements in the LCD buffer
    // pitch

    //score
    print_scores(lcd);
    _Alien.draw(lcd);
    _space_ship.draw(lcd);
    _bullet.draw(lcd);
}

void  SpaceInvadersEngine::update(Gamepad &pad)
{
    check_goal(pad);
    _bullet.update();
    _space_ship.update(_d,_mag);

    _A1ien.update(_d,_mag);

    check_Alien_collision(pad);
    check_space_ship_collisions(pad);


}
void SpaceInvadersEngine::check_Alien_collision(Gamepad &pad)
{

    // read current bullet attributes
    Vector2D bullet_pos = _bullet.get_pos();
    Vector2D bullet_velocity = _bullet.get_velocity();
    //check if the bullet hit the top of the alien

    // check if the bullet hits top of the Alien
    if (bullet_pos.y <= 1) {  //  1 due to 1 pixel boundary
        bullet_pos.y = 1;  // bounce off ceiling without going off screen
        bullet_velocity.y = -bullet_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }

    // check if the bullet hits bottom of the Alien
    else if (bullet_pos.y + _bullet_size >= (HEIGHT-1) ) { // bottom pixel is 47
        // hit bottom
        bullet_pos.y = (HEIGHT-1) - _bullet_size;  // stops bullet going off screen
        bullet_velocity.y = -bullet_velocity.y;
        // audio feedback
        pad.tone(750.0,0.1);
    }

    // update bullet parameters
    _bullet.set_velocity(bullet_velocity);
    _bullet.set_pos(bullet_pos);
}