From 638987ba6ecb2a65a0cb2b23d66bccacf408a5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 10 Sep 2018 18:23:53 +0200 Subject: [PATCH] Simplify main loop 1 --- demo1.cpp | 103 +++++++++++++++------------------------------------- general.hpp | 60 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 73 deletions(-) diff --git a/demo1.cpp b/demo1.cpp index 8436ccb..fee4a2d 100644 --- a/demo1.cpp +++ b/demo1.cpp @@ -20,8 +20,6 @@ Player player; #define SPRITES 7 #define SPRITE_MAX_DISTANCE 5 * UNITS_PER_SQUARE -#define JUMP_SPEED 500 - Sprite sprites[SPRITES]; /// For each level square says the texture index. @@ -801,36 +799,26 @@ int main() uint32_t previousTime = 0; uint32_t dt; + int16_t moveDirection; + int16_t shearDirection; + int16_t rotationDirection; + bool strafe; + while (pokitto.isRunning()) { if (pokitto.update()) { draw(); + moveDirection = 0; + shearDirection = 0; + rotationDirection = 0; + strafe = false; + uint32_t timeNow = pokitto.getTime(); dt = timeNow - previousTime; previousTime = timeNow; - int16_t horizontalStep = (dt * PLAYER_SPEED * (player.mRunning ? 2 : 1)) / 1000; - - Vector2D d = angleToDirection(player.mCamera.direction); - - Vector2D moveOffset; - - moveOffset.x = 0; - moveOffset.y = 0; - - d.x = (d.x * horizontalStep) / UNITS_PER_SQUARE; - d.y = (d.y * horizontalStep) / UNITS_PER_SQUARE; - - if (d.x == 0 && d.y == 0) - { - d.x = d.x > 0 ? horizontalStep : -1 * horizontalStep; - d.y = d.y > 0 ? horizontalStep : -1 * horizontalStep; - } - - bool aButton = pokitto.aBtn(); - if (pokitto.cBtn()) { if (runReleased) @@ -842,71 +830,40 @@ int main() else runReleased = true; + strafe = pokitto.aBtn(); + if (pokitto.upBtn()) { - if (aButton) - player.mCamera.shear = min(player.mCamera.shear + 10,60); + if (!strafe) + moveDirection = 1; else - moveOffset = d; + shearDirection = 1; } else if (pokitto.downBtn()) { - if (aButton) - player.mCamera.shear = max(player.mCamera.shear - 10,-60); + if (!strafe) + moveDirection = -1; else - { - moveOffset.x = -1 * d.x; - moveOffset.y = -1 * d.y; - } + shearDirection = -1; } - else - player.mCamera.shear /= 2; - - if (!aButton) - player.mCamera.shear /= 2; - - int addition = 0; if (pokitto.rightBtn()) - addition = 1; + { + if (!strafe) + rotationDirection = 1; + else + moveDirection = 1; + } else if (pokitto.leftBtn()) - addition = -1; - - if (aButton) { - d = angleToDirection(player.mCamera.direction + UNITS_PER_SQUARE / 4); - d.x = (d.x * horizontalStep * addition) / UNITS_PER_SQUARE; - d.y = (d.y * horizontalStep * addition) / UNITS_PER_SQUARE; - - moveOffset = d; + if (!strafe) + rotationDirection = -1; + else + moveDirection = -1; } - else - { - int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000; - player.mCamera.direction += addition * rotationStep; - } - Unit prevHeight = player.mCamera.height; - - moveCameraWithCollision(&player.mCamera,moveOffset,player.mVericalSpeed, - floorHeightAt, ceilingHeightAt, 1); - - Unit heightDiff = player.mCamera.height - prevHeight; - - if (heightDiff == 0) - player.mVericalSpeed = 0; // hit floor/ceiling - - if (player.mVericalSpeed == 0 && pokitto.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 - CAMERA_COLL_HEIGHT_BELOW - - floorHeightAt(camX,camY) < 2) - player.mVericalSpeed = JUMP_SPEED; // jump - } - - player.mVericalSpeed -= (dt * GRAVITY_ACCELERATION) / 1000; // gravity + player.update(moveDirection,strafe,rotationDirection,pokitto.bBtn(),shearDirection, + floorHeightAt,ceilingHeightAt,true,dt); } } diff --git a/general.hpp b/general.hpp index fb51edb..83fa89f 100644 --- a/general.hpp +++ b/general.hpp @@ -29,6 +29,10 @@ Pokitto::Core pokitto; #define PLAYER_ROTATION_SPEED (UNITS_PER_SQUARE / 2) #endif +#ifndef PLAYER_JUMP_SPEED +#define PLAYER_JUMP_SPEED 500 +#endif + #ifndef GRAVITY_ACCELERATION #define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2) #endif @@ -227,6 +231,62 @@ public: squareX * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, squareY * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2); } + + void update(int16_t moveDirection, bool strafe, int16_t turnDirection, bool jump, + int16_t shearDirection, ArrayFunction floorHeightFunction, + ArrayFunction ceilingHeightFunction, bool computeHeight, uint32_t dt) + { + Vector2D moveOffset; + + moveOffset.x = 0; + moveOffset.y = 0; + + if (moveDirection != 0) + { + int16_t horizontalStep = (dt * PLAYER_SPEED * (mRunning ? 2 : 1)) / 1000 * + (moveDirection > 0 ? 1 : -1); + + moveOffset = angleToDirection(mCamera.direction + (strafe ? UNITS_PER_SQUARE / 4 : 0)); + + moveOffset.x = (moveOffset.x * horizontalStep) / UNITS_PER_SQUARE; + moveOffset.y = (moveOffset.y * horizontalStep) / UNITS_PER_SQUARE; + } + + if (turnDirection != 0) + { + int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000; + mCamera.direction = wrap(mCamera.direction + turnDirection * rotationStep,UNITS_PER_SQUARE); + } + + Unit prevHeight = mCamera.height; + + moveCameraWithCollision(&mCamera,moveOffset,mVericalSpeed, + floorHeightFunction, ceilingHeightFunction, computeHeight ? 1 : 0); + + Unit heightDiff = mCamera.height - prevHeight; + + if (heightDiff == 0) + mVericalSpeed = 0; // hit floor/ceiling + + if (jump && mVericalSpeed == 0) + { + int16_t camX = divRoundDown(mCamera.position.x,UNITS_PER_SQUARE); + int16_t camY = divRoundDown(mCamera.position.y,UNITS_PER_SQUARE); + + if (mCamera.height - CAMERA_COLL_HEIGHT_BELOW - + floorHeightFunction(camX,camY) < 2) + mVericalSpeed = PLAYER_JUMP_SPEED; // jump + } + + if (shearDirection != 0) + mCamera.shear = clamp(mCamera.shear + shearDirection * 10, + -1 * mCamera.resolution.y, mCamera.resolution.y); + else + mCamera.shear /= 2; + + if (computeHeight) + mVericalSpeed -= (dt * GRAVITY_ACCELERATION) / 1000; // gravity + } }; class Sprite