]> git.localhorst.tv Git - blank.git/blobdiff - src/standalone/MasterState.cpp
decrease packet frequency
[blank.git] / src / standalone / MasterState.cpp
index 750e491141135ca52a14b534aab51ebbd4fdd46e..665f8d4708dcd99b042fd71cffd4444172a7da51 100644 (file)
@@ -36,8 +36,10 @@ MasterState::MasterState(
 , chunk_renderer(player.GetChunks())
 , spawner(world, res.models, env.rng)
 , sky(env.loader.LoadCubeMap("skybox"))
+, cli(world)
 , preload(env, chunk_loader, chunk_renderer)
-, unload(env, world.Chunks(), save) {
+, unload(env, world.Chunks(), save)
+, chat(env, *this, *this) {
        res.Load(env.loader, "default");
        if (res.models.size() < 2) {
                throw std::runtime_error("need at least two models to run");
@@ -51,8 +53,6 @@ MasterState::MasterState(
        chunk_renderer.FogDensity(wc.fog_density);
        if (save.Exists(player)) {
                save.Read(player);
-               glm::vec3 orient(glm::eulerAngles(player.GetEntity().Orientation()));
-               input.TurnHead(orient.x, orient.y);
        } else {
                spawn_player = true;
        }
@@ -66,25 +66,48 @@ MasterState::~MasterState() {
 void MasterState::OnResume() {
        if (spawn_index.MissingChunks() > 0) {
                env.state.Push(&preload);
-       }
-       if (config.input.mouse) {
-               env.window.GrabMouse();
+               return;
        }
        if (spawn_player) {
                // TODO: spawn
                spawn_player = false;
        }
+       hud.KeepMessages(false);
+       OnFocus();
 }
 
 void MasterState::OnPause() {
+       OnBlur();
+}
+
+void MasterState::OnFocus() {
+       if (config.input.mouse) {
+               env.window.GrabMouse();
+       }
+       interface.Unlock();
+}
+
+void MasterState::OnBlur() {
        env.window.ReleaseMouse();
+       interface.Lock();
 }
 
 
 void MasterState::Handle(const SDL_Event &event) {
        switch (event.type) {
                case SDL_KEYDOWN:
-                       interface.HandlePress(event.key);
+                       // TODO: move to interface
+                       if (event.key.keysym.sym == SDLK_RETURN) {
+                               chat.Clear();
+                               env.state.Push(&chat);
+                               hud.KeepMessages(true);
+                       } else if (event.key.keysym.sym == SDLK_SLASH) {
+                               chat.Preset("/");
+                               env.state.Push(&chat);
+                               hud.KeepMessages(true);
+                       } else {
+                               interface.HandlePress(event.key);
+                       }
                        break;
                case SDL_KEYUP:
                        interface.HandleRelease(event.key);
@@ -110,7 +133,8 @@ void MasterState::Handle(const SDL_Event &event) {
 }
 
 void MasterState::Update(int dt) {
-       input.Update(dt);
+       spawner.Update(dt);
+       world.Update(dt);
        if (input.BlockFocus()) {
                hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
        } else if (input.EntityFocus()) {
@@ -120,8 +144,6 @@ void MasterState::Update(int dt) {
        }
        hud.Display(res.block_types[player.GetInventorySlot() + 1]);
        hud.Update(dt);
-       spawner.Update(dt);
-       world.Update(dt);
        chunk_loader.Update(dt);
        chunk_renderer.Update(dt);
 
@@ -134,12 +156,13 @@ void MasterState::Update(int dt) {
 }
 
 void MasterState::Render(Viewport &viewport) {
-       viewport.WorldPosition(
-               player.GetEntity().Transform(player.GetEntity().ChunkCoords())
-               * player.GetEntity().GetModel().EyesTransform());
+       viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords()));
        if (config.video.world) {
                chunk_renderer.Render(viewport);
                world.Render(viewport);
+               if (config.video.debug) {
+                       world.RenderDebug(viewport);
+               }
                sky.Render(viewport);
        }
        hud.Render(viewport);
@@ -187,5 +210,16 @@ void MasterState::Exit() {
        env.state.Switch(&unload);
 }
 
+void MasterState::OnLineSubmit(const std::string &line) {
+       if (line.empty()) {
+               return;
+       }
+       if (line[0] == '/' && line.size() > 1 && line[1] != '/') {
+               cli.Execute(player, line.substr(1));
+       } else {
+               hud.PostMessage(line);
+       }
+}
+
 }
 }