* Add double-press space fly toggle

* Clean GuiComponent interactions
master
Nicole Collings 2020-03-04 19:20:44 -08:00
parent 24991f4590
commit af9c38abf3
18 changed files with 140 additions and 86 deletions

View File

@ -313,6 +313,6 @@ set(ZEPHA_SRC
lua/api/class/LocalLuaInventory.cpp
lua/api/class/LocalLuaInventory.h
lua/api/class/LocalLuaInventoryList.cpp
lua/api/class/LocalLuaInventoryList.h game/inventory/InventoryRefs.cpp game/inventory/InventoryRefs.h game/inventory/InventoryList.cpp game/inventory/InventoryList.h)
lua/api/class/LocalLuaInventoryList.h game/inventory/InventoryRefs.cpp game/inventory/InventoryRefs.h game/inventory/InventoryList.cpp game/inventory/InventoryList.h lua/api/modules/time.h)
add_library (Zepha_Core ${ZEPHA_SRC})

View File

@ -6,63 +6,52 @@
#include "components/compound/GuiInventoryList.h"
GameGui::GameGui(LocalInventoryRefs& refs, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer) :
menuRoot(std::make_shared<GuiContainer>("__luaroot")),
handList(std::make_shared<GuiInventoryList>("hand")),
builder(refs, defs, menuRoot),
renderer(renderer),
win(bufferSize),
refs(refs),
defs(defs) {
defs(defs),
win(bufferSize),
renderer(renderer),
hudBuilder(refs, defs, hudRoot),
menuBuilder(refs, defs, menuRoot) {
auto crosshair = std::make_shared<GuiRect>("crosshair");
crosshair->create({22, 22}, {}, defs.textures["crosshair"]);
crosshair->setPos({bufferSize.x / 2 - 11, bufferSize.y / 2 - 9});
builtIn.add(crosshair);
hudRoot->add(crosshair);
auto viginette = std::make_shared<GuiRect>("viginette");
viginette->create(bufferSize, {}, defs.textures["viginette"]);
builtIn.add(viginette);
add(menuRoot);
hudRoot->add(viginette);
handList->create({3, 3}, {}, {}, refs.getHand(), refs.getHand(), defs);
add(handList);
menuRoot->add(handList);
}
void GameGui::update(double delta) {
Drawable::update(delta);
menuRoot->update(delta);
hudRoot->update(delta);
handList->setPos((renderer.window.getMousePos() - glm::ivec2(24)) / 3 * 3);
renderer.window.setCursorHand(menuRoot->mouseActivity(renderer.window.getMousePos()));
if (renderer.window.input.isMousePressed(GLFW_MOUSE_BUTTON_LEFT))
menuRoot->leftClickEvent(true, renderer.window.getMousePos());
if (renderer.window.input.isMouseReleased(GLFW_MOUSE_BUTTON_LEFT))
menuRoot->leftClickEvent(false, renderer.window.getMousePos());
if (renderer.window.input.isMousePressed(GLFW_MOUSE_BUTTON_RIGHT))
menuRoot->rightClickEvent(true, renderer.window.getMousePos());
if (renderer.window.input.isMouseReleased(GLFW_MOUSE_BUTTON_RIGHT))
menuRoot->rightClickEvent(false, renderer.window.getMousePos());
menuRoot->handleMouseInput(renderer.window);
}
void GameGui::winResized(glm::ivec2 win) {
this->win = win;
builtIn.get<GuiRect>("crosshair")->setPos({win.x / 2 - 11, win.y / 2 - 9});
builtIn.get<GuiRect>("viginette")->setScale(win);
hudRoot->get<GuiRect>("crosshair")->setPos({win.x / 2 - 11, win.y / 2 - 9});
hudRoot->get<GuiRect>("viginette")->setScale(win);
builder.build(win);
menuBuilder.build(win);
}
void GameGui::setMenu(const std::string& menu, const std::map<std::string, GuiBuilder::ComponentCallbacks>& callbacks) {
menuState = "menu"; //TODO: Implement the menu state properly
builder.setGui(menu, callbacks);
builder.build(win);
menuBuilder.setGui(menu, callbacks);
menuBuilder.build(win);
}
void GameGui::closeMenu() {
builder.clear();
menuBuilder.clear();
menuState = "";
}
@ -71,10 +60,18 @@ const std::string &GameGui::getMenuState() {
}
void GameGui::setVisible(bool visible) {
GuiComponent::setVisible(visible);
builtIn.setVisible(visible);
menuRoot->setVisible(visible);
hudRoot->setVisible(visible);
}
void GameGui::drawViginette(Renderer &renderer) {
builtIn.draw(renderer);
bool GameGui::isVisible() {
return menuRoot->isVisible();
}
void GameGui::drawHud(Renderer &renderer) {
hudRoot->draw(renderer);
}
void GameGui::drawMenu(Renderer &renderer) {
menuRoot->draw(renderer);
}

View File

@ -16,19 +16,21 @@
#include "components/compound/GuiInventoryList.h"
#include "../inventory/Inventory.h"
class GameGui : public GuiContainer {
class GameGui {
public:
explicit GameGui(LocalInventoryRefs& refs, glm::vec2 bufferSize, ClientGame& defs, Renderer& renderer);
void winResized(glm::ivec2 win);
void update(double delta);
void update(double delta) override;
void setVisible(bool visible) override;
void setVisible(bool visible);
bool isVisible();
void setMenu(const std::string& menu, const std::map<std::string, GuiBuilder::ComponentCallbacks>& callbacks);
void closeMenu();
const std::string& getMenuState();
void drawViginette(Renderer& renderer);
void drawHud(Renderer& renderer);
void drawMenu(Renderer& renderer);
private:
ClientGame& defs;
Renderer& renderer;
@ -36,10 +38,14 @@ private:
glm::ivec2 win {};
std::string menuState = "";
std::shared_ptr<GuiContainer> menuRoot = {};
std::shared_ptr<GuiInventoryList> handList = {};
GuiContainer builtIn = {};
GameGuiBuilder builder;
std::shared_ptr<GuiContainer> menuRoot = std::make_shared<GuiInventoryList>("menuRoot");
std::shared_ptr<GuiContainer> menuLuaRoot = std::make_shared<GuiInventoryList>("menuLuaRoot");
GameGuiBuilder menuBuilder;
std::shared_ptr<GuiContainer> hudRoot = std::make_shared<GuiInventoryList>("hudRoot");
std::shared_ptr<GuiContainer> hudLuaRoot = std::make_shared<GuiInventoryList>("hudLuaRoot");
GameGuiBuilder hudBuilder;
std::shared_ptr<GuiInventoryList> handList = std::make_shared<GuiInventoryList>("hand");
LocalInventoryRefs& refs;
};

View File

@ -71,6 +71,14 @@ void GuiComponent::setCallback(GuiComponent::CallbackType type, const callback&
callbacks[static_cast<unsigned int>(type)] = cb;
}
void GuiComponent::handleMouseInput(Window &window) {
window.setCursorHand(mouseActivity(window.getMousePos()));
if (window.input.isMousePressed(GLFW_MOUSE_BUTTON_LEFT)) clickEvent(true, true, window.getMousePos());
if (window.input.isMouseReleased(GLFW_MOUSE_BUTTON_LEFT)) clickEvent(true, false, window.getMousePos());
if (window.input.isMousePressed(GLFW_MOUSE_BUTTON_RIGHT)) clickEvent(false, true, window.getMousePos());
if (window.input.isMouseReleased(GLFW_MOUSE_BUTTON_RIGHT)) clickEvent(false, false, window.getMousePos());
}
bool GuiComponent::mouseActivity(glm::ivec2 pos) {
bool isHovering = false;
for (auto& child : children) {
@ -97,24 +105,18 @@ bool GuiComponent::mouseActivity(glm::ivec2 pos) {
return isHovering;
}
void GuiComponent::leftClickEvent(bool state, glm::ivec2 pos) {
clickEvent(true, state, pos);
}
void GuiComponent::rightClickEvent(bool state, glm::ivec2 pos) {
clickEvent(false, state, pos);
}
void GuiComponent::insert(unsigned int index, std::shared_ptr<GuiComponent> component) {
std::shared_ptr<GuiComponent> GuiComponent::insert(unsigned int index, std::shared_ptr<GuiComponent> component) {
component->parent = this;
component->updatePos();
children.insert(std::next(children.begin(), index), std::move(component));
return component;
}
void GuiComponent::add(std::shared_ptr<GuiComponent> component) {
std::shared_ptr<GuiComponent> GuiComponent::add(std::shared_ptr<GuiComponent> component) {
component->parent = this;
component->updatePos();
children.push_back(std::move(component));
return component;
}
void GuiComponent::remove(const std::string& key) {
@ -168,4 +170,4 @@ void GuiComponent::updatePos() {
for (const auto& child : children) {
child->updatePos();
}
}
}

View File

@ -32,10 +32,7 @@ public:
void setVisible(bool visible) override;
virtual void setCallback(CallbackType type, const callback& cb);
bool mouseActivity(glm::ivec2 pos);
void leftClickEvent(bool state, glm::ivec2 pos);
void rightClickEvent(bool state, glm::ivec2 pos);
void handleMouseInput(Window& window);
template<class T> std::shared_ptr<T> get(const std::string &key) {
for (auto &it : children) {
@ -45,14 +42,15 @@ public:
}
};
void insert(unsigned int index, std::shared_ptr<GuiComponent> component);
void add(std::shared_ptr<GuiComponent> component);
std::shared_ptr<GuiComponent> insert(unsigned int index, std::shared_ptr<GuiComponent> component);
std::shared_ptr<GuiComponent> add(std::shared_ptr<GuiComponent> component);
void remove(const std::string& key);
void empty();
void draw(Renderer& renderer) override;
protected:
bool mouseActivity(glm::ivec2 pos);
bool clickEvent(bool left, bool state, glm::ivec2 pos);
std::string key = "";

View File

@ -72,9 +72,9 @@ void GameScene::draw() {
renderer.beginGUIDrawCalls();
renderer.enableTexture(&game.textures.atlasTexture);
player.drawViginette(renderer);
player.drawHud(renderer);
debugGui.draw(renderer);
player.drawGUI(renderer);
player.drawMenu(renderer);
renderer.swapBuffers();
}

View File

@ -196,15 +196,7 @@ void MainMenuScene::update() {
state.defs.textures.update();
sandbox.update(state.delta);
state.renderer.window.setCursorHand(components.mouseActivity(state.renderer.window.getMousePos()));
if (state.renderer.window.input.isMousePressed(GLFW_MOUSE_BUTTON_LEFT))
components.leftClickEvent(true, state.renderer.window.getMousePos());
if (state.renderer.window.input.isMouseReleased(GLFW_MOUSE_BUTTON_LEFT))
components.leftClickEvent(false, state.renderer.window.getMousePos());
if (state.renderer.window.input.isMousePressed(GLFW_MOUSE_BUTTON_RIGHT))
components.rightClickEvent(true, state.renderer.window.getMousePos());
if (state.renderer.window.input.isMouseReleased(GLFW_MOUSE_BUTTON_RIGHT))
components.rightClickEvent(false, state.renderer.window.getMousePos());
components.handleMouseInput(state.renderer.window);
}
void MainMenuScene::draw() {

View File

@ -21,6 +21,7 @@ void Player::update(Input &input, double delta, glm::vec2 mouseDelta) {
handItemModel.setModel(defs.defs.fromId(activeBlock).entityModel);
handItemModel.parent = &handModel;
}
handItemModel.setVisible(gameGui.isVisible());
gameGui.update(delta);
@ -299,16 +300,15 @@ std::string Player::getMenuState() {
void Player::draw(Renderer &renderer) {
wireframe.draw(renderer);
// handModel.draw(renderer);
handItemModel.draw(renderer);
}
void Player::drawGUI(Renderer &renderer) {
gameGui.draw(renderer);
void Player::drawHud(Renderer &renderer) {
gameGui.drawHud(renderer);
}
void Player::drawViginette(Renderer &renderer) {
gameGui.drawViginette(renderer);
void Player::drawMenu(Renderer &renderer) {
gameGui.drawMenu(renderer);
}
Player::~Player() {

View File

@ -49,8 +49,8 @@ public:
void setGuiVisible(bool hudVisible);
void draw(Renderer& renderer) override;
void drawGUI(Renderer& renderer);
void drawViginette(Renderer& renderer);
void drawHud(Renderer& renderer);
void drawMenu(Renderer& renderer);
LocalInventory& getInventory();
PointedThing& getPointedThing();

View File

@ -0,0 +1,29 @@
//
// Created by aurailus on 2020-03-04.
//
#pragma once
#include <sol2/sol.hpp>
#include "../../../util/Timer.h"
namespace Api {
static void time(sol::state& lua, sol::table& core) {
core["time"] = lua.create_table();
sol::table time = core["time"];
Timer t = Timer("timer");
time.set_function("ns", [=]() {
return t.elapsedNs();
});
time.set_function("ms", [=]() {
return t.elapsedNs() / 1000000.f;
});
time.set_function("s", [=]() {
return t.elapsedNs() / 1000000.f / 1000.f;
});
}
}

View File

@ -30,6 +30,7 @@
#include "../api/modules/add_entity.h"
#include "../api/modules/remove_entity.h"
#include "../api/modules/register_keybind.h"
#include "../api/modules/time.h"
// Functions
#include "../api/functions/trigger_event.h"
@ -94,6 +95,8 @@ void LocalLuaParser::loadApi(ClientGame &defs, LocalWorld &world, Player& player
Api::add_entity_c (lua, core, defs, world);
Api::remove_entity_c (lua, core, defs, world);
Api::time(lua, core);
// Functions
Api::update_entities(lua);

View File

@ -32,6 +32,7 @@
#include "../api/modules/remove_block.h"
#include "../api/modules/add_entity.h"
#include "../api/modules/remove_entity.h"
#include "../api/modules/time.h"
// Functions
#include "../api/functions/trigger_event.h"
@ -125,6 +126,8 @@ void ServerLuaParser::loadApi(ServerGame &defs, ServerWorld &world) {
Api::add_entity_s (lua, core, defs, world);
Api::remove_entity_s (lua, core, defs, world);
Api::time(lua, core);
// Functions
Api::trigger_event (lua);
Api::update_entities(lua);

View File

@ -12,7 +12,7 @@ Timer::Timer(const char* name) :
name(name), hasName(true),
start(std::chrono::high_resolution_clock::now()) {}
long Timer::elapsedNs() {
long Timer::elapsedNs() const {
auto finish = std::chrono::high_resolution_clock::now();
long elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count();
return elapsed;

View File

@ -12,7 +12,7 @@ public:
explicit Timer();
explicit Timer(const char* name);
long elapsedNs();
long elapsedNs() const;
void printElapsedNs();
void printElapsedMs();

View File

@ -101,7 +101,7 @@ namespace Util {
static std::string getKeyStr(unsigned short key) {
switch (key) {
default: return "";
case 32: return " ";
case 32: return "space";
case 39: return "'";
case 44: return ",";
case 45: return "-";

View File

@ -6,12 +6,5 @@ runfile(_PATH .. "dump")
runfile(_PATH .. "math")
runfile(_PATH .. "vector")
-- Set flying toggle
zepha.register_keybind("base:toggle_flying", {
description = "Toggle Flying",
default = zepha.keys.f,
on_press = function() zepha.player.flying = not zepha.player.flying end
})
-- Signal completion
print("Base definitions loaded.")

View File

@ -30,4 +30,26 @@ zepha.register_keybind("zeus:default:open_chat", {
description = "Open Chat",
default = zepha.keys.t,
on_press = () => { print "Opened chat!" }
})
## Flying toggles
local fn toggleFlying() {
zepha.player.flying = not zepha.player.flying
}
zepha.register_keybind("zeus:default:toggle_flying", {
description = "Toggle Flying",
default = zepha.keys.f,
on_press = toggleFlying
})
local last_press = -100
zepha.register_keybind("zeus:default:double_jump_fly", {
description = "Double Jump to Toggle Flying",
default = zepha.keys.space,
on_press = () => {
local press = zepha.time.s();
if (press - last_press < 0.25) { toggleFlying() }
last_press = press;
}
})

View File

@ -51,6 +51,15 @@ zepha.register_keybind("zeus:inventory:open_inventory", {
position: 41px -8px
size: 34px 52px
overflow: hidden
model
scale: 86 86
position: 15px 52px
type: model
source: zeus:default:player
texture: zeus:default:player
anim_range: 0 300
end
end
end