end
end,
JumpKeyReleased = function(self)
- game.dialog(30, 50, 400, 100)
- game.puts("dialog", 36, 52, "Hey. Hag?")
end,
MousePressed = function(self, mx, my)
- if mx > 30 and mx < 430 and my > 50 and my < 150 then
- game.dialogClear()
+ mp = game.uiToWorldCoord(mx, my)
+ if math.abs(mp.x - self.Position.x) < 1 and math.abs(mp.y - self.Position.y) < 1 then
+ game.dialog(30, 50, 400, 100)
+ game.puts("dialog", 36, 52, "What do you think you're doing?")
+ else
+ if mx > 30 and mx < 430 and my > 50 and my < 150 then
+ game.dialogClear()
+ end
end
end
},
systems.system<UISystem>().get()));
script->addToGameNamespace("dialogClear",
[this] { events.emit<HideDialog>(); });
+ script->addToGameNamespace("uiToWorldCoord",
+ bindInstance(&RenderSystem::uiToWorldCoord,
+ systems.system<RenderSystem>().get()));
script->init();
#include <components/Light.hpp>
#include <components/Script.hpp>
-void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
- [[maybe_unused]] entityx::EventManager& events)
+void RenderSystem::configure(entityx::EntityManager&,
+ entityx::EventManager& events)
{
events.subscribe<NewRenderEvent>(*this);
events.subscribe<DelRenderEvent>(*this);
init();
}
-void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
- [[maybe_unused]] entityx::EventManager& events,
- [[maybe_unused]] entityx::TimeDelta dt)
+float RenderSystem::getWorldViewWidth() const
+{
+ return camPos.z * height / width * 2.0;
+}
+
+float RenderSystem::getWorldViewHeight() const
+{
+ return getWorldViewWidth() * height / width;
+}
+
+Position RenderSystem::uiToWorldCoord(float x, float y) const
+{
+ glm::vec2 scaled;
+ scaled.x = x / width - 0.5;
+ scaled.y = y / height - 0.5;
+
+ Position world;
+ world.x = scaled.x * getWorldViewWidth() + camPos.x;
+ world.y = scaled.y * getWorldViewHeight() + camPos.y;
+ world.z = camPos.z;
+
+ return world;
+}
+
+void RenderSystem::update(entityx::EntityManager& entities,
+ entityx::EventManager&,
+ entityx::TimeDelta)
{
// TODO move these to only happen once to speed up rendering
static GLuint s = worldShader.getProgram();
SDL_GL_SwapWindow(window.get());
}
-int RenderSystem::init(void)
+int RenderSystem::init()
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
std::map<GLuint, WorldRenderData> worldRenders;
entityx::Entity player; // Save the player so we can track the camera
+
public:
RenderSystem() :
window(nullptr, SDL_DestroyWindow) {}
- ~RenderSystem(void)
+ ~RenderSystem()
{
SDL_GL_DeleteContext(context);
SDL_Quit();
* Initializes the rendering system
* @return Zero on success, non-zero on error
*/
- int init(void);
+ int init();
+
+ glm::vec3 getCameraPosition() const {
+ return camPos;
+ }
+
+ Position uiToWorldCoord(float x, float y) const;
+
+ /**
+ * Returns the width of the camera's view in world coordinates.
+ */
+ float getWorldViewWidth() const;
+
+ /**
+ * Returns the height of the camera's view in world coordinates.
+ */
+ float getWorldViewHeight() const;
+
/************
* EVENTS *
#ifndef SYSTEM_SCRIPT_HPP_
#define SYSTEM_SCRIPT_HPP_
+#include "world.hpp"
+
#include <entityx/entityx.h>
#include <sol/sol.hpp>
-#include "world.hpp"
+#include <concepts>
/**
* Utility for pairing class instances to their member function calls.
* This is useful for adding functions to the Lua game namespace.
*
+ * TODO I am certain that this can be done better. -clyne
+ *
* @param func The member function to call
* @param instance The instance to bind to
* @return A function that calls the member function using the given instance
template<class C, typename R, typename... Args>
auto bindInstance(R (C::* func)(Args...), C *instance)
{
- return [instance, func](Args... args) { (instance->*func)(args...); };
+ if constexpr (std::same_as<R, void>)
+ return [instance, func](Args... args) { (instance->*func)(args...); };
+ else
+ return [instance, func](Args... args) { return (instance->*func)(args...); };
+}
+template<class C, typename R, typename... Args>
+auto bindInstance(R (C::* func)(Args...) const, const C *instance)
+{
+ if constexpr (std::same_as<R, void>)
+ return [instance, func](Args... args) { (instance->*func)(args...); };
+ else
+ return [instance, func](Args... args) { return (instance->*func)(args...); };
}