Basic Client Side Prediction

This commit is contained in:
2023-07-09 01:15:05 +01:00
parent 16614a28ba
commit 4dbad3440d
4 changed files with 52 additions and 12 deletions

View File

@ -91,6 +91,15 @@ void * networkHandler(void * parameters)
}
}
void * gameThreadHandler(void * arguments)
{
while (true)
{
doGameTick(arguments);
usleep(15625);
}
}
void * graphicsThreadHandler(void * parameters)
{
struct gameState * state = ((struct threadParameters *)parameters)->state;
@ -201,7 +210,7 @@ void * graphicsThreadHandler(void * parameters)
int main(int argc, char ** argv)
{
int serverSocket = 0;
pthread_t graphicsThread, networkThread;
pthread_t graphicsThread, networkThread, gameThread;
struct CsptMessage currentMessage;
bool continueRunning = true;
struct gameState * currentState = calloc(1, sizeof(struct gameState));
@ -249,6 +258,7 @@ int main(int argc, char ** argv)
parameters.message = clientInput;
parameters.state = currentState;
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, &parameters);
pthread_create(&gameThread, NULL, gameThreadHandler, currentState);
pthread_create(&networkThread, NULL, networkHandler, &parameters);
while (continueRunning)

View File

@ -20,31 +20,50 @@ void doGameTick(struct gameState * state)
// Calculate acceleration:
if (state->clients[index].leftAcceleration)
{
state->clients[index].xVelocity -= 0.1;
state->clients[index].xVelocity -= 0.5;
}
if (state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity += 0.1;
state->clients[index].xVelocity += 0.5;
}
if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity *= 0.9;
}
if (state->clients[index].upAcceleration)
{
state->clients[index].yVelocity -= 0.1;
state->clients[index].yVelocity -= 0.5;
}
if (state->clients[index].downAcceleration)
{
state->clients[index].yVelocity += 0.1;
state->clients[index].yVelocity += 0.5;
}
if (!state->clients[index].upAcceleration && !state->clients[index].downAcceleration)
{
state->clients[index].yVelocity *= 0.9;
}
// 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)
if(state->clients[index].xPosition > 1000)
{
state->clients[index].xPosition = 300;
state->clients[index].xPosition = 1000;
state->clients[index].xVelocity = 0;
}
if(state->clients[index].yPosition > 600 || state->clients[index].yPosition < 0)
if(state->clients[index].xPosition < 0)
{
state->clients[index].yPosition = 300;
state->clients[index].xPosition = 0;
state->clients[index].xVelocity = 0;
}
if(state->clients[index].yPosition > 1000)
{
state->clients[index].yPosition = 1000;
state->clients[index].yVelocity = 0;
}
if(state->clients[index].yPosition < 0)
{
state->clients[index].yPosition = 0;
state->clients[index].yVelocity = 0;
}
}
}

View File

@ -70,10 +70,10 @@ void * networkThreadHandler(void * arguments)
void * gameThreadHandler(void * arguments)
{
while(true)
while (true)
{
doGameTick(arguments);
usleep(9000);
usleep(15625);
}
}
int main(int argc, char ** argv)