Skip to content

Instantly share code, notes, and snippets.

@ChristianHohlfeld
Created October 4, 2018 20:34
Show Gist options
  • Save ChristianHohlfeld/745de6e7541638eeb1d5fecc7fe66ecc to your computer and use it in GitHub Desktop.
Save ChristianHohlfeld/745de6e7541638eeb1d5fecc7fe66ecc to your computer and use it in GitHub Desktop.
GameClient.cpp
//
// GameClient.cpp
// UDPTEST
//
// Created by Christian Hohlfeld on 16.09.18.
//
#include "GameClient.h"
#include <iostream>
#include <string>
#include <time.h>
#include <iostream>
static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = { 0 };
static const int MAX_PLAYERS = 2;
GameClient::GameClient(yojimbo::Allocator& allocator, const yojimbo::Address* address, yojimbo::Adapter& adapter, yojimbo::ClientServerConfig& clientServerConfig, double time)
{
m_client = new yojimbo::Client(allocator, *address, clientServerConfig, adapter, time);
numChannels = clientServerConfig.numChannels;
}
GameClient::~GameClient() {
delete m_client;
}
void GameClient::SetServerAddress(yojimbo::Address* serverAddress) {
m_serverAddress = serverAddress;
}
void GameClient::SendMsg(yojimbo::Message* message) {
m_client->SendMessage((int)GameChannel::RELIABLE, message);
}
void GameClient::ProcessMessages() {
for (int i = 0; i < numChannels; i++) {
yojimbo::Message* message = m_client->ReceiveMessage(i);
while (message != NULL) {
ProcessMessage(message);
m_client->ReleaseMessage(message);
message = m_client->ReceiveMessage(i);
}
}
}
void GameClient::ProcessMessage(yojimbo::Message* message) {
switch (message->GetType()) {
case (int)GameMessageType::TEST:
ProcessTestMessage((MyMessage*)message);
break;
default:
break;
}
}
void GameClient::ProcessTestMessage(MyMessage* message) {
std::cout << "Test message received from server with data " << message->data.string << std::endl;
}
void GameClient::InsecureConnect(const uint8_t privateKey[], uint64_t clientId) {
m_client->InsecureConnect(privateKey, clientId, *m_serverAddress);
}
void GameClient::SendTestMessage(float positionX, float positionY, float rotation) {
MyMessage* message = (MyMessage*)m_client->CreateMessage((int)GameMessageType::TEST);
//strcpy( message->data.string, "Jump Jump!" );
message->data.float_valueX = positionX;
message->data.float_valueY = positionY;
message->data.float_valueRotation = rotation;
m_client->SendMessage((int)GameChannel::RELIABLE, message);
}
void GameClient::SendPackets() {
m_client->SendPackets();
}
void GameClient::ReceivePackets() {
m_client->ReceivePackets();
}
void GameClient::AdvanceTime(double time) {
m_client->AdvanceTime(time);
}
bool GameClient::ConnectionFailed()
{
return m_client->ConnectionFailed();
}
bool GameClient::IsDisconnected()
{
return m_client->IsDisconnected();
}
yojimbo::Address GameClient::GetAddress() {
return m_client->GetAddress();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment