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;
}
/*
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;

200
demo3.cpp
View file

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