+#include "ClientController.hpp"
+#include "DirectInput.hpp"
#include "HUD.hpp"
+#include "InteractiveManipulator.hpp"
#include "Interface.hpp"
+#include "Keymap.hpp"
+#include "PlayerController.hpp"
#include "../app/Assets.hpp"
+#include "../app/Config.hpp"
#include "../app/Environment.hpp"
#include "../app/FrameCounter.hpp"
#include "../app/init.hpp"
#include "../audio/Audio.hpp"
+#include "../audio/SoundBank.hpp"
#include "../graphics/Font.hpp"
#include "../graphics/Viewport.hpp"
-#include "../model/shapes.hpp"
+#include "../io/TokenStreamReader.hpp"
+#include "../model/bounds.hpp"
+#include "../world/BlockLookup.hpp"
#include "../world/World.hpp"
+#include "../world/WorldManipulator.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
+#include <map>
#include <sstream>
#include <glm/gtc/matrix_transform.hpp>
+#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/io.hpp>
namespace blank {
-HUD::HUD(const BlockTypeRegistry &types, const Font &font)
-: types(types)
-, font(font)
+PlayerController::PlayerController(World &world, Player &player)
+: world(world)
+, player(player)
+, move_dir(0.0f)
+, dirty(true)
+, aim_world()
+, aim_entity() {
+
+}
+
+void PlayerController::SetMovement(const glm::vec3 &m) noexcept {
+ if (dot(m, m) > 1.0f) {
+ move_dir = normalize(m);
+ } else {
+ move_dir = m;
+ }
+ Invalidate();
+}
+
+void PlayerController::TurnHead(float dp, float dy) noexcept {
+ player.GetEntity().TurnHead(dp, dy);
+}
+
+float PlayerController::GetPitch() const noexcept {
+ return player.GetEntity().Pitch();
+}
+
+float PlayerController::GetYaw() const noexcept {
+ return player.GetEntity().Yaw();
+}
+
+void PlayerController::SelectInventory(int i) noexcept {
+ player.SetInventorySlot(i);
+}
+
+int PlayerController::InventorySlot() const noexcept {
+ return player.GetInventorySlot();
+}
+
+void PlayerController::Invalidate() noexcept {
+ dirty = true;
+}
+
+void PlayerController::UpdatePlayer() noexcept {
+ constexpr float max_vel = 5.0f; // in m/s
+ if (dirty) {
+ player.GetEntity().TargetVelocity(glm::rotateY(move_dir * max_vel, player.GetEntity().Yaw()));
+
+ Ray aim = player.Aim();
+ if (!world.Intersection(aim, glm::mat4(1.0f), player.GetEntity().ChunkCoords(), aim_world)) {
+ aim_world = WorldCollision();
+ }
+ if (!world.Intersection(aim, glm::mat4(1.0f), player.GetEntity(), aim_entity)) {
+ aim_entity = EntityCollision();
+ }
+ if (aim_world && aim_entity) {
+ // got both, pick the closest one
+ if (aim_world.depth < aim_entity.depth) {
+ aim_entity = EntityCollision();
+ } else {
+ aim_world = WorldCollision();
+ }
+ }
+ dirty = false;
+ }
+}
+
+
+DirectInput::DirectInput(World &world, Player &player, WorldManipulator &manip)
+: PlayerController(world, player)
+, manip(manip)
+, place_timer(256)
+, remove_timer(256) {
+
+}
+
+void DirectInput::Update(int dt) {
+ Invalidate(); // world has changed in the meantime
+ UpdatePlayer();
+
+ remove_timer.Update(dt);
+ if (remove_timer.Hit()) {
+ RemoveBlock();
+ }
+
+ place_timer.Update(dt);
+ if (place_timer.Hit()) {
+ PlaceBlock();
+ }
+}
+
+void DirectInput::StartPrimaryAction() {
+ if (!remove_timer.Running()) {
+ RemoveBlock();
+ remove_timer.Start();
+ }
+}
+
+void DirectInput::StopPrimaryAction() {
+ remove_timer.Stop();
+}
+
+void DirectInput::StartSecondaryAction() {
+ if (!place_timer.Running()) {
+ PlaceBlock();
+ place_timer.Start();
+ }
+}
+
+void DirectInput::StopSecondaryAction() {
+ place_timer.Stop();
+}
+
+void DirectInput::StartTertiaryAction() {
+ PickBlock();
+}
+
+void DirectInput::StopTertiaryAction() {
+ // nothing
+}
+
+void DirectInput::PickBlock() {
+ UpdatePlayer();
+ if (!BlockFocus()) return;
+ SelectInventory(BlockFocus().GetBlock().type - 1);
+}
+
+void DirectInput::PlaceBlock() {
+ UpdatePlayer();
+ if (!BlockFocus()) return;
+
+ BlockLookup next_block(BlockFocus().chunk, BlockFocus().BlockPos(), Block::NormalFace(BlockFocus().normal));
+ if (!next_block) {
+ return;
+ }
+ manip.SetBlock(next_block.GetChunk(), next_block.GetBlockIndex(), Block(InventorySlot() + 1));
+ Invalidate();
+}
+
+void DirectInput::RemoveBlock() {
+ UpdatePlayer();
+ if (!BlockFocus()) return;
+ manip.SetBlock(BlockFocus().GetChunk(), BlockFocus().block, Block(0));
+ Invalidate();
+}
+
+
+HUD::HUD(Environment &env, Config &config, const Player &player)
+: env(env)
+, config(config)
+, player(player)
+// block focus
+, outline()
+, outline_transform(1.0f)
+, outline_visible(false)
+// "inventory"
, block()
, block_buf()
, block_transform(1.0f)
, block_label()
, block_visible(false)
+// debug overlay
+, counter_text()
+, position_text()
+, orientation_text()
+, block_text()
+, show_block(false)
+, show_entity(false)
+// message box
+, messages(env.assets.small_ui_font)
+, msg_timer(5000)
+, msg_keep(false)
+// crosshair
, crosshair() {
+ const float ls = env.assets.small_ui_font.LineSkip();
+
+ // "inventory"
block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
block_transform = glm::scale(block_transform, glm::vec3(50.0f));
block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
+ block_label.Position(
+ glm::vec3(50.0f, 85.0f, 0.0f),
+ Gravity::NORTH_WEST,
+ Gravity::NORTH
+ );
+ block_label.Foreground(glm::vec4(1.0f));
+ block_label.Background(glm::vec4(0.5f));
- OutlineModel::Buffer buf;
+ // debug overlay
+ counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
+ counter_text.Foreground(glm::vec4(1.0f));
+ counter_text.Background(glm::vec4(0.5f));
+ position_text.Position(glm::vec3(-25.0f, 25.0f + ls, 0.0f), Gravity::NORTH_EAST);
+ position_text.Foreground(glm::vec4(1.0f));
+ position_text.Background(glm::vec4(0.5f));
+ orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * ls, 0.0f), Gravity::NORTH_EAST);
+ orientation_text.Foreground(glm::vec4(1.0f));
+ orientation_text.Background(glm::vec4(0.5f));
+ block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
+ block_text.Foreground(glm::vec4(1.0f));
+ block_text.Background(glm::vec4(0.5f));
+ block_text.Set(env.assets.small_ui_font, "Block: none");
+ entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
+ entity_text.Foreground(glm::vec4(1.0f));
+ entity_text.Background(glm::vec4(0.5f));
+ entity_text.Set(env.assets.small_ui_font, "Entity: none");
+
+ // message box
+ messages.Position(glm::vec3(25.0f, -25.0f - 2 * ls, 0.0f), Gravity::SOUTH_WEST);
+ messages.Foreground(glm::vec4(1.0f));
+ messages.Background(glm::vec4(0.5f));
+
+ // crosshair
+ PrimitiveMesh::Buffer buf;
buf.vertices = std::vector<glm::vec3>({
{ -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
{ 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
});
- buf.indices = std::vector<OutlineModel::Index>({
+ buf.indices = std::vector<PrimitiveMesh::Index>({
0, 1, 2, 3
});
- buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
+ buf.colors.resize(4, { 10.0f, 10.0f, 10.0f, 1.0f });
crosshair.Update(buf);
+}
- block_label.Position(
- glm::vec3(50.0f, 85.0f, 0.0f),
- Gravity::NORTH_WEST,
- Gravity::NORTH
- );
- block_label.Foreground(glm::vec4(1.0f));
- block_label.Background(glm::vec4(0.5f));
+namespace {
+
+PrimitiveMesh::Buffer outl_buf;
+
+}
+
+void HUD::FocusBlock(const Chunk &chunk, int index) {
+ const Block &block = chunk.BlockAt(index);
+ const BlockType &type = chunk.Type(index);
+ outl_buf.Clear();
+ type.OutlinePrimitiveMesh(outl_buf);
+ outline.Update(outl_buf);
+ outline_transform = chunk.Transform(player.GetEntity().ChunkCoords());
+ outline_transform *= chunk.ToTransform(Chunk::ToPos(index), index);
+ outline_transform *= glm::scale(glm::vec3(1.005f));
+ outline_visible = true;
+ {
+ std::stringstream s;
+ s << "Block: "
+ << type.label
+ << ", face: " << block.GetFace()
+ << ", turn: " << block.GetTurn();
+ block_text.Set(env.assets.small_ui_font, s.str());
+ }
+ show_block = true;
+ show_entity = false;
+}
+
+void HUD::FocusEntity(const Entity &entity) {
+ {
+ std::stringstream s;
+ s << "Entity: " << entity.Name();
+ entity_text.Set(env.assets.small_ui_font, s.str());
+ }
+ show_block = false;
+ show_entity = true;
}
+void HUD::FocusNone() {
+ outline_visible = false;
+ show_block = false;
+ show_entity = false;
+}
-void HUD::Display(const Block &b) {
- const BlockType &type = types.Get(b.type);
+void HUD::DisplayNone() {
+ block_visible = false;
+}
+void HUD::Display(const BlockType &type) {
block_buf.Clear();
- type.FillEntityModel(block_buf, b.Transform());
+ type.FillEntityMesh(block_buf);
block.Update(block_buf);
- block_label.Set(font, type.label);
+ block_label.Set(env.assets.small_ui_font, type.label);
block_visible = type.visible;
}
+void HUD::UpdateDebug() {
+ UpdateCounter();
+ UpdatePosition();
+ UpdateOrientation();
+}
+
+void HUD::UpdateCounter() {
+ std::stringstream s;
+ s << std::setprecision(3) <<
+ "avg: " << env.counter.Average().running << "ms, "
+ "peak: " << env.counter.Peak().running << "ms";
+ std::string text = s.str();
+ counter_text.Set(env.assets.small_ui_font, text);
+}
+
+void HUD::UpdatePosition() {
+ std::stringstream s;
+ s << std::setprecision(3) << "pos: " << player.GetEntity().AbsolutePosition();
+ position_text.Set(env.assets.small_ui_font, s.str());
+}
+
+void HUD::UpdateOrientation() {
+ //std::stringstream s;
+ //s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
+ // << ", yaw: " << rad2deg(ctrl.Yaw());
+ //orientation_text.Set(env.assets.small_ui_font, s.str());
+}
+
+void HUD::PostMessage(const char *msg) {
+ messages.PushLine(msg);
+ msg_timer.Reset();
+ msg_timer.Start();
+ std::cout << msg << std::endl;
+}
+
+
+void HUD::Update(int dt) {
+ msg_timer.Update(dt);
+ if (msg_timer.HitOnce()) {
+ msg_timer.Stop();
+ }
+
+ if (config.video.debug) {
+ if (env.counter.Changed()) {
+ UpdateCounter();
+ }
+ UpdatePosition();
+ UpdateOrientation();
+ }
+}
+
void HUD::Render(Viewport &viewport) noexcept {
+ // block focus
+ if (outline_visible && config.video.world) {
+ PlainColor &outline_prog = viewport.WorldColorProgram();
+ outline_prog.SetM(outline_transform);
+ outline.DrawLines();
+ }
+
+ // clear depth buffer so everything renders above the world
viewport.ClearDepth();
- DirectionalLighting &world_prog = viewport.HUDProgram();
- world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
- // disable distance fog
- world_prog.SetFogDensity(0.0f);
-
- viewport.EnableInvertBlending();
- viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
- world_prog.SetM(viewport.Cursor());
- crosshair.Draw();
-
- if (block_visible) {
- viewport.DisableBlending();
- world_prog.SetM(block_transform);
- block.Draw();
- block_label.Render(viewport);
+ if (config.video.hud) {
+ // "inventory"
+ if (block_visible) {
+ DirectionalLighting &world_prog = viewport.HUDProgram();
+ world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
+ // disable distance fog
+ world_prog.SetFogDensity(0.0f);
+
+ viewport.DisableBlending();
+ world_prog.SetM(block_transform);
+ block.Draw();
+ block_label.Render(viewport);
+ }
+
+ // message box
+ if (msg_keep || msg_timer.Running()) {
+ messages.Render(viewport);
+ }
+
+ // crosshair
+ PlainColor &outline_prog = viewport.HUDColorProgram();
+ viewport.EnableInvertBlending();
+ viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
+ outline_prog.SetM(viewport.Cursor());
+ crosshair.DrawLines();
+ }
+
+ // debug overlay
+ if (config.video.debug) {
+ counter_text.Render(viewport);
+ position_text.Render(viewport);
+ orientation_text.Render(viewport);
+ if (show_block) {
+ block_text.Render(viewport);
+ } else if (show_entity) {
+ entity_text.Render(viewport);
+ }
+ }
+}
+
+
+InteractiveManipulator::InteractiveManipulator(Audio &audio, const SoundBank &sounds, Entity &player)
+: player(player)
+, audio(audio)
+, sounds(sounds) {
+
+}
+
+void InteractiveManipulator::SetBlock(Chunk &chunk, int index, const Block &block) {
+ const BlockType &old_type = chunk.Type(index);
+ chunk.SetBlock(index, block);
+ const BlockType &new_type = chunk.Type(index);
+ glm::vec3 coords = chunk.ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(index));
+ if (new_type.id == 0) {
+ if (old_type.remove_sound >= 0) {
+ audio.Play(sounds[old_type.remove_sound], coords);
+ }
+ } else {
+ if (new_type.place_sound >= 0) {
+ audio.Play(sounds[new_type.place_sound], coords);
+ }
}
}
Interface::Interface(
- const Config &config,
- Environment &env,
- World &world)
-: env(env)
-, world(world)
-, ctrl(world.Player())
-, font(env.assets.LoadFont("DejaVuSans", 16))
-, hud(world.BlockTypes(), font)
-, aim{{ 0, 0, 0 }, { 0, 0, -1 }}
-, aim_chunk(nullptr)
-, aim_block(0)
-, aim_normal()
-, outline()
-, outline_transform(1.0f)
-, counter_text()
-, messages(font)
-, msg_timer(5000)
-, config(config)
-, place_timer(256)
-, remove_timer(256)
-, remove(0)
-, selection(1)
-, place_sound(env.assets.LoadSound("thump"))
-, remove_sound(env.assets.LoadSound("plop"))
+ Config &config,
+ const Keymap &keymap,
+ PlayerController &pc,
+ ClientController &cc)
+: config(config)
+, keymap(keymap)
+, player_ctrl(pc)
+, client_ctrl(cc)
, fwd(0)
-, rev(0) {
- counter_text.Hide();
- counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
- counter_text.Foreground(glm::vec4(1.0f));
- counter_text.Background(glm::vec4(0.5f));
- position_text.Hide();
- position_text.Position(glm::vec3(-25.0f, 25.0f + font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
- position_text.Foreground(glm::vec4(1.0f));
- position_text.Background(glm::vec4(0.5f));
- messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
- messages.Foreground(glm::vec4(1.0f));
- messages.Background(glm::vec4(0.5f));
- hud.Display(selection);
+, rev(0)
+, slot(0)
+, num_slots(10) {
+
}
void Interface::HandlePress(const SDL_KeyboardEvent &event) {
- if (config.keyboard_disabled) return;
+ if (!config.input.keyboard) return;
- switch (event.keysym.sym) {
- case SDLK_w:
+ Keymap::Action action = keymap.Lookup(event);
+ switch (action) {
+ case Keymap::MOVE_FORWARD:
rev.z = 1;
+ UpdateMovement();
break;
- case SDLK_s:
+ case Keymap::MOVE_BACKWARD:
fwd.z = 1;
+ UpdateMovement();
break;
- case SDLK_a:
+ case Keymap::MOVE_LEFT:
rev.x = 1;
+ UpdateMovement();
break;
- case SDLK_d:
+ case Keymap::MOVE_RIGHT:
fwd.x = 1;
+ UpdateMovement();
break;
- case SDLK_SPACE:
+ case Keymap::MOVE_UP:
fwd.y = 1;
+ UpdateMovement();
break;
- case SDLK_LSHIFT:
+ case Keymap::MOVE_DOWN:
rev.y = 1;
+ UpdateMovement();
break;
- case SDLK_q:
- FaceBlock();
+ case Keymap::PRIMARY:
+ player_ctrl.StartPrimaryAction();
break;
- case SDLK_e:
- TurnBlock();
+ case Keymap::SECONDARY:
+ player_ctrl.StartSecondaryAction();
break;
-
- case SDLK_n:
- ToggleCollision();
+ case Keymap::TERTIARY:
+ player_ctrl.StartTertiaryAction();
break;
- case SDLK_b:
- PrintBlockInfo();
+ case Keymap::INV_NEXT:
+ InvRel(1);
break;
- case SDLK_c:
- PrintChunkInfo();
+ case Keymap::INV_PREVIOUS:
+ InvRel(-1);
break;
- case SDLK_l:
- PrintLightInfo();
+ case Keymap::INV_1:
+ case Keymap::INV_2:
+ case Keymap::INV_3:
+ case Keymap::INV_4:
+ case Keymap::INV_5:
+ case Keymap::INV_6:
+ case Keymap::INV_7:
+ case Keymap::INV_8:
+ case Keymap::INV_9:
+ case Keymap::INV_10:
+ InvAbs(action - Keymap::INV_1);
break;
- case SDLK_p:
- PrintSelectionInfo();
+
+ case Keymap::EXIT:
+ client_ctrl.Exit();
break;
- case SDLK_F1:
- ToggleVisual();
+ case Keymap::TOGGLE_AUDIO:
+ config.audio.enabled = !config.audio.enabled;
+ client_ctrl.SetAudio(config.audio.enabled);
+ break;
+ case Keymap::TOGGLE_VIDEO:
+ config.video.world = !config.video.world;
+ client_ctrl.SetVideo(config.video.world);
break;
- case SDLK_F3:
- ToggleDebug();
+ case Keymap::TOGGLE_HUD:
+ config.video.hud = !config.video.hud;
+ client_ctrl.SetHUD(config.video.hud);
break;
- case SDLK_F4:
- ToggleAudio();
+ case Keymap::TOGGLE_DEBUG:
+ config.video.debug = !config.video.debug;
+ client_ctrl.SetDebug(config.video.debug);
+ break;
+
+ default:
break;
}
}
void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
- if (config.keyboard_disabled) return;
+ if (!config.input.keyboard) return;
- switch (event.keysym.sym) {
- case SDLK_w:
+ switch (keymap.Lookup(event)) {
+ case Keymap::MOVE_FORWARD:
rev.z = 0;
+ UpdateMovement();
break;
- case SDLK_s:
+ case Keymap::MOVE_BACKWARD:
fwd.z = 0;
+ UpdateMovement();
break;
- case SDLK_a:
+ case Keymap::MOVE_LEFT:
rev.x = 0;
+ UpdateMovement();
break;
- case SDLK_d:
+ case Keymap::MOVE_RIGHT:
fwd.x = 0;
+ UpdateMovement();
break;
- case SDLK_SPACE:
+ case Keymap::MOVE_UP:
fwd.y = 0;
+ UpdateMovement();
break;
- case SDLK_LSHIFT:
+ case Keymap::MOVE_DOWN:
rev.y = 0;
+ UpdateMovement();
break;
- }
-}
-void Interface::FaceBlock() {
- selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
- hud.Display(selection);
-}
-
-void Interface::TurnBlock() {
- selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
- hud.Display(selection);
-}
-
-void Interface::ToggleCollision() {
- ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
- if (ctrl.Controlled().WorldCollidable()) {
- PostMessage("collision on");
- } else {
- PostMessage("collision off");
- }
-}
-
-void Interface::PrintBlockInfo() {
- std::cout << std::endl;
- if (!aim_chunk) {
- PostMessage("not looking at any block");
- Ray aim = ctrl.Aim();
- std::stringstream s;
- s << "aim ray: " << aim.orig << ", " << aim.dir;
- PostMessage(s.str());
- return;
- }
- std::stringstream s;
- s << "looking at block " << aim_block
- << " " << Chunk::ToCoords(aim_block)
- << " of chunk " << aim_chunk->Position()
- ;
- PostMessage(s.str());
- Print(aim_chunk->BlockAt(aim_block));
-}
-
-void Interface::PrintChunkInfo() {
- std::cout << std::endl;
- if (!aim_chunk) {
- PostMessage("not looking at any block");
- return;
- }
- std::stringstream s;
- s << "looking at chunk " << aim_chunk->Position();
- PostMessage(s.str());
-
- PostMessage(" neighbors:");
- if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
- s.str("");
- s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
- PostMessage(s.str());
- }
- if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
- s.str("");
- s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
- PostMessage(s.str());
- }
- if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
- s.str("");
- s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
- PostMessage(s.str());
- }
- if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
- s.str("");
- s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
- PostMessage(s.str());
- }
- if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
- s.str("");
- s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
- PostMessage(s.str());
- }
- if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
- s.str("");
- s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
- PostMessage(s.str());
- }
- std::cout << std::endl;
-}
-
-void Interface::PrintLightInfo() {
- std::stringstream s;
- s
- << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
- << " at position " << world.Player().Position()
- ;
- PostMessage(s.str());
-}
-
-void Interface::PrintSelectionInfo() {
- std::cout << std::endl;
- Print(selection);
-}
-
-void Interface::Print(const Block &block) {
- std::stringstream s;
- s << "type: " << block.type
- << ", face: " << block.GetFace()
- << ", turn: " << block.GetTurn()
- ;
- PostMessage(s.str());
-}
-
-void Interface::ToggleAudio() {
- config.audio_disabled = !config.audio_disabled;
- if (config.audio_disabled) {
- PostMessage("audio off");
- } else {
- PostMessage("audio on");
- }
-}
-
-void Interface::ToggleVisual() {
- config.visual_disabled = !config.visual_disabled;
- if (config.visual_disabled) {
- PostMessage("visual off");
- } else {
- PostMessage("visual on");
- }
-}
+ case Keymap::PRIMARY:
+ player_ctrl.StopPrimaryAction();
+ break;
+ case Keymap::SECONDARY:
+ player_ctrl.StopSecondaryAction();
+ break;
+ case Keymap::TERTIARY:
+ player_ctrl.StopTertiaryAction();
+ break;
-void Interface::ToggleDebug() {
- counter_text.Toggle();
- position_text.Toggle();
- if (counter_text.Visible()) {
- UpdateCounter();
- UpdatePosition();
+ default:
+ break;
}
}
-void Interface::UpdateCounter() {
- std::stringstream s;
- s << std::setprecision(3) <<
- "avg: " << env.counter.Average().running << "ms, "
- "peak: " << env.counter.Peak().running << "ms";
- std::string text = s.str();
- counter_text.Set(font, text);
-}
-
-void Interface::UpdatePosition() {
- std::stringstream s;
- s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
- position_text.Set(font, s.str());
-}
-
-
void Interface::Handle(const SDL_MouseMotionEvent &event) {
- if (config.mouse_disabled) return;
- ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
- ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
+ if (!config.input.mouse) return;
+ player_ctrl.TurnHead(
+ event.yrel * config.input.pitch_sensitivity,
+ event.xrel * config.input.yaw_sensitivity);
}
void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
- if (config.mouse_disabled) return;
+ if (!config.input.mouse) return;
- if (event.button == SDL_BUTTON_LEFT) {
- RemoveBlock();
- remove_timer.Start();
- } else if (event.button == SDL_BUTTON_MIDDLE) {
- PickBlock();
- } else if (event.button == SDL_BUTTON_RIGHT) {
- PlaceBlock();
- place_timer.Start();
+ switch (event.button) {
+ case SDL_BUTTON_LEFT:
+ player_ctrl.StartPrimaryAction();
+ break;
+ case SDL_BUTTON_RIGHT:
+ player_ctrl.StartSecondaryAction();
+ break;
+ case SDL_BUTTON_MIDDLE:
+ player_ctrl.StartTertiaryAction();
+ break;
}
}
void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
- if (config.mouse_disabled) return;
-
- if (event.button == SDL_BUTTON_LEFT) {
- remove_timer.Stop();
- } else if (event.button == SDL_BUTTON_RIGHT) {
- place_timer.Stop();
- }
-}
+ if (!config.input.mouse) return;
-void Interface::PickBlock() {
- if (!aim_chunk) return;
- selection = aim_chunk->BlockAt(aim_block);
- hud.Display(selection);
-}
-
-void Interface::PlaceBlock() {
- if (!aim_chunk) return;
- Chunk *mod_chunk = aim_chunk;
- glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
- if (!Chunk::InBounds(next_pos)) {
- mod_chunk = &world.Next(*aim_chunk, aim_normal);
- next_pos -= aim_normal * glm::vec3(Chunk::Extent());
+ switch (event.button) {
+ case SDL_BUTTON_LEFT:
+ player_ctrl.StopPrimaryAction();
+ break;
+ case SDL_BUTTON_RIGHT:
+ player_ctrl.StopSecondaryAction();
+ break;
+ case SDL_BUTTON_MIDDLE:
+ player_ctrl.StopTertiaryAction();
+ break;
}
- mod_chunk->SetBlock(next_pos, selection);
-
- if (config.audio_disabled) return;
- const Entity &player = ctrl.Controlled();
- env.audio.Play(
- place_sound,
- mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
- );
-}
-
-void Interface::RemoveBlock() noexcept {
- if (!aim_chunk) return;
- aim_chunk->SetBlock(aim_block, remove);
-
- if (config.audio_disabled) return;
- const Entity &player = ctrl.Controlled();
- env.audio.Play(
- remove_sound,
- aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
- );
}
void Interface::Handle(const SDL_MouseWheelEvent &event) {
- if (config.mouse_disabled) return;
+ if (!config.input.mouse) return;
if (event.y < 0) {
- SelectNext();
+ InvRel(1);
} else if (event.y > 0) {
- SelectPrevious();
+ InvRel(-1);
}
}
-void Interface::SelectNext() {
- ++selection.type;
- if (size_t(selection.type) >= world.BlockTypes().Size()) {
- selection.type = 1;
- }
- hud.Display(selection);
+void Interface::UpdateMovement() {
+ player_ctrl.SetMovement(glm::vec3(fwd - rev));
}
-void Interface::SelectPrevious() {
- --selection.type;
- if (selection.type <= 0) {
- selection.type = world.BlockTypes().Size() - 1;
+void Interface::InvAbs(int s) {
+ slot = s % num_slots;
+ while (slot < 0) {
+ slot += num_slots;
}
- hud.Display(selection);
+ player_ctrl.SelectInventory(slot);
}
-
-void Interface::PostMessage(const char *msg) {
- messages.PushLine(msg);
- msg_timer.Reset();
- msg_timer.Start();
- std::cout << msg << std::endl;
+void Interface::InvRel(int delta) {
+ InvAbs(slot + delta);
}
-void Interface::Update(int dt) {
- ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
- ctrl.Update(dt);
-
- msg_timer.Update(dt);
- place_timer.Update(dt);
- remove_timer.Update(dt);
-
- aim = ctrl.Aim();
- CheckAim();
-
- if (msg_timer.HitOnce()) {
- msg_timer.Stop();
- }
+Keymap::Keymap()
+: codemap{ NONE } {
- if (remove_timer.Hit()) {
- RemoveBlock();
- CheckAim();
- }
+}
- if (place_timer.Hit()) {
- PlaceBlock();
- CheckAim();
+void Keymap::Map(SDL_Scancode scancode, Action action) {
+ if (scancode > MAX_SCANCODE) {
+ throw std::runtime_error("refusing to map scancode: too damn high");
}
+ codemap[scancode] = action;
+}
- if (counter_text.Visible() && env.counter.Changed()) {
- UpdateCounter();
- }
- if (position_text.Visible()) {
- UpdatePosition();
+Keymap::Action Keymap::Lookup(SDL_Scancode scancode) const {
+ if (scancode < NUM_SCANCODES) {
+ return codemap[scancode];
+ } else {
+ return NONE;
}
}
-namespace {
-
-OutlineModel::Buffer outl_buf;
+void Keymap::LoadDefault() {
+ Map(SDL_SCANCODE_UP, MOVE_FORWARD);
+ Map(SDL_SCANCODE_W, MOVE_FORWARD);
+ Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
+ Map(SDL_SCANCODE_S, MOVE_BACKWARD);
+ Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
+ Map(SDL_SCANCODE_A, MOVE_LEFT);
+ Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
+ Map(SDL_SCANCODE_D, MOVE_RIGHT);
+ Map(SDL_SCANCODE_SPACE, MOVE_UP);
+ Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
+ Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
+ Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
+ Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
+
+ Map(SDL_SCANCODE_TAB, INV_NEXT);
+ Map(SDL_SCANCODE_RIGHTBRACKET, INV_NEXT);
+ Map(SDL_SCANCODE_LEFTBRACKET, INV_PREVIOUS);
+ Map(SDL_SCANCODE_1, INV_1);
+ Map(SDL_SCANCODE_2, INV_2);
+ Map(SDL_SCANCODE_3, INV_3);
+ Map(SDL_SCANCODE_4, INV_4);
+ Map(SDL_SCANCODE_5, INV_5);
+ Map(SDL_SCANCODE_6, INV_6);
+ Map(SDL_SCANCODE_7, INV_7);
+ Map(SDL_SCANCODE_8, INV_8);
+ Map(SDL_SCANCODE_9, INV_9);
+ Map(SDL_SCANCODE_0, INV_10);
+
+ Map(SDL_SCANCODE_INSERT, SECONDARY);
+ Map(SDL_SCANCODE_RETURN, SECONDARY);
+ Map(SDL_SCANCODE_MENU, TERTIARY);
+ Map(SDL_SCANCODE_DELETE, PRIMARY);
+ Map(SDL_SCANCODE_BACKSPACE, PRIMARY);
+
+ Map(SDL_SCANCODE_F1, TOGGLE_HUD);
+ Map(SDL_SCANCODE_F2, TOGGLE_VIDEO);
+ Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
+ Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
+
+ Map(SDL_SCANCODE_ESCAPE, EXIT);
+}
+
+
+void Keymap::Load(std::istream &is) {
+ TokenStreamReader in(is);
+ std::string key_name;
+ std::string action_name;
+ SDL_Scancode key;
+ Action action;
+ while (in.HasMore()) {
+ if (in.Peek().type == Token::STRING) {
+ in.ReadString(key_name);
+ key = SDL_GetScancodeFromName(key_name.c_str());
+ } else {
+ key = SDL_Scancode(in.GetInt());
+ }
+ in.Skip(Token::EQUALS);
+ in.ReadIdentifier(action_name);
+ action = StringToAction(action_name);
+ if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
+ in.Skip(Token::SEMICOLON);
+ }
+ Map(key, action);
+ }
}
-void Interface::CheckAim() {
- float dist;
- if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
- outl_buf.Clear();
- aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
- outline.Update(outl_buf);
- outline_transform = glm::scale(glm::vec3(1.0002f));
- outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
- outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
- } else {
- aim_chunk = nullptr;
+void Keymap::Save(std::ostream &out) {
+ for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
+ if (codemap[i] == NONE) continue;
+
+ const char *str = SDL_GetScancodeName(SDL_Scancode(i));
+ if (str && *str) {
+ out << '"';
+ while (*str) {
+ if (*str == '"') {
+ out << "\\\"";
+ } else {
+ out << *str;
+ }
+ ++str;
+ }
+ out << '"';
+ } else {
+ out << i;
+ }
+
+ out << " = " << ActionToString(codemap[i]) << std::endl;;
}
}
-void Interface::Render(Viewport &viewport) noexcept {
- if (config.visual_disabled) return;
+namespace {
- if (aim_chunk) {
- DirectionalLighting &world_prog = viewport.EntityProgram();
- world_prog.SetM(outline_transform);
- outline.Draw();
- }
-
- if (counter_text.Visible()) {
- counter_text.Render(viewport);
- }
- if (position_text.Visible()) {
- position_text.Render(viewport);
+std::map<std::string, Keymap::Action> action_map = {
+ { "none", Keymap::NONE },
+ { "move_forward", Keymap::MOVE_FORWARD },
+ { "move_backward", Keymap::MOVE_BACKWARD },
+ { "move_left", Keymap::MOVE_LEFT },
+ { "move_right", Keymap::MOVE_RIGHT },
+ { "move_up", Keymap::MOVE_UP },
+ { "move_down", Keymap::MOVE_DOWN },
+
+ { "primary", Keymap::PRIMARY },
+ { "secondary", Keymap::SECONDARY },
+ { "tertiary", Keymap::TERTIARY },
+
+ { "inventory_next", Keymap::INV_NEXT },
+ { "inventory_prev", Keymap::INV_PREVIOUS },
+ { "inventory_1", Keymap::INV_1 },
+ { "inventory_2", Keymap::INV_2 },
+ { "inventory_3", Keymap::INV_3 },
+ { "inventory_4", Keymap::INV_4 },
+ { "inventory_5", Keymap::INV_5 },
+ { "inventory_6", Keymap::INV_6 },
+ { "inventory_7", Keymap::INV_7 },
+ { "inventory_8", Keymap::INV_8 },
+ { "inventory_9", Keymap::INV_9 },
+ { "inventory_10", Keymap::INV_10 },
+
+ { "toggle_audio", Keymap::TOGGLE_AUDIO },
+ { "toggle_video", Keymap::TOGGLE_VIDEO },
+ { "toggle_hud", Keymap::TOGGLE_HUD },
+ { "toggle_debug", Keymap::TOGGLE_DEBUG },
+
+ { "exit", Keymap::EXIT },
+};
+
+}
+
+const char *Keymap::ActionToString(Action action) {
+ for (const auto &entry : action_map) {
+ if (action == entry.second) {
+ return entry.first.c_str();
+ }
}
+ return "none";
+}
- if (msg_timer.Running()) {
- messages.Render(viewport);
+Keymap::Action Keymap::StringToAction(const std::string &str) {
+ auto entry = action_map.find(str);
+ if (entry != action_map.end()) {
+ return entry->second;
+ } else {
+ std::cerr << "unknown action \"" << str << '"' << std::endl;
+ return NONE;
}
-
- hud.Render(viewport);
}
}