lua apicka
This commit is contained in:
parent
5f331dd548
commit
a4b77f420a
4
.gitignore
vendored
4
.gitignore
vendored
@ -365,4 +365,6 @@ FodyWeavers.xsd
|
|||||||
|
|
||||||
#TSR
|
#TSR
|
||||||
|
|
||||||
main/
|
main/*
|
||||||
|
!main/scripts
|
||||||
|
!main/shaders
|
||||||
33
src/tsr/entity.cpp
Normal file
33
src/tsr/entity.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "entity.hpp"
|
||||||
|
|
||||||
|
using namespace TSR;
|
||||||
|
|
||||||
|
entt::entity EntitySystem::Spawn(const Transform& trans) {
|
||||||
|
auto& reg = Game::Registry();
|
||||||
|
auto ent = reg.create();
|
||||||
|
auto& comp = reg.emplace<TransformComponent>(ent);
|
||||||
|
comp.trans = trans;
|
||||||
|
return ent;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntitySystem::Despawn(entt::entity ent) {
|
||||||
|
Game::Registry().destroy(ent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntitySystem::RegisterLuaFunctions() {
|
||||||
|
LuaAPI::RegisterFunction("ent_spawn", [](float x, float y, float z, float pitch, float yaw, float roll, float sx, float sy, float sz) {
|
||||||
|
Transform trans;
|
||||||
|
trans.pos.x = x;
|
||||||
|
trans.pos.y = y;
|
||||||
|
trans.pos.z = z;
|
||||||
|
trans.rot = glm::quat(glm::vec3(pitch, yaw, roll));
|
||||||
|
trans.scl.x = sx;
|
||||||
|
trans.scl.y = sy;
|
||||||
|
trans.scl.z = sz;
|
||||||
|
return EntitySystem::Spawn(trans);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("ent_despawn", [](entt::entity ent) {
|
||||||
|
EntitySystem::Despawn(ent);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,3 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <entt/entt.hpp>
|
#include <entt/entt.hpp>
|
||||||
#include "game.hpp"
|
#include "game.hpp"
|
||||||
|
|
||||||
|
namespace TSR {
|
||||||
|
|
||||||
|
struct TransformComponent {
|
||||||
|
Transform trans;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EntitySystem {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
static entt::entity Spawn(const Transform& trans);
|
||||||
|
static void Despawn(entt::entity ent);
|
||||||
|
|
||||||
|
static void RegisterLuaFunctions();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -37,8 +37,10 @@ static glm::vec3 CCTGetPos(CCTComponent& comp) {
|
|||||||
return glm::vec3(pos_px.x, pos_px.y, pos_px.z);
|
return glm::vec3(pos_px.x, pos_px.y, pos_px.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CCTTurnToAngle(CCTComponent& comp, float target, float step) {
|
static bool CCTTurnToAngle(CCTComponent& comp, float target, float speed) {
|
||||||
if (target < comp.angle)
|
auto step = speed / Game::TPS();
|
||||||
|
|
||||||
|
while (target < comp.angle)
|
||||||
target += glm::two_pi<float>();
|
target += glm::two_pi<float>();
|
||||||
|
|
||||||
auto diff_angle = target - comp.angle;
|
auto diff_angle = target - comp.angle;
|
||||||
@ -121,24 +123,30 @@ void CCTSystem::SetPos(entt::entity ent, const glm::vec3& pos) {
|
|||||||
CCTSetPos(comp, pos);
|
CCTSetPos(comp, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 TSR::CCTSystem::GetPos(entt::entity ent) {
|
glm::vec3 CCTSystem::GetPos(entt::entity ent) {
|
||||||
auto& reg = Game::Registry();
|
auto& reg = Game::Registry();
|
||||||
auto& comp = reg.get<CCTComponent>(ent);
|
auto& comp = reg.get<CCTComponent>(ent);
|
||||||
return CCTGetPos(comp);
|
return CCTGetPos(comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float step) {
|
glm::vec3 CCTSystem::GetForward(entt::entity ent) {
|
||||||
|
auto& reg = Game::Registry();
|
||||||
|
auto& comp = reg.get<CCTComponent>(ent);
|
||||||
|
return glm::vec3(std::sin(comp.angle), 0.0f, std::cos(comp.angle));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float speed) {
|
||||||
auto& reg = Game::Registry();
|
auto& reg = Game::Registry();
|
||||||
auto& comp = reg.get<CCTComponent>(ent);
|
auto& comp = reg.get<CCTComponent>(ent);
|
||||||
|
|
||||||
return CCTTurnTo(comp, pos, step);
|
return CCTTurnTo(comp, pos, speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TSR::CCTSystem::TurnToAngle(entt::entity ent, float target, float step) {
|
bool CCTSystem::TurnToAngle(entt::entity ent, float target, float speed) {
|
||||||
auto& reg = Game::Registry();
|
auto& reg = Game::Registry();
|
||||||
auto& comp = reg.get<CCTComponent>(ent);
|
auto& comp = reg.get<CCTComponent>(ent);
|
||||||
|
|
||||||
return CCTTurnToAngle(comp, target, step);
|
return CCTTurnToAngle(comp, target, speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CCTSystem::Update() {
|
void CCTSystem::Update() {
|
||||||
@ -152,3 +160,42 @@ void CCTSystem::Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TSR::CCTSystem::RegisterLuaFunctions() {
|
||||||
|
LuaAPI::RegisterFunction("cct_add", [](entt::entity ent, float radius, float height) {
|
||||||
|
CCTSystem::AddCCT(ent, radius, height);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_remove", [](entt::entity ent) {
|
||||||
|
CCTSystem::RemoveCCT(ent);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_move", [](entt::entity ent, float x, float y, float z) {
|
||||||
|
auto disp = glm::vec3(x, y, z);
|
||||||
|
return CCTSystem::Move(ent, disp, 1.0f / Game::TPS());
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_setpos", [](entt::entity ent, float x, float y, float z) {
|
||||||
|
auto pos = glm::vec3(x, y, z);
|
||||||
|
CCTSystem::SetPos(ent, pos);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_getpos", [](entt::entity ent) {
|
||||||
|
auto pos = CCTSystem::GetPos(ent);
|
||||||
|
return std::make_tuple(pos.x, pos.y, pos.z);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_getforwardxz", [](entt::entity ent) {
|
||||||
|
auto forward = CCTSystem::GetForward(ent);
|
||||||
|
return std::make_tuple(forward.x, forward.z);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_turnto", [](entt::entity ent, float x, float y, float z, float speed) {
|
||||||
|
auto pos = glm::vec3(x, y, z);
|
||||||
|
return CCTSystem::TurnTo(ent, pos, speed);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("cct_turntoangle", [](entt::entity ent, float angle, float speed) {
|
||||||
|
return CCTSystem::TurnToAngle(ent, angle, speed);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@ -27,10 +27,15 @@ namespace TSR {
|
|||||||
static int Move(entt::entity ent, const glm::vec3& disp, float dt);
|
static int Move(entt::entity ent, const glm::vec3& disp, float dt);
|
||||||
static void SetPos(entt::entity ent, const glm::vec3& pos);
|
static void SetPos(entt::entity ent, const glm::vec3& pos);
|
||||||
static glm::vec3 GetPos(entt::entity ent);
|
static glm::vec3 GetPos(entt::entity ent);
|
||||||
|
|
||||||
|
static glm::vec3 GetForward(entt::entity ent);
|
||||||
|
|
||||||
static bool TurnTo(entt::entity ent, const glm::vec3& pos, float step);
|
static bool TurnTo(entt::entity ent, const glm::vec3& pos, float step);
|
||||||
static bool TurnToAngle(entt::entity ent, float target, float step);
|
static bool TurnToAngle(entt::entity ent, float target, float step);
|
||||||
|
|
||||||
static void Update();
|
static void Update();
|
||||||
|
|
||||||
|
static void RegisterLuaFunctions();
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -36,7 +36,7 @@ void ModelSystem::AddModel(entt::entity ent, const std::string& name) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TSR::ModelSystem::RemoveModel(entt::entity ent) {
|
void ModelSystem::RemoveModel(entt::entity ent) {
|
||||||
auto& reg = Game::Registry();
|
auto& reg = Game::Registry();
|
||||||
reg.erase<ModelComponent>(ent);
|
reg.erase<ModelComponent>(ent);
|
||||||
//reg.erase<AnimationComponent>(ent);
|
//reg.erase<AnimationComponent>(ent);
|
||||||
@ -241,4 +241,22 @@ void ModelSystem::Render(TSR::Renderer& renderer) {
|
|||||||
|
|
||||||
//World::Get().Reg()
|
//World::Get().Reg()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ModelSystem::RegisterLuaFunctions() {
|
||||||
|
LuaAPI::RegisterFunction("mdl_set", [](entt::entity ent, const std::string& name) {
|
||||||
|
ModelSystem::AddModel(ent, name);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("mdl_remove", [](entt::entity ent) {
|
||||||
|
ModelSystem::RemoveModel(ent);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("mdl_anim", [](entt::entity ent, const std::string& anim_name, float t, float fade_in_time) {
|
||||||
|
ModelSystem::Anim(ent, anim_name, t, fade_in_time);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("mdl_stopanim", [](entt::entity ent, const std::string& anim_name, float fade_out_time) {
|
||||||
|
ModelSystem::StopAnim(ent, anim_name, fade_out_time);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@ -43,7 +43,7 @@ namespace TSR {
|
|||||||
static void Animate();
|
static void Animate();
|
||||||
static void Render(Renderer& renderer);
|
static void Render(Renderer& renderer);
|
||||||
|
|
||||||
|
static void RegisterLuaFunctions();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
197
src/tsr/game.cpp
197
src/tsr/game.cpp
@ -8,6 +8,10 @@
|
|||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
#include "worldmap.hpp"
|
#include "worldmap.hpp"
|
||||||
#include "physics.hpp"
|
#include "physics.hpp"
|
||||||
|
#include "input.hpp"
|
||||||
|
#include "entity.hpp"
|
||||||
|
#include "entity_model.hpp"
|
||||||
|
#include "entity_cct.hpp"
|
||||||
|
|
||||||
using namespace TSR;
|
using namespace TSR;
|
||||||
|
|
||||||
@ -31,6 +35,10 @@ static AssetPtr<Model> s_skybox_model;
|
|||||||
|
|
||||||
static uint32_t s_next_timer_id;
|
static uint32_t s_next_timer_id;
|
||||||
|
|
||||||
|
static bool s_camera_attached = false;
|
||||||
|
static entt::entity s_camera_attach_ent;
|
||||||
|
static glm::vec3 s_camera_attach_offset;
|
||||||
|
|
||||||
void Game::StartGame(const std::string& map_name) {
|
void Game::StartGame(const std::string& map_name) {
|
||||||
s_sv_frame = 0;
|
s_sv_frame = 0;
|
||||||
s_frame_t = 0.0f;
|
s_frame_t = 0.0f;
|
||||||
@ -44,36 +52,67 @@ void Game::StartGame(const std::string& map_name) {
|
|||||||
|
|
||||||
LuaAPI::Init();
|
LuaAPI::Init();
|
||||||
|
|
||||||
LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, sol::function callback) {
|
RegisterLuaFunctions();
|
||||||
|
Input::RegisterLuaFunctions();
|
||||||
|
EntitySystem::RegisterLuaFunctions();
|
||||||
|
ModelSystem::RegisterLuaFunctions();
|
||||||
|
CCTSystem::RegisterLuaFunctions();
|
||||||
|
|
||||||
|
LuaAPI::Load("scripts/autoexec.lua");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::RegisterLuaFunctions() {
|
||||||
|
LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, LuaFunction callback) {
|
||||||
auto& timer = s_timers[++s_next_timer_id];
|
auto& timer = s_timers[++s_next_timer_id];
|
||||||
timer.last_frame = s_sv_frame;
|
timer.last_frame = s_sv_frame;
|
||||||
timer.interval = interval;
|
timer.interval = interval;
|
||||||
timer.repeat = repeat;
|
timer.repeat = repeat;
|
||||||
timer.callback = callback;
|
timer.callback = callback;
|
||||||
return s_next_timer_id;
|
return s_next_timer_id;
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaAPI::RegisterFunction("g_cleartimer", [](uint32_t id) {
|
LuaAPI::RegisterFunction("g_cleartimer", [](uint32_t id) {
|
||||||
s_timers.erase(id);
|
s_timers.erase(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaAPI::RegisterFunction("g_getframe", []() {
|
LuaAPI::RegisterFunction("g_getframe", []() {
|
||||||
return s_sv_frame;
|
return s_sv_frame;
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaAPI::RegisterFunction("g_gettps", []() {
|
LuaAPI::RegisterFunction("g_gettps", []() {
|
||||||
return s_tps;
|
return s_tps;
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaAPI::RegisterFunction("g_require", [](const std::string& name) {
|
LuaAPI::RegisterFunction("g_require", [](const std::string& name) {
|
||||||
LuaAPI::Load(name);
|
LuaAPI::Load(name);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("g_getcameraforward", []() {
|
||||||
|
return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.y, cam.forward_backward_vector.z);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("g_getcameraforwardxz", []() {
|
||||||
|
return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.z);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("g_getcamerarightxz", []() {
|
||||||
|
return std::make_tuple(cam.right_vector.x, cam.right_vector.z);
|
||||||
|
});
|
||||||
|
|
||||||
|
LuaAPI::RegisterFunction("g_attachcamera", [](entt::entity ent, float x, float y, float z) {
|
||||||
|
s_camera_attached = true;
|
||||||
|
s_camera_attach_ent = ent;
|
||||||
|
s_camera_attach_offset = glm::vec3(x, y, z);
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaAPI::Load("scripts/autoexec.lua");
|
LuaAPI::RegisterFunction("g_detachcamera", []() {
|
||||||
|
s_camera_attached = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::EndGame() {
|
void Game::EndGame() {
|
||||||
s_timers.clear();
|
s_timers.clear();
|
||||||
|
Input::Shutdown();
|
||||||
LuaAPI::Shutdown();
|
LuaAPI::Shutdown();
|
||||||
|
|
||||||
s_registry.clear();
|
s_registry.clear();
|
||||||
@ -100,53 +139,6 @@ void Game::Run() {
|
|||||||
|
|
||||||
s_skybox_model = AssetMap::Get<Model>("models/skybox.mdl.json");
|
s_skybox_model = AssetMap::Get<Model>("models/skybox.mdl.json");
|
||||||
|
|
||||||
//s_physics_scene->EnableDebug(false);
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
auto ent = Game::Registry().create();
|
|
||||||
ModelSystem::AddModel(ent, "models/test_skeletal.mdl.json");
|
|
||||||
ModelSystem::Anim(ent, "init");
|
|
||||||
ModelSystem::Anim(ent, "ruce");
|
|
||||||
//ModelSystem::Anim(ent, "stand");
|
|
||||||
//ModelSystem::Anim(ent, "walk2", i * 13.0f);
|
|
||||||
|
|
||||||
glm::vec3 pos(2.0f * (i / 10), 0.0f, 2.0f * (i % 10));
|
|
||||||
|
|
||||||
auto& trans = Registry().emplace_or_replace<TransformComponent>(ent);
|
|
||||||
trans.trans.pos = pos;
|
|
||||||
trans.trans.rot = glm::quat(glm::vec3(0.0f));
|
|
||||||
trans.trans.scl = glm::vec3(1.0f);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
ent1 = Game::Registry().create();
|
|
||||||
ModelSystem::AddModel(ent1, "models/skrin.mdl.json");
|
|
||||||
ModelSystem::Anim(ent1, "init");
|
|
||||||
ModelSystem::Anim(ent1, "open_left");
|
|
||||||
ModelSystem::Anim(ent1, "open_right");
|
|
||||||
auto& trans = Registry().emplace_or_replace<TransformComponent>(ent1);
|
|
||||||
glm::vec3 pos(0.0f, 5.0f, 0.0f);
|
|
||||||
trans.trans.pos = pos;
|
|
||||||
trans.trans.rot = glm::quat(glm::vec3(0.0f));
|
|
||||||
trans.trans.scl = glm::vec3(1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
player = Game::Registry().create();
|
|
||||||
ModelSystem::AddModel(player, "models/test_skeletal.mdl.json");
|
|
||||||
ModelSystem::Anim(player, "init");
|
|
||||||
ModelSystem::Anim(player, "stand");
|
|
||||||
auto& trans = Registry().emplace_or_replace<TransformComponent>(player);
|
|
||||||
glm::vec3 pos(0.0f, 5.0f, 0.0f);
|
|
||||||
trans.trans.pos = pos;
|
|
||||||
trans.trans.rot = glm::quat(glm::vec3(0.0f));
|
|
||||||
trans.trans.scl = glm::vec3(1.0f);
|
|
||||||
|
|
||||||
CCTSystem::AddCCT(player, 0.25f, 1.2f);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
cam.third_person_distance = 5.0f;
|
cam.third_person_distance = 5.0f;
|
||||||
|
|
||||||
Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
@ -171,23 +163,12 @@ void Game::Run() {
|
|||||||
s_debug_draw = !s_debug_draw;
|
s_debug_draw = !s_debug_draw;
|
||||||
s_physics_scene->EnableDebug(s_debug_draw);
|
s_physics_scene->EnableDebug(s_debug_draw);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case GLFW_KEY_E:
|
|
||||||
ModelSystem::Anim(ent1, "open_left");
|
|
||||||
ModelSystem::Anim(ent1, "open_right");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GLFW_KEY_Q:
|
|
||||||
ModelSystem::Anim(ent1, "close_left");
|
|
||||||
ModelSystem::Anim(ent1, "close_right");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (action == GLFW_PRESS || action == GLFW_RELEASE)
|
||||||
|
Input::AddControlEvent(Input::ControlFromGlfwKey(key), action == GLFW_PRESS);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
cam.movement_speed *= 0.1f;
|
cam.movement_speed *= 0.1f;
|
||||||
@ -215,7 +196,7 @@ void Game::Run() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s_frame_t = (float)(real_time - time + frame_time_ms) / (float)frame_time_ms;
|
s_frame_t = glm::min((float)(real_time - time + frame_time_ms) / (float)frame_time_ms, 1.0f);
|
||||||
|
|
||||||
ClFrame(renderer, 0.0f);
|
ClFrame(renderer, 0.0f);
|
||||||
}
|
}
|
||||||
@ -233,18 +214,7 @@ void Game::Run() {
|
|||||||
void Game::SvFrame() {
|
void Game::SvFrame() {
|
||||||
float time = 1.0f / (float)s_tps;
|
float time = 1.0f / (float)s_tps;
|
||||||
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_W))
|
Input::ProcessEvents();
|
||||||
// cam.ProcessMovement(Camera::Movement::FORWARD, time);
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_S))
|
|
||||||
// cam.ProcessMovement(Camera::Movement::BACKWARD, time);
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_A))
|
|
||||||
// cam.ProcessMovement(Camera::Movement::LEFT, time);
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_D))
|
|
||||||
// cam.ProcessMovement(Camera::Movement::RIGHT, time);
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_SPACE))
|
|
||||||
// cam.ProcessMovement(Camera::Movement::UP, time);
|
|
||||||
//if (Window::KeyDown(GLFW_KEY_LEFT_SHIFT))
|
|
||||||
// cam.ProcessMovement(Camera::Movement::DOWN, time);
|
|
||||||
|
|
||||||
// execute timers
|
// execute timers
|
||||||
for (auto it = s_timers.begin(); it != s_timers.end();) {
|
for (auto it = s_timers.begin(); it != s_timers.end();) {
|
||||||
@ -252,8 +222,7 @@ void Game::SvFrame() {
|
|||||||
if (s_sv_frame != timer.last_frame && s_sv_frame - timer.last_frame >= (uint32_t)(timer.interval * (float)s_tps)) {
|
if (s_sv_frame != timer.last_frame && s_sv_frame - timer.last_frame >= (uint32_t)(timer.interval * (float)s_tps)) {
|
||||||
timer.last_frame = s_sv_frame;
|
timer.last_frame = s_sv_frame;
|
||||||
|
|
||||||
sol::protected_function func = timer.callback;
|
LuaAPI::Call(timer.callback);
|
||||||
func();
|
|
||||||
|
|
||||||
if (!timer.repeat) {
|
if (!timer.repeat) {
|
||||||
it = s_timers.erase(it);
|
it = s_timers.erase(it);
|
||||||
@ -264,59 +233,6 @@ void Game::SvFrame() {
|
|||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
||||||
float move_forward = 0.0f;
|
|
||||||
float move_right = 0.0f;
|
|
||||||
|
|
||||||
if (Window::KeyDown(GLFW_KEY_W))
|
|
||||||
move_forward += 1.0f;
|
|
||||||
if (Window::KeyDown(GLFW_KEY_S))
|
|
||||||
move_forward -= 1.0f;
|
|
||||||
if (Window::KeyDown(GLFW_KEY_A))
|
|
||||||
move_right -= 1.0f;
|
|
||||||
if (Window::KeyDown(GLFW_KEY_D))
|
|
||||||
move_right += 1.0f;
|
|
||||||
|
|
||||||
static bool walking = false;
|
|
||||||
|
|
||||||
if (move_forward != 0.0f || move_right != 0.0f) {
|
|
||||||
auto move_vector = cam.forward_backward_vector * move_forward + cam.right_vector * move_right;
|
|
||||||
|
|
||||||
auto move_angle = std::atan2(move_vector.x, move_vector.z);
|
|
||||||
|
|
||||||
CCTSystem::TurnToAngle(player, move_angle, 6.0f / TPS());
|
|
||||||
|
|
||||||
auto& cctcomp = Registry().get<CCTComponent>(player);
|
|
||||||
|
|
||||||
glm::vec3 forward_vector(std::sin(cctcomp.angle), -1.0f, std::cos(cctcomp.angle));
|
|
||||||
CCTSystem::Move(player, forward_vector * (3.0f / TPS()), time);
|
|
||||||
|
|
||||||
if (!walking) {
|
|
||||||
walking = true;
|
|
||||||
ModelSystem::Anim(player, "walk2", 6.0f, 0.2f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (walking) {
|
|
||||||
walking = false;
|
|
||||||
ModelSystem::StopAnim(player, "walk2", 0.1f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aiming = false;
|
|
||||||
|
|
||||||
if (Window::KeyDown(GLFW_KEY_E)) {
|
|
||||||
if (!aiming) {
|
|
||||||
aiming = true;
|
|
||||||
ModelSystem::Anim(player, "ruce", 0.0f, 0.2f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (aiming) {
|
|
||||||
aiming = false;
|
|
||||||
ModelSystem::StopAnim(player, "ruce", 0.2f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CCTSystem::Update();
|
CCTSystem::Update();
|
||||||
ModelSystem::Animate();
|
ModelSystem::Animate();
|
||||||
|
|
||||||
@ -340,7 +256,10 @@ void Game::ClFrame(Renderer& renderer, float frame_t) {
|
|||||||
if (s_skybox_model)
|
if (s_skybox_model)
|
||||||
s_skybox_model->Draw(renderer, nullptr);
|
s_skybox_model->Draw(renderer, nullptr);
|
||||||
|
|
||||||
cam.position = glm::vec3(Registry().get<ModelComponent>(player).ctx.model_matrix[3]) + glm::vec3(0.0f, 1.0f, 0.0f);
|
if (s_camera_attached) {
|
||||||
|
cam.position = glm::vec3(Registry().get<ModelComponent>(s_camera_attach_ent).ctx.model_matrix[3]) + s_camera_attach_offset;
|
||||||
|
}
|
||||||
|
|
||||||
renderer.SetViewMatrix(cam.GetViewMatrix());
|
renderer.SetViewMatrix(cam.GetViewMatrix());
|
||||||
|
|
||||||
renderer.Render(w, h);
|
renderer.Render(w, h);
|
||||||
|
|||||||
@ -16,7 +16,7 @@ namespace TSR {
|
|||||||
float interval = 0.0f;
|
float interval = 0.0f;
|
||||||
uint32_t last_frame = 0;
|
uint32_t last_frame = 0;
|
||||||
bool repeat = false;
|
bool repeat = false;
|
||||||
sol::reference callback;
|
LuaReference callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
@ -49,12 +49,9 @@ namespace TSR {
|
|||||||
static float FrameT() { return s_frame_t; }
|
static float FrameT() { return s_frame_t; }
|
||||||
static int TPS() { return s_tps; }
|
static int TPS() { return s_tps; }
|
||||||
|
|
||||||
|
static void RegisterLuaFunctions();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TransformComponent {
|
|
||||||
Transform trans;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
535
src/tsr/input.cpp
Normal file
535
src/tsr/input.cpp
Normal file
@ -0,0 +1,535 @@
|
|||||||
|
#include "input.hpp"
|
||||||
|
|
||||||
|
using namespace TSR;
|
||||||
|
|
||||||
|
const Control Input::s_glfw_key_map[] = {
|
||||||
|
KEY_NONE, // 0
|
||||||
|
KEY_NONE, // 1
|
||||||
|
KEY_NONE, // 2
|
||||||
|
KEY_NONE, // 3
|
||||||
|
KEY_NONE, // 4
|
||||||
|
KEY_NONE, // 5
|
||||||
|
KEY_NONE, // 6
|
||||||
|
KEY_NONE, // 7
|
||||||
|
KEY_NONE, // 8
|
||||||
|
KEY_NONE, // 9
|
||||||
|
KEY_NONE, // 10
|
||||||
|
KEY_NONE, // 11
|
||||||
|
KEY_NONE, // 12
|
||||||
|
KEY_NONE, // 13
|
||||||
|
KEY_NONE, // 14
|
||||||
|
KEY_NONE, // 15
|
||||||
|
KEY_NONE, // 16
|
||||||
|
KEY_NONE, // 17
|
||||||
|
KEY_NONE, // 18
|
||||||
|
KEY_NONE, // 19
|
||||||
|
KEY_NONE, // 20
|
||||||
|
KEY_NONE, // 21
|
||||||
|
KEY_NONE, // 22
|
||||||
|
KEY_NONE, // 23
|
||||||
|
KEY_NONE, // 24
|
||||||
|
KEY_NONE, // 25
|
||||||
|
KEY_NONE, // 26
|
||||||
|
KEY_NONE, // 27
|
||||||
|
KEY_NONE, // 28
|
||||||
|
KEY_NONE, // 29
|
||||||
|
KEY_NONE, // 30
|
||||||
|
KEY_NONE, // 31
|
||||||
|
KEY_SPACE, // 32
|
||||||
|
KEY_NONE, // 33
|
||||||
|
KEY_NONE, // 34
|
||||||
|
KEY_NONE, // 35
|
||||||
|
KEY_NONE, // 36
|
||||||
|
KEY_NONE, // 37
|
||||||
|
KEY_NONE, // 38
|
||||||
|
KEY_APOSTROPHE, // 39
|
||||||
|
KEY_NONE, // 40
|
||||||
|
KEY_NONE, // 41
|
||||||
|
KEY_NONE, // 42
|
||||||
|
KEY_NONE, // 43
|
||||||
|
KEY_COMMA, // 44
|
||||||
|
KEY_MINUS, // 45
|
||||||
|
KEY_PERIOD, // 46
|
||||||
|
KEY_SLASH, // 47
|
||||||
|
KEY_0, // 48
|
||||||
|
KEY_1, // 49
|
||||||
|
KEY_2, // 50
|
||||||
|
KEY_3, // 51
|
||||||
|
KEY_4, // 52
|
||||||
|
KEY_5, // 53
|
||||||
|
KEY_6, // 54
|
||||||
|
KEY_7, // 55
|
||||||
|
KEY_8, // 56
|
||||||
|
KEY_9, // 57
|
||||||
|
KEY_NONE, // 58
|
||||||
|
KEY_SEMICOLON, // 59
|
||||||
|
KEY_NONE, // 60
|
||||||
|
KEY_EQUAL, // 61
|
||||||
|
KEY_NONE, // 62
|
||||||
|
KEY_NONE, // 63
|
||||||
|
KEY_NONE, // 64
|
||||||
|
KEY_A, // 65
|
||||||
|
KEY_B, // 66
|
||||||
|
KEY_C, // 67
|
||||||
|
KEY_D, // 68
|
||||||
|
KEY_E, // 69
|
||||||
|
KEY_F, // 70
|
||||||
|
KEY_G, // 71
|
||||||
|
KEY_H, // 72
|
||||||
|
KEY_I, // 73
|
||||||
|
KEY_J, // 74
|
||||||
|
KEY_K, // 75
|
||||||
|
KEY_L, // 76
|
||||||
|
KEY_M, // 77
|
||||||
|
KEY_N, // 78
|
||||||
|
KEY_O, // 79
|
||||||
|
KEY_P, // 80
|
||||||
|
KEY_Q, // 81
|
||||||
|
KEY_R, // 82
|
||||||
|
KEY_S, // 83
|
||||||
|
KEY_T, // 84
|
||||||
|
KEY_U, // 85
|
||||||
|
KEY_V, // 86
|
||||||
|
KEY_W, // 87
|
||||||
|
KEY_X, // 88
|
||||||
|
KEY_Y, // 89
|
||||||
|
KEY_Z, // 90
|
||||||
|
KEY_LEFT_BRACKET, // 91
|
||||||
|
KEY_BACKSLASH, // 92
|
||||||
|
KEY_RIGHT_BRACKET, // 93
|
||||||
|
KEY_NONE, // 94
|
||||||
|
KEY_NONE, // 95
|
||||||
|
KEY_GRAVE_ACCENT, // 96
|
||||||
|
KEY_NONE, // 97
|
||||||
|
KEY_NONE, // 98
|
||||||
|
KEY_NONE, // 99
|
||||||
|
KEY_NONE, // 100
|
||||||
|
KEY_NONE, // 101
|
||||||
|
KEY_NONE, // 102
|
||||||
|
KEY_NONE, // 103
|
||||||
|
KEY_NONE, // 104
|
||||||
|
KEY_NONE, // 105
|
||||||
|
KEY_NONE, // 106
|
||||||
|
KEY_NONE, // 107
|
||||||
|
KEY_NONE, // 108
|
||||||
|
KEY_NONE, // 109
|
||||||
|
KEY_NONE, // 110
|
||||||
|
KEY_NONE, // 111
|
||||||
|
KEY_NONE, // 112
|
||||||
|
KEY_NONE, // 113
|
||||||
|
KEY_NONE, // 114
|
||||||
|
KEY_NONE, // 115
|
||||||
|
KEY_NONE, // 116
|
||||||
|
KEY_NONE, // 117
|
||||||
|
KEY_NONE, // 118
|
||||||
|
KEY_NONE, // 119
|
||||||
|
KEY_NONE, // 120
|
||||||
|
KEY_NONE, // 121
|
||||||
|
KEY_NONE, // 122
|
||||||
|
KEY_NONE, // 123
|
||||||
|
KEY_NONE, // 124
|
||||||
|
KEY_NONE, // 125
|
||||||
|
KEY_NONE, // 126
|
||||||
|
KEY_NONE, // 127
|
||||||
|
KEY_NONE, // 128
|
||||||
|
KEY_NONE, // 129
|
||||||
|
KEY_NONE, // 130
|
||||||
|
KEY_NONE, // 131
|
||||||
|
KEY_NONE, // 132
|
||||||
|
KEY_NONE, // 133
|
||||||
|
KEY_NONE, // 134
|
||||||
|
KEY_NONE, // 135
|
||||||
|
KEY_NONE, // 136
|
||||||
|
KEY_NONE, // 137
|
||||||
|
KEY_NONE, // 138
|
||||||
|
KEY_NONE, // 139
|
||||||
|
KEY_NONE, // 140
|
||||||
|
KEY_NONE, // 141
|
||||||
|
KEY_NONE, // 142
|
||||||
|
KEY_NONE, // 143
|
||||||
|
KEY_NONE, // 144
|
||||||
|
KEY_NONE, // 145
|
||||||
|
KEY_NONE, // 146
|
||||||
|
KEY_NONE, // 147
|
||||||
|
KEY_NONE, // 148
|
||||||
|
KEY_NONE, // 149
|
||||||
|
KEY_NONE, // 150
|
||||||
|
KEY_NONE, // 151
|
||||||
|
KEY_NONE, // 152
|
||||||
|
KEY_NONE, // 153
|
||||||
|
KEY_NONE, // 154
|
||||||
|
KEY_NONE, // 155
|
||||||
|
KEY_NONE, // 156
|
||||||
|
KEY_NONE, // 157
|
||||||
|
KEY_NONE, // 158
|
||||||
|
KEY_NONE, // 159
|
||||||
|
KEY_NONE, // 160
|
||||||
|
KEY_WORLD_1, // 161
|
||||||
|
KEY_WORLD_2, // 162
|
||||||
|
KEY_NONE, // 163
|
||||||
|
KEY_NONE, // 164
|
||||||
|
KEY_NONE, // 165
|
||||||
|
KEY_NONE, // 166
|
||||||
|
KEY_NONE, // 167
|
||||||
|
KEY_NONE, // 168
|
||||||
|
KEY_NONE, // 169
|
||||||
|
KEY_NONE, // 170
|
||||||
|
KEY_NONE, // 171
|
||||||
|
KEY_NONE, // 172
|
||||||
|
KEY_NONE, // 173
|
||||||
|
KEY_NONE, // 174
|
||||||
|
KEY_NONE, // 175
|
||||||
|
KEY_NONE, // 176
|
||||||
|
KEY_NONE, // 177
|
||||||
|
KEY_NONE, // 178
|
||||||
|
KEY_NONE, // 179
|
||||||
|
KEY_NONE, // 180
|
||||||
|
KEY_NONE, // 181
|
||||||
|
KEY_NONE, // 182
|
||||||
|
KEY_NONE, // 183
|
||||||
|
KEY_NONE, // 184
|
||||||
|
KEY_NONE, // 185
|
||||||
|
KEY_NONE, // 186
|
||||||
|
KEY_NONE, // 187
|
||||||
|
KEY_NONE, // 188
|
||||||
|
KEY_NONE, // 189
|
||||||
|
KEY_NONE, // 190
|
||||||
|
KEY_NONE, // 191
|
||||||
|
KEY_NONE, // 192
|
||||||
|
KEY_NONE, // 193
|
||||||
|
KEY_NONE, // 194
|
||||||
|
KEY_NONE, // 195
|
||||||
|
KEY_NONE, // 196
|
||||||
|
KEY_NONE, // 197
|
||||||
|
KEY_NONE, // 198
|
||||||
|
KEY_NONE, // 199
|
||||||
|
KEY_NONE, // 200
|
||||||
|
KEY_NONE, // 201
|
||||||
|
KEY_NONE, // 202
|
||||||
|
KEY_NONE, // 203
|
||||||
|
KEY_NONE, // 204
|
||||||
|
KEY_NONE, // 205
|
||||||
|
KEY_NONE, // 206
|
||||||
|
KEY_NONE, // 207
|
||||||
|
KEY_NONE, // 208
|
||||||
|
KEY_NONE, // 209
|
||||||
|
KEY_NONE, // 210
|
||||||
|
KEY_NONE, // 211
|
||||||
|
KEY_NONE, // 212
|
||||||
|
KEY_NONE, // 213
|
||||||
|
KEY_NONE, // 214
|
||||||
|
KEY_NONE, // 215
|
||||||
|
KEY_NONE, // 216
|
||||||
|
KEY_NONE, // 217
|
||||||
|
KEY_NONE, // 218
|
||||||
|
KEY_NONE, // 219
|
||||||
|
KEY_NONE, // 220
|
||||||
|
KEY_NONE, // 221
|
||||||
|
KEY_NONE, // 222
|
||||||
|
KEY_NONE, // 223
|
||||||
|
KEY_NONE, // 224
|
||||||
|
KEY_NONE, // 225
|
||||||
|
KEY_NONE, // 226
|
||||||
|
KEY_NONE, // 227
|
||||||
|
KEY_NONE, // 228
|
||||||
|
KEY_NONE, // 229
|
||||||
|
KEY_NONE, // 230
|
||||||
|
KEY_NONE, // 231
|
||||||
|
KEY_NONE, // 232
|
||||||
|
KEY_NONE, // 233
|
||||||
|
KEY_NONE, // 234
|
||||||
|
KEY_NONE, // 235
|
||||||
|
KEY_NONE, // 236
|
||||||
|
KEY_NONE, // 237
|
||||||
|
KEY_NONE, // 238
|
||||||
|
KEY_NONE, // 239
|
||||||
|
KEY_NONE, // 240
|
||||||
|
KEY_NONE, // 241
|
||||||
|
KEY_NONE, // 242
|
||||||
|
KEY_NONE, // 243
|
||||||
|
KEY_NONE, // 244
|
||||||
|
KEY_NONE, // 245
|
||||||
|
KEY_NONE, // 246
|
||||||
|
KEY_NONE, // 247
|
||||||
|
KEY_NONE, // 248
|
||||||
|
KEY_NONE, // 249
|
||||||
|
KEY_NONE, // 250
|
||||||
|
KEY_NONE, // 251
|
||||||
|
KEY_NONE, // 252
|
||||||
|
KEY_NONE, // 253
|
||||||
|
KEY_NONE, // 254
|
||||||
|
KEY_NONE, // 255
|
||||||
|
KEY_ESCAPE, // 256
|
||||||
|
KEY_ENTER, // 257
|
||||||
|
KEY_TAB, // 258
|
||||||
|
KEY_BACKSPACE, // 259
|
||||||
|
KEY_INSERT, // 260
|
||||||
|
KEY_DELETE, // 261
|
||||||
|
KEY_RIGHT, // 262
|
||||||
|
KEY_LEFT, // 263
|
||||||
|
KEY_DOWN, // 264
|
||||||
|
KEY_UP, // 265
|
||||||
|
KEY_PAGE_UP, // 266
|
||||||
|
KEY_PAGE_DOWN, // 267
|
||||||
|
KEY_HOME, // 268
|
||||||
|
KEY_END, // 269
|
||||||
|
KEY_NONE, // 270
|
||||||
|
KEY_NONE, // 271
|
||||||
|
KEY_NONE, // 272
|
||||||
|
KEY_NONE, // 273
|
||||||
|
KEY_NONE, // 274
|
||||||
|
KEY_NONE, // 275
|
||||||
|
KEY_NONE, // 276
|
||||||
|
KEY_NONE, // 277
|
||||||
|
KEY_NONE, // 278
|
||||||
|
KEY_NONE, // 279
|
||||||
|
KEY_CAPS_LOCK, // 280
|
||||||
|
KEY_SCROLL_LOCK, // 281
|
||||||
|
KEY_NUM_LOCK, // 282
|
||||||
|
KEY_PRINT_SCREEN, // 283
|
||||||
|
KEY_PAUSE, // 284
|
||||||
|
KEY_NONE, // 285
|
||||||
|
KEY_NONE, // 286
|
||||||
|
KEY_NONE, // 287
|
||||||
|
KEY_NONE, // 288
|
||||||
|
KEY_NONE, // 289
|
||||||
|
KEY_F1, // 290
|
||||||
|
KEY_F2, // 291
|
||||||
|
KEY_F3, // 292
|
||||||
|
KEY_F4, // 293
|
||||||
|
KEY_F5, // 294
|
||||||
|
KEY_F6, // 295
|
||||||
|
KEY_F7, // 296
|
||||||
|
KEY_F8, // 297
|
||||||
|
KEY_F9, // 298
|
||||||
|
KEY_F10, // 299
|
||||||
|
KEY_F11, // 300
|
||||||
|
KEY_F12, // 301
|
||||||
|
KEY_F13, // 302
|
||||||
|
KEY_F14, // 303
|
||||||
|
KEY_F15, // 304
|
||||||
|
KEY_F16, // 305
|
||||||
|
KEY_F17, // 306
|
||||||
|
KEY_F18, // 307
|
||||||
|
KEY_F19, // 308
|
||||||
|
KEY_F20, // 309
|
||||||
|
KEY_F21, // 310
|
||||||
|
KEY_F22, // 311
|
||||||
|
KEY_F23, // 312
|
||||||
|
KEY_F24, // 313
|
||||||
|
KEY_F25, // 314
|
||||||
|
KEY_NONE, // 315
|
||||||
|
KEY_NONE, // 316
|
||||||
|
KEY_NONE, // 317
|
||||||
|
KEY_NONE, // 318
|
||||||
|
KEY_NONE, // 319
|
||||||
|
KEY_KP_0, // 320
|
||||||
|
KEY_KP_1, // 321
|
||||||
|
KEY_KP_2, // 322
|
||||||
|
KEY_KP_3, // 323
|
||||||
|
KEY_KP_4, // 324
|
||||||
|
KEY_KP_5, // 325
|
||||||
|
KEY_KP_6, // 326
|
||||||
|
KEY_KP_7, // 327
|
||||||
|
KEY_KP_8, // 328
|
||||||
|
KEY_KP_9, // 329
|
||||||
|
KEY_KP_DECIMAL, // 330
|
||||||
|
KEY_KP_DIVIDE, // 331
|
||||||
|
KEY_KP_MULTIPLY, // 332
|
||||||
|
KEY_KP_SUBTRACT, // 333
|
||||||
|
KEY_KP_ADD, // 334
|
||||||
|
KEY_KP_ENTER, // 335
|
||||||
|
KEY_KP_EQUAL, // 336
|
||||||
|
KEY_NONE, // 337
|
||||||
|
KEY_NONE, // 338
|
||||||
|
KEY_NONE, // 339
|
||||||
|
KEY_LEFT_SHIFT, // 340
|
||||||
|
KEY_LEFT_CONTROL, // 341
|
||||||
|
KEY_LEFT_ALT, // 342
|
||||||
|
KEY_LEFT_SUPER, // 343
|
||||||
|
KEY_RIGHT_SHIFT, // 344
|
||||||
|
KEY_RIGHT_CONTROL, // 345
|
||||||
|
KEY_RIGHT_ALT, // 346
|
||||||
|
KEY_RIGHT_SUPER, // 347
|
||||||
|
KEY_MENU, // 348
|
||||||
|
};
|
||||||
|
|
||||||
|
const Control Input::s_glfw_mouse_map[] = {
|
||||||
|
MOUSE_1, // 0
|
||||||
|
MOUSE_2, // 1
|
||||||
|
MOUSE_3, // 2
|
||||||
|
MOUSE_4, // 3
|
||||||
|
MOUSE_5, // 4
|
||||||
|
MOUSE_6, // 5
|
||||||
|
};
|
||||||
|
|
||||||
|
const char* const Input::s_control_names[] = {
|
||||||
|
"KEY_NONE",
|
||||||
|
"KEY_SPACE",
|
||||||
|
"KEY_APOSTROPHE",
|
||||||
|
"KEY_COMMA",
|
||||||
|
"KEY_MINUS",
|
||||||
|
"KEY_PERIOD",
|
||||||
|
"KEY_SLASH",
|
||||||
|
"KEY_0",
|
||||||
|
"KEY_1",
|
||||||
|
"KEY_2",
|
||||||
|
"KEY_3",
|
||||||
|
"KEY_4",
|
||||||
|
"KEY_5",
|
||||||
|
"KEY_6",
|
||||||
|
"KEY_7",
|
||||||
|
"KEY_8",
|
||||||
|
"KEY_9",
|
||||||
|
"KEY_SEMICOLON",
|
||||||
|
"KEY_EQUAL",
|
||||||
|
"KEY_A",
|
||||||
|
"KEY_B",
|
||||||
|
"KEY_C",
|
||||||
|
"KEY_D",
|
||||||
|
"KEY_E",
|
||||||
|
"KEY_F",
|
||||||
|
"KEY_G",
|
||||||
|
"KEY_H",
|
||||||
|
"KEY_I",
|
||||||
|
"KEY_J",
|
||||||
|
"KEY_K",
|
||||||
|
"KEY_L",
|
||||||
|
"KEY_M",
|
||||||
|
"KEY_N",
|
||||||
|
"KEY_O",
|
||||||
|
"KEY_P",
|
||||||
|
"KEY_Q",
|
||||||
|
"KEY_R",
|
||||||
|
"KEY_S",
|
||||||
|
"KEY_T",
|
||||||
|
"KEY_U",
|
||||||
|
"KEY_V",
|
||||||
|
"KEY_W",
|
||||||
|
"KEY_X",
|
||||||
|
"KEY_Y",
|
||||||
|
"KEY_Z",
|
||||||
|
"KEY_LEFT_BRACKET",
|
||||||
|
"KEY_BACKSLASH",
|
||||||
|
"KEY_RIGHT_BRACKET",
|
||||||
|
"KEY_GRAVE_ACCENT",
|
||||||
|
"KEY_WORLD_1",
|
||||||
|
"KEY_WORLD_2",
|
||||||
|
"KEY_ESCAPE",
|
||||||
|
"KEY_ENTER",
|
||||||
|
"KEY_TAB",
|
||||||
|
"KEY_BACKSPACE",
|
||||||
|
"KEY_INSERT",
|
||||||
|
"KEY_DELETE",
|
||||||
|
"KEY_RIGHT",
|
||||||
|
"KEY_LEFT",
|
||||||
|
"KEY_DOWN",
|
||||||
|
"KEY_UP",
|
||||||
|
"KEY_PAGE_UP",
|
||||||
|
"KEY_PAGE_DOWN",
|
||||||
|
"KEY_HOME",
|
||||||
|
"KEY_END",
|
||||||
|
"KEY_CAPS_LOCK",
|
||||||
|
"KEY_SCROLL_LOCK",
|
||||||
|
"KEY_NUM_LOCK",
|
||||||
|
"KEY_PRINT_SCREEN",
|
||||||
|
"KEY_PAUSE",
|
||||||
|
"KEY_F1",
|
||||||
|
"KEY_F2",
|
||||||
|
"KEY_F3",
|
||||||
|
"KEY_F4",
|
||||||
|
"KEY_F5",
|
||||||
|
"KEY_F6",
|
||||||
|
"KEY_F7",
|
||||||
|
"KEY_F8",
|
||||||
|
"KEY_F9",
|
||||||
|
"KEY_F10",
|
||||||
|
"KEY_F11",
|
||||||
|
"KEY_F12",
|
||||||
|
"KEY_F13",
|
||||||
|
"KEY_F14",
|
||||||
|
"KEY_F15",
|
||||||
|
"KEY_F16",
|
||||||
|
"KEY_F17",
|
||||||
|
"KEY_F18",
|
||||||
|
"KEY_F19",
|
||||||
|
"KEY_F20",
|
||||||
|
"KEY_F21",
|
||||||
|
"KEY_F22",
|
||||||
|
"KEY_F23",
|
||||||
|
"KEY_F24",
|
||||||
|
"KEY_F25",
|
||||||
|
"KEY_KP_0",
|
||||||
|
"KEY_KP_1",
|
||||||
|
"KEY_KP_2",
|
||||||
|
"KEY_KP_3",
|
||||||
|
"KEY_KP_4",
|
||||||
|
"KEY_KP_5",
|
||||||
|
"KEY_KP_6",
|
||||||
|
"KEY_KP_7",
|
||||||
|
"KEY_KP_8",
|
||||||
|
"KEY_KP_9",
|
||||||
|
"KEY_KP_DECIMAL",
|
||||||
|
"KEY_KP_DIVIDE",
|
||||||
|
"KEY_KP_MULTIPLY",
|
||||||
|
"KEY_KP_SUBTRACT",
|
||||||
|
"KEY_KP_ADD",
|
||||||
|
"KEY_KP_ENTER",
|
||||||
|
"KEY_KP_EQUAL",
|
||||||
|
"KEY_LEFT_SHIFT",
|
||||||
|
"KEY_LEFT_CONTROL",
|
||||||
|
"KEY_LEFT_ALT",
|
||||||
|
"KEY_LEFT_SUPER",
|
||||||
|
"KEY_RIGHT_SHIFT",
|
||||||
|
"KEY_RIGHT_CONTROL",
|
||||||
|
"KEY_RIGHT_ALT",
|
||||||
|
"KEY_RIGHT_SUPER",
|
||||||
|
"KEY_MENU",
|
||||||
|
"MOUSE_1",
|
||||||
|
"MOUSE_2",
|
||||||
|
"MOUSE_3",
|
||||||
|
"MOUSE_4",
|
||||||
|
"MOUSE_5",
|
||||||
|
"MOUSE_6",
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<ControlEvent> Input::s_control_events;
|
||||||
|
LuaFunction Input::s_control_cb;
|
||||||
|
|
||||||
|
Control Input::ControlFromGlfwKey(int key) {
|
||||||
|
return s_glfw_key_map[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
Control Input::ControlFromGlfwMouseButton(int button) {
|
||||||
|
return s_glfw_mouse_map[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::AddControlEvent(Control control, bool press) {
|
||||||
|
s_control_events.push_back({ control, press });
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::RegisterLuaFunctions() {
|
||||||
|
LuaAPI::RegisterFunction("i_setcontrolcallback", [](LuaFunction callback) {
|
||||||
|
s_control_cb = callback;
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::ProcessEvents() {
|
||||||
|
if (s_control_cb) {
|
||||||
|
for (const auto& event : s_control_events) {
|
||||||
|
LuaAPI::Call(s_control_cb, s_control_names[event.control], event.press);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s_control_events.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::Shutdown() {
|
||||||
|
s_control_cb = LuaFunction();
|
||||||
|
s_control_events.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
168
src/tsr/input.hpp
Normal file
168
src/tsr/input.hpp
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
#include "luaapi.hpp"
|
||||||
|
|
||||||
|
namespace TSR {
|
||||||
|
|
||||||
|
enum Control {
|
||||||
|
KEY_NONE,
|
||||||
|
KEY_SPACE,
|
||||||
|
KEY_APOSTROPHE,
|
||||||
|
KEY_COMMA,
|
||||||
|
KEY_MINUS,
|
||||||
|
KEY_PERIOD,
|
||||||
|
KEY_SLASH,
|
||||||
|
KEY_0,
|
||||||
|
KEY_1,
|
||||||
|
KEY_2,
|
||||||
|
KEY_3,
|
||||||
|
KEY_4,
|
||||||
|
KEY_5,
|
||||||
|
KEY_6,
|
||||||
|
KEY_7,
|
||||||
|
KEY_8,
|
||||||
|
KEY_9,
|
||||||
|
KEY_SEMICOLON,
|
||||||
|
KEY_EQUAL,
|
||||||
|
KEY_A,
|
||||||
|
KEY_B,
|
||||||
|
KEY_C,
|
||||||
|
KEY_D,
|
||||||
|
KEY_E,
|
||||||
|
KEY_F,
|
||||||
|
KEY_G,
|
||||||
|
KEY_H,
|
||||||
|
KEY_I,
|
||||||
|
KEY_J,
|
||||||
|
KEY_K,
|
||||||
|
KEY_L,
|
||||||
|
KEY_M,
|
||||||
|
KEY_N,
|
||||||
|
KEY_O,
|
||||||
|
KEY_P,
|
||||||
|
KEY_Q,
|
||||||
|
KEY_R,
|
||||||
|
KEY_S,
|
||||||
|
KEY_T,
|
||||||
|
KEY_U,
|
||||||
|
KEY_V,
|
||||||
|
KEY_W,
|
||||||
|
KEY_X,
|
||||||
|
KEY_Y,
|
||||||
|
KEY_Z,
|
||||||
|
KEY_LEFT_BRACKET,
|
||||||
|
KEY_BACKSLASH,
|
||||||
|
KEY_RIGHT_BRACKET,
|
||||||
|
KEY_GRAVE_ACCENT,
|
||||||
|
KEY_WORLD_1,
|
||||||
|
KEY_WORLD_2,
|
||||||
|
KEY_ESCAPE,
|
||||||
|
KEY_ENTER,
|
||||||
|
KEY_TAB,
|
||||||
|
KEY_BACKSPACE,
|
||||||
|
KEY_INSERT,
|
||||||
|
KEY_DELETE,
|
||||||
|
KEY_RIGHT,
|
||||||
|
KEY_LEFT,
|
||||||
|
KEY_DOWN,
|
||||||
|
KEY_UP,
|
||||||
|
KEY_PAGE_UP,
|
||||||
|
KEY_PAGE_DOWN,
|
||||||
|
KEY_HOME,
|
||||||
|
KEY_END,
|
||||||
|
KEY_CAPS_LOCK,
|
||||||
|
KEY_SCROLL_LOCK,
|
||||||
|
KEY_NUM_LOCK,
|
||||||
|
KEY_PRINT_SCREEN,
|
||||||
|
KEY_PAUSE,
|
||||||
|
KEY_F1,
|
||||||
|
KEY_F2,
|
||||||
|
KEY_F3,
|
||||||
|
KEY_F4,
|
||||||
|
KEY_F5,
|
||||||
|
KEY_F6,
|
||||||
|
KEY_F7,
|
||||||
|
KEY_F8,
|
||||||
|
KEY_F9,
|
||||||
|
KEY_F10,
|
||||||
|
KEY_F11,
|
||||||
|
KEY_F12,
|
||||||
|
KEY_F13,
|
||||||
|
KEY_F14,
|
||||||
|
KEY_F15,
|
||||||
|
KEY_F16,
|
||||||
|
KEY_F17,
|
||||||
|
KEY_F18,
|
||||||
|
KEY_F19,
|
||||||
|
KEY_F20,
|
||||||
|
KEY_F21,
|
||||||
|
KEY_F22,
|
||||||
|
KEY_F23,
|
||||||
|
KEY_F24,
|
||||||
|
KEY_F25,
|
||||||
|
KEY_KP_0,
|
||||||
|
KEY_KP_1,
|
||||||
|
KEY_KP_2,
|
||||||
|
KEY_KP_3,
|
||||||
|
KEY_KP_4,
|
||||||
|
KEY_KP_5,
|
||||||
|
KEY_KP_6,
|
||||||
|
KEY_KP_7,
|
||||||
|
KEY_KP_8,
|
||||||
|
KEY_KP_9,
|
||||||
|
KEY_KP_DECIMAL,
|
||||||
|
KEY_KP_DIVIDE,
|
||||||
|
KEY_KP_MULTIPLY,
|
||||||
|
KEY_KP_SUBTRACT,
|
||||||
|
KEY_KP_ADD,
|
||||||
|
KEY_KP_ENTER,
|
||||||
|
KEY_KP_EQUAL,
|
||||||
|
KEY_LEFT_SHIFT,
|
||||||
|
KEY_LEFT_CONTROL,
|
||||||
|
KEY_LEFT_ALT,
|
||||||
|
KEY_LEFT_SUPER,
|
||||||
|
KEY_RIGHT_SHIFT,
|
||||||
|
KEY_RIGHT_CONTROL,
|
||||||
|
KEY_RIGHT_ALT,
|
||||||
|
KEY_RIGHT_SUPER,
|
||||||
|
KEY_MENU,
|
||||||
|
MOUSE_1,
|
||||||
|
MOUSE_2,
|
||||||
|
MOUSE_3,
|
||||||
|
MOUSE_4,
|
||||||
|
MOUSE_5,
|
||||||
|
MOUSE_6,
|
||||||
|
CONTROL_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ControlEvent {
|
||||||
|
Control control;
|
||||||
|
bool press;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Input {
|
||||||
|
|
||||||
|
static const Control s_glfw_key_map[];
|
||||||
|
static const Control s_glfw_mouse_map[];
|
||||||
|
static const char* const s_control_names[];
|
||||||
|
static std::vector<ControlEvent> s_control_events;
|
||||||
|
|
||||||
|
static LuaFunction s_control_cb;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static Control ControlFromGlfwKey(int key);
|
||||||
|
static Control ControlFromGlfwMouseButton(int button);
|
||||||
|
|
||||||
|
static void AddControlEvent(Control control, bool press);
|
||||||
|
|
||||||
|
static void RegisterLuaFunctions();
|
||||||
|
|
||||||
|
static void ProcessEvents();
|
||||||
|
|
||||||
|
static void Shutdown();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -20,6 +20,9 @@ void LuaAPI::Shutdown() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LuaAPI::Load(const std::string& name) {
|
void LuaAPI::Load(const std::string& name) {
|
||||||
|
if (s_loaded_scripts.contains(name))
|
||||||
|
return;
|
||||||
|
|
||||||
s_loaded_scripts.insert(name);
|
s_loaded_scripts.insert(name);
|
||||||
|
|
||||||
auto script = Filesystem::Read(name);
|
auto script = Filesystem::Read(name);
|
||||||
|
|||||||
@ -1,10 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#define SOL_CHECK_ARGUMENTS
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
namespace TSR {
|
namespace TSR {
|
||||||
|
|
||||||
|
using LuaReference = sol::reference;
|
||||||
|
using LuaFunction = sol::function;
|
||||||
|
|
||||||
class LuaAPI {
|
class LuaAPI {
|
||||||
|
|
||||||
static std::unique_ptr<sol::state> s_lua;
|
static std::unique_ptr<sol::state> s_lua;
|
||||||
@ -27,6 +32,18 @@ namespace TSR {
|
|||||||
func(std::forward<Args>(args)...);
|
func(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
static void Call(LuaReference& ref, Args&&... args) {
|
||||||
|
sol::protected_function func = ref;
|
||||||
|
auto res = func(std::forward<Args>(args)...);
|
||||||
|
|
||||||
|
if (!res.valid()) {
|
||||||
|
sol::error err = res;
|
||||||
|
throw std::runtime_error(err.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static sol::state& GetState() { return *s_lua; }
|
static sol::state& GetState() { return *s_lua; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -129,6 +129,8 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="src\tsr\entity.cpp" />
|
||||||
|
<ClCompile Include="src\tsr\input.cpp" />
|
||||||
<ClCompile Include="src\tsr\luaapi.cpp" />
|
<ClCompile Include="src\tsr\luaapi.cpp" />
|
||||||
<ClCompile Include="src\tsr\entity_cct.cpp" />
|
<ClCompile Include="src\tsr\entity_cct.cpp" />
|
||||||
<ClCompile Include="src\tsr\physics.cpp" />
|
<ClCompile Include="src\tsr\physics.cpp" />
|
||||||
@ -145,6 +147,7 @@
|
|||||||
<ClCompile Include="src\tsr\worldmap.cpp" />
|
<ClCompile Include="src\tsr\worldmap.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="src\tsr\input.hpp" />
|
||||||
<ClInclude Include="src\tsr\luaapi.hpp" />
|
<ClInclude Include="src\tsr\luaapi.hpp" />
|
||||||
<ClInclude Include="src\tsr\entity_cct.hpp" />
|
<ClInclude Include="src\tsr\entity_cct.hpp" />
|
||||||
<ClInclude Include="src\tsr\physics.hpp" />
|
<ClInclude Include="src\tsr\physics.hpp" />
|
||||||
|
|||||||
@ -57,6 +57,12 @@
|
|||||||
<ClCompile Include="src\tsr\luaapi.cpp">
|
<ClCompile Include="src\tsr\luaapi.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\tsr\input.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\tsr\entity.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="src\tsr\window.hpp">
|
<ClInclude Include="src\tsr\window.hpp">
|
||||||
@ -107,5 +113,8 @@
|
|||||||
<ClInclude Include="src\tsr\luaapi.hpp">
|
<ClInclude Include="src\tsr\luaapi.hpp">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\tsr\input.hpp">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
Loading…
x
Reference in New Issue
Block a user