using System;
using System.Collections.Generic;
using UnityEngine;
namespace coa4u
{
///
/// Runs one random action from the given list.
///
class ActionRandom : ActionInterval
{
protected ActionInstant[] actions;
protected int index;
public ActionRandom(ActionInstant action1, ActionInstant action2)
: base(0)
{
actions = new ActionInstant[] { action1, action2 };
}
public ActionRandom(ActionInstant[] actionsList)
: base(0)
{
actions = actionsList;
}
///
/// Returns a copy of the action.
///
public override ActionInstant clone()
{
ActionInstant[] aList = new ActionInstant[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].clone();
}
return new ActionRandom(aList);
}
///
/// Returns the reversed version of the action, if it is possible.
///
public override ActionInstant reverse()
{
ActionInstant[] aList = new ActionInstant[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].reverse();
}
return new ActionRandom(aList);
}
///
/// This method is called at the action start.
///
public override void start()
{
base.start();
index = UnityEngine.Random.Range(0, actions.Length);
actions[index].setActor(target);
actions[index].start();
}
///
/// This method is called every frame update.
///
public override void step(float dt)
{
dt *= timeScale;
if (actions[index].running)
actions[index].step(dt);
if (!actions[index].running)
{
stop();
dtr = actions[index].dtr;
}
}
///
/// This method is called after the interval action is stopped.
///
public override void stop()
{
base.stop();
actions[index].stop();
}
}
}