Simplify main loop 3

This commit is contained in:
Miloslav Číž 2018-09-10 19:02:23 +02:00
parent 96046c8689
commit 86ce3a20df
2 changed files with 83 additions and 128 deletions

View file

@ -768,15 +768,6 @@ void draw()
previousDepth = pos.depth; previousDepth = pos.depth;
} }
/*
pokitto.display.setColor(rgbToIndex(7,7,3));
pokitto.display.setCursor(1,1);
pokitto.display.print(player.mCamera.position.x);
pokitto.display.print(" ");
pokitto.display.print(player.mCamera.position.y);
pokitto.display.print(" ");
pokitto.display.print(player.mCamera.direction);
*/
} }
bool runReleased = false; bool runReleased = false;

136
demo3.cpp
View file

@ -274,21 +274,39 @@ 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; strafe = pokitto.aBtn();
Vector2D d = angleToDirection(player.mCamera.direction); if (pokitto.cBtn())
{
if (editReleased)
{
editing = !editing;
if (editing)
{
Vector2D facingOffset; Vector2D facingOffset;
facingOffset.x = 0;
facingOffset.y = 0;
if (player.mCamera.direction > (4 * UNITS_PER_SQUARE / 12) && if (player.mCamera.direction > (4 * UNITS_PER_SQUARE / 12) &&
player.mCamera.direction <= (8 * UNITS_PER_SQUARE / 12)) player.mCamera.direction <= (8 * UNITS_PER_SQUARE / 12))
@ -311,28 +329,6 @@ int main()
selectedSquare.x = divRoundDown(player.mCamera.position.x,UNITS_PER_SQUARE) + facingOffset.x; selectedSquare.x = divRoundDown(player.mCamera.position.x,UNITS_PER_SQUARE) + facingOffset.x;
selectedSquare.y = divRoundDown(player.mCamera.position.y,UNITS_PER_SQUARE) + facingOffset.y; selectedSquare.y = divRoundDown(player.mCamera.position.y,UNITS_PER_SQUARE) + facingOffset.y;
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 (editReleased)
{
editing = !editing;
changeIndex = (changeIndex + 1) % MAX_CHANGES; changeIndex = (changeIndex + 1) % MAX_CHANGES;
for (uint16_t i = 0; i < MAX_CHANGES; ++i) for (uint16_t i = 0; i < MAX_CHANGES; ++i)
@ -347,9 +343,11 @@ int main()
changes[changeIndex].mColor = colorAt(selectedSquare.x,selectedSquare.y); changes[changeIndex].mColor = colorAt(selectedSquare.x,selectedSquare.y);
changes[changeIndex].mCoords.x = selectedSquare.x; changes[changeIndex].mCoords.x = selectedSquare.x;
changes[changeIndex].mCoords.y = selectedSquare.y; changes[changeIndex].mCoords.y = selectedSquare.y;
editReleased = false;
editCounter = 0; editCounter = 0;
} }
editReleased = false;
}
} }
else else
editReleased = true; editReleased = true;
@ -364,10 +362,10 @@ int main()
editCounter = 4; editCounter = 4;
} }
} }
else if (aButton) else if (strafe)
player.mCamera.shear = min(player.mCamera.shear + 10,60); shearDirection = 1;
else else
moveOffset = d; moveDirection = 1;
} }
else if (pokitto.downBtn()) else if (pokitto.downBtn())
{ {
@ -379,77 +377,43 @@ int main()
editCounter = 4; editCounter = 4;
} }
} }
else if (aButton) else if (strafe)
player.mCamera.shear = max(player.mCamera.shear - 10,-60); shearDirection = -1;
else else
{ moveDirection = -1;
moveOffset.x = -1 * d.x;
moveOffset.y = -1 * d.y;
}
}
else
{
player.mCamera.shear /= 2;
} }
if (!aButton) uint16_t colorAddition = 0;
player.mCamera.shear /= 2;
int addition = 0;
if (pokitto.rightBtn()) if (pokitto.rightBtn())
addition = 1; {
if (strafe)
moveDirection = 1;
else if (!editing)
rotationDirection = 1;
else
colorAddition = 1;
}
else if (pokitto.leftBtn()) else if (pokitto.leftBtn())
addition = -1; {
if (strafe)
moveDirection = - 1;
else if (!editing)
rotationDirection = -1;
else
colorAddition = -1;
}
if (editing) if (editing && editCounter == 0)
{ {
if (editCounter == 0) changes[changeIndex].mColor = wrap(changes[changeIndex].mColor + colorAddition,SQUARE_COLORS);
{
changes[changeIndex].mColor = wrap(changes[changeIndex].mColor + addition,SQUARE_COLORS);
editCounter = 4; editCounter = 4;
} }
}
else
{
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;
}
else
{
int16_t rotationStep = (dt * PLAYER_ROTATION_SPEED) / 1000;
player.mCamera.direction = wrap(player.mCamera.direction + addition * rotationStep,UNITS_PER_SQUARE);
}
}
editCounter = max(0, editCounter - 1); editCounter = max(0, editCounter - 1);
Unit prevHeight = player.mCamera.height; player.update(moveDirection,strafe,rotationDirection,pokitto.bBtn(),shearDirection,
floorHeightAt,0,true,dt);
moveCameraWithCollision(&player.mCamera,moveOffset,player.mVericalSpeed,
floorHeightAt, 0, 1);
Unit heightDiff = player.mCamera.height - prevHeight;
if (heightDiff == 0)
player.mVericalSpeed = 0; // hit floor
if (!editing && 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
} }
} }