1 #include "WorldState.hpp"
3 #include "Environment.hpp"
4 #include "TextureIndex.hpp"
11 WorldState::WorldState(
13 const Interface::Config &ic,
14 const World::Config &wc,
19 , world(block_types, wc, save)
20 , chunk_renderer(world, wc.load.load_dist)
22 , interface(ic, env, world)
23 , preload(env, world.Loader(), chunk_renderer)
24 , unload(env, world.Loader()) {
25 TextureIndex tex_index;
26 env.assets.LoadBlockTypes("default", block_types, tex_index);
27 chunk_renderer.LoadTextures(env.assets, tex_index);
28 chunk_renderer.FogDensity(wc.fog_density);
29 // TODO: better solution for initializing HUD
30 interface.SelectNext();
34 void WorldState::OnEnter() {
35 env.state.Push(&preload);
39 void WorldState::Handle(const SDL_Event &event) {
42 interface.HandlePress(event.key);
45 interface.HandleRelease(event.key);
47 case SDL_MOUSEBUTTONDOWN:
48 interface.HandlePress(event.button);
50 case SDL_MOUSEBUTTONUP:
51 interface.HandleRelease(event.button);
54 interface.Handle(event.motion);
57 interface.Handle(event.wheel);
60 env.state.Switch(&unload);
67 void WorldState::Update(int dt) {
71 chunk_renderer.Rebase(world.Player().ChunkCoords());
72 chunk_renderer.Update(dt);
74 glm::mat4 trans = world.Player().Transform(world.Player().ChunkCoords());
75 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
76 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
77 env.audio.Position(world.Player().Position());
78 env.audio.Velocity(world.Player().Velocity());
79 env.audio.Orientation(dir, up);
83 void WorldState::Render(Viewport &viewport) {
84 viewport.WorldPosition(world.Player().Transform(world.Player().ChunkCoords()));
85 chunk_renderer.Render(viewport);
86 world.Render(viewport);
87 interface.Render(viewport);