forked from RavEngine/RavEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkManager.cpp
60 lines (53 loc) · 1.73 KB
/
NetworkManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "NetworkManager.hpp"
#include "NetworkIdentity.hpp"
#include "App.hpp"
#include "NetworkReplicable.hpp"
#include "Debug.hpp"
using namespace RavEngine;
using namespace std;
void NetworkManager::Spawn(Ref<World> source, Ref<NetworkIdentity> comp) {
// Running on the server?
if (IsServer()){
auto entity = comp->getOwner().lock();
if (entity){
server->SpawnEntity(entity);
}
}
else{
Debug::Warning("Cannot replicate entity creation from client");
}
//ownership is client? do nothing, clients cannot spawn things
//instead use an RPC to have the server construct it and then spawn it
}
void NetworkManager::Destroy(Ref<World> source, Ref<NetworkIdentity> comp) {
// ownership is server and running in the server? need to RPC clients
if (IsServer() && comp->Owner == k_HSteamNetConnection_Invalid){
auto entity = comp->getOwner().lock();
if (entity){
server->DestroyEntity(entity);
}
}
else{
Debug::Warning("Cannot replicate entity destruction from client");
}
}
bool NetworkManager::IsClient() {
return static_cast<bool>(App::networkManager.client);
}
bool NetworkManager::IsServer() {
return static_cast<bool>(App::networkManager.server);
}
void NetworkManager::SyncVarUpdate(const std::string_view &data){
if (IsServer()){
//local client (if there is one) already has the value, so just push to the other clients
server->SendMessageToAllClients(data, NetworkBase::Reliability::Reliable);
}
else if (IsClient()){
//update server, which will then update clients
client->SendMessageToServer(data, NetworkBase::Reliability::Reliable);
}
else{
// WAT
Debug::Fatal("Cannot propagate syncvar, network is not active");
}
}