ELEC2645 (2018/19) / Mbed 2 deprecated 2645_Project_SiutingWong201186503

Dependencies:   mbed

Avenger/Avenger.cpp

Committer:
davidwst421
Date:
2019-05-09
Revision:
12:660458c41c8e
Parent:
8:97576c8761a8
Child:
14:13e82f720bea

File content as of revision 12:660458c41c8e:

#include "Avenger.h"

// nothing doing in the constructor and destructor
Avenger::Avenger()
{

}

Avenger::~Avenger()
{

}

const int player0[6][11] = {
    { 0,0,0,0,1,1,1,0,0,0,0 },
    { 0,1,0,0,0,0,1,1,0,1,0 },
    { 1,1,1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1,1,1 },
    { 0,1,0,0,0,0,1,1,0,1,0 },
    { 0,0,0,0,1,1,1,0,0,0,0 },
};

const int easteregg[6][11] = {
    { 0,0,0,0,0,0,0,1,1,1,1 },
    { 1,0,0,0,0,0,0,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1,1,1 },
    { 1,0,0,0,0,0,0,1,1,1,1 },
    { 0,0,0,0,0,0,0,1,1,1,1 },
};

void Avenger::init(int x) {
    
    _x = x;  // x value on screen is fixed
    _y = HEIGHT/2 - 3;  // y depends on height of screen and height of paddle
    _scale = 5;
    _score = 1;
    _easter = rand() % 100;
}

void Avenger::draw(N5110 &lcd) {
    // draw paddle in screen buffer.
    if (_easter > 99) {
        lcd.drawSprite(_x,_y,6,11,(int *)easteregg);
    } else {
        lcd.drawSprite(_x,_y,6,11,(int *)player0);
    }
}

void Avenger::update(Direction d,float mag,Vector2D mapped) {
    
    _y -= int(mapped.y * _scale);  // scale is arbitrary, could be changed in future

    // update y value depending on direction of movement
    // North is decrement as origin is at the top-left so decreasing moves up
    
    // check the y origin to ensure that the paddle doesn't go off screen
    if (_y < 1) {
        _y = 1;
    }
    if (_y > HEIGHT - 7) {
        _y = HEIGHT - 7;
    }
}

void Avenger::add_score() {
    _score++;
}

void Avenger::lose_score() {
    _score--;
}

int Avenger::get_score() {
    return _score;
}

Vector2D Avenger::get_pos() {
    Vector2D p = {_x,_y};
    return p;    
}