using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// Class which reads a a list of logic blocks and applies to a character /// [System.Serializable] public class BlockReader { #region Inspector Variables [SerializeField] [Tooltip("List of LogicBlocks which will affected this character")] public List LogicChain = new List(); #endregion Inspector Variables #region Read-only Variables /// /// returns true if all LogicBlocks in LogicChain have been completed /// public bool Finished { get { return currentBlockIndex >= LogicChain.Count; } } /// /// Fired whenever the logic chain is updated, useful for UI /// public event System.Action OnUpdate; /// /// Gets the current block in the logic chain /// public LogicBlock CurrentBlock { get { return ((currentBlockIndex < LogicChain.Count) ? LogicChain[currentBlockIndex] : null); } } /// /// Sum of size of all Blocks in the chain /// public int Length { get { int retVal = 0; LogicChain.ForEach(p => retVal += p.Size()); return retVal; } } #endregion Read-only Variables #region Private Variables private int currentBlockIndex;//Block currently on #endregion Private Variables #region Class Implementation /// /// Resets Block reader; /// public void Reset() { currentBlockIndex = 0; } /// /// Reads the current block in the logic chain /// /// Character to control /// Returns false if other readers should wait for this one to run again public bool Read(Character character,float speedMultiplier, out float TotalAnimationTime) { //return that this is done if no more blocks left in chain if (LogicChain.Count <= currentBlockIndex) { TotalAnimationTime = 0; return true; } //get current Block LogicBlock currentBlock = LogicChain[currentBlockIndex]; //apply blocks to character bool runAgainNot = currentBlock.Run(character, speedMultiplier,out TotalAnimationTime); //if the block is finished reset it and move on to next one //(it might not be finished if it is a for loop or something) bool isFinished = currentBlock.isFinished(); if (isFinished) { currentBlock.Reset(); currentBlockIndex++; } //Should other readers wait for this one return (runAgainNot || !currentBlock.WaitUntilFinished); } /// /// Clears all elements out of the Block Reader /// public void Clear() { LogicChain.Clear(); if (OnUpdate != null) OnUpdate.Invoke(); } /// /// Removes the first instance found in the Block Reader /// /// LogicElement to remove public void Remove(LogicBlock item) { LogicChain.Remove(item); if (OnUpdate != null) OnUpdate.Invoke(); } /// /// Addes item to the end of the BlockReader /// /// item to add public void Add(LogicBlock item) { LogicChain.Add(item); if (OnUpdate != null) OnUpdate.Invoke(); } /// /// Inserts item at index of the Block Reader /// /// index to insert at /// item to insert public void Insert(int index, LogicBlock item) { LogicChain.Insert(index, item); if (OnUpdate != null) OnUpdate.Invoke(); } #endregion }