ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Doodler/Doodler.cpp

Committer:
el17m2h
Date:
2019-04-14
Revision:
7:0d9cee90ab0d
Parent:
6:848d1e4c1a31
Child:
8:90e789413e0b

File content as of revision 7:0d9cee90ab0d:

#include "Doodler.h"

Doodler::Doodler(){
}
Doodler::~Doodler(){
}

void Doodler::init(int radius){
// initial position of doodler at centre 
    _x = (70/2)+6; 
    _y = 24; 
    _radius = radius;
    _velocity.y = 1; // dropped down
    _gravity = 1; // moves down
    _up = -0.5;
}

void Doodler::draw(N5110 &lcd){
    lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
}

void Doodler::update(Direction d, float mag){
    if (_y < 43 ){
        _y += _velocity.y;
        _velocity.y = _gravity; // gravity pulls it down
    } else {
        _velocity.y = _up; //jumps
        _y += _velocity.y*20;
    }
    
    _velocity.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
    if (d == W){ // if direction is left
        _x-= _velocity.x;
    } else if (d == E){
        _x+= _velocity.x;
    }
    
// checking doodler does not leave screen:
    if (_x > WIDTH-4){ // right side
        _x = WIDTH-4;
    }
    if (_x < _radius){ // left side
        _x = _radius;
    }    
}


Vector2D Doodler::get_position(){
    Vector2D p = {_x,_y};
    return p;
}

Vector2D Doodler::get_velocity(){
    Vector2D v = {_velocity.x, _velocity.y};
    return v;
}

void Doodler::set_velocity(Vector2D v){
    _velocity.x = v.x;
    _velocity.y = v.y;
}

void Doodler::set_position(Vector2D p){
    _x = p.x;
    _y = p.y;
}