R-Type
R-Type project
NetworkComponent.hpp
1 #pragma once
2 
3 #include "Component.hpp"
4 #include "NetworkIdentity.hpp"
5 #include "KapMirror/NetworkClient.hpp"
6 #include "KapMirror/NetworkServer.hpp"
7 
8 namespace KapMirror {
10  protected:
11  NetworkIdentity* networkIdentity = nullptr;
12 
13  public:
14  NetworkComponent(std::shared_ptr<KapEngine::GameObject> go, const std::string& name);
15  ~NetworkComponent() = default;
16 
17  std::shared_ptr<NetworkServer> getServer() const;
18 
19  std::shared_ptr<NetworkClient> getClient() const;
20 
21  void onAwake() override;
22 
23  void onDestroy() override;
24 
28  bool isServer() const;
29 
33  bool isClient() const;
34 
38  bool isLocal() const;
39 
43  bool hasAuthority() const;
44 
48  unsigned int getNetworkId() const {
49  if (networkIdentity == nullptr) {
50  throw std::runtime_error("NetworkComponent: NetworkIdentity is not set");
51  }
52  return networkIdentity->getNetworkId();
53  }
54 
58  virtual void onStartServer() {}
59 
63  virtual void onStopServer() {}
64 
68  virtual void onStartClient() {}
69 
73  virtual void onStopClient() {}
74 
78  virtual void onObjectUpdate() {}
79 
83  virtual void serialize(KapMirror::NetworkWriter& writer) {}
84 
88  virtual void deserialize(KapMirror::NetworkReader& reader) {}
89 
90  public:
91  void _setNetworkIdentity(NetworkIdentity* identity) { networkIdentity = identity; }
92  };
93 } // namespace KapMirror
KapMirror::NetworkComponent::onObjectUpdate
virtual void onObjectUpdate()
Called when the object is spawned or updated.
Definition: NetworkComponent.hpp:78
KapMirror::NetworkComponent::isLocal
bool isLocal() const
True if this object is not spawned by the server or the client. (is local object)
Definition: NetworkComponent.cpp:41
KapMirror::NetworkComponent::getNetworkId
unsigned int getNetworkId() const
The unique network Id of this object (unique at runtime).
Definition: NetworkComponent.hpp:48
KapMirror::NetworkComponent::onStopServer
virtual void onStopServer()
Stop event, only called on server.
Definition: NetworkComponent.hpp:63
KapMirror::NetworkComponent::onStopClient
virtual void onStopClient()
Stop event, only called on client.
Definition: NetworkComponent.hpp:73
KapMirror::NetworkComponent::hasAuthority
bool hasAuthority() const
True on client if that component has been assigned to the client.
Definition: NetworkComponent.cpp:48
KapMirror::NetworkIdentity
Definition: NetworkIdentity.hpp:6
KapMirror::NetworkComponent::isServer
bool isServer() const
True if this object is on the server and has been spawned.
Definition: NetworkComponent.cpp:27
KapMirror::NetworkComponent::serialize
virtual void serialize(KapMirror::NetworkWriter &writer)
Serialize all the data from this component into payload.
Definition: NetworkComponent.hpp:83
KapMirror::NetworkReader
Definition: NetworkReader.hpp:10
KapMirror::NetworkComponent::deserialize
virtual void deserialize(KapMirror::NetworkReader &reader)
Deserialize all the data from payload into this component.
Definition: NetworkComponent.hpp:88
KapMirror::NetworkComponent::onStartClient
virtual void onStartClient()
Like start(), but only called on client.
Definition: NetworkComponent.hpp:68
KapMirror::NetworkComponent::onStartServer
virtual void onStartServer()
Like start(), but only called on server.
Definition: NetworkComponent.hpp:58
KapEngine::Component
Definition: Component.hpp:38
KapMirror::NetworkWriter
Definition: NetworkWriter.hpp:12
KapMirror::NetworkComponent::onAwake
void onAwake() override
call component awake
Definition: NetworkComponent.cpp:14
KapMirror::NetworkComponent::onDestroy
void onDestroy() override
call when component destroyed
Definition: NetworkComponent.cpp:21
KapMirror::NetworkIdentity::getNetworkId
unsigned int getNetworkId() const
The unique network Id of this object (unique at runtime).
Definition: NetworkIdentity.hpp:24
KapMirror::NetworkComponent::isClient
bool isClient() const
True if this object is on the client and has been spawned by the server.
Definition: NetworkComponent.cpp:34
KapMirror::NetworkComponent
Definition: NetworkComponent.hpp:9