init
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
using RPGCore.ObjectModules.EventObjectModule;
|
||||
using RPGCore.StatusEffect.ObjectModules.StatusObjectModule.Effects;
|
||||
using RPGCore.StatusEffect.ObjectModules.StatusObjectModule.Events;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCore.StatusEffect.ObjectModules.StatusObjectModule
|
||||
{
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(BaseObject))]
|
||||
public class StatusModule : ObjectModule<BaseObject>
|
||||
{
|
||||
internal List<Status> statuses { get; private set; } = new();
|
||||
[SerializeField] internal StatusUpdateType updateType;
|
||||
|
||||
/// <summary>Checks if any <see cref="Status"/> has <see cref="NotControllableEffect"/>.</summary>
|
||||
public virtual bool IsControllable()
|
||||
{
|
||||
return !statuses.Any(status => status.definition.effects.Any(effect => effect is NotControllableEffect));
|
||||
}
|
||||
|
||||
/// <summary>Checks if any active <see cref="Status"/> that uses <see cref="StatusDefinitionSO"/> is present.</summary>
|
||||
public bool Check(StatusDefinitionSO definition)
|
||||
{
|
||||
return statuses.Any(status => status.definition == definition);
|
||||
}
|
||||
|
||||
/// <summary>Adds new <see cref="Status"/>.</summary>
|
||||
public Status Apply(StatusDefinitionSO definition)
|
||||
{
|
||||
var status = new Status(definition, this);
|
||||
statuses.Add(status);
|
||||
status.OnApply_Internal();
|
||||
parent.events.Invoke(new StatusStartEvent { status = status, target = parent });
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>Removes given <see cref="Status"/> forcefully.</summary>
|
||||
public void Remove(Status status)
|
||||
{
|
||||
if (status == null) return;
|
||||
if (!statuses.Contains(status)) return;
|
||||
status.OnRemove_Internal();
|
||||
statuses.Remove(status);
|
||||
parent.events.Invoke(new StatusRemoveEvent { status = status, target = parent });
|
||||
}
|
||||
|
||||
/// <summary>Removes all <see cref="Status"/>es forcefully that uses <see cref="StatusDefinitionSO"/>.</summary>
|
||||
public void Remove(StatusDefinitionSO definition)
|
||||
{
|
||||
GetStatuses(definition).ForEach(Remove);
|
||||
}
|
||||
|
||||
/// <summary>Executed internally when status ending itself (by timer or effect).</summary>
|
||||
internal void End(Status status)
|
||||
{
|
||||
if (status == null) return;
|
||||
if (!statuses.Contains(status)) return;
|
||||
status.OnEnd_Internal();
|
||||
statuses.Remove(status);
|
||||
parent.events.Invoke(new StatusEndEvent { status = status, target = parent });
|
||||
}
|
||||
|
||||
/// <summary>Returns all active <see cref="Status"/>es.</summary>
|
||||
public List<Status> GetStatuses()
|
||||
{
|
||||
return statuses.ToList();
|
||||
}
|
||||
|
||||
/// <summary>Returns all active <see cref="Status"/>es that uses <see cref="StatusDefinitionSO"/>.</summary>
|
||||
public List<Status> GetStatuses(StatusDefinitionSO definition)
|
||||
{
|
||||
return statuses.Where(status => status.definition == definition).ToList();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (updateType is StatusUpdateType.Automatically)
|
||||
statuses.ToList().ForEach(status => status.UpdateTime_Internal(Time.fixedDeltaTime));
|
||||
}
|
||||
|
||||
/// <summary>Update each status timer, decreases them by given seconds.</summary>
|
||||
public void UpdateTime(float deltaTime)
|
||||
{
|
||||
if (updateType is not StatusUpdateType.Manually)
|
||||
{
|
||||
Debug.LogError($"Updating status time available only when: {nameof(StatusUpdateType)} = {nameof(StatusUpdateType.Manually)}");
|
||||
return;
|
||||
}
|
||||
|
||||
statuses.ForEach(status => status.UpdateTime_Internal(deltaTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user