Add Physicsharer
This commit is contained in:
108
src/network/Shared.h
Normal file
108
src/network/Shared.h
Normal file
@@ -0,0 +1,108 @@
|
||||
#ifndef SHARED_H
|
||||
#define SHARED_H
|
||||
|
||||
#include <yojimbo.h>
|
||||
|
||||
// two channels, one for each type that Yojimbo supports
|
||||
enum class GameChannel
|
||||
{
|
||||
RELIABLE,
|
||||
UNRELIABLE,
|
||||
COUNT
|
||||
};
|
||||
|
||||
// the client and server config
|
||||
struct GameConnectionConfig : yojimbo::ClientServerConfig
|
||||
{
|
||||
GameConnectionConfig()
|
||||
{
|
||||
numChannels = 2;
|
||||
channel[(int)GameChannel::RELIABLE].type = yojimbo::CHANNEL_TYPE_RELIABLE_ORDERED;
|
||||
channel[(int)GameChannel::UNRELIABLE].type = yojimbo::CHANNEL_TYPE_UNRELIABLE_UNORDERED;
|
||||
}
|
||||
};
|
||||
|
||||
// a simple test message
|
||||
enum class GameMessageType
|
||||
{
|
||||
TEST,
|
||||
SETNAME,
|
||||
PHYSICSTATEMESSAGE,
|
||||
COUNT
|
||||
};
|
||||
|
||||
class TestMessage : public yojimbo::Message
|
||||
{
|
||||
public:
|
||||
int m_data;
|
||||
|
||||
TestMessage() : m_data(0) {}
|
||||
|
||||
template <typename Stream>
|
||||
bool Serialize(Stream &stream)
|
||||
{
|
||||
serialize_int(stream, m_data, 0, 512);
|
||||
return true;
|
||||
}
|
||||
|
||||
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
|
||||
};
|
||||
|
||||
class SetNameMessage : public yojimbo::Message
|
||||
{
|
||||
public:
|
||||
char buffer[yojimbo::MaxAddressLength];
|
||||
SetNameMessage() {}
|
||||
|
||||
template <typename Stream>
|
||||
bool Serialize(Stream &stream)
|
||||
{
|
||||
serialize_string(stream, buffer, sizeof(buffer));
|
||||
return true;
|
||||
}
|
||||
|
||||
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
|
||||
};
|
||||
|
||||
class PhysicStateMessage : public yojimbo::Message
|
||||
{
|
||||
public:
|
||||
int m_objectID;
|
||||
float m_posX;
|
||||
float m_posY;
|
||||
bool isActive;
|
||||
PhysicStateMessage() : m_objectID(0), m_posX(0), m_posY(0), isActive(false) {}
|
||||
|
||||
template <typename Stream>
|
||||
bool Serialize(Stream &stream)
|
||||
{
|
||||
serialize_int(stream, m_objectID, 0, 512);
|
||||
serialize_float(stream, m_posX);
|
||||
serialize_float(stream, m_posY);
|
||||
serialize_bool(stream, isActive);
|
||||
return true;
|
||||
}
|
||||
|
||||
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
|
||||
};
|
||||
|
||||
// the message factory
|
||||
YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT);
|
||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage);
|
||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage);
|
||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage);
|
||||
YOJIMBO_MESSAGE_FACTORY_FINISH();
|
||||
|
||||
// the adapter
|
||||
class SharedAdapter : public yojimbo::Adapter
|
||||
{
|
||||
public:
|
||||
yojimbo::MessageFactory *CreateMessageFactory(yojimbo::Allocator &allocator) override
|
||||
{
|
||||
return YOJIMBO_NEW(allocator, GameMessageFactory, allocator);
|
||||
}
|
||||
};
|
||||
|
||||
static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = {0};
|
||||
|
||||
#endif //SHARED_H
|
||||
Reference in New Issue
Block a user