67 lines
2.6 KiB
C#
67 lines
2.6 KiB
C#
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<BaseObject> BaseModuleProvider(Type moduleType) => GetComponent(moduleType) as ObjectModule<BaseObject>;
|
|
|
|
protected void Start()
|
|
{
|
|
GetComponent<EventModule>().Invoke(new SpawnEvent());
|
|
}
|
|
|
|
protected void OnValidate()
|
|
{
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
events = GetComponent<EventModule>();
|
|
actions = GetComponent<ActionModule>();
|
|
data = GetComponent<DataModule>();
|
|
GetComponents<ObjectModule>().ForEach(module => module.parent = this);
|
|
}
|
|
|
|
/// <summary>Removes this object from game.</summary>
|
|
public void Remove()
|
|
{
|
|
events.Invoke(new RemoveEvent{ obj = this });
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
/// <summary>It'll execute <see cref="ITrigger"/>.<see cref="ITrigger.OnEnter"/> when this object enter its collider.</summary>
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
other.GetComponentsInParent<ITrigger>().ForEach(trigger => {
|
|
trigger.OnEnter(this);
|
|
events.Invoke(new TriggerEnterEvent { target = this, trigger = trigger });
|
|
});
|
|
}
|
|
|
|
/// <summary>It'll execute <see cref="ITrigger"/>.<see cref="ITrigger.OnExit"/> when this object exit its collider.</summary>
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
other.GetComponentsInParent<ITrigger>().ForEach(trigger => {
|
|
trigger.OnExit(this);
|
|
events.Invoke(new TriggerExitEvent { target = this, trigger = trigger });
|
|
});
|
|
}
|
|
}
|
|
} |