mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-26 05:50:18 +00:00
extract StateContainer class
This commit is contained in:
parent
214c0291a7
commit
e09d0ba020
4 changed files with 77 additions and 23 deletions
|
@ -46,8 +46,8 @@ namespace SanAndreasUnity.Behaviours
|
|||
[SerializeField] private Vector2 m_cameraClampValue = new Vector2(60, 60);
|
||||
public Vector2 CameraClampValue { get { return m_cameraClampValue; } set { m_cameraClampValue = value; } }
|
||||
|
||||
|
||||
public Peds.States.BaseScriptState[] States { get; private set; }
|
||||
|
||||
private readonly StateContainer<Peds.States.BaseScriptState> _stateContainer = new StateContainer<Peds.States.BaseScriptState>();
|
||||
public Peds.States.BaseScriptState CurrentState { get { return (Peds.States.BaseScriptState) m_stateMachine.CurrentState; } }
|
||||
|
||||
public Cell Cell { get { return Cell.Instance; } }
|
||||
|
@ -144,7 +144,7 @@ namespace SanAndreasUnity.Behaviours
|
|||
this.characterController = this.GetComponent<CharacterController>();
|
||||
m_weaponHolder = GetComponent<WeaponHolder> ();
|
||||
|
||||
this.States = this.GetComponentsInChildren<Peds.States.BaseScriptState> ();
|
||||
_stateContainer.AddStates(this.GetComponentsInChildren<Peds.States.BaseScriptState> ());
|
||||
|
||||
this.AwakeForDamage ();
|
||||
|
||||
|
@ -216,46 +216,37 @@ namespace SanAndreasUnity.Behaviours
|
|||
|
||||
public Peds.States.BaseScriptState GetState(System.Type type)
|
||||
{
|
||||
return this.States.FirstOrDefault (s => s.GetType ().Equals (type));
|
||||
return _stateContainer.GetState(type);
|
||||
}
|
||||
|
||||
public T GetState<T>() where T : Peds.States.BaseScriptState
|
||||
public T GetState<T>()
|
||||
where T : Peds.States.BaseScriptState
|
||||
{
|
||||
return (T) this.GetState(typeof(T));
|
||||
return _stateContainer.GetState<T>();
|
||||
}
|
||||
|
||||
public Peds.States.BaseScriptState GetStateOrLogError(System.Type type)
|
||||
{
|
||||
var state = this.GetState (type);
|
||||
if(null == state)
|
||||
Debug.LogErrorFormat ("Failed to find state: {0}", type);
|
||||
return state;
|
||||
return _stateContainer.GetStateOrLogError(type);
|
||||
}
|
||||
|
||||
public T GetStateOrLogError<T>() where T : Peds.States.BaseScriptState
|
||||
public T GetStateOrLogError<T>()
|
||||
where T : Peds.States.BaseScriptState
|
||||
{
|
||||
return (T) this.GetStateOrLogError(typeof(T));
|
||||
return _stateContainer.GetStateOrLogError<T>();
|
||||
}
|
||||
|
||||
public void SwitchState(System.Type type)
|
||||
{
|
||||
|
||||
var state = this.GetStateOrLogError (type);
|
||||
if (null == state)
|
||||
return;
|
||||
|
||||
// IState oldState = this.CurrentState;
|
||||
|
||||
m_stateMachine.SwitchState (state);
|
||||
|
||||
// if (oldState != state)
|
||||
// {
|
||||
// Debug.LogFormat ("Switched to state: {0}", state.GetType ().Name);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
public void SwitchState<T>() where T : Peds.States.BaseScriptState
|
||||
public void SwitchState<T>()
|
||||
where T : Peds.States.BaseScriptState
|
||||
{
|
||||
this.SwitchState(typeof(T));
|
||||
}
|
||||
|
|
|
@ -220,7 +220,7 @@ namespace SanAndreasUnity.Behaviours
|
|||
// forcefully change the state
|
||||
|
||||
F.RunExceptionSafe( () => {
|
||||
var newState = this.States.FirstOrDefault(state => state.GetType().Name == newStateData.state);
|
||||
var newState = _stateContainer.States.FirstOrDefault(state => state.GetType().Name == newStateData.state);
|
||||
if (null == newState)
|
||||
{
|
||||
Debug.LogErrorFormat("New ped state '{0}' could not be found", newStateData.state);
|
||||
|
|
52
Assets/Scripts/Utilities/StateContainer.cs
Normal file
52
Assets/Scripts/Utilities/StateContainer.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SanAndreasUnity.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Container for <see cref="IState"/> objects.
|
||||
/// </summary>
|
||||
public class StateContainer<TState>
|
||||
where TState : IState
|
||||
{
|
||||
private readonly List<TState> _states = new List<TState>();
|
||||
public IReadOnlyList<TState> States => _states;
|
||||
|
||||
|
||||
public TState GetState(System.Type type)
|
||||
{
|
||||
return this._states.FirstOrDefault (s => s.GetType ().Equals (type));
|
||||
}
|
||||
|
||||
public T GetState<T>()
|
||||
where T : TState
|
||||
{
|
||||
return (T) this.GetState(typeof(T));
|
||||
}
|
||||
|
||||
public TState GetStateOrLogError(System.Type type)
|
||||
{
|
||||
var state = this.GetState (type);
|
||||
if(null == state)
|
||||
Debug.LogErrorFormat ("Failed to find state: {0}", type);
|
||||
return state;
|
||||
}
|
||||
|
||||
public T GetStateOrLogError<T>()
|
||||
where T : TState
|
||||
{
|
||||
return (T) this.GetStateOrLogError(typeof(T));
|
||||
}
|
||||
|
||||
public void AddState(TState stateToAdd)
|
||||
{
|
||||
_states.Add(stateToAdd);
|
||||
}
|
||||
|
||||
public void AddStates(IEnumerable<TState> statesToAdd)
|
||||
{
|
||||
_states.AddRange(statesToAdd);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Utilities/StateContainer.cs.meta
Normal file
11
Assets/Scripts/Utilities/StateContainer.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e69f407670bd6c74cbedc3900bf033a9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue