add FindEntityByName function

This commit is contained in:
2025-10-26 13:12:04 +08:00
parent 9c45444354
commit e8dec2e2d3
13 changed files with 129 additions and 37 deletions

View File

@ -9,18 +9,24 @@ namespace Sandbox {
// private RigidBody2DComponent m_Rigidbody;
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<TransformComponent>();
m_Rigidbody = GetComponent<RigidBody2DComponent>();
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;

View File

@ -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<Camera>();
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);
}
}

View File

@ -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<Scene>();
m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
m_SceneHierachyPanel.SetContext(m_ActiveScene);
m_EditorScenePath = std::filesystem::path();

View File

@ -266,7 +266,7 @@ namespace Hazel
auto& tag = entity.GetComponent<TagComponent>().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> scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID());
const Ref<ScriptInstance> scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID());
const bool sceneRunning = scene->IsRunning();
if (sceneRunning)

View File

@ -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);

View File

@ -32,11 +32,26 @@ namespace Hazel{
public T GetComponent<T>() where T : Component, new()
{
if(!HasComponent<T>())
if (!HasComponent<T>())
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<T>() where T : Entity, new()
{
object instance = InternalCalls.GetScriptInstance(ID);
return instance as T;
}
}
}

View File

@ -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;

View File

@ -45,7 +45,6 @@ namespace Hazel {
Application::~Application()
{
ScriptEngine::Shutdown();
Renderer::
}
void Application::Run() {

View File

@ -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<TagComponent>();
for (const auto entity : view)
{
const TagComponent& tc = view.get<TagComponent>(entity);
if (tc.Tag == name)
return Entity {entity, this};
}
return {};
}
Entity Scene::GetPrimaryCameraEntity()
{
auto view = m_Registry.view<CameraComponent>();

View File

@ -44,6 +44,7 @@ namespace Hazel
void DuplicateEntity(Entity entity);
Entity FindEntityByName(std::string_view name);
Entity GetPrimaryCameraEntity();
Entity GetEntityByUUID(UUID uuid);

View File

@ -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)
{

View File

@ -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();

View File

@ -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<typename... Component>
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);
}
}