using System; using System.Collections.Generic; using Lean.Pool; using UnityEngine; namespace Cielonos.MainGame { public partial class TimeSubmodule : AttackAreaSubmoduleBase { public float lifeTime; public float delayTime; public float enablingTimer; public float remainingEnableTime; public float remainingLifeTime; public Action enableAction; public Action timeOutAction; public List scheduledActions; private List toBeExecutedScheduledActions = new List(); public TimeSubmodule(AttackAreaBase attackArea, float lifeTime, Action timeOutAction = null) : base(attackArea) { this.isEnabling = true; this.lifeTime = lifeTime; this.remainingLifeTime = lifeTime; this.enablingTimer = 0; this.scheduledActions = new List(); if (attackArea is NormalArea) { this.remainingEnableTime = lifeTime * 0.25f; } else if (attackArea is Projectile) { this.remainingEnableTime = lifeTime; } else { this.remainingEnableTime = lifeTime; } this.timeOutAction = timeOutAction ?? (() => { if (attackArea is Projectile projectile) { projectile.Explode(projectile.transform.position); } else { LeanPool.Despawn(attackArea.topParent); } }); } public TimeSubmodule(AttackAreaBase attackArea, float lifeTime, float delayTime, float enableTime, Action enableAction, Action timeOutAction) : base(attackArea) { this.isEnabling = true; this.lifeTime = lifeTime; this.delayTime = delayTime; this.attackArea.isEnabling = delayTime <= 0; this.scheduledActions = new List(); this.timeOutAction = timeOutAction ?? (() => { if (attackArea is Projectile projectile) { projectile.Explode(projectile.transform.position); } else { LeanPool.Despawn(attackArea.topParent); } }); if (this.attackArea.isEnabling) { enableAction?.Invoke(); } this.remainingLifeTime = lifeTime; this.enablingTimer = 0; this.remainingEnableTime = enableTime; this.enableAction = enableAction; } public TimeSubmodule AddScheduleAction(Action action, float delay) { scheduledActions.Add(new ScheduledAction(action, delay)); return this; } } public partial class TimeSubmodule { public void Update() { if (!isEnabling) return; if (delayTime > 0) { delayTime -= attackArea.creator.selfTimeSm.DeltaTime; return; } if (!attackArea.isEnabling) { attackArea.isEnabling = true; enableAction?.Invoke(); } toBeExecutedScheduledActions.Clear(); foreach (var scheduledAction in scheduledActions) { scheduledAction.delay -= attackArea.creator.selfTimeSm.DeltaTime; if (scheduledAction.delay <= 0) { toBeExecutedScheduledActions.Add(scheduledAction); } } foreach (var scheduledAction in toBeExecutedScheduledActions) { scheduledAction.action.Invoke(); scheduledActions.Remove(scheduledAction); } if (remainingLifeTime <= 0) { this.isEnabling = false; timeOutAction?.Invoke(); } if (remainingEnableTime <= 0) { attackArea.isEnabling = false; } enablingTimer += attackArea.creator.selfTimeSm.DeltaTime; remainingLifeTime -= attackArea.creator.selfTimeSm.DeltaTime; remainingEnableTime -= attackArea.creator.selfTimeSm.DeltaTime; } public void ModifyLifeTime(float modifyValue) { lifeTime += modifyValue; remainingLifeTime += modifyValue; } } public partial class TimeSubmodule { public class ScheduledAction { public Action action; public float delay; public ScheduledAction(Action action, float delay) { this.action = action; this.delay = delay; } } } }