Let activities know when they are being run for the first or last time

This commit is contained in:
Oliver Brakmann
2016-11-28 19:41:27 +01:00
parent 85053d8d59
commit 0dfbd46b1f
3 changed files with 38 additions and 1 deletions

View File

@@ -9,14 +9,19 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Activities
{
public enum ActivityState { Queued, Active, Done }
public abstract class Activity
{
public ActivityState State { get; private set; }
public Activity NextActivity { get; set; }
public bool IsInterruptible { get; protected set; }
protected bool IsCanceled { get; private set; }
@@ -26,8 +31,39 @@ namespace OpenRA.Activities
IsInterruptible = true;
}
public Activity TickOuter(Actor self)
{
if (State == ActivityState.Done && Game.Settings.Debug.StrictActivityChecking)
throw new InvalidOperationException("Actor {0} attempted to tick activity {1} after it had already completed.".F(self, this.GetType()));
if (State == ActivityState.Queued)
{
OnFirstRun(self);
State = ActivityState.Active;
}
var ret = Tick(self);
if (ret != this)
{
State = ActivityState.Done;
OnLastRun(self);
}
return ret;
}
public abstract Activity Tick(Actor self);
/// <summary>
/// Runs once immediately before the first Tick() execution.
/// </summary>
protected virtual void OnFirstRun(Actor self) { }
/// <summary>
/// Runs once immediately after the last Tick() execution.
/// </summary>
protected virtual void OnLastRun(Actor self) { }
public virtual bool Cancel(Actor self)
{
if (!IsInterruptible)