ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Project_el15as

Dependencies:   mbed

Revision:
5:158e2951654b
Parent:
4:1f7f32f3e017
Child:
7:b54323241435
diff -r 1f7f32f3e017 -r 158e2951654b Player/Player.cpp
--- a/Player/Player.cpp	Mon Apr 24 22:32:40 2017 +0000
+++ b/Player/Player.cpp	Sun Apr 30 22:51:35 2017 +0000
@@ -30,6 +30,8 @@
     for (int i = 0; i < 8; i++) {
 		collisionPoint[i] = points[i];
     };
+
+    _isJumping = true;
 }
 
 void Player::draw(N5110 &lcd)
@@ -38,10 +40,8 @@
     lcd.drawRect(_x, _y, 3, 3, FILL_TRANSPARENT);
 }
 
-void Player::update(float jx, Serial &pc)
+void Player::update(float jx, Serial &pc, int lvl, bool theSwitch)
 {
-    //_velocity.x = jx;
-
     if (jx < 0) {
         _velocity.x = int(jx*3.2f); // to make up for mechanical imbalance
     } else {
@@ -51,16 +51,20 @@
     // Jump if not already jumping and the jump key was pressed
     if (_didReleaseJumpKey && !_isJumping && _didPressJump)
     {
-        _isJumping = true;
-        _didReleaseJumpKey = false;
-        _velocity.y = JUMPSPEED;
+        apply_jump(lvl, theSwitch);
     }
 
     // Jump key released
-    if (!_didPressJump)
+    if (!_didPressJump) {
         _didReleaseJumpKey = true;
+    }
+    apply_gravity(lvl, theSwitch);
 
-    _velocity.y += GRAVITY; // apply gravity
+    // Assume player is in the air to prevent jumping
+    // If that's not true it will be set to false in collsion detection
+    _isJumping = true;
+
+    //if (_velocity.y > 6) _velocity.y = 6; // Terminal velocity
 }
 
 intVector2D Player::get_pos() {
@@ -88,3 +92,27 @@
     _velocity.y = 0;
 }
 
+void Player::apply_gravity(int lvl, bool theSwitch)
+{
+    // Opening the door on level 2 inverts gravity
+    if ((lvl == 2 || lvl == 3) && theSwitch == true) {
+        _velocity.y -= GRAVITY;
+    }
+    else {
+        _velocity.y += GRAVITY;
+    }
+}
+
+void Player::apply_jump(int lvl, bool theSwitch)
+{
+    _isJumping = true;
+    _didReleaseJumpKey = false;
+
+    // Opening the door on level 2 inverts gravity
+    if ((lvl == 2 || lvl == 3) && theSwitch == true) {
+        _velocity.y = -JUMPSPEED;
+    }
+    else {
+        _velocity.y = JUMPSPEED;
+    }
+}
\ No newline at end of file