using System; using RPGCore.ObjectModules.ActionObjectModule; using RPGCore.ObjectModules.EventObjectModule; using RPGCore.ObjectModules.EventObjectModule.Events; using RPGCoreCommon.DynamicValues; using RPGCoreCommon.Helpers; using RPGCoreCommon.Helpers.PropertyAttributeDrawers; using UnityEngine; namespace RPGCore.Core.Objects { [SelectionBase] [DisallowMultipleComponent] [RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(EventModule))] [RequireComponent(typeof(ActionModule))] [RequireComponent(typeof(DataModule))] public abstract class BaseObject : MonoBehaviour { [field: SerializeField, ReadOnly] public new Rigidbody rigidbody { get; private set; } [field: SerializeField, ReadOnly] public EventModule events { get; private set; } [field: SerializeField, ReadOnly] public ActionModule actions { get; private set; } [field: SerializeField, ReadOnly] public DataModule data { get; private set; } [DynamicValueProvider] private ObjectModule BaseModuleProvider(Type moduleType) => GetComponent(moduleType) as ObjectModule; protected void Start() { GetComponent().Invoke(new SpawnEvent()); } protected void OnValidate() { rigidbody = GetComponent(); events = GetComponent(); actions = GetComponent(); data = GetComponent(); GetComponents().ForEach(module => module.parent = this); } /// Removes this object from game. public void Remove() { events.Invoke(new RemoveEvent{ obj = this }); Destroy(gameObject); } /// It'll execute . when this object enter its collider. private void OnTriggerEnter(Collider other) { other.GetComponentsInParent().ForEach(trigger => { trigger.OnEnter(this); events.Invoke(new TriggerEnterEvent { target = this, trigger = trigger }); }); } /// It'll execute . when this object exit its collider. private void OnTriggerExit(Collider other) { other.GetComponentsInParent().ForEach(trigger => { trigger.OnExit(this); events.Invoke(new TriggerExitEvent { target = this, trigger = trigger }); }); } } }