/// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Samples { using UnityEngine; using Unity.Entities; using System; /// /// Simple health component. Can be baked to an entity or used with property bindings. /// public class Health : MonoBehaviour { [Tooltip("The maximum amount of health that the entity can have.")] [SerializeField] protected float m_MaxHealth = 100; [Tooltip("Should the health value be baked to a DOTS component?")] [SerializeField] protected bool m_BakeToEntity; public float MaxHealth { get => m_MaxHealth; } private float m_Value; private Action m_OnUpdateValue; public float Value { get => m_Value; set { UpdateHealthValue(value); } } public Action OnUpdateValue { get => m_OnUpdateValue; set => m_OnUpdateValue = value; } /// /// Initializes the default values. /// private void Awake() { m_Value = m_MaxHealth; } /// /// Updates the health value amount. /// /// The target value. public void UpdateHealthValue(float value) { value = Mathf.Clamp(value, 0, m_MaxHealth); if (value == m_Value) { return; } m_Value = value; m_OnUpdateValue?.Invoke(value); } /// /// Converts the health component to a DOTS entity. /// public class HealthBaker : Baker { /// /// Bakes the health component to the DOTS entity. /// /// The authoring behavior tree. public override void Bake(Health health) { if (!health.m_BakeToEntity) { return; } var entity = GetEntity(TransformUsageFlags.Dynamic); AddComponent(entity, new HealthComponent() { Value = health.m_MaxHealth }); } } } /// /// The DOTS health component. /// public struct HealthComponent : IComponentData { [Tooltip("The amount of health remaining.")] public float Value; } }