ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu Apr 18 14:54:51 2019 +0000
Revision:
13:10851784af9a
Parent:
12:c5123abb4fbe
Child:
14:529f798adae4
Fixed the order of functions in the update section of the engine so that the doodler and floors are updated after checking both the floors collision and the shift of the floors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 4:8ec314f806ae 1 #include "Doodler.h"
el17m2h 4:8ec314f806ae 2 Doodler::Doodler(){
el17m2h 4:8ec314f806ae 3 }
el17m2h 4:8ec314f806ae 4 Doodler::~Doodler(){
el17m2h 4:8ec314f806ae 5 }
el17m2h 4:8ec314f806ae 6
el17m2h 5:8814d6de77d0 7 void Doodler::init(int radius){
el17m2h 7:0d9cee90ab0d 8 // initial position of doodler at centre
el17m2h 5:8814d6de77d0 9 _radius = radius;
el17m2h 12:c5123abb4fbe 10 _pos_x = 35;
el17m2h 12:c5123abb4fbe 11 _pos_y = 35;
el17m2h 10:e1d2289705ef 12 _velocity_y = 1.1;
el17m2h 10:e1d2289705ef 13 _gravity = 1.1; // moves down
el17m2h 10:e1d2289705ef 14 _up = -0.5; // elasticity
el17m2h 4:8ec314f806ae 15 }
el17m2h 4:8ec314f806ae 16
el17m2h 4:8ec314f806ae 17 void Doodler::draw(N5110 &lcd){
el17m2h 10:e1d2289705ef 18 lcd.drawCircle(_pos_x, _pos_y, _radius, FILL_BLACK);
el17m2h 4:8ec314f806ae 19 }
el17m2h 4:8ec314f806ae 20
el17m2h 13:10851784af9a 21 void Doodler::update(Direction d, float mag){
el17m2h 9:5e53bca2a4c2 22 //////////////////////////// y - direction of doodler /////////////////////////////////////
el17m2h 13:10851784af9a 23 _velocity_y = (double)get_velocity_y();
el17m2h 5:8814d6de77d0 24
el17m2h 10:e1d2289705ef 25 if ((double)_velocity_y >= 0.00){ // no jump
el17m2h 10:e1d2289705ef 26 _pos_y += _velocity_y;
el17m2h 10:e1d2289705ef 27 _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating
el17m2h 10:e1d2289705ef 28 } else { // jump has started until vel = 0
el17m2h 10:e1d2289705ef 29 _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
el17m2h 10:e1d2289705ef 30 _pos_y += _velocity_y*5;
el17m2h 10:e1d2289705ef 31
el17m2h 13:10851784af9a 32 if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
el17m2h 10:e1d2289705ef 33 _velocity_y = _gravity;
el17m2h 10:e1d2289705ef 34 }
el17m2h 9:5e53bca2a4c2 35 }
el17m2h 8:90e789413e0b 36
el17m2h 9:5e53bca2a4c2 37 /////////////////////////// x - direction of doodler /////////////////////////////////////////
el17m2h 10:e1d2289705ef 38 _velocity_x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value)
el17m2h 5:8814d6de77d0 39 if (d == W){ // if direction is left
el17m2h 10:e1d2289705ef 40 _pos_x -= _velocity_x;
el17m2h 5:8814d6de77d0 41 } else if (d == E){
el17m2h 10:e1d2289705ef 42 _pos_x += _velocity_x;
el17m2h 8:90e789413e0b 43 }
el17m2h 7:0d9cee90ab0d 44
el17m2h 9:5e53bca2a4c2 45 // checking doodler does not leave screen:
el17m2h 10:e1d2289705ef 46 if (_pos_x > WIDTH-4.00){ // right side
el17m2h 10:e1d2289705ef 47 _pos_x = WIDTH-4.00;
el17m2h 9:5e53bca2a4c2 48 }
el17m2h 10:e1d2289705ef 49 if (_pos_x < _radius){ // left side
el17m2h 10:e1d2289705ef 50 _pos_x = _radius;
el17m2h 9:5e53bca2a4c2 51 }
el17m2h 10:e1d2289705ef 52 set_position(_pos_x, _pos_y);
el17m2h 10:e1d2289705ef 53 set_velocity(_velocity_x, (double)_velocity_y);
el17m2h 4:8ec314f806ae 54 }
el17m2h 4:8ec314f806ae 55
el17m2h 10:e1d2289705ef 56 float Doodler::get_position_x(){
el17m2h 10:e1d2289705ef 57 float p_x = _pos_x;
el17m2h 10:e1d2289705ef 58 return p_x;
el17m2h 10:e1d2289705ef 59 }
el17m2h 10:e1d2289705ef 60
el17m2h 10:e1d2289705ef 61 float Doodler::get_position_y(){
el17m2h 10:e1d2289705ef 62 float p_y = _pos_y;
el17m2h 10:e1d2289705ef 63 return p_y;
el17m2h 4:8ec314f806ae 64 }
el17m2h 7:0d9cee90ab0d 65
el17m2h 10:e1d2289705ef 66 float Doodler::get_velocity_x(){
el17m2h 10:e1d2289705ef 67 float v_x = _velocity_x;
el17m2h 10:e1d2289705ef 68 return v_x;
el17m2h 10:e1d2289705ef 69 }
el17m2h 10:e1d2289705ef 70
el17m2h 10:e1d2289705ef 71 double Doodler::get_velocity_y(){
el17m2h 10:e1d2289705ef 72 double v_y = (double)_velocity_y;
el17m2h 10:e1d2289705ef 73 return (double)v_y;
el17m2h 7:0d9cee90ab0d 74 }
el17m2h 7:0d9cee90ab0d 75
el17m2h 10:e1d2289705ef 76 void Doodler::set_velocity(float v_x, double v_y){
el17m2h 10:e1d2289705ef 77 _velocity_x = v_x;
el17m2h 10:e1d2289705ef 78 _velocity_y = (double)v_y;
el17m2h 7:0d9cee90ab0d 79 }
el17m2h 7:0d9cee90ab0d 80
el17m2h 10:e1d2289705ef 81 void Doodler::set_position(float p_x, float p_y){
el17m2h 10:e1d2289705ef 82 _pos_x = p_x;
el17m2h 10:e1d2289705ef 83 _pos_y = p_y;
el17m2h 7:0d9cee90ab0d 84 }