]> git.localhorst.tv Git - blank.git/blob - src/client/MasterState.hpp
aaea6a99b442aa152b209d0f6953261746025b50
[blank.git] / src / client / MasterState.hpp
1 #ifndef BLANK_CLIENT_CLIENTSTATE_HPP_
2 #define BLANK_CLIENT_CLIENTSTATE_HPP_
3
4 #include "InitialState.hpp"
5 #include "InteractiveState.hpp"
6 #include "../app/State.hpp"
7 #include "../net/Client.hpp"
8 #include "../net/ConnectionHandler.hpp"
9
10 #include <map>
11 #include <memory>
12
13
14 namespace blank {
15
16 class Environment;
17
18 namespace client {
19
20 class InteractiveState;
21
22 class MasterState
23 : public State
24 , public ConnectionHandler {
25
26 public:
27         MasterState(
28                 Environment &,
29                 const World::Config &,
30                 const Interface::Config &,
31                 const Client::Config &
32         );
33
34         Client &GetClient() noexcept { return client; }
35         Environment &GetEnv() noexcept { return env; }
36
37         World::Config &GetWorldConf() noexcept { return world_conf; }
38         const World::Config &GetWorldConf() const noexcept { return world_conf; }
39         const Interface::Config &GetInterfaceConf() const noexcept { return intf_conf; }
40         const Client::Config &GetClientConf() const noexcept { return client_conf; }
41
42         void Quit();
43
44         void OnEnter() override;
45
46         void Handle(const SDL_Event &) override;
47         void Update(int dt) override;
48         void Render(Viewport &) override;
49
50         void OnPacketLost(std::uint16_t) override;
51         void OnTimeout() override;
52
53         void On(const Packet::Join &) override;
54         void On(const Packet::Part &) override;
55         void On(const Packet::SpawnEntity &) override;
56         void On(const Packet::DespawnEntity &) override;
57         void On(const Packet::EntityUpdate &) override;
58         void On(const Packet::PlayerCorrection &) override;
59
60 private:
61         /// flag entity as updated by given packet
62         /// returns false if the update should be ignored
63         bool UpdateEntity(std::uint32_t id, std::uint16_t seq);
64         /// drop update information or given entity
65         void ClearEntity(std::uint32_t id);
66
67 private:
68         Environment &env;
69         World::Config world_conf;
70         const Interface::Config &intf_conf;
71         const Client::Config &client_conf;
72         std::unique_ptr<InteractiveState> state;
73         Client client;
74
75         InitialState init_state;
76
77         int login_packet;
78
79         struct UpdateStatus {
80                 std::uint16_t last_packet;
81                 int last_update;
82         };
83         std::map<std::uint32_t, UpdateStatus> update_status;
84         IntervalTimer update_timer;
85
86 };
87
88 }
89 }
90
91 #endif