// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. using System; namespace FlaxEngine { partial class Actor : ITransformable, ISceneObject { /// /// The rotation as Euler angles in degrees. /// /// /// The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order). /// Angles order (xyz): pitch, yaw and roll. /// [HideInEditor, NoSerialize, NoAnimate] public Vector3 EulerAngles { get => Orientation.EulerAngles; set { Quaternion.Euler(ref value, out var orientation); Internal_SetOrientation(__unmanagedPtr, ref orientation); } } /// /// The local rotation as Euler angles in degrees. /// /// /// The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order). /// Angles order (xyz): pitch, yaw and roll. /// [HideInEditor, NoSerialize, NoAnimate] public Vector3 LocalEulerAngles { get => LocalOrientation.EulerAngles; set { Quaternion.Euler(ref value, out var orientation); Internal_SetLocalOrientation(__unmanagedPtr, ref orientation); } } /// /// Returns true if actor has any children /// [HideInEditor, NoSerialize] public bool HasChildren => Internal_GetChildrenCount(__unmanagedPtr) != 0; /// /// Resets actor local transform. /// public void ResetLocalTransform() { LocalTransform = Transform.Identity; } /// /// Creates a new child actor of the given type. /// /// Type of the actor. /// The child actor. public Actor AddChild(Type type) { var result = (Actor)New(type); result.SetParent(this, false); return result; } /// /// Creates a new child actor of the given type. /// /// Type of the actor to search for. Includes any actors derived from the type. /// The child actor. public T AddChild() where T : Actor { var result = New(); result.SetParent(this, false); return result; } /// /// Finds the child actor of the given type. /// /// Type of the actor to search for. Includes any actors derived from the type. /// The child actor or null if failed to find. public T GetChild() where T : Actor { return GetChild(typeof(T)) as T; } /// /// Tries to the child actor of the given type. /// /// Type of the actor to search for. Includes any actors derived from the type. /// The returned actor, valid only if method returns true. /// True if found an child actor of that type or false if failed to find. public bool TryGetChild(out T actor) where T : Actor { actor = GetChild(typeof(T)) as T; return actor != null; } /// /// Finds the child actor of the given type or creates a new one. /// /// Type of the actor to search for. Includes any actors derived from the type. /// The child actor. public T GetOrAddChild() where T : Actor { var result = GetChild(); if (result == null) { result = New(); result.SetParent(this, false); } return result; } /// /// Creates a new script of a specific type and adds it to the actor. /// /// Type of the script to create. /// The created script instance, null otherwise. public Script AddScript(Type type) { var script = (Script)New(type); script.Parent = this; return script; } /// /// Creates a new script of a specific type and adds it to the actor. /// /// Type of the script to search for. Includes any scripts derived from the type. /// The created script instance, null otherwise. public T AddScript() where T : Script { var script = New(); script.Parent = this; return script; } /// /// Finds the script of the given type. /// /// Type of the script to search for. Includes any scripts derived from the type. /// The script or null if failed to find. public T GetScript() where T : Script { return GetScript(typeof(T)) as T; } /// /// Tries to find the script of the given type. /// /// Type of the script to search for. Includes any scripts derived from the type. /// The returned script, valid only if method returns true. /// True if found a script of that type or false if failed to find. public bool TryGetScript(out T script) where T : Script { script = GetScript(typeof(T)) as T; return script != null; } /// /// Searches for all actors of a specific type in this actor children list. /// /// Type of the actor to search for. Includes any actors derived from the type. /// All actors matching the specified type public T[] GetChildren() where T : Actor { // TODO: use a proper array allocation and converting on backend to reduce memory allocations var children = GetChildren(typeof(T)); var output = new T[children.Length]; for (int i = 0; i < children.Length; i++) output[i] = (T)children[i]; return output; } /// /// Searches for all scripts of a specific type. /// /// Type of the scripts to search for. Includes any scripts derived from the type. /// All scripts matching the specified type. public T[] GetScripts() where T : Script { // TODO: use a proper array allocation and converting on backend to reduce memory allocations var scripts = GetScripts(typeof(T)); var output = new T[scripts.Length]; for (int i = 0; i < scripts.Length; i++) output[i] = (T)scripts[i]; return output; } /// /// Destroys the children. Calls Object.Destroy on every child actor and unlink them for the parent. /// /// The time left to destroy object (in seconds). [NoAnimate] public void DestroyChildren(float timeLeft = 0.0f) { Actor[] children = Children; for (var i = 0; i < children.Length; i++) { children[i].Parent = null; Destroy(children[i], timeLeft); } } /// /// Gets the matrix that transforms a point from the world space to local space of the actor. /// public Matrix WorldToLocalMatrix { get { GetWorldToLocalMatrix(out var worldToLocal); return worldToLocal; } } /// /// Gets the matrix that transforms a point from the local space of the actor to world space. /// public Matrix LocalToWorldMatrix { get { GetLocalToWorldMatrix(out var localToWorld); return localToWorld; } } /// public override string ToString() { return $"{Name} ({GetType().Name})"; } } }