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