ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Doodler/Doodler.cpp

Committer:
el17m2h
Date:
2019-04-21
Revision:
14:529f798adae4
Parent:
13:10851784af9a
Child:
15:4efa04a6a376

File content as of revision 14:529f798adae4:

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

void Doodler::init(int radius, float position_x, float position_y, double velocity_y){  
// initial position of doodler at centre 
    _radius = radius;
    _velocity_y = (double)velocity_y;
    _position_x = position_x;
    _position_y = position_y;
    _gravity = 1.1; // moves down
    _up = -0.5; // elasticity
    
    if ((double)_velocity_y > 0.00){ // no jump 
        _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating 
    } else if ( (double)_velocity_y == 0.00){
        _velocity_y = (double)_gravity; 
    } else { 
        _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
        if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
            _velocity_y = 0.00;
        }
    }
}

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

void Doodler::update(Direction d, float mag, double velocity_y){
//////////////////////////// y - direction of doodler /////////////////////////////////////
    _position_x = get_position_x(); 
    _position_y = get_position_y(); 
    _velocity_y = (double)velocity_y;
    if ((double)_velocity_y >= 0.00){ // no jump 
        _position_y += _velocity_y; 
    } else { // jump has started until vel = 0 
        _position_y += _velocity_y*5;
    }
    
/////////////////////////// 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
        _position_x -= _velocity_x;
    } else if (d == E){
        _position_x += _velocity_x;
    }
    // checking doodler does not leave screen:
    if (_position_x > WIDTH-4.00){ // right side
        _position_x = WIDTH-4.00;
    }
    if (_position_x < _radius){ // left side
        _position_x = _radius;
    }
}


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

float Doodler::get_position_y(){
    float p_y = _position_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 vel_x, double vel_y){
    _velocity_x = vel_x;
    _velocity_y = (double)vel_y;
}

void Doodler::set_position(float pos_x, float pos_y){
    _position_x = pos_x; 
    _position_y = pos_y;
}