using System.Collections;
using System.Collections.Generic;
namespace Wayne.Lib.StateEngine
{
///
/// A collection of State factories. A state machine can have states created by multiple state factories.
/// When a state object should be created, the factories are queried one by one for the state name.
///
public class StateFactories
{
#region Fields
private List factoryList = new List();
#endregion
#region Construction
///
/// Constructor
///
public StateFactories()
{
}
#endregion
#region Public Methods
///
/// Clears the list.
///
public void Clear()
{
factoryList.Clear();
}
///
/// Add a factory to the StateFactory Collection.
///
///
/// The factory that should be added.
///
public void AddFactory(IStateFactory stateFactory)
{
if ((stateFactory != null) && !factoryList.Contains(stateFactory))
factoryList.Add(stateFactory);
}
///
/// Calls each registered State Factory to create the requested State.
/// If more than one factory returns a state with the specified name,
/// the method will throw an exception.
///
/// If no factory returns a state, null will be returned.
///
/// If exactly one state was created, it will be returned.
///
/// Name of the State that is going to be created.
///
///The created state
/// If more than one factory creates the state, an exception will be thrown.
public State CreateState(string stateFactoryName, StateTypeContainer stateTypeContainer)
{
State foundState = null;
//Ask each factory to create the state object.
foreach (IStateFactory factory in factoryList)
{
IStateFactory2 stateFactory2 = factory as IStateFactory2;
State state;
if (stateFactory2 != null)
state = stateFactory2.CreateState(stateFactoryName, stateTypeContainer);
else
state = factory.CreateState(stateFactoryName);
if (state != null)
{
foundState = state;
break;
}
}
CompositeState compositeState = foundState as CompositeState;
if (compositeState != null)
{
compositeState.StateMachine.stateFactories = this;
compositeState.StateMachine.SetAndMergeTypeContainer(stateTypeContainer);
}
return foundState;
}
#endregion
}
}