ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Doodler/Doodler.cpp

Committer:
el17m2h
Date:
2019-04-18
Revision:
13:10851784af9a
Parent:
12:c5123abb4fbe
Child:
14:529f798adae4

File content as of revision 13:10851784af9a:

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

void Doodler::init(int radius){
// initial position of doodler at centre 
    _radius = radius;
    _pos_x = 35; 
    _pos_y = 35;
    _velocity_y = 1.1;
    _gravity = 1.1; // moves down
    _up = -0.5; // elasticity
}

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

void Doodler::update(Direction d, float mag){
//////////////////////////// y - direction of doodler /////////////////////////////////////
    _velocity_y = (double)get_velocity_y(); 
    
    if ((double)_velocity_y >= 0.00){ // no jump 
        _pos_y += _velocity_y; 
        _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating 
    } else { // jump has started until vel = 0 
        _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
        _pos_y += _velocity_y*5;
        
        if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
            _velocity_y = _gravity;
        }
    }
    
/////////////////////////// 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.00){ // right side
        _pos_x = WIDTH-4.00;
    }
    if (_pos_x < _radius){ // left side
        _pos_x = _radius;
    }
    set_position(_pos_x, _pos_y);
    set_velocity(_velocity_x, (double)_velocity_y);
}

float Doodler::get_position_x(){
    float p_x = _pos_x; 
    return p_x;
}

float Doodler::get_position_y(){
    float p_y = _pos_y;
    return p_y;
}

float Doodler::get_velocity_x(){
    float v_x = _velocity_x;
    return v_x;
}

double Doodler::get_velocity_y(){
    double v_y = (double)_velocity_y;
    return (double)v_y;
}

void Doodler::set_velocity(float v_x, double v_y){
    _velocity_x = v_x;
    _velocity_y = (double)v_y;
}

void Doodler::set_position(float p_x, float p_y){
    _pos_x = p_x; 
    _pos_y = p_y;
}