Added basic keyboard input and game thread

This commit is contained in:
2023-07-06 02:07:07 +01:00
parent a08dc7f2a6
commit a573c4f785
3 changed files with 163 additions and 105 deletions

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include "cspt-state.h"
void updateInput(struct gameState * state, struct clientInput * message)
{
@ -16,55 +17,34 @@ void doGameTick(struct gameState * state)
state->timestamp = time(NULL);
for (int index = 0; index < 16; index++)
{
if (state->clients[index].registered == true)
// Calculate acceleration:
if (state->clients[index].leftAcceleration)
{
// Calculate acceleration:
if (state->clients[index].leftAcceleration)
{
state->clients[index].xVelocity -= 0.5;
}
if (state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity += 0.5;
}
if (state->clients[index].upAcceleration)
{
state->clients[index].yVelocity += 0.5;
}
if (state->clients[index].downAcceleration)
{
state->clients[index].yVelocity += 0.5;
}
if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity *= 0.1;
}
if (!state->clients[index].upAcceleration && !state->clients[index].downAcceleration)
{
state->clients[index].yVelocity *= 0.1;
}
state->clients[index].xVelocity -= 0.1;
}
if (state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity += 0.1;
}
if (state->clients[index].upAcceleration)
{
state->clients[index].yVelocity -= 0.1;
}
if (state->clients[index].downAcceleration)
{
state->clients[index].yVelocity += 0.1;
}
// Clamp speed:
if (state->clients[index].xVelocity > 15)
{
state->clients[index].xVelocity = 15;
}
if (state->clients[index].xVelocity < -15)
{
state->clients[index].xVelocity = -15;
}
if (state->clients[index].yVelocity > 15)
{
state->clients[index].yVelocity = 15;
}
if (state->clients[index].yVelocity < -15)
{
state->clients[index].yVelocity = -15;
}
// Do movement:
state->clients[index].xPosition += state->clients[index].xVelocity;
state->clients[index].yPosition += state->clients[index].yVelocity;
// Do movement:
state->clients[index].xPosition += state->clients[index].xVelocity;
state->clients[index].yPosition += state->clients[index].yVelocity;
if(state->clients[index].xPosition > 600 || state->clients[index].xPosition < 0)
{
state->clients[index].xPosition = 300;
}
if(state->clients[index].yPosition > 600 || state->clients[index].yPosition < 0)
{
state->clients[index].yPosition = 300;
}
}
}