Use delta timing

This commit is contained in:
Miloslav Číž 2018-09-08 15:53:11 +02:00
parent 9129659e62
commit f6cdc77a59
2 changed files with 33 additions and 11 deletions

View file

@ -699,14 +699,24 @@ int main()
pokitto.setFrameRate(FPS); pokitto.setFrameRate(FPS);
pokitto.display.setFont(fontTiny); pokitto.display.setFont(fontTiny);
uint32_t previousTime = 0;
uint32_t dt;
while (pokitto.isRunning()) while (pokitto.isRunning())
{ {
if (pokitto.update()) if (pokitto.update())
{ {
uint32_t timeNow = pokitto.getTime();
dt = timeNow - previousTime;
previousTime = timeNow;
draw(); draw();
int16_t step = max(1,UNITS_PER_SQUARE / 7); int16_t horizontalStep = (dt * PLAYER_SPEED) / 1000;
const int16_t step2 = max(UNITS_PER_SQUARE / 25,1); const int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000;
Vector2D d = angleToDirection(player.mCamera.direction); Vector2D d = angleToDirection(player.mCamera.direction);
@ -715,13 +725,13 @@ int main()
moveOffset.x = 0; moveOffset.x = 0;
moveOffset.y = 0; moveOffset.y = 0;
d.x = (d.x * step) / UNITS_PER_SQUARE; d.x = (d.x * horizontalStep) / UNITS_PER_SQUARE;
d.y = (d.y * step) / UNITS_PER_SQUARE; d.y = (d.y * horizontalStep) / UNITS_PER_SQUARE;
if (d.x == 0 && d.y == 0) if (d.x == 0 && d.y == 0)
{ {
d.x = d.x > 0 ? step : -step; d.x = d.x > 0 ? horizontalStep : -1 * horizontalStep;
d.y = d.y > 0 ? step : -step; d.y = d.y > 0 ? horizontalStep : -1 * horizontalStep;
} }
bool strafe = pokitto.aBtn(); bool strafe = pokitto.aBtn();
@ -760,13 +770,13 @@ int main()
if (strafe) if (strafe)
{ {
d = angleToDirection(player.mCamera.direction + UNITS_PER_SQUARE / 4); d = angleToDirection(player.mCamera.direction + UNITS_PER_SQUARE / 4);
d.x = (d.x * step * addition) / UNITS_PER_SQUARE; d.x = (d.x * horizontalStep * addition) / UNITS_PER_SQUARE;
d.y = (d.y * step * addition) / UNITS_PER_SQUARE; d.y = (d.y * horizontalStep * addition) / UNITS_PER_SQUARE;
moveOffset = d; moveOffset = d;
} }
else else
player.mCamera.direction += addition * step2; player.mCamera.direction += addition * rotationStep;
Unit prevHeight = player.mCamera.height; Unit prevHeight = player.mCamera.height;
@ -788,7 +798,7 @@ int main()
player.mVericalSpeed = 500; // jump player.mVericalSpeed = 500; // jump
} }
player.mVericalSpeed -= 80; // gravity player.mVericalSpeed -= (dt * GRAVITY_ACCELERATION) / 1000; // gravity
} }
} }

View file

@ -21,6 +21,18 @@ Pokitto::Core pokitto;
#define FPS 30 #define FPS 30
#endif #endif
#ifndef PLAYER_SPEED
#define PLAYER_SPEED (2 * UNITS_PER_SQUARE)
#endif
#ifndef PLAYER_ROTATION_SPEED
#define PLAYER_ROTATION_SPEED (UNITS_PER_SQUARE / 2)
#endif
#ifndef GRAVITY_ACCELERATION
#define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2)
#endif
#define SCREEN_WIDTH 110 #define SCREEN_WIDTH 110
#define SCREEN_HEIGHT 88 #define SCREEN_HEIGHT 88