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; // private RigidBody2DComponent m_Rigidbody;
public float MoveSpeed; public float MoveSpeed;
/* public float DistanceFromPlayer = 5;
private Entity m_Player;
void OnCreate() void OnCreate()
{ {
Console.WriteLine($"Player.OnCreate - {ID}"); Console.WriteLine($"Camera.OnCreate - {ID}");
m_Transform = GetComponent<TransformComponent>(); m_Player = FindEntityByName("Player");
m_Rigidbody = GetComponent<RigidBody2DComponent>();
} }
*/
void OnUpdate(float ts) void OnUpdate(float ts)
{ {
if(m_Player != null)
{
Translation = new Vector3(m_Player.Translation.XY, DistanceFromPlayer);
}
// Console.WriteLine($"Player.OnUpdate: {ts}"); // Console.WriteLine($"Player.OnUpdate: {ts}");
float speed = MoveSpeed; float speed = MoveSpeed;

View File

@ -36,6 +36,17 @@ namespace Sandbox {
else if(Input.IsKeyDown(KeyCode.D)) else if(Input.IsKeyDown(KeyCode.D))
velocity.X = 1.0f; 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); 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_CameraController.OnResize(m_ViewPortSize.x, m_ViewPortSize.y);
m_EditorCamera.SetViewPortSize(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 // update camera
@ -501,7 +499,6 @@ namespace Hazel
if (serializer.Deserialize(scenePath.string())) if (serializer.Deserialize(scenePath.string()))
{ {
m_EditorScene = newScene; m_EditorScene = newScene;
m_EditorScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
m_ActiveScene = m_EditorScene; m_ActiveScene = m_EditorScene;
@ -514,7 +511,6 @@ namespace Hazel
void EditorLayer::NewScene() void EditorLayer::NewScene()
{ {
m_ActiveScene = CreateRef<Scene>(); m_ActiveScene = CreateRef<Scene>();
m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
m_SceneHierachyPanel.SetContext(m_ActiveScene); m_SceneHierachyPanel.SetContext(m_ActiveScene);
m_EditorScenePath = std::filesystem::path(); m_EditorScenePath = std::filesystem::path();

View File

@ -266,7 +266,7 @@ namespace Hazel
auto& tag = entity.GetComponent<TagComponent>().Tag; auto& tag = entity.GetComponent<TagComponent>().Tag;
char buffer[256] = {}; 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))) if (ImGui::InputText("Tag", buffer, sizeof(buffer)))
{ {
tag = std::string(buffer); tag = std::string(buffer);
@ -368,7 +368,7 @@ namespace Hazel
bool setStyleFlag = false; bool setStyleFlag = false;
static char buffer[64] = {}; static char buffer[64] = {};
snprintf(buffer, sizeof(buffer), "%s", component.ClassName.c_str()); strcpy_s(buffer, sizeof(buffer), component.ClassName.c_str());
if (!scriptClassExists) if (!scriptClassExists)
{ {
@ -384,7 +384,7 @@ namespace Hazel
// Fields // Fields
Ref<ScriptInstance> scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID()); const Ref<ScriptInstance> scriptInstance = ScriptEngine::GetEntityScriptInstance(entity.GetUUID());
const bool sceneRunning = scene->IsRunning(); const bool sceneRunning = scene->IsRunning();
if (sceneRunning) if (sceneRunning)

View File

@ -5,6 +5,12 @@ namespace Hazel
{ {
public static class InternalCalls 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)] [MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static bool Entity_HasComponent(ulong entityID, Type componentType); internal extern static bool Entity_HasComponent(ulong entityID, Type componentType);

View File

@ -38,5 +38,20 @@ namespace Hazel{
return component; 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; Z = scale;
} }
public Vector3(Vector2 xy, float z)
{
X = xy.X;
Y = xy.Y;
Z = z;
}
public Vector3(float x, float y, float z) { public Vector3(float x, float y, float z) {
X = x; X = x;
Y = y; Y = y;

View File

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

View File

@ -414,6 +414,8 @@ namespace Hazel
void Scene::OnViewportResize(uint32_t width, uint32_t height) void Scene::OnViewportResize(uint32_t width, uint32_t height)
{ {
if (m_ViewportWidth == width && m_ViewportHeight == height) return;
m_ViewportWidth = width; m_ViewportWidth = width;
m_ViewportHeight = height; 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() Entity Scene::GetPrimaryCameraEntity()
{ {
auto view = m_Registry.view<CameraComponent>(); auto view = m_Registry.view<CameraComponent>();

View File

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

View File

@ -402,6 +402,12 @@ namespace Hazel
return s_Data->CoreAssemblyImage; 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) ScriptClass::ScriptClass(const std::string& classNamespace, const std::string& className, bool isCore)
: m_classNamespace(classNamespace), m_className(className) : m_classNamespace(classNamespace), m_className(className)
{ {

View File

@ -120,6 +120,7 @@ namespace Hazel
SetFieldValueInternal(name, &data); SetFieldValueInternal(name, &data);
} }
MonoObject* GetManagedObject() const { return m_Instance; }
private: private:
bool GetFieldValueInternal(const std::string& name, void* buffer); bool GetFieldValueInternal(const std::string& name, void* buffer);
bool SetFieldValueInternal(const std::string& name, const void* value); bool SetFieldValueInternal(const std::string& name, const void* value);
@ -164,6 +165,7 @@ namespace Hazel
static MonoImage* GetCoreAssemblyImage(); static MonoImage* GetCoreAssemblyImage();
static MonoObject* GetManagedInstance(UUID uuid);
private: private:
static void InitMono(); static void InitMono();
static void MonoShutdown(); static void MonoShutdown();

View File

@ -36,6 +36,12 @@ namespace Hazel
return glm::length(*param); return glm::length(*param);
} }
static MonoObject* GetScriptInstance(UUID entityID)
{
return ScriptEngine::GetManagedInstance(entityID);
}
static bool Entity_HasComponent(const UUID entityID, MonoReflectionType* componentType) static bool Entity_HasComponent(const UUID entityID, MonoReflectionType* componentType)
{ {
HZ_CORE_ASSERT(entityID, "Entity is Not exist!"); HZ_CORE_ASSERT(entityID, "Entity is Not exist!");
@ -50,6 +56,24 @@ namespace Hazel
return s_EntityHasComponentFuncs.at(managedType)(entity); 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) static void TransformComponent_GetTranslation(const UUID entityID, glm::vec3* outTranslation)
{ {
HZ_CORE_ASSERT(entityID, "Entity is Not exist!"); 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> template<typename... Component>
static void RegisterComponent() static void RegisterComponent()
@ -153,4 +161,25 @@ namespace Hazel
{ {
RegisterComponent(AllComponents{}); 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);
}
} }