1 #include "WorldState.hpp"
3 #include "Environment.hpp"
5 #include "TextureIndex.hpp"
12 WorldState::WorldState(
14 const Generator::Config &gc,
15 const Interface::Config &ic,
16 const World::Config &wc,
21 , world(block_types, wc)
22 , interface(ic, env, world, world.AddPlayer(ic.player_name))
24 , chunk_loader(world.Chunks(), generator, save)
25 , chunk_renderer(*interface.GetPlayer().chunks)
27 , spawner(world, skeletons, gc.seed)
28 , preload(env, chunk_loader, chunk_renderer)
29 , unload(env, world.Chunks(), save) {
30 TextureIndex tex_index;
31 env.loader.LoadBlockTypes("default", block_types, tex_index);
32 chunk_renderer.LoadTextures(env.loader, tex_index);
33 chunk_renderer.FogDensity(wc.fog_density);
35 // TODO: better solution for initializing HUD
36 interface.SelectNext();
40 void WorldState::OnEnter() {
41 env.state.Push(&preload);
42 env.window.GrabMouse();
46 void WorldState::Handle(const SDL_Event &event) {
49 interface.HandlePress(event.key);
52 interface.HandleRelease(event.key);
54 case SDL_MOUSEBUTTONDOWN:
55 interface.HandlePress(event.button);
57 case SDL_MOUSEBUTTONUP:
58 interface.HandleRelease(event.button);
61 interface.Handle(event.motion);
64 interface.Handle(event.wheel);
67 env.state.Switch(&unload);
74 void WorldState::Update(int dt) {
78 chunk_loader.Update(dt);
79 chunk_renderer.Update(dt);
81 Entity &player = *interface.GetPlayer().entity;
83 glm::mat4 trans = player.Transform(player.ChunkCoords());
84 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
85 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
86 env.audio.Position(player.Position());
87 env.audio.Velocity(player.Velocity());
88 env.audio.Orientation(dir, up);
91 void WorldState::Render(Viewport &viewport) {
92 Entity &player = *interface.GetPlayer().entity;
93 viewport.WorldPosition(player.Transform(player.ChunkCoords()));
94 chunk_renderer.Render(viewport);
95 world.Render(viewport);
96 interface.Render(viewport);