ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Doodler/Doodler.cpp

Committer:
el17m2h
Date:
2019-04-16
Revision:
9:5e53bca2a4c2
Parent:
8:90e789413e0b
Child:
10:e1d2289705ef

File content as of revision 9:5e53bca2a4c2:

#include "Doodler.h"
Doodler::Doodler(){
}
Doodler::~Doodler(){
}

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

void Doodler::draw(N5110 &lcd){
    lcd.drawCircle(_pos.x, _pos.y, _radius, FILL_BLACK);
}

void Doodler::update(Direction d, float mag, Vector2D current_vel){
//////////////////////////// y - direction of doodler /////////////////////////////////////
    _current_vel = current_vel;
    
    if (_current_vel.y > 0 ){ // no jump 
        _velocity.y *=  _gravity; // falls accelerating 
    } else if(_current_vel.y < 0){ // jump has started and continues 
        _velocity.y = -(_velocity.y*_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
    } else if (_current_vel.y == 0) { // jumping has finished
        _velocity.y = _gravity;
    }
    _pos.y += _velocity.y; 
    
/////////////////////////// x - direction of doodler /////////////////////////////////////////
    _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
        _pos.x -= _velocity.x;
    } else if (d == E){
        _pos.x += _velocity.x;
    }
    
    // checking doodler does not leave screen:
    if (_pos.x > WIDTH-4){ // right side
        _pos.x = WIDTH-4;
    }
    if (_pos.x < _radius){ // left side
        _pos.x = _radius;
    }
    set_position(_pos);
    set_velocity(_velocity);
}

Vector2D Doodler::get_position(){
    Vector2D p = {_pos.x, _pos.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){
    _pos.x = p.x; 
    _pos.y = p.y;
}