Initial commit - Migration from cmake to gradle
This commit is contained in:
commit
279f292225
217 files changed
+14945
No files matched your search
@@ -0,0 +1,12 @@
|
||||
plugins { id 'cpp-library' }
|
||||
apply from: rootProject.file('gradle/common.gradle')
|
||||
|
||||
library {
|
||||
baseName = 'ScriptsModule'
|
||||
linkage = [Linkage.SHARED]
|
||||
targetMachines.add(machines.windows.x86_64)
|
||||
targetMachines.add(machines.linux.x86_64)
|
||||
source.setFrom('src/main')
|
||||
privateHeaders.setFrom('src/main')
|
||||
dependencies { implementation project(':CosmicEngine') }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "CScriptRegister.hpp"
|
||||
#include <vector>
|
||||
|
||||
std::vector<ScriptRegisterFunc>& CScriptRegister::getRegistrars() {
|
||||
static std::vector<ScriptRegisterFunc> registrars;
|
||||
return registrars;
|
||||
}
|
||||
|
||||
void CScriptRegister::addScriptRegistrar(ScriptRegisterFunc func) {
|
||||
getRegistrars().push_back(func);
|
||||
}
|
||||
|
||||
void CScriptRegister::registerAllScripts(CScriptRegistry* reg) {
|
||||
for (auto& registrar : getRegistrars()) {
|
||||
registrar(reg);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void registerAllScripts(CScriptRegistry* reg) {
|
||||
CScriptRegister::registerAllScripts(reg);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef CSCRIPTREGISTER_HPP
|
||||
#define CSCRIPTREGISTER_HPP
|
||||
|
||||
// Forward declarations des types de l'Engine
|
||||
#include "Engine/Core/Systems/Scripts/CScriptRegistry.hpp"
|
||||
#include <functional>
|
||||
namespace CosmicEngine {
|
||||
class CAbstractScript;
|
||||
class CEntity;
|
||||
}
|
||||
|
||||
// Définition des macros d'exportation
|
||||
#ifdef _WIN32
|
||||
#ifdef SCRIPT_MODULE_EXPORTS
|
||||
#define SCRIPT_API __declspec(dllexport)
|
||||
#else
|
||||
#define SCRIPT_API __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define SCRIPT_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
// Type de fonction d'enregistrement
|
||||
using ScriptRegisterFunc = std::function<void(CScriptRegistry* reg)>;
|
||||
|
||||
class SCRIPT_API CScriptRegister
|
||||
{
|
||||
public:
|
||||
// Point d'entrée que l'Engine appellera
|
||||
static void registerAllScripts(CScriptRegistry* reg);
|
||||
// Macro pour enregistrer un script automatiquement
|
||||
static void addScriptRegistrar(ScriptRegisterFunc func);
|
||||
|
||||
private:
|
||||
static std::vector<ScriptRegisterFunc>& getRegistrars();
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
SCRIPT_API void registerAllScripts(CScriptRegistry* reg);
|
||||
}
|
||||
|
||||
// Macro à utiliser dans chaque script .cpp
|
||||
#define REGISTER_SCRIPT(ClassName) \
|
||||
namespace { \
|
||||
struct ClassName##_AutoReg { \
|
||||
ClassName##_AutoReg() { \
|
||||
CScriptRegister::addScriptRegistrar([](CScriptRegistry* reg) { \
|
||||
reg->registerScript<ClassName>(#ClassName); \
|
||||
}); \
|
||||
} \
|
||||
}; \
|
||||
static ClassName##_AutoReg className##_autoReg; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "CExampleScript.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "Scripts/CScriptRegister.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include "Engine/Core/Component/Input/CInputManager.hpp"
|
||||
|
||||
#include "Engine/Core/Component/Geometry/CTransform.hpp"
|
||||
|
||||
void CExampleScript::start()
|
||||
{
|
||||
std::cout << "Hello example script" << std::endl;
|
||||
}
|
||||
|
||||
void CExampleScript::onCreate()
|
||||
{
|
||||
using namespace CosmicCore;
|
||||
auto& reg = getEntity().getRegistry().registry;
|
||||
|
||||
// vérifie que l'entité a un CInputComponent
|
||||
if (!reg.all_of<CInputManager>(getEntity().getHandle()))
|
||||
reg.emplace<CInputManager>(getEntity().getHandle(), getEntity());
|
||||
|
||||
auto& input = reg.get<CInputManager>(getEntity().getHandle());
|
||||
|
||||
// bind les actions depuis le script
|
||||
input.onHeld("MoveForward", [](CEntity& e) {
|
||||
e.getRegistry().registry.get<CTransform>(e.getHandle())
|
||||
.forward({0, 0, -0.05f});
|
||||
});
|
||||
|
||||
input.onHeld("MoveBackward", [](CEntity& e) {
|
||||
e.getRegistry().registry.get<CTransform>(e.getHandle())
|
||||
.forward({0, 0, 0.05f});
|
||||
});
|
||||
|
||||
input.addOnMouseMove([](CEntity& e, glm::vec2 delta) {
|
||||
auto& tr = e.getRegistry().registry.get<CTransform>(e.getHandle());
|
||||
float sensitivity = 0.3f;
|
||||
glm::quat yaw = glm::angleAxis(
|
||||
glm::radians(-delta.x * sensitivity), glm::vec3(0,1,0));
|
||||
glm::quat pitch = glm::angleAxis(
|
||||
glm::radians(-delta.y * sensitivity), glm::vec3(1,0,0));
|
||||
tr.setQuaternion(yaw * tr.getOrientation() * pitch);
|
||||
});
|
||||
}
|
||||
|
||||
void CExampleScript::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CExampleScript::destroy()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
nlohmann::json CExampleScript::to_json()
|
||||
{
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
CExampleScript::~CExampleScript(){
|
||||
|
||||
}
|
||||
|
||||
REGISTER_SCRIPT(CExampleScript);
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef CEXAMPLESCRIPT_HPP
|
||||
#define CEXAMPLESCRIPT_HPP
|
||||
#include "Engine/Core/Entity/CEntity.hpp"
|
||||
#include "Engine/Core/Component/Script/CAbstractScript.hpp"
|
||||
|
||||
class CExampleScript : public CosmicCore::CAbstractScript
|
||||
{
|
||||
public:
|
||||
CExampleScript(CosmicCore::CEntity& entity, std::string name): CosmicCore::CAbstractScript(entity, name){};
|
||||
void start() override;
|
||||
void update() override;
|
||||
void destroy() override;
|
||||
void onCreate() override;
|
||||
virtual nlohmann::json to_json() override;
|
||||
virtual ~CExampleScript();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "LaunchSceneTest.hpp"
|
||||
#include "Engine/Core/Entity/CEntity.hpp"
|
||||
#include "Engine/Core/Kernel/CKernel.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "Scripts/CScriptRegister.hpp"
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
#include "Engine/Core/Component/Input/CInputManager.hpp"
|
||||
#include "Engine/Core/Component/Geometry/CTransform.hpp"
|
||||
void LaunchSceneTest::start()
|
||||
{
|
||||
}
|
||||
|
||||
void LaunchSceneTest::onCreate()
|
||||
{
|
||||
using namespace CosmicCore;
|
||||
auto& reg = getEntity().getRegistry().registry;
|
||||
// vérifie que l'entité a un CInputComponent
|
||||
if (!reg.all_of<CInputManager>(getEntity().getHandle()))
|
||||
reg.emplace<CInputManager>(getEntity().getHandle(), getEntity());
|
||||
|
||||
auto& input = reg.get<CInputManager>(getEntity().getHandle());
|
||||
|
||||
// bind les actions depuis le script
|
||||
input.addOnGenericMouseLeftClick([](CEntity& entity){
|
||||
|
||||
auto& collider = entity.getRegistry().registry.get<CCollider2D>(entity.getHandle());
|
||||
|
||||
if(collider.shapeContains(CInputDispatcher::mousePosition()))
|
||||
{
|
||||
CosmicCore::CKernelBase::instance()->loadScene("assets/scenes/scene_test.json");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void LaunchSceneTest::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void LaunchSceneTest::destroy()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
nlohmann::json LaunchSceneTest::to_json()
|
||||
{
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
LaunchSceneTest::~LaunchSceneTest(){
|
||||
|
||||
}
|
||||
|
||||
REGISTER_SCRIPT(LaunchSceneTest);
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef LAUNCHSCENETEST_HPP
|
||||
#define LAUNCHSCENETEST_HPP
|
||||
#include "Engine/Core/Entity/CEntity.hpp"
|
||||
#include "Engine/Core/Component/Script/CAbstractScript.hpp"
|
||||
|
||||
class LaunchSceneTest : public CosmicCore::CAbstractScript
|
||||
{
|
||||
public:
|
||||
LaunchSceneTest(CosmicCore::CEntity& entity, std::string name): CosmicCore::CAbstractScript(entity, name){};
|
||||
void start() override;
|
||||
void update() override;
|
||||
void destroy() override;
|
||||
void onCreate() override;
|
||||
virtual nlohmann::json to_json() override;
|
||||
virtual ~LaunchSceneTest();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "QuitHandler.hpp"
|
||||
#include "Engine/Core/Component/Collider/CCollider2D.hpp"
|
||||
#include "Engine/Core/Entity/CEntity.hpp"
|
||||
#include "Engine/Core/Kernel/CKernel.hpp"
|
||||
#include "Engine/Core/Systems/Input/CInputDispatcher.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "Scripts/CScriptRegister.hpp"
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
#include "Engine/Core/Component/Input/CInputManager.hpp"
|
||||
#include "Engine/Core/Component/Geometry/CTransform.hpp"
|
||||
void QuitHandler::start()
|
||||
{
|
||||
}
|
||||
|
||||
void QuitHandler::onCreate()
|
||||
{
|
||||
using namespace CosmicCore;
|
||||
auto& reg = getEntity().getRegistry().registry;
|
||||
// vérifie que l'entité a un CInputComponent
|
||||
if (!reg.all_of<CInputManager>(getEntity().getHandle()))
|
||||
reg.emplace<CInputManager>(getEntity().getHandle(), getEntity());
|
||||
|
||||
auto& input = reg.get<CInputManager>(getEntity().getHandle());
|
||||
|
||||
// bind les actions depuis le script
|
||||
input.addOnGenericMouseLeftClick([](CEntity& entity){
|
||||
auto& collider = entity.getRegistry().registry.get<CCollider2D>(entity.getHandle());
|
||||
|
||||
if(collider.shapeContains(CInputDispatcher::mousePosition()))
|
||||
{
|
||||
kill(getpid(), SIGTERM);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void QuitHandler::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void QuitHandler::destroy()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
nlohmann::json QuitHandler::to_json()
|
||||
{
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
QuitHandler::~QuitHandler(){
|
||||
|
||||
}
|
||||
|
||||
REGISTER_SCRIPT(QuitHandler);
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef QUITHANDLER_HPP
|
||||
#define QUITHANDLER_HPP
|
||||
#include "Engine/Core/Entity/CEntity.hpp"
|
||||
#include "Engine/Core/Component/Script/CAbstractScript.hpp"
|
||||
|
||||
class QuitHandler : public CosmicCore::CAbstractScript
|
||||
{
|
||||
public:
|
||||
QuitHandler(CosmicCore::CEntity& entity, std::string name): CosmicCore::CAbstractScript(entity, name){};
|
||||
void start() override;
|
||||
void update() override;
|
||||
void destroy() override;
|
||||
void onCreate() override;
|
||||
virtual nlohmann::json to_json() override;
|
||||
virtual ~QuitHandler();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user