Simplify main loop 1

This commit is contained in:
Miloslav Číž 2018-09-10 18:23:53 +02:00
parent f3353d09e2
commit 638987ba6e
2 changed files with 90 additions and 73 deletions

103
demo1.cpp
View file

@ -20,8 +20,6 @@ Player player;
#define SPRITES 7 #define SPRITES 7
#define SPRITE_MAX_DISTANCE 5 * UNITS_PER_SQUARE #define SPRITE_MAX_DISTANCE 5 * UNITS_PER_SQUARE
#define JUMP_SPEED 500
Sprite sprites[SPRITES]; Sprite sprites[SPRITES];
/// For each level square says the texture index. /// For each level square says the texture index.
@ -801,36 +799,26 @@ int main()
uint32_t previousTime = 0; uint32_t previousTime = 0;
uint32_t dt; uint32_t dt;
int16_t moveDirection;
int16_t shearDirection;
int16_t rotationDirection;
bool strafe;
while (pokitto.isRunning()) while (pokitto.isRunning())
{ {
if (pokitto.update()) if (pokitto.update())
{ {
draw(); draw();
moveDirection = 0;
shearDirection = 0;
rotationDirection = 0;
strafe = false;
uint32_t timeNow = pokitto.getTime(); uint32_t timeNow = pokitto.getTime();
dt = timeNow - previousTime; dt = timeNow - previousTime;
previousTime = timeNow; 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 (pokitto.cBtn())
{ {
if (runReleased) if (runReleased)
@ -842,71 +830,40 @@ int main()
else else
runReleased = true; runReleased = true;
strafe = pokitto.aBtn();
if (pokitto.upBtn()) if (pokitto.upBtn())
{ {
if (aButton) if (!strafe)
player.mCamera.shear = min(player.mCamera.shear + 10,60); moveDirection = 1;
else else
moveOffset = d; shearDirection = 1;
} }
else if (pokitto.downBtn()) else if (pokitto.downBtn())
{ {
if (aButton) if (!strafe)
player.mCamera.shear = max(player.mCamera.shear - 10,-60); moveDirection = -1;
else else
{ shearDirection = -1;
moveOffset.x = -1 * d.x;
moveOffset.y = -1 * d.y;
}
} }
else
player.mCamera.shear /= 2;
if (!aButton)
player.mCamera.shear /= 2;
int addition = 0;
if (pokitto.rightBtn()) if (pokitto.rightBtn())
addition = 1; {
if (!strafe)
rotationDirection = 1;
else
moveDirection = 1;
}
else if (pokitto.leftBtn()) else if (pokitto.leftBtn())
addition = -1;
if (aButton)
{ {
d = angleToDirection(player.mCamera.direction + UNITS_PER_SQUARE / 4); if (!strafe)
d.x = (d.x * horizontalStep * addition) / UNITS_PER_SQUARE; rotationDirection = -1;
d.y = (d.y * horizontalStep * addition) / UNITS_PER_SQUARE; else
moveDirection = -1;
moveOffset = d;
} }
else
{
int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000;
player.mCamera.direction += addition * rotationStep;
}
Unit prevHeight = player.mCamera.height; player.update(moveDirection,strafe,rotationDirection,pokitto.bBtn(),shearDirection,
floorHeightAt,ceilingHeightAt,true,dt);
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
} }
} }

View file

@ -29,6 +29,10 @@ Pokitto::Core pokitto;
#define PLAYER_ROTATION_SPEED (UNITS_PER_SQUARE / 2) #define PLAYER_ROTATION_SPEED (UNITS_PER_SQUARE / 2)
#endif #endif
#ifndef PLAYER_JUMP_SPEED
#define PLAYER_JUMP_SPEED 500
#endif
#ifndef GRAVITY_ACCELERATION #ifndef GRAVITY_ACCELERATION
#define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2) #define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2)
#endif #endif
@ -227,6 +231,62 @@ public:
squareX * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, squareX * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2,
squareY * 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 class Sprite