From d7aad665e20359c17e863d37fa16d534bf7a954b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 8 Sep 2018 07:45:54 +0200 Subject: [PATCH] Add jumping --- game.cpp | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/game.cpp b/game.cpp index 50f9be2..32504cb 100644 --- a/game.cpp +++ b/game.cpp @@ -675,13 +675,13 @@ class Character { public: Camera mCamera; + Unit mVericalSpeed; Character() { mCamera.position.x = 7807;//UNITS_PER_SQUARE * 1; mCamera.position.y = 18489;//UNITS_PER_SQUARE * 5; mCamera.direction = -3550;//200; - mCamera.fovAngle = UNITS_PER_SQUARE / 4; mCamera.height = UNITS_PER_SQUARE * 3; mCamera.resolution.x = 110 / SUBSAMPLE; mCamera.resolution.y = 88; @@ -691,7 +691,9 @@ public: mCamera.collisionHeightBelow = UNITS_PER_SQUARE; mCamera.collisionHeightAbove = UNITS_PER_SQUARE / 3; mCamera.collisionStepHeight = UNITS_PER_SQUARE / 2; - } + + mVericalSpeed = 0; + } }; Character player; @@ -848,13 +850,13 @@ int main() { draw(); - int16_t step = max(1,UNITS_PER_SQUARE / (p.bBtn() ? 7 : 15)); + int16_t step = max(1,UNITS_PER_SQUARE / 15); const int16_t step2 = max(UNITS_PER_SQUARE / 40,1); Vector2D d = angleToDirection(player.mCamera.direction); Vector2D moveOffset; - Unit heightOffset = -100; + Unit heightOffset = player.mVericalSpeed; moveOffset.x = 0; moveOffset.y = 0; @@ -916,8 +918,28 @@ int main() else player.mCamera.direction += addition * step2; + Unit prevHeight = player.mCamera.height; + moveCameraWithCollision(&player.mCamera,moveOffset,heightOffset, floorHeightAt, ceilingHeightAt); + + Unit heightDiff = player.mCamera.height - prevHeight; + + if (heightDiff == 0) + player.mVericalSpeed = 0; // hit floor/ceiling + + + if (player.mVericalSpeed == 0 && p.bBtn()) + { + int16_t camX = divRoundDown(player.mCamera.position.x,UNITS_PER_SQUARE); + int16_t camY = divRoundDown(player.mCamera.position.y,UNITS_PER_SQUARE); + + if (player.mCamera.height - player.mCamera.collisionHeightBelow - + floorHeightAt(camX,camY) < 2) + player.mVericalSpeed = 200; // jump + } + + player.mVericalSpeed -= 20; // gravity } }