1 #include "InitialState.hpp"
2 #include "InteractiveState.hpp"
3 #include "MasterState.hpp"
5 #include "../app/Environment.hpp"
6 #include "../app/init.hpp"
7 #include "../app/TextureIndex.hpp"
10 #include <glm/gtx/io.hpp>
18 InitialState::InitialState(MasterState &master)
21 message.Position(glm::vec3(0.0f), Gravity::CENTER);
22 message.Set(master.GetEnv().assets.large_ui_font, "logging in");
25 void InitialState::OnEnter() {
29 void InitialState::Handle(const SDL_Event &evt) {
30 if (evt.type == SDL_QUIT) {
35 void InitialState::Update(int dt) {
39 void InitialState::Render(Viewport &viewport) {
40 message.Render(viewport);
44 // TODO: this clutter is a giant mess
45 InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
48 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetClientConf().host))
49 , world(block_types, master.GetWorldConf(), save)
50 , chunk_renderer(world, master.GetWorldConf().load.load_dist)
52 master.GetInterfaceConf(),
55 *world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
57 TextureIndex tex_index;
58 master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
59 chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
60 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
61 // TODO: better solution for initializing HUD
62 interface.SelectNext();
65 void InteractiveState::OnEnter() {
66 master.GetEnv().window.GrabMouse();
69 void InteractiveState::Handle(const SDL_Event &event) {
72 interface.HandlePress(event.key);
75 interface.HandleRelease(event.key);
77 case SDL_MOUSEBUTTONDOWN:
78 interface.HandlePress(event.button);
80 case SDL_MOUSEBUTTONUP:
81 interface.HandleRelease(event.button);
84 interface.Handle(event.motion);
87 interface.Handle(event.wheel);
97 void InteractiveState::Update(int dt) {
100 interface.Update(dt);
102 chunk_renderer.Rebase(interface.Player().ChunkCoords());
103 chunk_renderer.Update(dt);
105 master.GetClient().SendPlayerUpdate(interface.Player());
107 glm::mat4 trans = interface.Player().Transform(interface.Player().ChunkCoords());
108 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
109 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
110 master.GetEnv().audio.Position(interface.Player().Position());
111 master.GetEnv().audio.Velocity(interface.Player().Velocity());
112 master.GetEnv().audio.Orientation(dir, up);
115 void InteractiveState::Render(Viewport &viewport) {
116 viewport.WorldPosition(interface.Player().Transform(interface.Player().ChunkCoords()));
117 chunk_renderer.Render(viewport);
118 world.Render(viewport);
119 interface.Render(viewport);
123 MasterState::MasterState(
125 const World::Config &wc,
126 const Interface::Config &ic,
127 const Client::Config &cc)
136 client.GetConnection().SetHandler(this);
139 void MasterState::Quit() {
140 if (!client.GetConnection().Closed()) {
143 env.state.PopUntil(this);
147 void MasterState::OnEnter() {
148 login_packet = client.SendLogin(intf_conf.player_name);
149 env.state.Push(&init_state);
153 void MasterState::Handle(const SDL_Event &event) {
158 void MasterState::Update(int dt) {
164 void MasterState::Render(Viewport &) {
169 void MasterState::OnPacketLost(uint16_t id) {
170 if (id == login_packet) {
171 login_packet = client.SendLogin(intf_conf.player_name);
175 void MasterState::OnTimeout() {
176 if (client.GetConnection().Closed()) {
177 // TODO: push disconnected message
178 cout << "connection timed out" << endl;
183 void MasterState::On(const Packet::Join &pack) {
184 pack.ReadWorldName(world_conf.name);
188 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
191 cout << "joined game \"" << world_conf.name << '"' << endl;
195 pack.ReadPlayerID(player_id);
196 state.reset(new InteractiveState(*this, player_id));
198 pack.ReadPlayer(state->GetInterface().Player());
200 env.state.PopAfter(this);
201 env.state.Push(state.get());
204 void MasterState::On(const Packet::Part &pack) {
207 cout << "kicked by server" << endl;
210 cout << "login refused by server" << endl;
215 void MasterState::On(const Packet::SpawnEntity &pack) {
217 cout << "got entity spawn before world was created" << endl;
222 pack.ReadEntityID(entity_id);
223 Entity *entity = state->GetWorld().AddEntity(entity_id);
225 cout << "entity ID inconsistency" << endl;
229 pack.ReadEntity(*entity);
230 cout << "spawned entity " << entity->Name() << " at " << entity->AbsolutePosition() << endl;
233 void MasterState::On(const Packet::DespawnEntity &pack) {
235 cout << "got entity despawn before world was created" << endl;
240 pack.ReadEntityID(entity_id);
241 for (Entity &entity : state->GetWorld().Entities()) {
242 if (entity.ID() == entity_id) {
244 cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl;
250 void MasterState::On(const Packet::EntityUpdate &pack) {
252 cout << "got entity update before world was created" << endl;
257 auto world_iter = state->GetWorld().Entities().begin();
258 auto world_end = state->GetWorld().Entities().end();
261 pack.ReadEntityCount(count);
263 for (uint32_t i = 0; i < count; ++i) {
264 uint32_t entity_id = 0;
265 pack.ReadEntityID(entity_id, i);
267 while (world_iter != world_end && world_iter->ID() < entity_id) {
270 if (world_iter == world_end) {
271 // nothing can be done from here
274 if (world_iter->ID() == entity_id) {
275 pack.ReadEntity(*world_iter, i);