X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=3c30a62495127a52ace02decb18233c845e463f5;hb=225a7e66ed3f3f03ab458ab39c914ed55cd69600;hp=236fa1a41fe0872a35d5a819da2f186fa764b998;hpb=9c5308ba4108bd842af6d9d2e893ea575a7e6ca8;p=blank.git diff --git a/src/client/client.cpp b/src/client/client.cpp index 236fa1a..3c30a62 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -48,39 +48,75 @@ void InitialState::Render(Viewport &viewport) { InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) : master(master) , res() +, sounds() , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host)) , world(res.block_types, master.GetWorldConf()) -, player(*world.AddPlayer(master.GetConfig().player.name)) +, player(*world.AddPlayer(master.GetConfig().player.name, player_id)) , hud(master.GetEnv(), master.GetConfig(), player) -, manip(master.GetEnv(), player.GetEntity()) +, manip(master.GetEnv().audio, sounds, player.GetEntity()) , input(world, player, master.GetClient()) , interface(master.GetConfig(), master.GetEnv().keymap, input, *this) , chunk_receiver(world.Chunks(), save) , chunk_renderer(player.GetChunks()) , loop_timer(16) +, stat_timer(1000) , sky(master.GetEnv().loader.LoadCubeMap("skybox")) -, update_status() { +, update_status() +, chat(master.GetEnv(), *this, *this) { if (!save.Exists()) { save.Write(master.GetWorldConf()); } res.Load(master.GetEnv().loader, "default"); + if (res.models.size() < 1) { + throw std::runtime_error("need at least one model to run"); + } + res.models[0].Instantiate(player.GetEntity().GetModel()); + sounds.Load(master.GetEnv().loader, res.snd_index); interface.SetInventorySlots(res.block_types.size() - 1); chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index); chunk_renderer.FogDensity(master.GetWorldConf().fog_density); loop_timer.Start(); + stat_timer.Start(); if (save.Exists(player)) { save.Read(player); } } -void InteractiveState::OnEnter() { - master.GetEnv().window.GrabMouse(); +void InteractiveState::OnResume() { + OnFocus(); +} + +void InteractiveState::OnPause() { + OnBlur(); +} + +void InteractiveState::OnFocus() { + if (master.GetConfig().input.mouse) { + master.GetEnv().window.GrabMouse(); + } + interface.Unlock(); +} + +void InteractiveState::OnBlur() { + master.GetEnv().window.ReleaseMouse(); + interface.Lock(); } void InteractiveState::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(); + master.GetEnv().state.Push(&chat); + hud.KeepMessages(true); + } else if (event.key.keysym.sym == SDLK_SLASH) { + chat.Preset("/"); + master.GetEnv().state.Push(&chat); + hud.KeepMessages(true); + } else { + interface.HandlePress(event.key); + } break; case SDL_KEYUP: interface.HandleRelease(event.key); @@ -106,20 +142,11 @@ void InteractiveState::Handle(const SDL_Event &event) { } void InteractiveState::Update(int dt) { - input.Update(dt); - if (input.BlockFocus()) { - hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block); - } else if (input.EntityFocus()) { - hud.FocusEntity(*input.EntityFocus().entity); - } else { - hud.FocusNone(); - } - hud.Display(res.block_types[player.GetInventorySlot() + 1]); loop_timer.Update(dt); + stat_timer.Update(dt); master.Update(dt); chunk_receiver.Update(dt); - hud.Update(dt); int world_dt = 0; while (loop_timer.HitOnce()) { world.Update(loop_timer.Interval()); @@ -128,9 +155,21 @@ void InteractiveState::Update(int dt) { } chunk_renderer.Update(dt); + if (input.BlockFocus()) { + hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block); + } else if (input.EntityFocus()) { + hud.FocusEntity(*input.EntityFocus().entity); + } else { + hud.FocusNone(); + } if (world_dt > 0) { input.PushPlayerUpdate(world_dt); } + hud.Display(res.block_types[player.GetInventorySlot() + 1]); + if (stat_timer.Hit()) { + hud.UpdateNetStats(master); + } + hud.Update(dt); glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords()); glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); @@ -141,7 +180,9 @@ void InteractiveState::Update(int dt) { } void InteractiveState::Render(Viewport &viewport) { - viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords())); + viewport.WorldPosition( + player.GetEntity().Transform(player.GetEntity().ChunkCoords()) + * player.GetEntity().GetModel().EyesTransform()); if (master.GetConfig().video.world) { chunk_renderer.Render(viewport); world.Render(viewport); @@ -156,14 +197,11 @@ void InteractiveState::Handle(const Packet::SpawnEntity &pack) { Entity &entity = world.ForceAddEntity(entity_id); UpdateEntity(entity_id, pack.Seq()); pack.ReadEntity(entity); - uint32_t skel_id; - pack.ReadSkeletonID(skel_id); - if (skel_id > 0 && skel_id <= res.models.size()) { - Model &skel = res.models.Get(skel_id); - skel.Instantiate(entity.GetModel()); + uint32_t model_id; + pack.ReadModelID(model_id); + if (model_id > 0 && model_id <= res.models.size()) { + res.models.Get(model_id).Instantiate(entity.GetModel()); } - cout << "spawned entity #" << entity_id << " (" << entity.Name() - << ") at " << entity.AbsolutePosition() << endl; } void InteractiveState::Handle(const Packet::DespawnEntity &pack) { @@ -173,7 +211,6 @@ void InteractiveState::Handle(const Packet::DespawnEntity &pack) { for (Entity &entity : world.Entities()) { if (entity.ID() == entity_id) { entity.Kill(); - cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl; return; } } @@ -184,7 +221,10 @@ void InteractiveState::Handle(const Packet::EntityUpdate &pack) { auto world_end = world.Entities().end(); uint32_t count = 0; + glm::ivec3 base; pack.ReadEntityCount(count); + pack.ReadChunkBase(base); + EntityState state; for (uint32_t i = 0; i < count; ++i) { uint32_t entity_id = 0; @@ -199,7 +239,8 @@ void InteractiveState::Handle(const Packet::EntityUpdate &pack) { } if (world_iter->ID() == entity_id) { if (UpdateEntity(entity_id, pack.Seq())) { - pack.ReadEntityState(world_iter->GetState(), i); + pack.ReadEntityState(state, base, i); + world_iter->SetState(state); } } } @@ -257,6 +298,12 @@ void InteractiveState::Handle(const Packet::BlockUpdate &pack) { } } +void InteractiveState::Handle(const Packet::Message &pack) { + string msg; + pack.ReadMessage(msg); + hud.PostMessage(msg); +} + void InteractiveState::SetAudio(bool b) { master.GetConfig().audio.enabled = b; if (b) { @@ -298,6 +345,12 @@ void InteractiveState::Exit() { master.Quit(); } +void InteractiveState::OnLineSubmit(const string &line) { + if (!line.empty()) { + master.GetClient().SendMessage(1, 0, line); + } +} + MasterState::MasterState( Environment &env, @@ -373,7 +426,9 @@ void MasterState::On(const Packet::Join &pack) { pack.ReadPlayerID(player_id); state.reset(new InteractiveState(*this, player_id)); - pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState()); + EntityState player_state; + pack.ReadPlayerState(player_state); + state->GetPlayer().GetEntity().SetState(player_state); env.state.PopAfter(this); env.state.Push(state.get()); @@ -448,5 +503,15 @@ void MasterState::On(const Packet::BlockUpdate &pack) { state->Handle(pack); } +void MasterState::On(const Packet::Message &pack) { + if (state) { + state->Handle(pack); + } else { + string msg; + pack.ReadMessage(msg); + cout << "got message before interface was created: " << msg << endl; + } +} + } }