ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Doodler/Doodler.cpp

Committer:
el17m2h
Date:
2019-04-16
Revision:
8:90e789413e0b
Parent:
7:0d9cee90ab0d
Child:
9:5e53bca2a4c2

File content as of revision 8:90e789413e0b:

#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.5; // dropped down
    _gravity = 1.5; // moves down
    _up = 0.9;
}

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

void Doodler::update(Direction d, float mag, Vector2D current_pos, Vector2D current_vel){
////////////// y - direction of doodler ////////////////////////
    _current_pos = current_pos; /// tienes que ponerlo para la direccion de x tambien y borrar lo del engine
    _current_vel = current_vel;
    
    if ((_current_vel.y > 0) || (_current_vel.y == 0 )){ // no jump or jump finishes
        _new_vel.y *= _gravity; // falls accelerating 
    } else if(_current_vel.y < 0){ // jump has started and continues 
        _new_vel.y *= (-_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
    } 
    _current_pos.y += _new_vel.y; 
    
////////////// x - direction of doodler ////////////////////////
    _new_vel.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
    if (d == W){ // if direction is left
        _current_pos.x-= _new_vel.x;
    } else if (d == E){
        _current_pos.x+= _new_vel.x;
    }
    // checking doodler does not leave screen:
    if (_current_pos.x > WIDTH-4){ // right side
        _current_pos.x = WIDTH-4;
    }
    if (_current_pos.x < _radius){ // left side
        _current_pos.x = _radius;
    }    
    
    set_position(_current_pos);
    set_velocity(_new_vel);
}


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 = (int)p.x; //integer position values    
    _y = (int)p.y;
}