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 spawner.LimitSkeletons(0, skeletons.Size());
36 // TODO: better solution for initializing HUD
37 interface.SelectNext();
41 void WorldState::OnEnter() {
42 env.state.Push(&preload);
43 env.window.GrabMouse();
47 void WorldState::Handle(const SDL_Event &event) {
50 interface.HandlePress(event.key);
53 interface.HandleRelease(event.key);
55 case SDL_MOUSEBUTTONDOWN:
56 interface.HandlePress(event.button);
58 case SDL_MOUSEBUTTONUP:
59 interface.HandleRelease(event.button);
62 interface.Handle(event.motion);
65 interface.Handle(event.wheel);
68 env.state.Switch(&unload);
75 void WorldState::Update(int dt) {
79 chunk_loader.Update(dt);
80 chunk_renderer.Update(dt);
82 Entity &player = *interface.GetPlayer().entity;
84 glm::mat4 trans = player.Transform(player.ChunkCoords());
85 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
86 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
87 env.audio.Position(player.Position());
88 env.audio.Velocity(player.Velocity());
89 env.audio.Orientation(dir, up);
92 void WorldState::Render(Viewport &viewport) {
93 Entity &player = *interface.GetPlayer().entity;
94 viewport.WorldPosition(player.Transform(player.ChunkCoords()));
95 chunk_renderer.Render(viewport);
96 world.Render(viewport);
97 interface.Render(viewport);