ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Tue Apr 16 16:16:08 2019 +0000
Revision:
8:90e789413e0b
Parent:
7:0d9cee90ab0d
Child:
9:5e53bca2a4c2
Changed the update function in the doodler so that it will be reading the position and velocity and setting a new one every time it updates (so that I will not have to write it in the engine file).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 4:8ec314f806ae 1 #include "Doodler.h"
el17m2h 4:8ec314f806ae 2
el17m2h 4:8ec314f806ae 3 Doodler::Doodler(){
el17m2h 4:8ec314f806ae 4 }
el17m2h 4:8ec314f806ae 5 Doodler::~Doodler(){
el17m2h 4:8ec314f806ae 6 }
el17m2h 4:8ec314f806ae 7
el17m2h 5:8814d6de77d0 8 void Doodler::init(int radius){
el17m2h 7:0d9cee90ab0d 9 // initial position of doodler at centre
el17m2h 7:0d9cee90ab0d 10 _x = (70/2)+6;
el17m2h 7:0d9cee90ab0d 11 _y = 24;
el17m2h 5:8814d6de77d0 12 _radius = radius;
el17m2h 8:90e789413e0b 13 _velocity.y = 1.5; // dropped down
el17m2h 8:90e789413e0b 14 _gravity = 1.5; // moves down
el17m2h 8:90e789413e0b 15 _up = 0.9;
el17m2h 4:8ec314f806ae 16 }
el17m2h 4:8ec314f806ae 17
el17m2h 4:8ec314f806ae 18 void Doodler::draw(N5110 &lcd){
el17m2h 4:8ec314f806ae 19 lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
el17m2h 4:8ec314f806ae 20 }
el17m2h 4:8ec314f806ae 21
el17m2h 8:90e789413e0b 22 void Doodler::update(Direction d, float mag, Vector2D current_pos, Vector2D current_vel){
el17m2h 8:90e789413e0b 23 ////////////// y - direction of doodler ////////////////////////
el17m2h 8:90e789413e0b 24 _current_pos = current_pos; /// tienes que ponerlo para la direccion de x tambien y borrar lo del engine
el17m2h 8:90e789413e0b 25 _current_vel = current_vel;
el17m2h 5:8814d6de77d0 26
el17m2h 8:90e789413e0b 27 if ((_current_vel.y > 0) || (_current_vel.y == 0 )){ // no jump or jump finishes
el17m2h 8:90e789413e0b 28 _new_vel.y *= _gravity; // falls accelerating
el17m2h 8:90e789413e0b 29 } else if(_current_vel.y < 0){ // jump has started and continues
el17m2h 8:90e789413e0b 30 _new_vel.y *= (-_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
el17m2h 8:90e789413e0b 31 }
el17m2h 8:90e789413e0b 32 _current_pos.y += _new_vel.y;
el17m2h 8:90e789413e0b 33
el17m2h 8:90e789413e0b 34 ////////////// x - direction of doodler ////////////////////////
el17m2h 8:90e789413e0b 35 _new_vel.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
el17m2h 5:8814d6de77d0 36 if (d == W){ // if direction is left
el17m2h 8:90e789413e0b 37 _current_pos.x-= _new_vel.x;
el17m2h 5:8814d6de77d0 38 } else if (d == E){
el17m2h 8:90e789413e0b 39 _current_pos.x+= _new_vel.x;
el17m2h 5:8814d6de77d0 40 }
el17m2h 8:90e789413e0b 41 // checking doodler does not leave screen:
el17m2h 8:90e789413e0b 42 if (_current_pos.x > WIDTH-4){ // right side
el17m2h 8:90e789413e0b 43 _current_pos.x = WIDTH-4;
el17m2h 8:90e789413e0b 44 }
el17m2h 8:90e789413e0b 45 if (_current_pos.x < _radius){ // left side
el17m2h 8:90e789413e0b 46 _current_pos.x = _radius;
el17m2h 8:90e789413e0b 47 }
el17m2h 7:0d9cee90ab0d 48
el17m2h 8:90e789413e0b 49 set_position(_current_pos);
el17m2h 8:90e789413e0b 50 set_velocity(_new_vel);
el17m2h 4:8ec314f806ae 51 }
el17m2h 4:8ec314f806ae 52
el17m2h 4:8ec314f806ae 53
el17m2h 7:0d9cee90ab0d 54 Vector2D Doodler::get_position(){
el17m2h 8:90e789413e0b 55 Vector2D p = {_x,_y};
el17m2h 4:8ec314f806ae 56 return p;
el17m2h 4:8ec314f806ae 57 }
el17m2h 7:0d9cee90ab0d 58
el17m2h 7:0d9cee90ab0d 59 Vector2D Doodler::get_velocity(){
el17m2h 7:0d9cee90ab0d 60 Vector2D v = {_velocity.x, _velocity.y};
el17m2h 7:0d9cee90ab0d 61 return v;
el17m2h 7:0d9cee90ab0d 62 }
el17m2h 7:0d9cee90ab0d 63
el17m2h 7:0d9cee90ab0d 64 void Doodler::set_velocity(Vector2D v){
el17m2h 7:0d9cee90ab0d 65 _velocity.x = v.x;
el17m2h 7:0d9cee90ab0d 66 _velocity.y = v.y;
el17m2h 7:0d9cee90ab0d 67 }
el17m2h 7:0d9cee90ab0d 68
el17m2h 7:0d9cee90ab0d 69 void Doodler::set_position(Vector2D p){
el17m2h 8:90e789413e0b 70 _x = (int)p.x; //integer position values
el17m2h 8:90e789413e0b 71 _y = (int)p.y;
el17m2h 7:0d9cee90ab0d 72 }