Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1a59e0ed2 | |||
| b3038ef713 | |||
| f161fce719 | |||
| 89d0d5753f | |||
| 748a178801 | |||
| eac9a9daf6 | |||
| 059fe5c6c0 | |||
| fb465ede81 | |||
| f66fa7a183 | |||
| 508d8a6f00 | |||
| 8ce6308649 | |||
| f10958c268 | |||
| fa13c1d065 |
@@ -24,6 +24,7 @@ public:
|
||||
private:
|
||||
Octree<EntityAABB>* m_Octree;
|
||||
std::vector<EntityAABB> m_OctreeResult;
|
||||
std::unordered_map<EntityWrapper, glm::vec3> m_PrevPositions;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "Core/ConfigFile.h"
|
||||
#include "Input/EInputCommand.h"
|
||||
#include "Core/EPlayerDamage.h"
|
||||
#include "../Game/Events/EDoubleJump.h"
|
||||
#include "Network/EInterpolate.h"
|
||||
#include "Network/SnapshotFilter.h"
|
||||
#include "Core/EPlayerSpawned.h"
|
||||
@@ -47,7 +48,9 @@ public:
|
||||
|
||||
void Connect(std::string address, int port);
|
||||
void Update() override;
|
||||
|
||||
private:
|
||||
UDPClient m_Unreliable;
|
||||
TCPClient m_Reliable;
|
||||
std::vector<Events::PlayerSpawned> m_PlayerSpawnEvents;
|
||||
void parseSpawnEvents();
|
||||
// Save for children
|
||||
@@ -101,9 +104,9 @@ public:
|
||||
void parseEntityDeletion(Packet& packet);
|
||||
void parsePlayerDamage(Packet& packet);
|
||||
void parseComponentDeletion(Packet& packet);
|
||||
void parseDoubleJump(Packet& packet);
|
||||
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
|
||||
void parseSnapshot(Packet& packet);
|
||||
void UpdateLocalCapturePointHUD(EntityWrapper capturePointHUD);
|
||||
void identifyPacketLoss();
|
||||
void hasServerTimedOut();
|
||||
EntityID createPlayer();
|
||||
@@ -127,11 +130,10 @@ public:
|
||||
EventRelay<Client, Events::PlayerSpawned> m_EPlayerSpawned;
|
||||
bool OnPlayerSpawned(const Events::PlayerSpawned& e);
|
||||
EventRelay< Client, Events::SearchForServers> m_ESearchForServers;
|
||||
EventRelay<Client, Events::DoubleJump> m_EDoubleJump;
|
||||
bool OnDoubleJump(Events::DoubleJump & e);
|
||||
bool OnSearchForServers(const Events::SearchForServers& e);
|
||||
private:
|
||||
UDPClient m_Unreliable;
|
||||
UDPClient m_ServerlistRequest;
|
||||
TCPClient m_Reliable;
|
||||
std::vector<ServerInfo> m_Serverlist;
|
||||
bool m_SearchingForServers = false;
|
||||
std::clock_t m_StartSearchTime;
|
||||
|
||||
@@ -19,6 +19,7 @@ enum class MessageType
|
||||
EntityDeleted,
|
||||
ComponentDeleted,
|
||||
PlayerTransform,
|
||||
OnDoubleJump,
|
||||
ServerlistRequest,
|
||||
Invalid
|
||||
};
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include "Network/TCPServer.h"
|
||||
#include "Network/UDPServer.h"
|
||||
#include "Network/UDPClient.h" //LOL
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
@@ -18,6 +17,7 @@
|
||||
#include "Core/EPlayerDamage.h"
|
||||
#include "Network/EPlayerDisconnected.h"
|
||||
#include "Core/EPlayerSpawned.h"
|
||||
#include "../Game/Events/EDoubleJump.h"
|
||||
#include "Core/EEntityDeleted.h"
|
||||
#include "Core/EComponentDeleted.h"
|
||||
|
||||
@@ -81,8 +81,9 @@ private:
|
||||
void parseOnInputCommand(Packet& packet);
|
||||
void parseClientPing();
|
||||
void parsePing();
|
||||
void parseUDPConnect(Packet & packet);
|
||||
void parseTCPConnect(Packet & packet);
|
||||
bool parseDoubleJump(Packet& packet);
|
||||
void parseUDPConnect(Packet& packet);
|
||||
void parseTCPConnect(Packet& packet);
|
||||
void parseDisconnect();
|
||||
void parseServerlistRequest(boost::asio::ip::udp::endpoint endpoint);
|
||||
bool shouldSendToClient(EntityWrapper childEntity);
|
||||
|
||||
@@ -25,10 +25,9 @@ private:
|
||||
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
|
||||
boost::shared_ptr<boost::asio::ip::tcp::socket> lastReceivedSocket;
|
||||
|
||||
void handle_accept(boost::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||||
int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
const boost::system::error_code& error);
|
||||
int readBuffer(PlayerDefinition& playerDefinition);
|
||||
PlayerID getPlayerIDFromEndpoint(const std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
boost::asio::ip::address address, unsigned short port);
|
||||
int GetPort();
|
||||
std::string GetAddress();
|
||||
int m_Port = 0;
|
||||
|
||||
@@ -21,6 +21,8 @@ public:
|
||||
std::string GetFileName() const;
|
||||
GLuint GetHandle() const;
|
||||
bool IsCompiled() const;
|
||||
static std::string ReadFile(std::string fileName);
|
||||
private:
|
||||
protected:
|
||||
GLenum m_ShaderType;
|
||||
std::string m_FileName;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Events
|
||||
|
||||
struct DoubleJump : public Event
|
||||
{
|
||||
|
||||
EntityID entityID;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -34,9 +34,13 @@ private:
|
||||
glm::vec3 m_LastPosition = glm::vec3();
|
||||
// The logic for making the sound play when player is moving
|
||||
void playerStep(double dt);
|
||||
// Spawn a hexagon at origin of an Entity
|
||||
void spawnHexagon(EntityWrapper target);
|
||||
|
||||
EventRelay<PlayerMovementSystem, Events::PlayerSpawned> m_EPlayerSpawned;
|
||||
bool OnPlayerSpawned(Events::PlayerSpawned& e);
|
||||
EventRelay<PlayerMovementSystem, Events::DoubleJump> m_EDoubleJump;
|
||||
bool PlayerMovementSystem::OnDoubleJump(Events::DoubleJump & e);
|
||||
|
||||
void updateMovementControllers(double dt);
|
||||
void updateVelocity(EntityWrapper player, double dt);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
<Physics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Physics.xsd">
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
<Gravity>true</Gravity>
|
||||
<PrevOrigin X="-9876.5" Y="-9876.5" Z="-9876.5"/>
|
||||
<IsOnGround>false</IsOnGround>
|
||||
<VerticalStepHeight>0.33</VerticalStepHeight>
|
||||
</Physics>
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
<xs:annotation><xs:documentation>m/s^2</xs:documentation></xs:annotation>
|
||||
</xs:element>
|
||||
<xs:element name="Gravity" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="PrevOrigin" type="t:Vector" minOccurs="0"/>
|
||||
<xs:element name="IsOnGround" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="VerticalStepHeight" type="t:double" minOccurs="0">
|
||||
<xs:annotation><xs:documentation>The largest height of a "stair-step" that can be walked over</xs:documentation></xs:annotation>
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
<c:DashAbility/>
|
||||
<c:Health/>
|
||||
<c:Physics>
|
||||
<PrevOrigin X="0" Y="0.772000015" Z="0"/>
|
||||
<Velocity X="2.30999646e-23" Y="0" Z="1.05272533e-23"/>
|
||||
</c:Physics>
|
||||
<c:Player>
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
<c:DashAbility/>
|
||||
<c:Health/>
|
||||
<c:Physics>
|
||||
<PrevOrigin X="0" Y="0.772000015" Z="0"/>
|
||||
<Velocity X="2.30999646e-23" Y="0" Z="1.05272533e-23"/>
|
||||
</c:Physics>
|
||||
<c:Player>
|
||||
|
||||
@@ -116,12 +116,7 @@ LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensi
|
||||
return result;
|
||||
}
|
||||
|
||||
vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap)
|
||||
{
|
||||
mat3 TBN = mat3(tangent, bitangent, normal);
|
||||
vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0);
|
||||
return vec4(TBN * normalize(NormalMap), 0.0);
|
||||
}
|
||||
#include "Shaders/Util/CommonUniforms.glsl"
|
||||
|
||||
void main()
|
||||
{
|
||||
@@ -180,6 +175,7 @@ void main()
|
||||
color_result += FillColor;
|
||||
}
|
||||
sceneColor = vec4(color_result.xyz, clamp(color_result.a, 0, 1));
|
||||
//sceneColor = CommonUniforms.testColour;
|
||||
//sceneColor = vec4(reflectionColor.xyz, 1);
|
||||
color_result += glowTexel*GlowIntensity;
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap)
|
||||
{
|
||||
mat3 TBN = mat3(tangent, bitangent, normal);
|
||||
vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0);
|
||||
return vec4(TBN * normalize(NormalMap), 0.0);
|
||||
}
|
||||
@@ -16,51 +16,51 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
||||
EntityAABB& boxA = *boundingBox;
|
||||
bool everHitTheGround = false;
|
||||
|
||||
glm::vec3 size = boxA.Size();
|
||||
float diameter = std::min(size.x, size.z);
|
||||
glm::vec3 prevOrigin = (glm::vec3)cPhysics["PrevOrigin"];
|
||||
glm::vec3 toCurrentPos = boxA.Origin() - prevOrigin;
|
||||
float rayLength = glm::length(toCurrentPos) + 0.5f*diameter;
|
||||
//If the entity has moved farther than the size of its box, we need to handle it specially.
|
||||
bool traceCollision = rayLength > diameter;
|
||||
//hack solution: If prevOrigin is less than -9000 in all dimensions,
|
||||
//then it means it is not set, i.e. this is the first collision check for the entity.
|
||||
if (traceCollision && glm::any(glm::greaterThan((glm::vec3)cPhysics["PrevOrigin"], glm::vec3(-9000.f)))) {
|
||||
Ray ray(prevOrigin, toCurrentPos);
|
||||
m_OctreeResult.clear();
|
||||
m_Octree->ObjectsPossiblyHitByRay(ray, m_OctreeResult);
|
||||
for (auto& boxB : m_OctreeResult) {
|
||||
if (boxA.Entity == boxB.Entity) {
|
||||
continue;
|
||||
}
|
||||
bool hit;
|
||||
float dist;
|
||||
if (boxB.Entity.HasComponent("Model")) {
|
||||
RawModel* model;
|
||||
std::string res = (std::string)boxB.Entity["Model"]["Resource"];
|
||||
try {
|
||||
model = ResourceManager::Load<RawModel, true>(res);
|
||||
} catch (const std::exception&) {
|
||||
auto prevPosIt = m_PrevPositions.find(entity);
|
||||
if (prevPosIt != m_PrevPositions.end()) {
|
||||
glm::vec3 size = boxA.Size();
|
||||
float diameter = std::min(size.x, size.z);
|
||||
glm::vec3 prevOrigin = prevPosIt->second;
|
||||
glm::vec3 toCurrentPos = boxA.Origin() - prevOrigin;
|
||||
float rayLength = glm::length(toCurrentPos) + 0.5f*diameter;
|
||||
//If the entity has moved farther than the size of its box, we need to handle it specially.
|
||||
if (rayLength > diameter) {
|
||||
Ray ray(prevOrigin, toCurrentPos);
|
||||
m_OctreeResult.clear();
|
||||
m_Octree->ObjectsPossiblyHitByRay(ray, m_OctreeResult);
|
||||
for (auto& boxB : m_OctreeResult) {
|
||||
if (boxA.Entity == boxB.Entity) {
|
||||
continue;
|
||||
}
|
||||
float u, v;
|
||||
hit = Collision::RayVsModel(ray, model->Vertices(), model->m_Indices, Transform::ModelMatrix(boxB.Entity), dist, u, v);
|
||||
} else {
|
||||
hit = Collision::RayVsAABB(ray, boxB, dist);
|
||||
}
|
||||
if (hit && dist < rayLength) {
|
||||
//Set the entity to where it was colliding, minus the maximum box size.
|
||||
//TODO: Perhaps this should be done slightly more properly.
|
||||
glm::vec3 newOriginPos = ray.Origin() + (dist - 0.707107f*diameter) * ray.Direction();
|
||||
glm::vec3 resolve = newOriginPos - boxA.Origin();
|
||||
(glm::vec3&)cTransform["Position"] += resolve;
|
||||
boxA = *Collision::EntityAbsoluteAABB(entity);
|
||||
if (resolve.y > 0) {
|
||||
everHitTheGround = true;
|
||||
(bool)cPhysics["IsOnGround"] = true;
|
||||
((glm::vec3&)cPhysics["Velocity"]).y = 0.f;
|
||||
bool hit;
|
||||
float dist;
|
||||
if (boxB.Entity.HasComponent("Model")) {
|
||||
RawModel* model;
|
||||
std::string res = (std::string)boxB.Entity["Model"]["Resource"];
|
||||
try {
|
||||
model = ResourceManager::Load<RawModel, true>(res);
|
||||
} catch (const std::exception&) {
|
||||
continue;
|
||||
}
|
||||
float u, v;
|
||||
hit = Collision::RayVsModel(ray, model->Vertices(), model->m_Indices, Transform::ModelMatrix(boxB.Entity), dist, u, v);
|
||||
} else {
|
||||
hit = Collision::RayVsAABB(ray, boxB, dist);
|
||||
}
|
||||
if (hit && dist < rayLength) {
|
||||
//Set the entity to where it was colliding, minus the maximum box size.
|
||||
//TODO: Perhaps this should be done slightly more properly.
|
||||
glm::vec3 newOriginPos = ray.Origin() + (dist - 0.707107f*diameter) * ray.Direction();
|
||||
glm::vec3 resolve = newOriginPos - boxA.Origin();
|
||||
(glm::vec3&)cTransform["Position"] += resolve;
|
||||
boxA = *Collision::EntityAbsoluteAABB(entity);
|
||||
if (resolve.y > 0) {
|
||||
everHitTheGround = true;
|
||||
(bool)cPhysics["IsOnGround"] = true;
|
||||
((glm::vec3&)cPhysics["Velocity"]).y = 0.f;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
||||
float verticalStepHeight = (float)(double)cPhysics["VerticalStepHeight"];
|
||||
if (Collision::AABBvsTriangles(boxA, model->Vertices(), model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) {
|
||||
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||
boxA = *Collision::EntityAbsoluteAABB(entity);
|
||||
cPhysics["Velocity"] = inOutVelocity;
|
||||
if (isOnGround) {
|
||||
everHitTheGround = true;
|
||||
@@ -99,6 +100,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
||||
} else if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
|
||||
//Enter here if boxB has no Model.
|
||||
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||
boxA = *Collision::EntityAbsoluteAABB(entity);
|
||||
if (resolutionVector.y > 0) {
|
||||
everHitTheGround = true;
|
||||
(bool)cPhysics["IsOnGround"] = true;
|
||||
@@ -112,5 +114,5 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
||||
(bool)cPhysics["IsOnGround"] = false;
|
||||
}
|
||||
|
||||
(glm::vec3&)cPhysics["PrevOrigin"] = boxA.Origin();
|
||||
m_PrevPositions[entity] = boxA.Origin();
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ void Client::Connect(std::string address, int port)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Client::OnInputCommand);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &Client::OnPlayerDamage);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &Client::OnPlayerSpawned);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &Client::OnDoubleJump);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ESearchForServers, &Client::OnSearchForServers);
|
||||
auto config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
m_Address = address;
|
||||
@@ -140,6 +141,9 @@ void Client::parseMessageType(Packet& packet)
|
||||
case MessageType::OnPlayerDamage:
|
||||
parsePlayerDamage(packet);
|
||||
break;
|
||||
case MessageType::OnDoubleJump:
|
||||
parseDoubleJump(packet);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -272,6 +276,20 @@ void Client::parseComponentDeletion(Packet & packet)
|
||||
}
|
||||
}
|
||||
|
||||
void Client::parseDoubleJump(Packet & packet)
|
||||
{
|
||||
EntityID serverID = packet.ReadPrimitive<EntityID>();
|
||||
if (!serverClientMapsHasEntity(serverID)) {
|
||||
return;
|
||||
}
|
||||
Events::DoubleJump e;
|
||||
e.entityID = m_ServerIDToClientID.at(serverID);
|
||||
// If player is local player do not publish to prevent infinite feedback loop
|
||||
if (e.entityID != m_LocalPlayer.ID) {
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID)
|
||||
{
|
||||
for (auto field : componentInfo.FieldsInOrder) {
|
||||
@@ -348,9 +366,7 @@ void Client::parseSnapshot(Packet& packet)
|
||||
EntityWrapper localEntity(m_World, localEntityID);
|
||||
// Update entity
|
||||
if (m_World->HasComponent(localEntityID, componentType)) {
|
||||
if (localEntity.Name() == "CapturePointHUD") {
|
||||
UpdateLocalCapturePointHUD(localEntity);
|
||||
}
|
||||
// TODO Fix memory leak here
|
||||
SharedComponentWrapper newComponent = createSharedComponent(packet, localEntityID, componentInfo);
|
||||
bool shouldApply = true;
|
||||
// Apply potential filter function
|
||||
@@ -361,6 +377,7 @@ void Client::parseSnapshot(Packet& packet)
|
||||
ComponentWrapper currentComponent = m_World->GetComponent(localEntityID, componentType);
|
||||
memcpy(currentComponent.Data, newComponent.Data, componentInfo.Stride);
|
||||
}
|
||||
|
||||
//if (localEntity != m_LocalPlayer && !localEntity.IsChildOf(m_LocalPlayer)) {
|
||||
// updateFields(packet, componentInfo, localEntityID);
|
||||
//} else {
|
||||
@@ -377,7 +394,11 @@ void Client::parseSnapshot(Packet& packet)
|
||||
if (serverParentID == EntityID_Invalid) {
|
||||
newLocalEntityID = m_World->CreateEntity(EntityID_Invalid);
|
||||
} else {
|
||||
newLocalEntityID = m_World->CreateEntity(m_ServerIDToClientID.at(serverParentID));
|
||||
if (serverClientMapsHasEntity(serverParentID)) {
|
||||
newLocalEntityID = m_World->CreateEntity(m_ServerIDToClientID.at(serverParentID));
|
||||
} else {
|
||||
newLocalEntityID = m_World->CreateEntity(EntityID_Invalid);
|
||||
}
|
||||
}
|
||||
m_World->SetName(newLocalEntityID, serverEntityName);
|
||||
insertIntoServerClientMaps(serverEntityID, newLocalEntityID);
|
||||
@@ -387,26 +408,16 @@ void Client::parseSnapshot(Packet& packet)
|
||||
}
|
||||
// Parent logic
|
||||
// This should be enough beacause we know that the entities arives in pre-order (there will always be a parent)
|
||||
if (serverParentID != EntityID_Invalid) {
|
||||
if (serverParentID != EntityID_Invalid && serverClientMapsHasEntity(serverParentID)) {
|
||||
EntityID localEntityID = m_ServerIDToClientID.at(serverEntityID);
|
||||
m_World->SetParent(localEntityID, m_ServerIDToClientID.at(serverParentID));
|
||||
if (m_World->GetParent(localEntityID) != m_ServerIDToClientID.at(serverParentID)) {
|
||||
m_World->SetParent(localEntityID, m_ServerIDToClientID.at(serverParentID));
|
||||
}
|
||||
}
|
||||
}
|
||||
parseSpawnEvents();
|
||||
}
|
||||
|
||||
|
||||
void Client::UpdateLocalCapturePointHUD(EntityWrapper capturePointHUD)
|
||||
{
|
||||
//auto children = m_World->GetChildren(capturePointHUD.ID);
|
||||
//for (auto it = children.first; it != children.second; it++) {
|
||||
// it->first
|
||||
//}
|
||||
//
|
||||
//EntityWrapper& localHUD = m_LocalPlayer.FirstChildByName("HUD").FirstChildByName("CapturePointHUD");
|
||||
//m_World->GetComponentPools()
|
||||
}
|
||||
|
||||
void Client::disconnect()
|
||||
{
|
||||
m_IsConnected = false;
|
||||
@@ -469,6 +480,11 @@ bool Client::OnPlayerDamage(const Events::PlayerDamage & e)
|
||||
if (e.Inflictor != m_LocalPlayer) {
|
||||
return false;
|
||||
}
|
||||
// Could this happen?
|
||||
//if (!clientServerMapsHasEntity(e.Inflictor.ID)
|
||||
// || !clientServerMapsHasEntity(e.Victim.ID)) {
|
||||
// return;
|
||||
//}
|
||||
|
||||
Packet packet(MessageType::OnPlayerDamage, m_SendPacketID);
|
||||
packet.WritePrimitive(m_ClientIDToServerID.at(e.Inflictor.ID));
|
||||
@@ -503,7 +519,7 @@ void Client::parsePlayerDamage(Packet& packet)
|
||||
Events::PlayerDamage e;
|
||||
PlayerID victimID = packet.ReadPrimitive<EntityID>();
|
||||
PlayerID inflictorID = packet.ReadPrimitive<EntityID>();
|
||||
if(!serverClientMapsHasEntity(victimID) || !serverClientMapsHasEntity(inflictorID)){
|
||||
if (!serverClientMapsHasEntity(victimID) || !serverClientMapsHasEntity(inflictorID)) {
|
||||
return;
|
||||
}
|
||||
e.Inflictor = EntityWrapper(m_World, m_ServerIDToClientID.at(victimID));
|
||||
@@ -515,6 +531,17 @@ void Client::parsePlayerDamage(Packet& packet)
|
||||
}
|
||||
}
|
||||
|
||||
bool Client::OnDoubleJump(Events::DoubleJump & e)
|
||||
{
|
||||
if (!clientServerMapsHasEntity(e.entityID) || e.entityID != m_LocalPlayer.ID) {
|
||||
return false;
|
||||
}
|
||||
Packet packet(MessageType::OnDoubleJump);
|
||||
packet.WritePrimitive(m_ClientIDToServerID.at(e.entityID));
|
||||
m_Reliable.Send(packet);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Client::sendLocalPlayerTransform()
|
||||
{
|
||||
if (!m_LocalPlayer.Valid()) {
|
||||
|
||||
@@ -29,9 +29,8 @@ Server::~Server()
|
||||
|
||||
void Server::Update()
|
||||
{
|
||||
PlayerDefinition pd;
|
||||
|
||||
m_Reliable.AcceptNewConnections(m_NextPlayerID, m_ConnectedPlayers);
|
||||
|
||||
for (auto& kv : m_ConnectedPlayers) {
|
||||
while (kv.second.TCPSocket->available()) {
|
||||
// Packet will get real data in receive
|
||||
@@ -47,6 +46,7 @@ void Server::Update()
|
||||
}
|
||||
}
|
||||
|
||||
PlayerDefinition pd;
|
||||
while (m_Unreliable.IsSocketAvailable()) {
|
||||
// Packet will get real data in receive
|
||||
Packet packet(MessageType::Invalid);
|
||||
@@ -65,7 +65,7 @@ void Server::Update()
|
||||
PlayerDefinition localArea;
|
||||
localArea.Endpoint = boost::asio::ip::udp::endpoint();
|
||||
m_ServerlistRequest.Receive(packet, localArea);
|
||||
if(packet.GetMessageType() == MessageType::ServerlistRequest) {
|
||||
if (packet.GetMessageType() == MessageType::ServerlistRequest) {
|
||||
packet.ReadPrimitive<int>(); // Pop size
|
||||
packet.ReadPrimitive<int>(); // Pop MsgType
|
||||
packet.ReadPrimitive<int>(); // Pop packet ID
|
||||
@@ -76,7 +76,7 @@ void Server::Update()
|
||||
}
|
||||
|
||||
// Check if players have disconnected
|
||||
for (int i = 0; i < m_PlayersToDisconnect.size(); i++) {
|
||||
for (int i = 0; i < m_PlayersToDisconnect.size(); i++) {
|
||||
disconnect(m_PlayersToDisconnect.at(i));
|
||||
}
|
||||
m_PlayersToDisconnect.clear();
|
||||
@@ -136,7 +136,10 @@ void Server::parseMessageType(Packet& packet)
|
||||
parseOnPlayerDamage(packet);
|
||||
break;
|
||||
case MessageType::PlayerTransform:
|
||||
parsePlayerTransform(packet);
|
||||
parsePlayerTransform(packet);
|
||||
break;
|
||||
case MessageType::OnDoubleJump:
|
||||
parseDoubleJump(packet);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -191,43 +194,41 @@ void Server::addPlayersToPacket(Packet & packet, EntityID entityID)
|
||||
// HACK: Only sync players for now, since the map turned out to be TOO LARGE to send in one snapshot and Simon's computer shits itself
|
||||
// HACK: Also checked CapturePointHUD for now. (this would get out of sync);
|
||||
EntityWrapper childEntity(m_World, childEntityID);
|
||||
if (!shouldSendToClient(childEntity)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Write EntityID and parentsID and Entity name
|
||||
packet.WritePrimitive(childEntityID);
|
||||
packet.WritePrimitive(entityID);
|
||||
packet.WriteString(m_World->GetName(childEntityID));
|
||||
// Write components to child
|
||||
int numberOfComponents = 0;
|
||||
for (auto& i : worldComponentPools) {
|
||||
if (i.second->KnowsEntity(childEntityID)) {
|
||||
numberOfComponents++;
|
||||
if (shouldSendToClient(childEntity)) {
|
||||
// Write EntityID and parentsID and Entity name
|
||||
packet.WritePrimitive(childEntityID);
|
||||
packet.WritePrimitive(entityID);
|
||||
packet.WriteString(m_World->GetName(childEntityID));
|
||||
// Write components to child
|
||||
int numberOfComponents = 0;
|
||||
for (auto& i : worldComponentPools) {
|
||||
if (i.second->KnowsEntity(childEntityID)) {
|
||||
numberOfComponents++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Write how many components should be read
|
||||
packet.WritePrimitive(numberOfComponents);
|
||||
for (auto& i : worldComponentPools) {
|
||||
// If the entity exist in the pool
|
||||
if (i.second->KnowsEntity(childEntityID)) {
|
||||
ComponentWrapper componentWrapper = i.second->GetByEntity(childEntityID);
|
||||
// ComponentType
|
||||
packet.WriteString(componentWrapper.Info.Name);
|
||||
// Loop through fields
|
||||
for (auto& componentField : componentWrapper.Info.FieldsInOrder) {
|
||||
ComponentInfo::Field_t fieldInfo = componentWrapper.Info.Fields.at(componentField);
|
||||
if (fieldInfo.Type == "string") {
|
||||
std::string& value = componentWrapper[componentField];
|
||||
packet.WriteString(value);
|
||||
} else {
|
||||
packet.WriteData(componentWrapper.Data + fieldInfo.Offset, fieldInfo.Stride);
|
||||
// Write how many components should be read
|
||||
packet.WritePrimitive(numberOfComponents);
|
||||
for (auto& i : worldComponentPools) {
|
||||
// If the entity exist in the pool
|
||||
if (i.second->KnowsEntity(childEntityID)) {
|
||||
ComponentWrapper componentWrapper = i.second->GetByEntity(childEntityID);
|
||||
// ComponentType
|
||||
packet.WriteString(componentWrapper.Info.Name);
|
||||
// Loop through fields
|
||||
for (auto& componentField : componentWrapper.Info.FieldsInOrder) {
|
||||
ComponentInfo::Field_t fieldInfo = componentWrapper.Info.Fields.at(componentField);
|
||||
if (fieldInfo.Type == "string") {
|
||||
std::string& value = componentWrapper[componentField];
|
||||
packet.WriteString(value);
|
||||
} else {
|
||||
packet.WriteData(componentWrapper.Data + fieldInfo.Offset, fieldInfo.Stride);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Go to to your children
|
||||
addChildrenToPacket(packet, childEntityID);
|
||||
addPlayersToPacket(packet, childEntityID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,7 +344,7 @@ void Server::parseTCPConnect(Packet & packet)
|
||||
// Read packet ID
|
||||
m_PreviousPacketID = m_PacketID; // Set previous packet id
|
||||
m_PacketID = packet.ReadPrimitive<int>(); //Read new packet id
|
||||
|
||||
|
||||
LOG_INFO("Parsing connections");
|
||||
// Check if player is already connected
|
||||
// Ska vara till lagd i TCPServer receive
|
||||
@@ -455,8 +456,7 @@ bool Server::OnInputCommand(const Events::InputCommand & e)
|
||||
}
|
||||
isReadingData = !isReadingData;
|
||||
m_SaveDataTimer = std::clock();
|
||||
}
|
||||
else if (e.Command == "KickPlayer" && e.Value > 0) {
|
||||
} else if (e.Command == "KickPlayer" && e.Value > 0) {
|
||||
kick(0);
|
||||
}
|
||||
|
||||
@@ -507,7 +507,7 @@ bool Server::OnPlayerDamage(const Events::PlayerDamage& e)
|
||||
packet.WritePrimitive(e.Damage);
|
||||
reliableBroadcast(packet);
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Server::parseClientPing()
|
||||
@@ -536,6 +536,12 @@ void Server::parsePing()
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::parseDoubleJump(Packet & packet)
|
||||
{
|
||||
reliableBroadcast(packet);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Server::parseOnInputCommand(Packet& packet)
|
||||
{
|
||||
PlayerID player = -1;
|
||||
@@ -595,8 +601,15 @@ void Server::parsePlayerTransform(Packet& packet)
|
||||
|
||||
bool Server::shouldSendToClient(EntityWrapper childEntity)
|
||||
{
|
||||
auto children = m_World->GetChildren(childEntity.ID);
|
||||
for (auto it = children.first; it != children.second; it++) {
|
||||
EntityWrapper child(m_World, it->second);
|
||||
if(child.HasComponent("CapturePoint")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return childEntity.HasComponent("Player") || childEntity.FirstParentWithComponent("Player").Valid()
|
||||
|| childEntity.HasComponent("CapturePoint") || childEntity.FirstParentWithComponent("CapturePoint").Valid();
|
||||
|| childEntity.HasComponent("CapturePoint");
|
||||
}
|
||||
|
||||
PlayerID Server::GetPlayerIDFromEndpoint()
|
||||
|
||||
@@ -4,6 +4,8 @@ using namespace boost::asio::ip;
|
||||
TCPServer::TCPServer()
|
||||
{
|
||||
acceptor = std::unique_ptr<tcp::acceptor>(new tcp::acceptor(m_IOService, tcp::endpoint(tcp::v4(), 27666)));
|
||||
// Make the acceptor non-blocking so we wont get stuck in AcceptNewConnections().
|
||||
acceptor->non_blocking(true);
|
||||
m_Port = GetPort();
|
||||
m_Address = GetAddress();
|
||||
}
|
||||
@@ -13,14 +15,24 @@ TCPServer::~TCPServer()
|
||||
|
||||
void TCPServer::AcceptNewConnections(int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers)
|
||||
{
|
||||
boost::system::error_code error;
|
||||
boost::shared_ptr<tcp::socket> newSocket = boost::shared_ptr<tcp::socket>(new tcp::socket(m_IOService));
|
||||
m_IOService.poll();
|
||||
acceptor->async_accept(*newSocket,
|
||||
boost::bind(&TCPServer::handle_accept, this, newSocket, boost::ref(nextPlayerID), boost::ref(connectedPlayers),
|
||||
boost::asio::placeholders::error));
|
||||
acceptor->accept(*newSocket, error);
|
||||
// If no error occured add new tcp connection
|
||||
if (!error) {
|
||||
// Add tcp socket to connections
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
newSocket->set_option(option);
|
||||
PlayerDefinition pd;
|
||||
pd.StopTime = std::clock();
|
||||
pd.TCPSocket = newSocket;
|
||||
pd.TCPAddress = newSocket.get()->remote_endpoint().address();
|
||||
pd.TCPPort = newSocket.get()->remote_endpoint().port();
|
||||
connectedPlayers[nextPlayerID++] = pd;
|
||||
}
|
||||
}
|
||||
|
||||
PlayerID GetPlayerIDFromEndpoint(const std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
PlayerID TCPServer::getPlayerIDFromEndpoint(const std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
boost::asio::ip::address address, unsigned short port)
|
||||
{
|
||||
for (auto& kv : connectedPlayers) {
|
||||
@@ -32,24 +44,6 @@ PlayerID GetPlayerIDFromEndpoint(const std::map<PlayerID, PlayerDefinition>& con
|
||||
return -1;
|
||||
}
|
||||
|
||||
void TCPServer::handle_accept(boost::shared_ptr<tcp::socket> socket,
|
||||
int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
const boost::system::error_code& error)
|
||||
{
|
||||
if (!error && GetPlayerIDFromEndpoint(connectedPlayers, socket->remote_endpoint().address(),
|
||||
socket->remote_endpoint().port()) == -1) {
|
||||
// Add tcp socket to connections
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
socket->set_option(option);
|
||||
PlayerDefinition pd;
|
||||
pd.StopTime = std::clock();
|
||||
pd.TCPSocket = socket;
|
||||
pd.TCPAddress = socket.get()->remote_endpoint().address();
|
||||
pd.TCPPort = socket.get()->remote_endpoint().port();
|
||||
connectedPlayers[nextPlayerID++] = pd;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPServer::Send(Packet & packet, PlayerDefinition & playerDefinition)
|
||||
{
|
||||
packet.UpdateSize();
|
||||
|
||||
@@ -4,22 +4,32 @@ GLuint Shader::CompileShader(GLenum shaderType, std::string fileName)
|
||||
{
|
||||
LOG_INFO("Compiling shader \"%s\"", fileName.c_str());
|
||||
|
||||
std::string shaderFile;
|
||||
std::ifstream in(fileName, std::ios::in);
|
||||
if (!in) {
|
||||
LOG_ERROR("Error: Failed to open shader file \"%s\"", fileName.c_str());
|
||||
return 0;
|
||||
}
|
||||
in.seekg(0, std::ios::end);
|
||||
shaderFile.resize((int)in.tellg());
|
||||
in.seekg(0, std::ios::beg);
|
||||
in.read(&shaderFile[0], shaderFile.size());
|
||||
in.close();
|
||||
std::string shaderFile = ReadFile(fileName);
|
||||
|
||||
GLuint shader = glCreateShader(shaderType);
|
||||
if (GLERROR("glCreateShader"))
|
||||
return 0;
|
||||
|
||||
std::size_t startPos = 0;
|
||||
std::size_t SEofNewFile[2];
|
||||
std::string key = "#include";
|
||||
while((startPos = shaderFile.find(key, startPos)) != std::string::npos)
|
||||
{
|
||||
SEofNewFile[0] = shaderFile.find('"', startPos+key.length())+1;
|
||||
SEofNewFile[1] = shaderFile.find('"', SEofNewFile[0]);
|
||||
if (SEofNewFile[0] == std::string::npos || SEofNewFile[1] == std::string::npos)
|
||||
return 0;
|
||||
|
||||
std::string replacementFileName = shaderFile.substr(SEofNewFile[0], SEofNewFile[1] - SEofNewFile[0]);
|
||||
std::string replacementString = ReadFile(replacementFileName);
|
||||
size_t firstof = replacementString.find_first_of((char)0);
|
||||
replacementString.erase(firstof, replacementString.size() - firstof);
|
||||
if (replacementString.length() <= 0)
|
||||
return 0;
|
||||
shaderFile.replace(startPos, SEofNewFile[1]+2 - startPos, replacementString + "\n");
|
||||
startPos += replacementString.length(); //This might not be wanted.
|
||||
}
|
||||
|
||||
const GLchar* shaderFiles = shaderFile.c_str();
|
||||
const GLint length = static_cast<GLint>(shaderFile.length());
|
||||
glShaderSource(shader, 1, &shaderFiles, &length);
|
||||
@@ -46,6 +56,24 @@ GLuint Shader::CompileShader(GLenum shaderType, std::string fileName)
|
||||
return shader;
|
||||
}
|
||||
|
||||
std::string Shader::ReadFile(std::string fileName)
|
||||
{
|
||||
std::string shaderFile;
|
||||
std::ifstream in(fileName, std::ios::in);
|
||||
if (!in) {
|
||||
LOG_ERROR("Error: Failed to open shader file \"%s\"", fileName.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
in.seekg(0, std::ios::end);
|
||||
shaderFile.resize((int)in.tellg());
|
||||
in.seekg(0, std::ios::beg);
|
||||
in.read(&shaderFile[0], shaderFile.size());
|
||||
in.close();
|
||||
return shaderFile;
|
||||
}
|
||||
|
||||
|
||||
Shader::Shader(GLenum shaderType, std::string fileName) : m_ShaderType(shaderType), m_FileName(fileName)
|
||||
{
|
||||
m_ShaderHandle = 0;
|
||||
|
||||
@@ -6,9 +6,11 @@ CapturePointSystem::CapturePointSystem(SystemParams params)
|
||||
, PureSystem("CapturePoint")
|
||||
{
|
||||
//subscribe/listenTo playerdamage,healthpickup events (using the eventBroker)
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &CapturePointSystem::OnTriggerTouch);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ETriggerLeave, &CapturePointSystem::OnTriggerLeave);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &CapturePointSystem::OnCaptured);
|
||||
if (!IsClient) {
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &CapturePointSystem::OnTriggerTouch);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ETriggerLeave, &CapturePointSystem::OnTriggerLeave);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ECaptured, &CapturePointSystem::OnCaptured);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +18,9 @@ CapturePointSystem::CapturePointSystem(SystemParams params)
|
||||
//NOTE: needs to run each frame, since we're possibly modifying the captureTimer for the capturePoints by dt
|
||||
void CapturePointSystem::UpdateComponent(EntityWrapper& capturePointEntity, ComponentWrapper& cCapturePoint, double dt)
|
||||
{
|
||||
if (IsClient) {
|
||||
return;
|
||||
}
|
||||
if (m_WinnerWasFound) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ PlayerMovementSystem::PlayerMovementSystem(SystemParams params)
|
||||
: System(params)
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &PlayerMovementSystem::OnPlayerSpawned);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &PlayerMovementSystem::OnDoubleJump);
|
||||
}
|
||||
|
||||
PlayerMovementSystem::~PlayerMovementSystem()
|
||||
@@ -36,7 +37,6 @@ void PlayerMovementSystem::updateMovementControllers(double dt)
|
||||
if (!player.Valid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Aim pitch
|
||||
EntityWrapper cameraEntity = player.FirstChildByName("Camera");
|
||||
if (cameraEntity.Valid()) {
|
||||
@@ -122,15 +122,14 @@ void PlayerMovementSystem::updateMovementControllers(double dt)
|
||||
if (isOnGround) {
|
||||
controller->SetDoubleJumping(false);
|
||||
} else {
|
||||
// If IsServer and network is off this will not work
|
||||
if (IsClient) {
|
||||
//put a hexagon at the players feet
|
||||
auto hexagonEffect = ResourceManager::Load<EntityFile>("Schema/Entities/DoubleJumpHexagon.xml");
|
||||
EntityFileParser parser(hexagonEffect);
|
||||
EntityID hexagonEffectID = parser.MergeEntities(m_World);
|
||||
EntityWrapper hexagonEW = EntityWrapper(m_World, hexagonEffectID);
|
||||
hexagonEW["Transform"]["Position"] = (glm::vec3)player["Transform"]["Position"];
|
||||
spawnHexagon(player);
|
||||
controller->SetDoubleJumping(true);
|
||||
// Publish event for client to listen to
|
||||
Events::DoubleJump e;
|
||||
e.entityID = player.ID;
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
@@ -295,3 +294,26 @@ bool PlayerMovementSystem::OnPlayerSpawned(Events::PlayerSpawned& e)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlayerMovementSystem::OnDoubleJump(Events::DoubleJump & e)
|
||||
{
|
||||
// If entity does not exist, exit
|
||||
if (!EntityWrapper(m_World, e.entityID).Valid()) {
|
||||
return false;
|
||||
}
|
||||
// If entity IsLocalPlayer, exit
|
||||
if (e.entityID == m_LocalPlayer.ID) {
|
||||
return false;
|
||||
}
|
||||
spawnHexagon(EntityWrapper(m_World, e.entityID));
|
||||
}
|
||||
|
||||
void PlayerMovementSystem::spawnHexagon(EntityWrapper target)
|
||||
{
|
||||
//put a hexagon at the entitys... feet?
|
||||
auto hexagonEffect = ResourceManager::Load<EntityFile>("Schema/Entities/DoubleJumpHexagon.xml");
|
||||
EntityFileParser parser(hexagonEffect);
|
||||
EntityID hexagonEffectID = parser.MergeEntities(m_World);
|
||||
EntityWrapper hexagonEW = EntityWrapper(m_World, hexagonEffectID);
|
||||
hexagonEW["Transform"]["Position"] = (glm::vec3)target["Transform"]["Position"];
|
||||
}
|
||||
Reference in New Issue
Block a user