浏览代码

Renaming from 'Scriptable' prefix to 'SO' suffix.

Co-Authored-By: Ciro Continisio <20049224+ciro-unity@users.noreply.github.com>
/main
DeivSky 4 年前
当前提交
d3e9ddbc
共有 37 个文件被更改,包括 257 次插入153 次删除
  1. 2
      UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/AttackingToChasing.asset
  2. 2
      UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/ChasingToAttacking.asset
  3. 2
      UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/ChasingToEating.asset
  4. 2
      UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachine.cs
  5. 2
      UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs
  6. 4
      UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs
  7. 8
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs
  8. 45
      UOP1_Project/Assets/Scripts/StateMachineTest/ChaseActionSO.cs
  9. 39
      UOP1_Project/Assets/Scripts/StateMachineTest/CloseToTargetConditionSO.cs
  10. 19
      UOP1_Project/Assets/Scripts/StateMachineTest/HungerActionSO.cs
  11. 16
      UOP1_Project/Assets/Scripts/StateMachineTest/HungerConditionSO.cs
  12. 23
      UOP1_Project/Assets/Scripts/StateMachineTest/TimerConditionSO.cs
  13. 25
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateActionSO.cs
  14. 26
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs
  15. 53
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateSO.cs
  16. 45
      UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableChaseAction.cs
  17. 39
      UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableCloseToTargetCondition.cs
  18. 19
      UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerAction.cs
  19. 16
      UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerCondition.cs
  20. 23
      UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableTimedCondition.cs
  21. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects.meta
  22. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs.meta
  23. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseActionSO.cs.meta
  24. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/CloseToTargetConditionSO.cs.meta
  25. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs.meta
  26. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/HungerActionSO.cs.meta
  27. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/HungerConditionSO.cs.meta
  28. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/TimerConditionSO.cs.meta
  29. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs
  30. 0
      /UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs
  31. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateSO.cs.meta
  32. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateActionSO.cs.meta
  33. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs.meta
  34. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs
  35. 0
      /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs.meta

2
UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/AttackingToChasing.asset


m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9e1893bf30b6f554a9d80007ee336691, type: 3}
m_Name: AttackToChase
m_Name: AttackingToChasing
m_EditorClassIdentifier:
_targetState: {fileID: 11400000, guid: 4b9ef9df9c479aa469d161e9019b218e, type: 2}
_conditions:

2
UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/ChasingToAttacking.asset


m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9e1893bf30b6f554a9d80007ee336691, type: 3}
m_Name: ChaseToAttack
m_Name: ChasingToAttacking
m_EditorClassIdentifier:
_targetState: {fileID: 11400000, guid: 1224ca0b6beeb2046aec4a8b312c69ec, type: 2}
_conditions:

2
UOP1_Project/Assets/ScriptableObjects/StateMachinesTest/Transitions/ChasingToEating.asset


m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9e1893bf30b6f554a9d80007ee336691, type: 3}
m_Name: ChaseToEat
m_Name: ChasingToEating
m_EditorClassIdentifier:
_targetState: {fileID: 11400000, guid: 8c494c32526baba4b96a81f042f10e01, type: 2}
_conditions:

2
UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachine.cs


public string CurrentState;
public bool debug;
#endif
[SerializeField] private Scriptables.ScriptableState _initialStateSO = null;
[SerializeField] private ScriptableObjects.StateSO _initialStateSO = null;
[SerializeField] private ScriptableObject[] _scriptableObjects = null;
private List<Type> _scriptableObjectsTypes = null;
private readonly Dictionary<Type, Component> _cachedComponents = new Dictionary<Type, Component>();

2
UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs


using UnityEngine;
[CreateAssetMenu(fileName = "ChaseData", menuName = "State Machines/Tests/Data/Chase Data")]
public class ChaseDataObject : ScriptableObject
public class ChaseDataSO : ScriptableObject
{
[SerializeField] private string _targetName = "Player";
[SerializeField] private float _speed = 10f;

4
UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using DeivSky.StateMachine.ScriptableObjects;
public class ScriptableEatAction : ScriptableStateAction
public class EatActionSO : StateActionSO
{
public float amount = 10f;

8
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs


using System.Collections.Generic;
using UnityEngine;
namespace DeivSky.StateMachine.Scriptables
namespace DeivSky.StateMachine.ScriptableObjects
public class ScriptableStateTransition : ScriptableObject
public class StateTransitionSO : ScriptableObject
[SerializeField] private ScriptableState _targetState = null;
[SerializeField] private StateSO _targetState = null;
[SerializeField] private ConditionUsage[] _conditions = default;
internal StateTransition GetTransition(StateMachine stateMachine, Dictionary<ScriptableObject, object> createdInstances)

public struct ConditionUsage
{
public Result ExpectedResult;
public ScriptableStateCondition Condition;
public StateConditionSO Condition;
public Operator Operator;
}

45
UOP1_Project/Assets/Scripts/StateMachineTest/ChaseActionSO.cs


using System;
using DeivSky.StateMachine;
using DeivSky.StateMachine.ScriptableObjects;
using UnityEngine;
[CreateAssetMenu(fileName = "Chase", menuName = "State Machines/Tests/Actions/Chase")]
public class ChaseActionSO : StateActionSO
{
[SerializeField] private ChaseDataSO _chaseData = null;
protected override StateAction CreateAction() => new ChaseAction(_chaseData);
}
public class ChaseAction : StateAction
{
private Transform _transform;
private Transform _chaseTransform;
private ChaseDataSO _chaseData;
public ChaseAction(ChaseDataSO chaseData) => _chaseData = chaseData;
public override void Awake(StateMachine stateMachine)
{
_transform = stateMachine.transform;
if (stateMachine.TryGetScriptableObject<ChaseDataSO>(out var chaseData))
_chaseData = chaseData;
if (_chaseData == null)
throw new ArgumentNullException(nameof(_chaseData));
if (string.IsNullOrEmpty(_chaseData.TargetName))
throw new ArgumentNullException(nameof(_chaseData.TargetName));
_chaseTransform = GameObject.Find(_chaseData.TargetName).transform;
}
public override void Perform()
{
_transform.position = Vector3.MoveTowards(
_transform.position,
_chaseTransform.position,
_chaseData.Speed * Time.deltaTime);
}
}

39
UOP1_Project/Assets/Scripts/StateMachineTest/CloseToTargetConditionSO.cs


using System;
using DeivSky.StateMachine;
using DeivSky.StateMachine.ScriptableObjects;
using UnityEngine;
[CreateAssetMenu(fileName = "CloseToTarget", menuName = "State Machines/Tests/Conditions/Close To Target")]
public class CloseToTargetConditionSO : StateConditionSO
{
[SerializeField] private ChaseDataSO _chaseData = null;
protected override Condition CreateCondition() => new CloseToTargetCondition(_chaseData);
}
public class CloseToTargetCondition : Condition
{
private Transform _transform;
private Transform _chaseTransform;
private ChaseDataSO _chaseData;
public CloseToTargetCondition(ChaseDataSO chaseData) => _chaseData = chaseData;
public override void Awake(StateMachine stateMachine)
{
_transform = stateMachine.transform;
if (stateMachine.TryGetScriptableObject<ChaseDataSO>(out var chaseData))
_chaseData = chaseData;
if (_chaseData == null)
throw new ArgumentNullException(nameof(_chaseData));
if (string.IsNullOrEmpty(_chaseData.TargetName))
throw new ArgumentNullException(nameof(_chaseData.TargetName));
_chaseTransform = GameObject.Find(_chaseData.TargetName).transform;
}
public override bool Statement() => Vector3.Distance(_transform.position, _chaseTransform.position) < 1f;
}

19
UOP1_Project/Assets/Scripts/StateMachineTest/HungerActionSO.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.ScriptableObjects;
using UnityEngine;
[CreateAssetMenu(fileName = "EnableHunger", menuName = "State Machines/Tests/Actions/Enable Hunger")]
public class HungerActionSO : ScriptableStateAction<HungerAction> { }
public class HungerAction : StateAction
{
private HungerComponent _hungerComponent;
public override void Awake(StateMachine stateMachine)
=> _hungerComponent = stateMachine.GetComponent<HungerComponent>();
public override void Perform() { }
public override void OnStateEnter() => _hungerComponent.ToggleHunger(true);
public override void OnStateExit() => _hungerComponent.ToggleHunger(false);
}

16
UOP1_Project/Assets/Scripts/StateMachineTest/HungerConditionSO.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.ScriptableObjects;
using UnityEngine;
[CreateAssetMenu(fileName = "IsHungry", menuName = "State Machines/Tests/Conditions/IsHungry")]
public class HungerConditionSO : ScriptableStateCondition<HungerCondition> { }
public class HungerCondition : Condition
{
private HungerComponent _hungerComponent;
public override void Awake(StateMachine stateMachine)
=> _hungerComponent = stateMachine.GetComponent<HungerComponent>();
public override bool Statement() => _hungerComponent.IsHungry;
}

23
UOP1_Project/Assets/Scripts/StateMachineTest/TimerConditionSO.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.ScriptableObjects;
using UnityEngine;
[CreateAssetMenu(fileName = "Timer", menuName = "State Machines/Tests/Conditions/Timer")]
public class TimerConditionSO : StateConditionSO
{
public float Duration = 10f;
protected override Condition CreateCondition() => new TimerCondition(Duration);
}
public class TimerCondition : Condition
{
private float _duration = 10f;
private float _time;
public TimerCondition(float duration) => _duration = duration;
public override void OnStateEnter() => _time = Time.time + _duration;
public override bool Statement() => Time.time >= _time;
}

25
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateActionSO.cs


using System.Collections.Generic;
using UnityEngine;
namespace DeivSky.StateMachine.ScriptableObjects
{
public abstract class StateActionSO : ScriptableObject
{
internal StateAction GetAction(StateMachine stateMachine, Dictionary<ScriptableObject, object> createdInstances)
{
if (createdInstances.TryGetValue(this, out var obj))
return (StateAction)obj;
var action = CreateAction();
createdInstances.Add(this, action);
action.Awake(stateMachine);
return action;
}
protected abstract StateAction CreateAction();
}
public abstract class ScriptableStateAction<T> : StateActionSO where T : StateAction, new()
{
protected override StateAction CreateAction() => new T();
}
}

26
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs


using System.Collections.Generic;
using UnityEngine;
namespace DeivSky.StateMachine.ScriptableObjects
{
public abstract class StateConditionSO : ScriptableObject
{
internal StateCondition GetCondition(StateMachine stateMachine, bool expectedResult, Dictionary<ScriptableObject, object> createdInstances)
{
if (createdInstances.TryGetValue(this, out var cond))
return new StateCondition((Condition)cond, expectedResult);
var condition = new StateCondition(CreateCondition(), expectedResult);
createdInstances.Add(this, condition._condition);
condition._condition.Awake(stateMachine);
return condition;
}
protected abstract Condition CreateCondition();
}
public abstract class ScriptableStateCondition<T> : StateConditionSO where T : Condition, new()
{
protected override Condition CreateCondition() => new T();
}
}

53
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateSO.cs


using System.Collections.Generic;
using UnityEngine;
namespace DeivSky.StateMachine.ScriptableObjects
{
[CreateAssetMenu(fileName = "New State", menuName = "State Machines/State")]
public class StateSO : ScriptableObject
{
[SerializeField] private StateActionSO[] _actions = null;
[SerializeField] private StateTransitionSO[] _transitions = null;
public State GetState(StateMachine stateMachine)
=> GetState(stateMachine, new Dictionary<ScriptableObject, object>());
internal State GetState(StateMachine stateMachine, Dictionary<ScriptableObject, object> createdInstances)
{
if (createdInstances.TryGetValue(this, out var obj))
return (State)obj;
var state = new State();
createdInstances.Add(this, state);
state.Name = name;
state._stateMachine = stateMachine;
state._transitions = GetTransitions(_transitions, stateMachine, createdInstances);
state._actions = GetActions(_actions, stateMachine, createdInstances);
return state;
}
private static StateTransition[] GetTransitions(StateTransitionSO[] scriptableTransitions,
StateMachine stateMachine, Dictionary<ScriptableObject, object> createdInstances)
{
int count = scriptableTransitions.Length;
var transitions = new StateTransition[count];
for (int i = 0; i < count; i++)
transitions[i] = scriptableTransitions[i].GetTransition(stateMachine, createdInstances);
return transitions;
}
private static StateAction[] GetActions(StateActionSO[] scriptableActions,
StateMachine stateMachine, Dictionary<ScriptableObject, object> createdInstances)
{
int count = scriptableActions.Length;
var actions = new StateAction[count];
for (int i = 0; i < count; i++)
actions[i] = scriptableActions[i].GetAction(stateMachine, createdInstances);
return actions;
}
}
}

45
UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableChaseAction.cs


using System;
using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using UnityEngine;
[CreateAssetMenu(fileName = "Chase", menuName = "State Machines/Tests/Actions/Chase")]
public class ScriptableChaseAction : ScriptableStateAction
{
[SerializeField] private ChaseDataObject _chaseData = null;
protected override StateAction CreateAction() => new ChaseAction(_chaseData);
}
public class ChaseAction : StateAction
{
private Transform _transform;
private Transform _chaseTransform;
private ChaseDataObject _chaseData;
public ChaseAction(ChaseDataObject chaseData) => _chaseData = chaseData;
public override void Awake(StateMachine stateMachine)
{
_transform = stateMachine.transform;
if (stateMachine.TryGetScriptableObject<ChaseDataObject>(out var chaseData))
_chaseData = chaseData;
if (_chaseData == null)
throw new ArgumentNullException(nameof(_chaseData));
if (string.IsNullOrEmpty(_chaseData.TargetName))
throw new ArgumentNullException(nameof(_chaseData.TargetName));
_chaseTransform = GameObject.Find(_chaseData.TargetName).transform;
}
public override void Perform()
{
_transform.position = Vector3.MoveTowards(
_transform.position,
_chaseTransform.position,
_chaseData.Speed * Time.deltaTime);
}
}

39
UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableCloseToTargetCondition.cs


using System;
using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using UnityEngine;
[CreateAssetMenu(fileName = "CloseToTarget", menuName = "State Machines/Tests/Conditions/Close To Target")]
public class ScriptableCloseToTargetCondition : ScriptableStateCondition
{
[SerializeField] private ChaseDataObject _chaseData = null;
protected override Condition CreateCondition() => new CloseToTargetCondition(_chaseData);
}
public class CloseToTargetCondition : Condition
{
private Transform _transform;
private Transform _chaseTransform;
private ChaseDataObject _chaseData;
public CloseToTargetCondition(ChaseDataObject chaseData) => _chaseData = chaseData;
public override void Awake(StateMachine stateMachine)
{
_transform = stateMachine.transform;
if (stateMachine.TryGetScriptableObject<ChaseDataObject>(out var chaseData))
_chaseData = chaseData;
if (_chaseData == null)
throw new ArgumentNullException(nameof(_chaseData));
if (string.IsNullOrEmpty(_chaseData.TargetName))
throw new ArgumentNullException(nameof(_chaseData.TargetName));
_chaseTransform = GameObject.Find(_chaseData.TargetName).transform;
}
public override bool Statement() => Vector3.Distance(_transform.position, _chaseTransform.position) < 1f;
}

19
UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerAction.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using UnityEngine;
[CreateAssetMenu(fileName = "EnableHunger", menuName = "State Machines/Tests/Actions/Enable Hunger")]
public class ScriptableHungerAction : ScriptableStateAction<HungerAction> { }
public class HungerAction : StateAction
{
private HungerComponent _hungerComponent;
public override void Awake(StateMachine stateMachine)
=> _hungerComponent = stateMachine.GetComponent<HungerComponent>();
public override void Perform() { }
public override void OnStateEnter() => _hungerComponent.ToggleHunger(true);
public override void OnStateExit() => _hungerComponent.ToggleHunger(false);
}

16
UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerCondition.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using UnityEngine;
[CreateAssetMenu(fileName = "IsHungry", menuName = "State Machines/Tests/Conditions/IsHungry")]
public class ScriptableHungerCondition : ScriptableStateCondition<HungerCondition> { }
public class HungerCondition : Condition
{
private HungerComponent _hungerComponent;
public override void Awake(StateMachine stateMachine)
=> _hungerComponent = stateMachine.GetComponent<HungerComponent>();
public override bool Statement() => _hungerComponent.IsHungry;
}

23
UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableTimedCondition.cs


using DeivSky.StateMachine;
using DeivSky.StateMachine.Scriptables;
using UnityEngine;
[CreateAssetMenu(fileName = "Timer", menuName = "State Machines/Tests/Conditions/Timer")]
public class ScriptableTimedCondition : ScriptableStateCondition
{
public float Duration = 10f;
protected override Condition CreateCondition() => new TimedCondition(Duration);
}
public class TimedCondition : Condition
{
private float _duration = 10f;
private float _time;
public TimedCondition(float duration) => _duration = duration;
public override void OnStateEnter() => _time = Time.time + _duration;
public override bool Statement() => Time.time >= _time;
}

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables.meta → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataObject.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableChaseAction.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseActionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableCloseToTargetCondition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/CloseToTargetConditionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableEatAction.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerAction.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/HungerActionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableHungerCondition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/HungerConditionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableTimedCondition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachineTest/TimerConditionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataObject.cs → /UOP1_Project/Assets/Scripts/StateMachineTest/ChaseDataSO.cs

/UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableEatAction.cs → /UOP1_Project/Assets/Scripts/StateMachineTest/EatActionSO.cs

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableState.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateAction.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateActionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateCondition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateTransition.cs → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs

/UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateTransition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateTransitionSO.cs.meta

正在加载...
取消
保存