WIP
Adding models now crashes. Threading Added a create player event that currently is not used.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
#include "Input/EInputCommand.h"
|
||||
|
||||
|
||||
class Client
|
||||
@@ -75,6 +76,8 @@ private:
|
||||
bool OnKeyDown(const Events::KeyDown &e);
|
||||
EventRelay<Client, Events::KeyUp> m_EKeyUp;
|
||||
bool OnKeyUp(const Events::KeyUp &e);
|
||||
EventRelay<Client, Events::InputCommand> m_EInputCommand;
|
||||
bool OnInputCommand(const Events::InputCommand &e);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
|
||||
struct PlayerDefinition {
|
||||
int EntityID = -1;
|
||||
unsigned int EntityID = -1;
|
||||
std::string Name = "";
|
||||
boost::asio::ip::udp::endpoint Endpoint;
|
||||
};
|
||||
|
||||
@@ -12,13 +12,15 @@
|
||||
#include "Network/NetworkDefinitions.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Game/ECreatePlayer.h"
|
||||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
void Start(World* m_world);
|
||||
void Start(World* m_world, EventBroker *eventBroker);
|
||||
void Close();
|
||||
|
||||
private:
|
||||
@@ -32,6 +34,7 @@ private:
|
||||
std::clock_t m_StopTimes[8];
|
||||
// Game logic
|
||||
World* m_World;
|
||||
EventBroker* m_EventBroker;
|
||||
// Packet loss logic
|
||||
unsigned int m_PacketID;
|
||||
unsigned int m_PreviousPacketID;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef Events_CreatePlayer_h__
|
||||
#define Events_CreatePlayer_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/World.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct CreatePlayer : Event
|
||||
{
|
||||
unsigned int entityID;
|
||||
std::string modelPath;
|
||||
World* world;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
#include "ECreatePlayer.h"
|
||||
|
||||
struct KeyInput
|
||||
{
|
||||
@@ -26,6 +27,7 @@ public:
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PlayerSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PlayerSystem::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECreatePlayer, &PlayerSystem::OnCreatePlayer);
|
||||
}
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& player, double dt) override;
|
||||
@@ -34,11 +36,15 @@ private:
|
||||
float m_Speed = 5;
|
||||
glm::vec3 m_Direction;
|
||||
KeyInput input;
|
||||
bool ShouldCreatePlayer = false;
|
||||
|
||||
void CreatePlayer(World * world, unsigned int& entityID);
|
||||
EventRelay<PlayerSystem, Events::KeyDown> m_EKeyDown;
|
||||
bool OnKeyDown(const Events::KeyDown &event);
|
||||
EventRelay<PlayerSystem, Events::KeyUp> m_EKeyUp;
|
||||
bool OnKeyUp(const Events::KeyUp &event);
|
||||
EventRelay<PlayerSystem, Events::CreatePlayer> m_ECreatePlayer;
|
||||
bool OnCreatePlayer(const Events::CreatePlayer &event);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,7 @@
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
<Forward>false</Forward>
|
||||
<Left>false</Left>
|
||||
<Back>false</Back>
|
||||
<Right>false</Right>
|
||||
</c:Player>
|
||||
@@ -7,6 +7,10 @@
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Velocity" type="t:Vector" minOccurs="0"/>
|
||||
<xs:element name="Forward" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Left" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Back" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Right" type="t:bool" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
@@ -28,7 +28,8 @@ void Client::Start(World* world, EventBroker* eventBroker)
|
||||
m_EventBroker->Subscribe(m_EKeyDown);
|
||||
m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Client::OnKeyUp, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_EKeyUp);
|
||||
|
||||
m_EInputCommand = decltype(m_EInputCommand)(std::bind(&Client::OnInputCommand, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_EInputCommand);
|
||||
std::cout << "Please enter you name: ";
|
||||
std::cin >> m_PlayerName;
|
||||
while (m_PlayerName.size() > 7) {
|
||||
@@ -74,9 +75,9 @@ void Client::ReadFromServer()
|
||||
void Client::SendSnapshotToServer()
|
||||
{
|
||||
// Reset previouse key state in snapshot.
|
||||
Package message(MessageType::Event, m_SendPacketID);
|
||||
message.AddString(m_NextSnapshot.InputForward);
|
||||
Send(message);
|
||||
//Package message(MessageType::Event, m_SendPacketID);
|
||||
//message.AddString(m_NextSnapshot.InputForward);
|
||||
//Send(message);
|
||||
m_NextSnapshot.InputForward = "";
|
||||
m_NextSnapshot.InputRight = "";
|
||||
// See if any movement keys are down
|
||||
@@ -208,7 +209,7 @@ void Client::ParseSnapshot(char* data, size_t length)
|
||||
// Apply the position data read to the player entity
|
||||
// New player connected on the server side
|
||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||
CreateNewPlayer(i);
|
||||
//CreateNewPlayer(i);
|
||||
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
||||
// Someone disconnected
|
||||
// TODO: Insert code here
|
||||
@@ -216,7 +217,7 @@ void Client::ParseSnapshot(char* data, size_t length)
|
||||
// Not a connected player
|
||||
break;
|
||||
}
|
||||
m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"] = playerPos;
|
||||
//m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"] = playerPos;
|
||||
m_PlayerDefinitions[i].Name = tempName;
|
||||
}
|
||||
}
|
||||
@@ -322,6 +323,35 @@ bool Client::OnKeyUp(const Events::KeyUp & e)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Client::OnInputCommand(const Events::InputCommand & e)
|
||||
{
|
||||
if (e.Command == "Forward") {
|
||||
if (e.Value > 0) {
|
||||
m_IsWASDKeyDown.W = true;
|
||||
}
|
||||
else if (e.Value < 0) {
|
||||
m_IsWASDKeyDown.S = true;
|
||||
} else {
|
||||
m_IsWASDKeyDown.W = false;
|
||||
m_IsWASDKeyDown.S = false;
|
||||
}
|
||||
}
|
||||
if (e.Command == "Right") {
|
||||
if (e.Value > 0) {
|
||||
m_IsWASDKeyDown.D = true;
|
||||
} else if (e.Value < 0) {
|
||||
m_IsWASDKeyDown.A = true;
|
||||
} else {
|
||||
m_IsWASDKeyDown.A = false;
|
||||
m_IsWASDKeyDown.D = false;
|
||||
}
|
||||
}
|
||||
if (e.Command == "Sprint") { // Temp connect
|
||||
Connect();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Client::CreateNewPlayer(int i)
|
||||
{
|
||||
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
||||
@@ -335,6 +365,6 @@ void Client::IdentifyPacketLoss()
|
||||
// if no packets lost, difference should be equal to 1
|
||||
int difference = m_PacketID - m_PreviousPacketID;
|
||||
if (difference != 1) {
|
||||
LOG_INFO("%i Packet(s) were lost...", difference);
|
||||
LOG_INFO("%i Packet(s) were lost...", difference);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ Server::~Server()
|
||||
{ }
|
||||
|
||||
|
||||
void Server::Start(World* world)
|
||||
void Server::Start(World* world, EventBroker* eventBroker)
|
||||
{
|
||||
m_World = world;
|
||||
m_EventBroker = eventBroker;
|
||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||
m_StopTimes[i] = std::clock();
|
||||
}
|
||||
@@ -42,7 +43,7 @@ void Server::ReadFromClients()
|
||||
std::clock_t previousePingMessage = std::clock();
|
||||
std::clock_t previousSnapshotMessage = std::clock();
|
||||
std::clock_t timOutTimer = std::clock();
|
||||
// How offen we send messages (milliseconds)
|
||||
// How often we send messages (milliseconds)
|
||||
int intervallMs = 1000;
|
||||
int snapshotInterval = 50;
|
||||
int timeToCheckTimeOutTime = 100;
|
||||
@@ -63,6 +64,7 @@ void Server::ReadFromClients()
|
||||
std::cout << m_PacketID << ": Read from client crashed: " << err.what();
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
std::clock_t currentTime = std::clock();
|
||||
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
|
||||
@@ -121,7 +123,7 @@ void Server::ParseMessageType(char * data, size_t length)
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
memcpy(&m_PacketID, data, sizeof(int)); //Read new packet id
|
||||
MoveMessageHead(data, length, sizeof(int));
|
||||
//IdentifyPacketLoss(); // crashed when it started to spam!
|
||||
//IdentifyPacketLoss();
|
||||
|
||||
switch (static_cast<MessageType>(messageType)) {
|
||||
case MessageType::Connect:
|
||||
@@ -210,7 +212,8 @@ void Server::SendSnapshot()
|
||||
continue;
|
||||
}
|
||||
// Pack player pos into data package
|
||||
glm::vec3 playerPos = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"];
|
||||
//glm::vec3 playerPos = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"];
|
||||
glm::vec3 playerPos = glm::vec3(1.0f);
|
||||
package.AddPrimitive<float>(playerPos.x);
|
||||
package.AddPrimitive<float>(playerPos.y);
|
||||
package.AddPrimitive<float>(playerPos.z);
|
||||
@@ -241,7 +244,6 @@ void Server::SendPing()
|
||||
void Server::CheckForTimeOuts()
|
||||
{
|
||||
int timeOutTimeMs = 5000;
|
||||
|
||||
int tempStartPing = 1000 * m_StartPingTime
|
||||
/ static_cast<double>(CLOCKS_PER_SEC);
|
||||
|
||||
@@ -276,33 +278,33 @@ void Server::ParseEvent(char * data, size_t length)
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no player matches the ip return.
|
||||
// If no player matches the address return.
|
||||
if (i >= 8)
|
||||
return;
|
||||
|
||||
unsigned int entityId = m_PlayerDefinitions[i].EntityID;
|
||||
if ("+Forward" == std::string(data)) {
|
||||
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
temp.z -= 0.1f;
|
||||
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
}
|
||||
if ("-Forward" == std::string(data)) {
|
||||
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
temp.z += 0.1f;
|
||||
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
}
|
||||
//unsigned int entityId = m_PlayerDefinitions[i].EntityID;
|
||||
//if ("+Forward" == std::string(data)) {
|
||||
// glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
// temp.z -= 0.1f;
|
||||
// m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
//}
|
||||
//if ("-Forward" == std::string(data)) {
|
||||
// glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
// temp.z += 0.1f;
|
||||
// m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
//}
|
||||
|
||||
if ("+Right" == std::string(data)) {
|
||||
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
temp.x += 0.1f;
|
||||
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
}
|
||||
//if ("+Right" == std::string(data)) {
|
||||
// glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
// temp.x += 0.1f;
|
||||
// m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
//}
|
||||
|
||||
if ("-Right" == std::string(data)) {
|
||||
glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
temp.x -= 0.1f;
|
||||
m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
}
|
||||
//if ("-Right" == std::string(data)) {
|
||||
// glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"];
|
||||
// temp.x -= 0.1f;
|
||||
// m_World->GetComponent(entityId, "Transform")["Position"] = temp;
|
||||
//}
|
||||
}
|
||||
|
||||
void Server::ParseConnect(char * data, size_t length)
|
||||
@@ -317,15 +319,20 @@ void Server::ParseConnect(char * data, size_t length)
|
||||
|
||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||
if (m_PlayerDefinitions[i].Endpoint.address() == boost::asio::ip::address()) {
|
||||
|
||||
//Events::CreatePlayer e;
|
||||
//e.entityID = (m_PlayerDefinitions[i].EntityID);
|
||||
//e.modelPath = "Models/Core/UnitSphere.obj";
|
||||
//e.world = m_World;
|
||||
//m_EventBroker->Publish(e);
|
||||
|
||||
|
||||
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
||||
ComponentWrapper transform = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Transform");
|
||||
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||
ComponentWrapper model = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Model");
|
||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||
model["Color"] = glm::vec4(rand()%255 / 255.f, rand()%255 / 255.f, rand() %255 / 255.f, 1.f);
|
||||
|
||||
//int entityID = m_World->CreateEntity();
|
||||
//ComponentWrapper transform = m_World->AttachComponent(entityID, "Transform");
|
||||
//transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||
//ComponentWrapper model = m_World->AttachComponent(entityID, "Model");
|
||||
//model["Resource"] = "Models/Core/UnitSphere.obj";//modelPath; // You fix this :)
|
||||
//model["Color"] = glm::vec4(rand()%255 / 255.f, rand()%255 / 255.f, rand() %255 / 255.f, 1.f);
|
||||
|
||||
m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint;
|
||||
m_PlayerDefinitions[i].Name = std::string(data);
|
||||
// +1 is the null terminator
|
||||
|
||||
+1
-4
@@ -53,9 +53,6 @@ Game::Game(int argc, char* argv[])
|
||||
m_SystemPipeline->AddSystem<RaptorCopterSystem>();
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>();
|
||||
|
||||
|
||||
|
||||
|
||||
// Invoke network
|
||||
if(m_Config->Get<bool>("Networking.StartNetwork", false) == true)
|
||||
boost::thread workerThread(&Game::NetworkFunction, this);
|
||||
@@ -144,6 +141,6 @@ void Game::NetworkFunction()
|
||||
}
|
||||
if (inputMessage == "s" || inputMessage == "S") {
|
||||
Server m_Server;
|
||||
m_Server.Start(m_World);
|
||||
m_Server.Start(m_World, m_EventBroker);
|
||||
}
|
||||
}
|
||||
+40
-18
@@ -3,24 +3,40 @@
|
||||
|
||||
void PlayerSystem::Update(World * world, ComponentWrapper & player, double dt)
|
||||
{
|
||||
if (input.Forward) {
|
||||
m_Direction.z = -1;
|
||||
} else if (input.Back) {
|
||||
m_Direction.z = 1;
|
||||
} else {
|
||||
m_Direction.z = 0;
|
||||
}
|
||||
if (input.Left) {
|
||||
m_Direction.x = -1;
|
||||
} else if (input.Right) {
|
||||
m_Direction.x = 1;
|
||||
} else {
|
||||
m_Direction.x = 0;
|
||||
}
|
||||
m_EventBroker->Process<PlayerSystem>();
|
||||
ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
(glm::vec3&)player["Velocity"] = m_Speed * float(dt) * m_Direction;
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
//if (player["Forward"]) {
|
||||
// ((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt) * -1;
|
||||
//}
|
||||
//if (player["Left"]) {
|
||||
// ((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt) * -1;
|
||||
//}
|
||||
//if (player["Back"]) {
|
||||
// ((glm::vec3&)player["Velocity"]).z = m_Speed * float(dt);
|
||||
//}
|
||||
//if (player["Right"]) {
|
||||
// ((glm::vec3&)player["Velocity"]).x = m_Speed * float(dt);
|
||||
//}
|
||||
//ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
//(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
|
||||
//m_EventBroker->Process<PlayerSystem>();
|
||||
|
||||
//// TODO Jag tror inte vi kan göra det vi vill i updaten.
|
||||
//// om man lägger till parametrar och tar bort overriden så blir det kanske inte så kul?
|
||||
//// aja, lycka till!
|
||||
//if (ShouldCreatePlayer) {
|
||||
// int entityID = world->CreateEntity();
|
||||
// ComponentWrapper transform = world->AttachComponent(entityID, "Transform");
|
||||
// transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||
// ComponentWrapper model = world->AttachComponent(entityID, "Model");
|
||||
// model["Resource"] = "Models/Core/UnitSphere.obj";//modelPath; // You fix this :)
|
||||
// model["Color"] = glm::vec4(rand()%255 / 255.f, rand()%255 / 255.f, rand() %255 / 255.f, 1.f);
|
||||
// ShouldCreatePlayer = false;
|
||||
//}
|
||||
}
|
||||
|
||||
void PlayerSystem::CreatePlayer(World * world, unsigned int& entityID)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnKeyDown(const Events::KeyDown & event)
|
||||
@@ -56,3 +72,9 @@ bool PlayerSystem::OnKeyUp(const Events::KeyUp & event)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnCreatePlayer(const Events::CreatePlayer & event)
|
||||
{
|
||||
ShouldCreatePlayer = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user