2023-07-06 02:07:07 +01:00
|
|
|
#include <stdio.h>
|
2023-08-03 00:15:44 +01:00
|
|
|
#include <stdint.h>
|
2023-07-02 13:23:07 +01:00
|
|
|
#include "cspt-state.h"
|
2023-08-03 00:15:44 +01:00
|
|
|
|
2023-07-06 00:22:59 +01:00
|
|
|
void updateInput(struct gameState * state, struct clientInput * message)
|
2023-07-02 23:08:59 +01:00
|
|
|
{
|
2023-07-06 00:22:59 +01:00
|
|
|
if(message->clientNumber < 16 && message->clientNumber >= 0)
|
2023-07-02 23:08:59 +01:00
|
|
|
{
|
2023-07-06 00:22:59 +01:00
|
|
|
state->clients[message->clientNumber].leftAcceleration = message->left;
|
|
|
|
state->clients[message->clientNumber].rightAcceleration = message->right;
|
|
|
|
state->clients[message->clientNumber].upAcceleration = message->up;
|
|
|
|
state->clients[message->clientNumber].downAcceleration = message->down;
|
2023-07-02 23:08:59 +01:00
|
|
|
}
|
2023-07-06 00:22:59 +01:00
|
|
|
|
2023-07-02 23:08:59 +01:00
|
|
|
}
|
2023-07-02 13:23:07 +01:00
|
|
|
|
|
|
|
void doGameTick(struct gameState * state)
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
if ((state->tickNumber % UINT64_MAX) == 0)
|
|
|
|
{
|
|
|
|
state->tickNumber = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tickNumber++;
|
|
|
|
}
|
2023-07-02 13:23:07 +01:00
|
|
|
for (int index = 0; index < 16; index++)
|
|
|
|
{
|
2023-07-06 02:07:07 +01:00
|
|
|
// Calculate acceleration:
|
|
|
|
if (state->clients[index].leftAcceleration)
|
2023-07-02 13:23:07 +01:00
|
|
|
{
|
2023-07-09 01:15:05 +01:00
|
|
|
state->clients[index].xVelocity -= 0.5;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
|
|
|
if (state->clients[index].rightAcceleration)
|
|
|
|
{
|
2023-07-09 01:15:05 +01:00
|
|
|
state->clients[index].xVelocity += 0.5;
|
|
|
|
}
|
|
|
|
if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration)
|
|
|
|
{
|
|
|
|
state->clients[index].xVelocity *= 0.9;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
|
|
|
if (state->clients[index].upAcceleration)
|
|
|
|
{
|
2023-07-09 01:15:05 +01:00
|
|
|
state->clients[index].yVelocity -= 0.5;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
|
|
|
if (state->clients[index].downAcceleration)
|
|
|
|
{
|
2023-07-09 01:15:05 +01:00
|
|
|
state->clients[index].yVelocity += 0.5;
|
|
|
|
}
|
|
|
|
if (!state->clients[index].upAcceleration && !state->clients[index].downAcceleration)
|
|
|
|
{
|
|
|
|
state->clients[index].yVelocity *= 0.9;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
2023-08-03 00:15:44 +01:00
|
|
|
|
2023-07-06 02:07:07 +01:00
|
|
|
// Do movement:
|
|
|
|
state->clients[index].xPosition += state->clients[index].xVelocity;
|
|
|
|
state->clients[index].yPosition += state->clients[index].yVelocity;
|
2023-07-13 00:44:25 +01:00
|
|
|
if(state->clients[index].xPosition > 512)
|
2023-07-09 01:15:05 +01:00
|
|
|
{
|
2023-07-13 00:44:25 +01:00
|
|
|
state->clients[index].xPosition = 0;
|
2023-07-09 01:15:05 +01:00
|
|
|
}
|
|
|
|
if(state->clients[index].xPosition < 0)
|
|
|
|
{
|
2023-07-13 00:44:25 +01:00
|
|
|
state->clients[index].xPosition = 512;
|
2023-07-09 01:15:05 +01:00
|
|
|
}
|
2023-07-13 00:44:25 +01:00
|
|
|
if(state->clients[index].yPosition > 512)
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-13 00:44:25 +01:00
|
|
|
state->clients[index].yPosition = 0;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
2023-07-09 01:15:05 +01:00
|
|
|
if(state->clients[index].yPosition < 0)
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-13 00:44:25 +01:00
|
|
|
state->clients[index].yPosition = 512;
|
2023-07-02 13:23:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|