This commit is contained in:
Antoine Pilote
2024-04-02 09:29:07 -04:00
33 changed files with 751 additions and 312 deletions

View File

@@ -2,9 +2,9 @@
namespace Nuake {
void Log(Coral::NativeString string)
void Log(Coral::String string)
{
Logger::Log(string.ToString(), ".net", VERBOSE);
Logger::Log(string, ".net", VERBOSE);
}
void EngineNetAPI::RegisterMethods()

View File

@@ -2,25 +2,45 @@
#include "src/Core/Input.h"
#include <Coral/NativeArray.hpp>
#include <Coral/Array.hpp>
namespace Nuake {
void ShowMouse(bool visible)
{
if (visible)
{
Input::ShowMouse();
}
else
{
Input::HideMouse();
}
}
bool IsKeyDown(int keyCode)
{
return Input::IsKeyDown(keyCode);
}
Coral::NativeArray<float> GetMousePosition()
bool IsKeyPressed(int keyCode)
{
return Input::IsKeyPressed(keyCode);
}
Coral::Array<float> GetMousePosition()
{
Vector2 mousePosition = Input::GetMousePosition();
return { mousePosition.x, mousePosition.y};
return Coral::Array<float>::New({ mousePosition.x, mousePosition.y });
}
void InputNetAPI::RegisterMethods()
{
RegisterMethod("Input.ShowMouseIcall", &ShowMouse);
RegisterMethod("Input.IsKeyDownIcall", &IsKeyDown);
RegisterMethod("Input.IsKeyPressedIcall", &IsKeyPressed);
RegisterMethod("Input.GetMousePositionIcall", &GetMousePosition);
}

View File

@@ -2,7 +2,7 @@
#include "src/Core/Core.h"
#include "src/Core/Logger.h"
#include <Coral/NativeString.hpp>
#include <Coral/String.hpp>
namespace Nuake {

View File

@@ -21,23 +21,40 @@
#include "src/Scene/Components/QuakeMap.h"
#include "src/Physics/PhysicsManager.h"
#include "src/Scripting/ScriptingEngineNet.h"
#include <Coral/NativeArray.hpp>
#include <Coral/Array.hpp>
namespace Nuake {
uint32_t GetEntity(Coral::NativeString entityName)
uint32_t GetEntity(Coral::String entityName)
{
auto scene = Engine::GetCurrentScene();
std::string entityNameString = entityName.ToString();
if (!scene->EntityExists(entityNameString))
if (!scene->EntityExists(entityName))
{
return UINT32_MAX; // Error code: entity not found.
}
return scene->GetEntity(entityNameString).GetHandle();
return scene->GetEntity(entityName).GetHandle();
}
Coral::ManagedObject GetEntityScript(Coral::String entityName)
{
auto scene = Engine::GetCurrentScene();
if (!scene->EntityExists(entityName))
{
return Coral::ManagedObject(); // Error code: entity not found.
}
Entity entity = scene->GetEntity(entityName);
auto& scriptingEngine = ScriptingEngineNet::Get();
if (scriptingEngine.HasEntityScriptInstance(entity))
{
auto instance = scriptingEngine.GetEntityScript(entity);
return instance;
}
}
static enum ComponentTypes
@@ -112,6 +129,24 @@ namespace Nuake {
{
auto& component = entity.GetComponent<TransformComponent>();
component.SetLocalPosition({ x, y, z });
if (entity.HasComponent<CharacterControllerComponent>())
{
PhysicsManager::Get().SetCharacterControllerPosition(entity, { x, y, z });
}
}
}
Coral::Array<float> TransformGetGlobalPosition(int entityId)
{
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
if (entity.IsValid() && entity.HasComponent<TransformComponent>())
{
auto& component = entity.GetComponent<TransformComponent>();
const auto& globalPosition = component.GetGlobalPosition();
Coral::Array<float> result = Coral::Array<float>::New({ globalPosition.x, globalPosition.y, globalPosition.z });
return result;
}
}
@@ -127,7 +162,7 @@ namespace Nuake {
}
}
Coral::NativeArray<float> CameraGetDirection(int entityId)
Coral::Array<float> CameraGetDirection(int entityId)
{
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
@@ -135,7 +170,7 @@ namespace Nuake {
{
auto& component = entity.GetComponent<CameraComponent>();
const Vector3 camDirection = component.CameraInstance->GetDirection();
return { camDirection.x, camDirection.y, camDirection.z };
return Coral::Array<float>::New({ camDirection.x, camDirection.y, camDirection.z });
}
}
@@ -169,19 +204,50 @@ namespace Nuake {
return false;
}
void Play(int entityId, Coral::String animation)
{
Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get());
if (entity.IsValid() && entity.HasComponent<SkinnedModelComponent>())
{
auto& skinnedModel = entity.GetComponent<SkinnedModelComponent>();
if (skinnedModel.ModelResource)
{
auto& model = skinnedModel.ModelResource;
// Find animation from name
int animIndex = 0;
for (const auto& anim : model->GetAnimations())
{
if (anim->GetName() == animation)
{
model->PlayAnimation(animIndex);
}
animIndex++;
}
}
}
}
void Nuake::SceneNetAPI::RegisterMethods()
{
RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent);
RegisterMethod("Scene.GetEntityIcall", &GetEntity);
RegisterMethod("Scene.GetEntityScriptIcall", &GetEntityScript);
// Components
RegisterMethod("TransformComponent.SetPositionIcall", &TransformSetPosition);
RegisterMethod("TransformComponent.GetGlobalPositionIcall", &TransformGetGlobalPosition);
RegisterMethod("TransformComponent.RotateIcall", &TransformRotate);
RegisterMethod("CameraComponent.GetDirectionIcall", &CameraGetDirection);
RegisterMethod("CharacterControllerComponent.MoveAndSlideIcall", &MoveAndSlide);
RegisterMethod("CharacterControllerComponent.IsOnGroundIcall", &IsOnGround);
RegisterMethod("SkinnedModelComponent.PlayIcall", &Play);
}
}

View File

@@ -3,6 +3,7 @@
#include "src/Core/Logger.h"
#include "src/Core/FileSystem.h"
#include "src/Core/OS.h"
#include "src/Threading/JobSystem.h"
#include "src/Resource/Project.h"
#include "src/Scene/Components/NetScriptComponent.h"
@@ -12,7 +13,7 @@
#include <Coral/HostInstance.hpp>
#include <Coral/GC.hpp>
#include <Coral/NativeArray.hpp>
#include <Coral/Array.hpp>
#include <Coral/Attribute.hpp>
@@ -136,7 +137,7 @@ namespace Nuake
}
const std::string sanitizedProjectName = String::Sanitize(project->Name);
const std::string assemblyPath = "/bin/Debug/net7.0/" + sanitizedProjectName + ".dll";
const std::string assemblyPath = "/bin/Debug/net8.0/" + sanitizedProjectName + ".dll";
if (!FileSystem::FileExists(assemblyPath))
{
@@ -149,13 +150,15 @@ namespace Nuake
for (auto& type : m_GameAssembly.GetTypes())
{
Logger::Log(std::string("Detected type: ") + std::string(type->GetName()), ".net");
Logger::Log(std::string("Detected base type: ") + std::string(type->GetBaseType().GetName()), ".net");
Logger::Log(std::string("Detected type: ") + std::string(type->GetFullName()), ".net");
Logger::Log(std::string("Detected base type: ") + std::string(type->GetBaseType().GetFullName()), ".net");
const std::string baseTypeName = std::string(type->GetBaseType().GetName());
if (baseTypeName == "Entity")
const std::string baseTypeName = std::string(type->GetBaseType().GetFullName());
if (baseTypeName == "Nuake.Net.Entity")
{
m_GameEntityTypes[std::string(type->GetName())] = type; // We have found an entity script.
auto typeSplits = String::Split(type->GetFullName(), '.');
std::string shortenedTypeName = typeSplits[typeSplits.size() - 1];
m_GameEntityTypes[shortenedTypeName] = type; // We have found an entity script.
}
}
}
@@ -222,7 +225,6 @@ namespace Nuake
size_t classNameLength = semiColonPos - classNameStartIndex;
const std::string className = fileContent.substr(classNameStartIndex, classNameLength);
if(m_GameEntityTypes.find(className) == m_GameEntityTypes.end())
{
// The class name parsed in the file was not found in the game's DLL.
@@ -246,8 +248,10 @@ namespace Nuake
{
if (!HasEntityScriptInstance(entity))
{
std::string name = entity.GetComponent<NameComponent>().Name;
Logger::Log(name);
Logger::Log("Failed to get entity .Net script instance, doesn't exist", ".net", CRITICAL);
throw std::exception("Failed to get entity .Net script instance, doesn't exist");
return Coral::ManagedObject();
}
return m_EntityToManagedObjects[entity.GetID()];
@@ -294,10 +298,10 @@ namespace Nuake
const std::string cleanProjectName = String::Sanitize(projectName);
const std::string premakeScript = R"(
workspace ")" + cleanProjectName + R"("
configurations { "Debug", "Release" }
project ")" + cleanProjectName + R"("
language "C#"
dotnetframework "net7.0"
dotnetframework "net8.0"
kind "SharedLib"
clr "Unsafe"