From 96046c8689dfb4acc141e3d4ebc970ec799d9300 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:30:19 +0200 Subject: [PATCH] Simplify main loop 2 --- demo2.cpp | 71 ++++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/demo2.cpp b/demo2.cpp index d19f15f..37abbb6 100644 --- a/demo2.cpp +++ b/demo2.cpp @@ -841,6 +841,10 @@ int main() uint32_t previousTime = 0; uint32_t dt; + int16_t moveDirection; + int16_t rotationDirection; + bool strafe; + while (pokitto.isRunning()) { if (pokitto.update()) @@ -851,10 +855,10 @@ int main() dt = timeNow - previousTime; previousTime = timeNow; - int16_t horizontalStep = (dt * PLAYER_SPEED * (pokitto.bBtn() ? 2 : 1)) / 1000; - - Vector2D d = angleToDirection(player.mCamera.direction); - + moveDirection = 0; + rotationDirection = 0; + strafe = false; + if (shotFired) { // update the shot @@ -874,63 +878,34 @@ int main() else if (pokitto.cBtn()) { shotPosition = player.mCamera.position; - shotDirection = d; + shotDirection = angleToDirection(player.mCamera.direction); shotFired = true; } - 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 strafe = pokitto.aBtn(); if (pokitto.upBtn()) - { - moveOffset = d; - } + moveDirection = 1; else if (pokitto.downBtn()) - { - moveOffset.x = -1 * d.x; - moveOffset.y = -1 * d.y; - } - - int addition = 0; + moveDirection = -1; if (pokitto.rightBtn()) - addition = 1; + { + if (!strafe) + rotationDirection = 1; + else + moveDirection = 1; + } else if (pokitto.leftBtn()) - addition = -1; - - if (strafe) { - 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,0, - floorHeightAt, 0, 0); - - Unit heightDiff = player.mCamera.height - prevHeight; + player.update(moveDirection,strafe,rotationDirection,false,0, + floorHeightAt,0,false,dt); } }