]> git.localhorst.tv Git - blank.git/blob - src/app/WorldState.cpp
sky box model & shader
[blank.git] / src / app / WorldState.cpp
1 #include "WorldState.hpp"
2
3 #include "Environment.hpp"
4 #include "init.hpp"
5 #include "TextureIndex.hpp"
6
7 #include <SDL.h>
8
9
10 namespace blank {
11
12 WorldState::WorldState(
13         Environment &env,
14         const Generator::Config &gc,
15         const Interface::Config &ic,
16         const World::Config &wc,
17         const WorldSave &save
18 )
19 : env(env)
20 , block_types()
21 , world(block_types, wc)
22 , interface(ic, env, world, world.AddPlayer(ic.player_name))
23 , generator(gc)
24 , chunk_loader(world.Chunks(), generator, save)
25 , chunk_renderer(*interface.GetPlayer().chunks)
26 , skeletons()
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);
34         skeletons.Load();
35         spawner.LimitSkeletons(0, skeletons.Size());
36         // TODO: better solution for initializing HUD
37         interface.SelectNext();
38 }
39
40
41 void WorldState::OnEnter() {
42         env.state.Push(&preload);
43         env.window.GrabMouse();
44 }
45
46
47 void WorldState::Handle(const SDL_Event &event) {
48         switch (event.type) {
49                 case SDL_KEYDOWN:
50                         interface.HandlePress(event.key);
51                         break;
52                 case SDL_KEYUP:
53                         interface.HandleRelease(event.key);
54                         break;
55                 case SDL_MOUSEBUTTONDOWN:
56                         interface.HandlePress(event.button);
57                         break;
58                 case SDL_MOUSEBUTTONUP:
59                         interface.HandleRelease(event.button);
60                         break;
61                 case SDL_MOUSEMOTION:
62                         interface.Handle(event.motion);
63                         break;
64                 case SDL_MOUSEWHEEL:
65                         interface.Handle(event.wheel);
66                         break;
67                 case SDL_QUIT:
68                         env.state.Switch(&unload);
69                         break;
70                 default:
71                         break;
72         }
73 }
74
75 void WorldState::Update(int dt) {
76         interface.Update(dt);
77         spawner.Update(dt);
78         world.Update(dt);
79         chunk_loader.Update(dt);
80         chunk_renderer.Update(dt);
81
82         Entity &player = *interface.GetPlayer().entity;
83
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);
90 }
91
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);
98 }
99
100 }