WIP Model component not working.
This commit is contained in:
@@ -22,6 +22,8 @@ public:
|
||||
void RegisterComponent(ComponentInfo& ci);
|
||||
// Attach a component to an entity and fill it with default values
|
||||
ComponentWrapper AttachComponent(EntityID entity, std::string componentType);
|
||||
// Check if world has an entity
|
||||
bool HasEntity(EntityID entity);
|
||||
// Check if an entity has a component
|
||||
bool HasComponent(EntityID entity, std::string componentType) const;
|
||||
// Get a component of an entity
|
||||
|
||||
@@ -14,8 +14,9 @@ public:
|
||||
Packet(MessageType type, unsigned int& packetID);
|
||||
// Used to create packet from already existing data buffer.
|
||||
Packet(char* data, const int sizeOfPacket);
|
||||
|
||||
~Packet();
|
||||
void Init(MessageType type, unsigned int& packetID);
|
||||
|
||||
// Add primitive types like int, float, char...
|
||||
template<typename T>
|
||||
void WritePrimitive(T val)
|
||||
@@ -23,6 +24,7 @@ public:
|
||||
// Check if we are trying to add more than the package can fit.
|
||||
if (m_MaxPacketSize < m_Offset + sizeof(T)) {
|
||||
LOG_WARNING("Packet AddPrimitive(): You are trying to add more than we have allocated for!");
|
||||
return;
|
||||
}
|
||||
memcpy(m_Data + m_Offset, &val, sizeof(T));
|
||||
m_Offset += sizeof(T);
|
||||
@@ -50,6 +52,8 @@ public:
|
||||
|
||||
int Size() { return m_Offset; };
|
||||
char* Data() { return m_Data; };
|
||||
unsigned int DataReadSize() { return m_ReturnDataOffset; }
|
||||
unsigned int MaxSize() { return m_MaxPacketSize; }
|
||||
|
||||
private:
|
||||
char* m_Data;
|
||||
|
||||
@@ -67,6 +67,7 @@ private:
|
||||
void moveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||
void broadcast(std::string message);
|
||||
void broadcast(Packet& packet);
|
||||
//void parseShitTest(Packet& packet); // Remove this when network is working
|
||||
void sendSnapshot();
|
||||
void sendPing();
|
||||
void checkForTimeOuts();
|
||||
|
||||
@@ -244,9 +244,13 @@ bool attachAABBComponentFromModel(World* world, EntityID id)
|
||||
}
|
||||
|
||||
bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox)
|
||||
{
|
||||
if (world->HasComponent(AABBComponent.EntityID, "Transform") && world->HasComponent(AABBComponent.EntityID, "Model"))
|
||||
{
|
||||
ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform");
|
||||
ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model");
|
||||
if(AABBComponent.EntityID == 3);
|
||||
std::string checkPath = model["Resource"];
|
||||
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
|
||||
outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]);
|
||||
glm::vec3 mini = outBox.MinCorner();
|
||||
@@ -263,6 +267,8 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox)
|
||||
modelMatrix * glm::vec4(maxi.x, maxi.y, maxi.z, 1));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetEntityBox(World* world, EntityID entity, AABB& outBox, bool forceBoxFromModel)
|
||||
{
|
||||
|
||||
@@ -80,7 +80,13 @@ ComponentWrapper World::AttachComponent(EntityID entity, std::string componentTy
|
||||
return c;
|
||||
}
|
||||
|
||||
bool World::HasComponent(EntityID entity, std::string componentType) const
|
||||
bool World::HasEntity(EntityID entity)
|
||||
{
|
||||
return m_EntityParents.find(entity) != m_EntityParents.end();
|
||||
}
|
||||
|
||||
|
||||
bool World::HasComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||
return pool->KnowsEntity(entity);
|
||||
|
||||
@@ -50,7 +50,7 @@ void Client::Close()
|
||||
|
||||
void Client::readFromServer()
|
||||
{
|
||||
if (m_Socket.available()) {
|
||||
while (m_Socket.available()) {
|
||||
bytesRead = receive(readBuf, INPUTSIZE);
|
||||
if (bytesRead > 0) {
|
||||
Packet packet(readBuf, bytesRead);
|
||||
@@ -60,7 +60,7 @@ void Client::readFromServer()
|
||||
std::clock_t currentTime = std::clock();
|
||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||
if (isConnected()) {
|
||||
sendSnapshotToServer();
|
||||
//sendSnapshotToServer();
|
||||
}
|
||||
previousSnapshotMessage = currentTime;
|
||||
}
|
||||
@@ -120,6 +120,8 @@ void Client::parseMessageType(Packet& packet)
|
||||
// Read packet ID
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
m_PacketID = packet.ReadPrimitive<int>(); //Read new packet id
|
||||
if (m_PacketID <= m_PreviousPacketID)
|
||||
return;
|
||||
//IdentifyPacketLoss();
|
||||
|
||||
switch (static_cast<MessageType>(messageType)) {
|
||||
@@ -181,33 +183,45 @@ void Client::parseEventMessage(Packet& packet)
|
||||
|
||||
void Client::parseSnapshot(Packet& packet)
|
||||
{
|
||||
std::string tempName;
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
// We're checking for empty name for now. This might not be the best way,
|
||||
// but it is to avoid sending redundant data.
|
||||
tempName = packet.ReadString();
|
||||
|
||||
|
||||
// Apply the position data read to the player entity
|
||||
// New player connected on the server side
|
||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||
m_PlayerDefinitions[i].Name = tempName;
|
||||
m_PlayerDefinitions[i].EntityID = createPlayer();
|
||||
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
||||
// Someone disconnected
|
||||
// TODO: Insert code here
|
||||
break;
|
||||
} else if (m_PlayerDefinitions[i].Name == "" && tempName == "") {
|
||||
// Not a connected player
|
||||
break;
|
||||
std::string componentType = packet.ReadString();
|
||||
//LOG_INFO("A snapshot was parsed. first component type is: %s", componentType.c_str());
|
||||
int stride = packet.ReadPrimitive<int>();
|
||||
int nrOfComponents = (packet.Size() - packet.DataReadSize()) / (stride + sizeof(EntityID));
|
||||
if (componentType == "Model")
|
||||
return;
|
||||
for (size_t i = 0; i < nrOfComponents; i++) {
|
||||
EntityID entityID = packet.ReadPrimitive<EntityID>();
|
||||
//ComponentWrapper model = m_World->GetComponent(entityID, "Model");
|
||||
//std::string checkPath = model["Resource"];
|
||||
// Check if entity exists
|
||||
if (m_World->HasEntity(entityID)) {
|
||||
// check if component exists
|
||||
if (m_World->HasComponent(entityID, componentType)) {
|
||||
//Copy data to component
|
||||
memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
} else {
|
||||
// If component doesen't exist
|
||||
// Create component
|
||||
m_World->AttachComponent(entityID, componentType);
|
||||
// Copy data to newly created component
|
||||
memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
}
|
||||
} else {
|
||||
// If entity dosen't exist
|
||||
EntityID newEntityID = m_World->CreateEntity();
|
||||
// Check if EntityIDs are out of sync
|
||||
if (newEntityID != entityID) {
|
||||
LOG_INFO("Client::parseSnapshot(Packet& packet): Newly created EntityID is not the same as the one sent by server \
|
||||
EntityIDs are out of sync");
|
||||
}
|
||||
m_World->AttachComponent(newEntityID, componentType);
|
||||
// Copy data to newly created component
|
||||
memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
}
|
||||
//ComponentWrapper model2 = m_World->GetComponent(entityID, "Model");
|
||||
//std::string checkPath2 = model2["Resource"];
|
||||
}
|
||||
if (m_PlayerDefinitions[i].EntityID != -1) {
|
||||
|
||||
// Move player to server position
|
||||
int dataSize = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform").Info.Meta.Stride;
|
||||
memcpy(m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform").Data, packet.ReadData(dataSize), dataSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Client::receive(char* data, size_t length)
|
||||
|
||||
@@ -3,13 +3,7 @@
|
||||
Packet::Packet(MessageType type, unsigned int& packetID)
|
||||
{
|
||||
m_Data = new char[m_MaxPacketSize];
|
||||
// Create message header
|
||||
// Add message type
|
||||
int messageType = static_cast<int>(type);
|
||||
Packet::WritePrimitive<int>(messageType);
|
||||
packetID = packetID % 1000; // Packet id modulos
|
||||
Packet::WritePrimitive<int>(packetID);
|
||||
packetID++;
|
||||
Init(type, packetID);
|
||||
}
|
||||
|
||||
// Create message
|
||||
@@ -28,12 +22,26 @@ Packet::~Packet()
|
||||
delete[] m_Data;
|
||||
}
|
||||
|
||||
void Packet::Init(MessageType type, unsigned int & packetID)
|
||||
{
|
||||
m_ReturnDataOffset = 0;
|
||||
m_Offset = 0;
|
||||
// Create message header
|
||||
// Add message type
|
||||
int messageType = static_cast<int>(type);
|
||||
Packet::WritePrimitive<int>(messageType);
|
||||
packetID = packetID % 1000; // Packet id modulos
|
||||
Packet::WritePrimitive<int>(packetID);
|
||||
packetID++;
|
||||
}
|
||||
|
||||
void Packet::WriteString(std::string str)
|
||||
{
|
||||
// Message, add one extra byte for null terminator
|
||||
int sizeOfString = str.size() + 1;
|
||||
if (m_Offset + sizeOfString > m_MaxPacketSize) {
|
||||
LOG_WARNING("Package::WriteString(): Data size in packet exceeded maximum package size.\n");
|
||||
return;
|
||||
}
|
||||
memcpy(m_Data + m_Offset, str.data(), sizeOfString * sizeof(char));
|
||||
m_Offset += sizeOfString * sizeof(char);
|
||||
@@ -43,6 +51,7 @@ void Packet::WriteData(char * data, int sizeOfData)
|
||||
{
|
||||
if (m_Offset + sizeOfData > m_MaxPacketSize) {
|
||||
LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size.\n");
|
||||
return;
|
||||
}
|
||||
memcpy(m_Data + m_Offset, data, sizeOfData);
|
||||
m_Offset += sizeOfData;
|
||||
|
||||
+108
-13
@@ -36,7 +36,7 @@ void Server::readFromClients()
|
||||
// program crashed if it executed m_Socket.available()
|
||||
// when closing the program.
|
||||
|
||||
if (m_Socket.available()) {
|
||||
while (m_Socket.available()) {
|
||||
try {
|
||||
bytesRead = receive(readBuffer, INPUTSIZE);
|
||||
Packet packet(readBuffer, bytesRead);
|
||||
@@ -61,7 +61,7 @@ void Server::readFromClients()
|
||||
|
||||
// Time out logic
|
||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||
checkForTimeOuts();
|
||||
//checkForTimeOuts();
|
||||
timOutTimer = currentTime;
|
||||
}
|
||||
}
|
||||
@@ -152,24 +152,119 @@ void Server::broadcast(Packet& packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
//void Server::parseShitTest(Packet& packet)
|
||||
//{
|
||||
// packet.ReadPrimitive<int>(); // MessageType
|
||||
// packet.ReadPrimitive<int>(); // Packet ID
|
||||
//
|
||||
// std::string componentType = packet.ReadString();
|
||||
// //LOG_INFO("A snapshot was parsed. first component type is: %s", componentType.c_str());
|
||||
// int stride = packet.ReadPrimitive<int>();
|
||||
// int nrOfComponents = (packet.Size() - packet.DataReadSize()) / (stride + sizeof(EntityID));
|
||||
// //if (componentType == "Model")
|
||||
// // return;
|
||||
// for (size_t i = 0; i < nrOfComponents; i++) {
|
||||
// EntityID entityID = packet.ReadPrimitive<EntityID>();
|
||||
// ComponentWrapper model = m_World->GetComponent(entityID, "Model");
|
||||
// std::string checkPath = model["Resource"];
|
||||
// // Check if entity exists
|
||||
// if (m_World->HasEntity(entityID)) {
|
||||
// // check if component exists
|
||||
// if (m_World->HasComponent(entityID, componentType)) {
|
||||
// //Copy data to component
|
||||
// memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
// } else {
|
||||
// // If component doesen't exist
|
||||
// // Create component
|
||||
// m_World->AttachComponent(entityID, componentType);
|
||||
// // Copy data to newly created component
|
||||
// memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
// }
|
||||
// } else {
|
||||
// // If entity dosen't exist
|
||||
// EntityID newEntityID = m_World->CreateEntity();
|
||||
// // Check if EntityIDs are out of sync
|
||||
// if (newEntityID != entityID) {
|
||||
// LOG_INFO("Client::parseSnapshot(Packet& packet): Newly created EntityID is not the same as the one sent by server \
|
||||
// EntityIDs are out of sync");
|
||||
// }
|
||||
// m_World->AttachComponent(newEntityID, componentType);
|
||||
// // Copy data to newly created component
|
||||
// memcpy(m_World->GetComponent(entityID, componentType).Data, packet.ReadData(stride), stride);
|
||||
// }
|
||||
// ComponentWrapper model2 = m_World->GetComponent(entityID, "Model");
|
||||
// std::string checkPath2 = model2["Resource"];
|
||||
// }
|
||||
//}
|
||||
|
||||
void Server::sendSnapshot()
|
||||
{
|
||||
// Should time this
|
||||
std::unordered_map<std::string, ComponentPool*> worldComponentPools = m_World->GetComponentPools();
|
||||
for (auto it : worldComponentPools) {
|
||||
Packet packet(MessageType::Snapshot, m_SendPacketID);
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
|
||||
// Send an empty name if there is no player connected on this position.
|
||||
packet.WriteString(m_PlayerDefinitions[i].Name);
|
||||
|
||||
if (m_PlayerDefinitions[i].EntityID == -1) {
|
||||
continue;
|
||||
std::string componentType = it.first;
|
||||
//if (componentType != "Model")
|
||||
// continue;
|
||||
ComponentPool* componentPool = it.second;
|
||||
ComponentInfo componentInfo = componentPool->ComponentInfo();
|
||||
packet.WriteString(componentInfo.Name);
|
||||
packet.WritePrimitive(componentInfo.Meta.Stride);
|
||||
for (auto componentWrapper : *componentPool) {
|
||||
if (packet.Size() + componentInfo.Meta.Stride >= packet.MaxSize()) {
|
||||
broadcast(packet);
|
||||
//parseShitTest(packet);
|
||||
// Packet destructor is called (which is what we want).
|
||||
packet.Init(MessageType::Snapshot, m_SendPacketID);
|
||||
// Add Component header
|
||||
packet.WriteString(componentInfo.Name);
|
||||
packet.WritePrimitive(componentInfo.Meta.Stride);
|
||||
}
|
||||
// Pack transfrom component into data packet
|
||||
auto transform = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform");
|
||||
packet.WriteData(transform.Data, transform.Info.Meta.Stride);
|
||||
// Component data
|
||||
packet.WritePrimitive(componentWrapper.EntityID);
|
||||
packet.WriteData(componentWrapper.Data, componentWrapper.Info.Meta.Stride);
|
||||
}
|
||||
broadcast(packet);
|
||||
}
|
||||
}
|
||||
|
||||
//void Server::sendSnapshot()
|
||||
//{
|
||||
// // Should time this
|
||||
// std::unordered_map<std::string, ComponentPool*> worldComponentPools = m_World->GetComponentPools();
|
||||
// for (auto it : worldComponentPools) {
|
||||
// Packet packet(MessageType::Snapshot, m_SendPacketID);
|
||||
// std::string componentType = it.first;
|
||||
// ComponentPool* componentPool = it.second;
|
||||
// ComponentInfo componentInfo = componentPool->ComponentInfo();
|
||||
// if (componentInfo.Name != "Transform")
|
||||
// continue;
|
||||
// packet.WriteString(componentInfo.Name);
|
||||
// packet.WritePrimitive(componentInfo.Meta.Stride);
|
||||
// auto componentWrapper = *componentPool->begin();
|
||||
// packet.WritePrimitive(componentWrapper.EntityID);
|
||||
// packet.WriteData(componentWrapper.Data, componentWrapper.Info.Meta.Stride);
|
||||
//
|
||||
// for (auto componentWrapper : *componentPool) {
|
||||
// //// When packet is full send it
|
||||
// //if (packet.Size() + componentInfo.Meta.Stride >= packet.MaxSize()) {
|
||||
// // broadcast(packet);
|
||||
// // // Packet destructor is called (which is what we want).
|
||||
// // packet.Init(MessageType::Snapshot, m_SendPacketID);
|
||||
// // // Add Component header
|
||||
// // packet.WriteString(componentInfo.Name);
|
||||
// // packet.WritePrimitive(componentInfo.Meta.Stride);
|
||||
//
|
||||
// //}
|
||||
// //// Component data
|
||||
// //packet.WritePrimitive(componentWrapper.EntityID);
|
||||
// //packet.WriteData(componentWrapper.Data, componentWrapper.Info.Meta.Stride);
|
||||
// broadcast(packet);
|
||||
// }
|
||||
// broadcast(packet);
|
||||
// }
|
||||
//}
|
||||
|
||||
void Server::sendPing()
|
||||
{
|
||||
@@ -177,7 +272,7 @@ void Server::sendPing()
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
|
||||
int ping = 1000 * (m_StopTimes[i] - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
|
||||
LOG_INFO("%i: Player %i's ping: %i", m_PacketID, i, ping);
|
||||
LOG_INFO("Last packetID received %i: Player %i's ping: %i", m_PacketID, i, ping);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user