/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Engine/Common/GameObject.cs
178 строк
5 KB
EugenyCh7
UpdateComponent(float milliseconds)
25 авг 2025, 21:13
25 авг 2025, 21:13
3101026
Код
Авторство
О чём код?
using Puppet2D.Engine.Components; using Puppet2D.Engine.Components.Joints; using Puppet2D.Engine.Components.RigidBodies; using Puppet2D.Engine.GameWindows; using Puppet2D.Engine.Worlds; using System; using System.Collections.Generic; namespace Puppet2D.Engine.Common { public class GameObject : GameResource, ICloneable<GameObject> { private List<Component> components = new List<Component>(); private List<Joint> joints = new List<Joint>(); public World World { get; internal set; } public GameWindow Window => World.Window; public string Name { get; set; } public string Tag { get; set; } public Transformation Transformation { get; private set; } public RigidBody RigidBody { get; private set; } public ObjectBehavior ObjectBehavior { get; private set; } internal override void Create() { base.Create(); foreach (Component component in components) component.Create(); ObjectBehavior?.OnCreate(); State = ObjectState.Created; } internal override void Destroy() { base.Destroy(); ObjectBehavior?.OnDestroy(); foreach (Joint joint in joints) { if (joint.State == ObjectState.Created) joint.Destroy(); } joints = null; foreach (Component component in components) { if (component.State == ObjectState.Created) component.Destroy(); } components = null; Transformation = null; RigidBody = null; ObjectBehavior = null; World = null; State = ObjectState.Destroyed; } public GameObject Clone() { GameObject clone = new GameObject { Name = Name, Tag = Tag }; foreach (Component component in components) clone.AddComponent(component.Clone()); return clone; } public override string ToString() { return $"GameObject(Name={Name}, Tag={Tag})"; } internal void RemoveComponent(Component component) { if (!components.Contains(component)) throw new ArgumentException("The component was not found!"); if (component is Joint joint) { joints.Remove(joint); joint.Connected.GameObject.joints.Remove(joint); } component.Destroy(); component.GameObject = null; components.Remove(component); if (component is Transformation) Transformation = null; if (component is RigidBody) RigidBody = null; if (component is ObjectBehavior) ObjectBehavior = null; } public T AddComponent<T>(Action<T> initializer = null) where T : Component, new() { T component = new T(); initializer?.Invoke(component); return AddComponent(component); } public T AddComponent<T>(T component) where T : Component { if (State == ObjectState.Destroyed) throw new InvalidOperationException("You cannot add components to a destroyed object!"); component.GameObject = this; components.Add(component); if (component is Transformation transformation) Transformation = transformation; if (component is RigidBody rigidBody) RigidBody = rigidBody; if (component is ObjectBehavior objectBehavior) ObjectBehavior = objectBehavior; if (State == ObjectState.Created) component.Create(); if (component is Joint joint) { joints.Add(joint); joint.Connected.GameObject.joints.Add(joint); } return component; } public IEnumerable<Component> AllComponents() { foreach (Component component in components) yield return component; } public T GetComponent<T>() where T : Component { foreach (Component component in components) { if (component is T result) return result; } return null; } public T GetComponentOrThrow<T>() where T : Component { foreach (Component component in components) { if (component is T result) return result; } throw new NotImplementedException($"The '{typeof(T)}' component was not found!"); } public IEnumerable<T> GetComponents<T>() where T : Component { foreach (Component component in components) { if (component is T result) yield return result; } } internal void UpdateComponents(float milliseconds) { foreach (Component component in components) component.UpdateComponent(milliseconds); } object ICloneable.Clone() { return Clone(); } } }