From e8dec2e2d38432b27914d343f6549ea124abf77b Mon Sep 17 00:00:00 2001 From: Atdunbg <979541498@qq.com> Date: Sun, 26 Oct 2025 13:12:04 +0800 Subject: [PATCH] add FindEntityByName function --- Editor/SandboxProject/Source/Camera.cs | 18 ++++-- Editor/SandboxProject/Source/Player.cs | 11 ++++ Editor/src/Editor/EditorLayer.cpp | 4 -- .../src/Editor/Panels/SceneHierachyPanel.cpp | 6 +- .../Sources/Hazel/InternalCalls.cs | 6 ++ .../Sources/Hazel/Scene/Entity.cs | 17 +++++- Hazel-ScriptCore/Sources/Hazel/Vector.cs | 7 +++ Hazel/src/Hazel/Core/Application.cpp | 1 - Hazel/src/Hazel/Scene/Scene.cpp | 14 +++++ Hazel/src/Hazel/Scene/Scene.h | 1 + Hazel/src/Hazel/Scripting/ScriptEngine.cpp | 6 ++ Hazel/src/Hazel/Scripting/ScriptEngine.h | 14 +++-- Hazel/src/Hazel/Scripting/ScriptGlue.cpp | 61 ++++++++++++++----- 13 files changed, 129 insertions(+), 37 deletions(-) diff --git a/Editor/SandboxProject/Source/Camera.cs b/Editor/SandboxProject/Source/Camera.cs index 6294f57..5c860a9 100644 --- a/Editor/SandboxProject/Source/Camera.cs +++ b/Editor/SandboxProject/Source/Camera.cs @@ -8,19 +8,25 @@ namespace Sandbox { // private TransformComponent m_Transform; // private RigidBody2DComponent m_Rigidbody; - public float MoveSpeed; -/* + public float MoveSpeed; + public float DistanceFromPlayer = 5; + + private Entity m_Player; + void OnCreate() { - Console.WriteLine($"Player.OnCreate - {ID}"); + Console.WriteLine($"Camera.OnCreate - {ID}"); - m_Transform = GetComponent(); - m_Rigidbody = GetComponent(); + m_Player = FindEntityByName("Player"); } - */ + void OnUpdate(float ts) { + if(m_Player != null) + { + Translation = new Vector3(m_Player.Translation.XY, DistanceFromPlayer); + } // Console.WriteLine($"Player.OnUpdate: {ts}"); float speed = MoveSpeed; diff --git a/Editor/SandboxProject/Source/Player.cs b/Editor/SandboxProject/Source/Player.cs index e81121e..cdef7c7 100644 --- a/Editor/SandboxProject/Source/Player.cs +++ b/Editor/SandboxProject/Source/Player.cs @@ -36,6 +36,17 @@ namespace Sandbox { else if(Input.IsKeyDown(KeyCode.D)) velocity.X = 1.0f; + Entity cameraEntity = FindEntityByName("Camera"); + if(cameraEntity != null) + { + Camera camera = cameraEntity.As(); + + if(Input.IsKeyDown(KeyCode.Q)) + camera.DistanceFromPlayer -= speed * 2.0f * ts; + else if(Input.IsKeyDown(KeyCode.E)) + camera.DistanceFromPlayer += speed * 2.0f * ts; + } + m_Rigidbody.ApplyLinearImpulseToCenter(velocity.XY * speed, true); } } diff --git a/Editor/src/Editor/EditorLayer.cpp b/Editor/src/Editor/EditorLayer.cpp index 34e7715..943f542 100644 --- a/Editor/src/Editor/EditorLayer.cpp +++ b/Editor/src/Editor/EditorLayer.cpp @@ -82,8 +82,6 @@ namespace Hazel m_CameraController.OnResize(m_ViewPortSize.x, m_ViewPortSize.y); m_EditorCamera.SetViewPortSize(m_ViewPortSize.x, m_ViewPortSize.y); - m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); - } // update camera @@ -501,7 +499,6 @@ namespace Hazel if (serializer.Deserialize(scenePath.string())) { m_EditorScene = newScene; - m_EditorScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); m_ActiveScene = m_EditorScene; @@ -514,7 +511,6 @@ namespace Hazel void EditorLayer::NewScene() { m_ActiveScene = CreateRef(); - m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); m_SceneHierachyPanel.SetContext(m_ActiveScene); m_EditorScenePath = std::filesystem::path(); diff --git a/Editor/src/Editor/Panels/SceneHierachyPanel.cpp b/Editor/src/Editor/Panels/SceneHierachyPanel.cpp index 7177bc5..f794850 100644 --- a/Editor/src/Editor/Panels/SceneHierachyPanel.cpp +++ b/Editor/src/Editor/Panels/SceneHierachyPanel.cpp @@ -266,7 +266,7 @@ namespace Hazel auto& tag = entity.GetComponent().Tag; char buffer[256] = {}; - strcpy_s(buffer,sizeof(buffer), tag.c_str()); + strncpy_s(buffer,sizeof(buffer), tag.c_str(), sizeof(buffer)); if (ImGui::InputText("Tag", buffer, sizeof(buffer))) { tag = std::string(buffer); @@ -368,7 +368,7 @@ namespace Hazel bool setStyleFlag = false; static char buffer[64] = {}; - snprintf(buffer, sizeof(buffer), "%s", component.ClassName.c_str()); + strcpy_s(buffer, sizeof(buffer), component.ClassName.c_str()); if (!scriptClassExists) { @@ -384,7 +384,7 @@ namespace Hazel // Fields - Ref scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID()); + const Ref scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID()); const bool sceneRunning = scene->IsRunning(); if (sceneRunning) diff --git a/Hazel-ScriptCore/Sources/Hazel/InternalCalls.cs b/Hazel-ScriptCore/Sources/Hazel/InternalCalls.cs index 4920c4f..a02799d 100644 --- a/Hazel-ScriptCore/Sources/Hazel/InternalCalls.cs +++ b/Hazel-ScriptCore/Sources/Hazel/InternalCalls.cs @@ -5,6 +5,12 @@ namespace Hazel { public static class InternalCalls { + [MethodImplAttribute(MethodImplOptions.InternalCall)] + internal extern static ulong Entity_FindEntityByName(String name); + + [MethodImplAttribute(MethodImplOptions.InternalCall)] + internal extern static object GetScriptInstance(ulong entityID); + [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static bool Entity_HasComponent(ulong entityID, Type componentType); diff --git a/Hazel-ScriptCore/Sources/Hazel/Scene/Entity.cs b/Hazel-ScriptCore/Sources/Hazel/Scene/Entity.cs index 6dc004b..32f8098 100644 --- a/Hazel-ScriptCore/Sources/Hazel/Scene/Entity.cs +++ b/Hazel-ScriptCore/Sources/Hazel/Scene/Entity.cs @@ -32,11 +32,26 @@ namespace Hazel{ public T GetComponent() where T : Component, new() { - if(!HasComponent()) + if (!HasComponent()) return null; T component = new T() { Entity = this }; return component; } + + public Entity FindEntityByName(string name) + { + ulong entityID = InternalCalls.Entity_FindEntityByName(name); + if (entityID == 0) + return null; + + return new Entity(entityID); + } + + public T As() where T : Entity, new() + { + object instance = InternalCalls.GetScriptInstance(ID); + return instance as T; + } } } diff --git a/Hazel-ScriptCore/Sources/Hazel/Vector.cs b/Hazel-ScriptCore/Sources/Hazel/Vector.cs index 5b4cc9f..8e8b112 100644 --- a/Hazel-ScriptCore/Sources/Hazel/Vector.cs +++ b/Hazel-ScriptCore/Sources/Hazel/Vector.cs @@ -40,6 +40,13 @@ namespace Hazel Z = scale; } + public Vector3(Vector2 xy, float z) + { + X = xy.X; + Y = xy.Y; + Z = z; + } + public Vector3(float x, float y, float z) { X = x; Y = y; diff --git a/Hazel/src/Hazel/Core/Application.cpp b/Hazel/src/Hazel/Core/Application.cpp index 35c2b62..a62d2c0 100644 --- a/Hazel/src/Hazel/Core/Application.cpp +++ b/Hazel/src/Hazel/Core/Application.cpp @@ -45,7 +45,6 @@ namespace Hazel { Application::~Application() { ScriptEngine::Shutdown(); - Renderer:: } void Application::Run() { diff --git a/Hazel/src/Hazel/Scene/Scene.cpp b/Hazel/src/Hazel/Scene/Scene.cpp index 3b049bf..cfcc0ae 100644 --- a/Hazel/src/Hazel/Scene/Scene.cpp +++ b/Hazel/src/Hazel/Scene/Scene.cpp @@ -414,6 +414,8 @@ namespace Hazel void Scene::OnViewportResize(uint32_t width, uint32_t height) { + if (m_ViewportWidth == width && m_ViewportHeight == height) return; + m_ViewportWidth = width; m_ViewportHeight = height; @@ -447,6 +449,18 @@ namespace Hazel */ } + Entity Scene::FindEntityByName(std::string_view name) + { + const auto view = m_Registry.view(); + for (const auto entity : view) + { + const TagComponent& tc = view.get(entity); + if (tc.Tag == name) + return Entity {entity, this}; + } + return {}; + } + Entity Scene::GetPrimaryCameraEntity() { auto view = m_Registry.view(); diff --git a/Hazel/src/Hazel/Scene/Scene.h b/Hazel/src/Hazel/Scene/Scene.h index 0eca445..f6417e1 100644 --- a/Hazel/src/Hazel/Scene/Scene.h +++ b/Hazel/src/Hazel/Scene/Scene.h @@ -44,6 +44,7 @@ namespace Hazel void DuplicateEntity(Entity entity); + Entity FindEntityByName(std::string_view name); Entity GetPrimaryCameraEntity(); Entity GetEntityByUUID(UUID uuid); diff --git a/Hazel/src/Hazel/Scripting/ScriptEngine.cpp b/Hazel/src/Hazel/Scripting/ScriptEngine.cpp index 33dbf46..7d21f6c 100644 --- a/Hazel/src/Hazel/Scripting/ScriptEngine.cpp +++ b/Hazel/src/Hazel/Scripting/ScriptEngine.cpp @@ -402,6 +402,12 @@ namespace Hazel return s_Data->CoreAssemblyImage; } + MonoObject* ScriptEngine::GetManagedInstance(const UUID uuid) + { + HZ_CORE_ASSERT(s_Data->EntityInstances.contains(uuid),"could not find entity"); + return s_Data->EntityInstances.at(uuid)->GetManagedObject(); + } + ScriptClass::ScriptClass(const std::string& classNamespace, const std::string& className, bool isCore) : m_classNamespace(classNamespace), m_className(className) { diff --git a/Hazel/src/Hazel/Scripting/ScriptEngine.h b/Hazel/src/Hazel/Scripting/ScriptEngine.h index 2c61c1c..ab8dbd1 100644 --- a/Hazel/src/Hazel/Scripting/ScriptEngine.h +++ b/Hazel/src/Hazel/Scripting/ScriptEngine.h @@ -11,12 +11,12 @@ #include "Hazel/Scene/Entity.h" extern "C" { -typedef struct _MonoObject MonoObject; -typedef struct _MonoClass MonoClass; -typedef struct _MonoMethod MonoMethod; -typedef struct _MonoAssembly MonoAssembly; -typedef struct _MonoImage MonoImage; -typedef struct _MonoClassField MonoClassField; + typedef struct _MonoObject MonoObject; + typedef struct _MonoClass MonoClass; + typedef struct _MonoMethod MonoMethod; + typedef struct _MonoAssembly MonoAssembly; + typedef struct _MonoImage MonoImage; + typedef struct _MonoClassField MonoClassField; } @@ -120,6 +120,7 @@ namespace Hazel SetFieldValueInternal(name, &data); } + MonoObject* GetManagedObject() const { return m_Instance; } private: bool GetFieldValueInternal(const std::string& name, void* buffer); bool SetFieldValueInternal(const std::string& name, const void* value); @@ -164,6 +165,7 @@ namespace Hazel static MonoImage* GetCoreAssemblyImage(); + static MonoObject* GetManagedInstance(UUID uuid); private: static void InitMono(); static void MonoShutdown(); diff --git a/Hazel/src/Hazel/Scripting/ScriptGlue.cpp b/Hazel/src/Hazel/Scripting/ScriptGlue.cpp index 94f091d..fbb2f01 100644 --- a/Hazel/src/Hazel/Scripting/ScriptGlue.cpp +++ b/Hazel/src/Hazel/Scripting/ScriptGlue.cpp @@ -36,6 +36,12 @@ namespace Hazel return glm::length(*param); } + static MonoObject* GetScriptInstance(UUID entityID) + { + return ScriptEngine::GetManagedInstance(entityID); + } + + static bool Entity_HasComponent(const UUID entityID, MonoReflectionType* componentType) { HZ_CORE_ASSERT(entityID, "Entity is Not exist!"); @@ -50,6 +56,24 @@ namespace Hazel return s_EntityHasComponentFuncs.at(managedType)(entity); } + static uint64_t Entity_FindEntityByName(MonoString* name) + { + char* nameStr = mono_string_to_utf8(name); + + Scene* scene = ScriptEngine::GetSceneContext(); + HZ_CORE_ASSERT(scene); + + Entity entity = scene->FindEntityByName(nameStr); + mono_free(nameStr); + + if (!entity) + return 0; + + return entity.GetUUID(); + } + + + static void TransformComponent_GetTranslation(const UUID entityID, glm::vec3* outTranslation) { HZ_CORE_ASSERT(entityID, "Entity is Not exist!"); @@ -106,22 +130,6 @@ namespace Hazel } - void ScriptGlue::RegisterFunctions() - { - HZ_ADD_INTERNAL_CALL(Entity_HasComponent); - - HZ_ADD_INTERNAL_CALL(NativeLog); - HZ_ADD_INTERNAL_CALL(NativeLog_Vector); - HZ_ADD_INTERNAL_CALL(NativeLog_VectorDot); - - HZ_ADD_INTERNAL_CALL(Input_IsKeyDown); - - HZ_ADD_INTERNAL_CALL(TransformComponent_GetTranslation); - HZ_ADD_INTERNAL_CALL(TransformComponent_SetTranslation); - - HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulse); - HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulseToCenter); - } template static void RegisterComponent() @@ -153,4 +161,25 @@ namespace Hazel { RegisterComponent(AllComponents{}); } + + void ScriptGlue::RegisterFunctions() + { + + HZ_ADD_INTERNAL_CALL(NativeLog); + HZ_ADD_INTERNAL_CALL(NativeLog_Vector); + HZ_ADD_INTERNAL_CALL(NativeLog_VectorDot); + + HZ_ADD_INTERNAL_CALL(GetScriptInstance); + + HZ_ADD_INTERNAL_CALL(Entity_HasComponent); + HZ_ADD_INTERNAL_CALL(Entity_FindEntityByName); + + HZ_ADD_INTERNAL_CALL(TransformComponent_GetTranslation); + HZ_ADD_INTERNAL_CALL(TransformComponent_SetTranslation); + + HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulse); + HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulseToCenter); + + HZ_ADD_INTERNAL_CALL(Input_IsKeyDown); + } }