From a80fb5241ed081f889b3b948815e49a864dcc898 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 12:15:52 -0400 Subject: [PATCH] Added ability to play an animation from C# --- .../src/Scripting/NetModules/SceneNetAPI.cpp | 29 +++++++++++++++++++ NuakeNet/src/Components.cs | 12 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 1bbe5e9e..fadb5070 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -169,6 +169,33 @@ namespace Nuake { return false; } + void Play(int entityId, Coral::NativeString animation) + { + Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get()); + + if (entity.IsValid() && entity.HasComponent()) + { + auto& skinnedModel = entity.GetComponent(); + + if (skinnedModel.ModelResource) + { + auto& model = skinnedModel.ModelResource; + + // Find animation from name + int animIndex = 0; + for (const auto& anim : model->GetAnimations()) + { + if (anim->GetName() == animation.ToString()) + { + model->PlayAnimation(animIndex); + } + + animIndex++; + } + } + } + } + void Nuake::SceneNetAPI::RegisterMethods() { RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent); @@ -182,6 +209,8 @@ namespace Nuake { RegisterMethod("CharacterControllerComponent.MoveAndSlideIcall", &MoveAndSlide); RegisterMethod("CharacterControllerComponent.IsOnGroundIcall", &IsOnGround); + + RegisterMethod("SkinnedModelComponent.PlayIcall", &Play); } } diff --git a/NuakeNet/src/Components.cs b/NuakeNet/src/Components.cs index 73f82568..6e7fb641 100644 --- a/NuakeNet/src/Components.cs +++ b/NuakeNet/src/Components.cs @@ -152,10 +152,20 @@ namespace Nuake.Net public class SkinnedModelComponent : IComponent { - public SkinnedModelComponent(int entityId) { } + internal static unsafe delegate* PlayIcall; + + public SkinnedModelComponent(int entityId) + { + EntityID = entityId; + } public bool Playing { get; set; } public int CurrentAnimation { get; set; } + + public void Play(String name) + { + unsafe { PlayIcall(EntityID, name); } + } } public class BoneComponent : IComponent