77 lines
3.0 KiB
C++
77 lines
3.0 KiB
C++
#include "MonoGameObjectSystem.hpp"
|
|
|
|
namespace ougge::modules {
|
|
|
|
const std::string& MonoGameObjectSystem::getName() {
|
|
return ougge_type_name<MonoGameObjectSystem>();
|
|
}
|
|
|
|
MonoGameObjectSystem::MonoGameObjectSystem(Engine& engine, u32 max_game_objects) :
|
|
IEngineModule(engine),
|
|
gameObjectPool(max_game_objects)
|
|
{
|
|
engineManagedAssembly = mono.loadAssembly("Ougge.dll");
|
|
gameObjectClass = engineManagedAssembly->getClass("Ougge", "GameObject");
|
|
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
|
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
|
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
|
registerNativeCallbacks();
|
|
}
|
|
|
|
// sets implementations for extern functions from src-csharp/NativeFunctions.cs
|
|
void MonoGameObjectSystem::registerNativeCallbacks(){
|
|
static MonoGameObjectSystem* this_static = this;
|
|
if(this_static != this)
|
|
throw new UsefulException("creation of more than one instance of MonoGameObjectSystem is not allowed");
|
|
|
|
auto createGameObject_callback = static_cast<void(*)(u64*, u32*)> (
|
|
[](u64* id_out, u32* index_out) -> void {
|
|
std::cout<<"createGameObject_callback"<<std::endl;
|
|
this_static->createGameObjectInPool(id_out, index_out);
|
|
}
|
|
);
|
|
mono_add_internal_call("Ougge.NativeFunctions::createGameObject", (void*)createGameObject_callback);
|
|
|
|
auto freeGameObject_callback = static_cast<bool(*)(u32)>(
|
|
[](u32 index) -> bool {
|
|
std::cout<<"freeGameObject_callback"<<std::endl;
|
|
return this_static->gameObjectPool.erase(index);
|
|
}
|
|
);
|
|
mono_add_internal_call("Ougge.NativeFunctions::freeGameObject", (void*)freeGameObject_callback);
|
|
}
|
|
|
|
void MonoGameObjectSystem::beginFrame(){
|
|
for(auto pair : gameObjectPool){
|
|
|
|
auto obj = pair.second.getObjectHandle().getObject();
|
|
std::cout<<"updating obj: "<<obj<<std::endl;
|
|
gameObjectInvokeUpdate(obj, engine.deltaTime);
|
|
}
|
|
}
|
|
|
|
// is used in NativeFunctions.cs
|
|
game::GameObject& MonoGameObjectSystem::createGameObjectInPool(u64* id_out, u32* index_out){
|
|
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
|
*id_out = ++obj_id;
|
|
*index_out = pair.first;
|
|
game::GameObject& obj = pair.second;
|
|
return obj;
|
|
}
|
|
|
|
game::GameObject& MonoGameObjectSystem::createAndConstructGameObject(){
|
|
u64 obj_id;
|
|
u32 obj_index;
|
|
game::GameObject& obj = createGameObjectInPool(&obj_id, &obj_index);
|
|
gameObjectCtor(obj.getObjectHandle().getObject(), obj_id, obj_index);
|
|
return obj;
|
|
}
|
|
|
|
bool MonoGameObjectSystem::tryCreateComponent(game::GameObject& obj, const std::string& componentClassName){
|
|
Mono::String componentClassNameManaged = mono.createString(componentClassName);
|
|
Mono::Bool created = gameObjectTryCreateComponent(obj.getObjectHandle().getObject(), componentClassNameManaged);
|
|
return created.wide_bool;
|
|
}
|
|
|
|
}
|