Fix atomstag bug

This commit is contained in:
Adam Ramberg 2020-03-15 23:18:29 +01:00
parent e6ac211e70
commit 90079f5873
24 changed files with 332 additions and 94 deletions

View File

@ -16,8 +16,13 @@ public class DestroyMe : MonoBehaviour
}
}
public void DestroyImmediate()
public void DestroyImmediate() => Destroy(gameObject);
public void DestroyIfZeroOfBelow(int value)
{
Destroy(gameObject);
if (value <= 0)
{
DestroyImmediate();
}
}
}

View File

@ -215,6 +215,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b6d7e87282ce6479184ce08bf6dbe121, type: 3}
m_Name:
m_EditorClassIdentifier:
_tagToTarget:
_usage: 1
_value:
_constant: {fileID: 11400000, guid: 23f42a77324bd4158a00555f04f7824e, type: 2}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_enemyState:
_usage: 1
_fsm: {fileID: 0}
@ -290,8 +296,8 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1740680909}
m_MethodName: DestroyImmediate
m_Mode: 1
m_MethodName: DestroyIfZeroOfBelow
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine

View File

@ -13,9 +13,9 @@ MonoBehaviour:
m_Name: EnemyHealthBase
m_EditorClassIdentifier:
_developerDescription:
_value: 0
_initialValue: 0
_oldValue: 0
_value: 20
_initialValue: 20
_oldValue: 20
Changed: {fileID: 11400000, guid: d481ae3b5fe854cd187b0e79017e8d1e, type: 2}
ChangedWithHistory: {fileID: 0}
_preChangeTransformers: []

View File

@ -20,16 +20,18 @@ public class EnemyMovement : MonoBehaviour
void Awake()
{
var target = AtomTags.FindByTag(_tagToTarget.Value).transform;
Transform target = null;
AtomTags.OnInitialization(() => target = AtomTags.FindByTag(_tagToTarget.Value).transform);
var body = GetComponent<Rigidbody2D>();
_enemyState.Machine.OnUpdate((deltaTime, value) => body.Move((target.position - transform.position), value == "CHASING" ? 2f : 0f, deltaTime), gameObject);
_enemyState.Machine.DispatchWhen(command: "ATTACK", (value) => value == "CHASING" && (_shootingRange.Value >= Vector3.Distance(target.position, transform.position)), gameObject);
_enemyState.Machine.DispatchWhen(command: "CHASE", (value) => value == "ATTACKING" && (_shootingRange.Value < Vector3.Distance(target.position, transform.position)), gameObject);
_enemyState.Machine.OnUpdate((deltaTime, value) =>
{
if (target)
{
body.Move((target.position - transform.position), value == "CHASING" ? 2f : 0f, deltaTime);
}
}, gameObject);
_enemyState.Machine.DispatchWhen(command: "ATTACK", (value) => target != null && value == "CHASING" && (_shootingRange.Value >= Vector3.Distance(target.position, transform.position)), gameObject);
_enemyState.Machine.DispatchWhen(command: "CHASE", (value) => target != null && value == "ATTACKING" && (_shootingRange.Value < Vector3.Distance(target.position, transform.position)), gameObject);
}
// void Start()
// {
// _enemyState.Machine.Begin();
// }
}

View File

@ -1,9 +1,12 @@
using UnityAtoms.BaseAtoms;
using UnityAtoms.FSM;
using UnityEngine;
using UnityAtoms.Tags;
public class EnemyShooting : MonoBehaviour
{
[SerializeField]
private StringReference _tagToTarget;
[SerializeField]
private FiniteStateMachineReference _enemyState;
[SerializeField]
@ -11,10 +14,17 @@ public class EnemyShooting : MonoBehaviour
void Awake()
{
Transform target = null;
AtomTags.OnInitialization(() => target = AtomTags.FindByTag(_tagToTarget.Value).transform);
_enemyState.Machine.OnStateCooldown("ATTACKING", (value) =>
{
var spawnPos = transform.position + transform.right;
Instantiate(_projectile, spawnPos, transform.rotation);
if (target)
{
Debug.Log(target.gameObject.name);
var spawnPos = transform.position + transform.right;
Instantiate(_projectile, spawnPos, transform.rotation);
}
}, gameObject);
}
}

View File

@ -0,0 +1,16 @@
using UnityEngine;
using UnityAtoms.FSM;
public class GameStateDispatcher : MonoBehaviour
{
[SerializeField]
private FiniteStateMachineReference _gameStateRef;
public void DispatchGameOverIfDead(int health)
{
if (health <= 0)
{
_gameStateRef.Machine.Dispatch("SetGameOver");
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 4702c99e0a5c246d4b93548c01ce6cd0
guid: 578b39322c0ba4e3fb1c53fbaf8ea6df
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,73 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a56d454e768904df09296076c1aaea26, type: 3}
m_Name: GameStateMachine
m_EditorClassIdentifier:
_developerDescription:
_value:
_initialValue: InGame
_oldValue:
Changed: {fileID: 11400000, guid: 3979f8535cda044809f98bde2acbadcd, type: 2}
ChangedWithHistory: {fileID: 0}
_preChangeTransformers: []
_transitionStarted: {fileID: 0}
_completeCurrentTransition: {fileID: 0}
_states:
_list:
- _id:
_usage: 0
_value: InGame
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_cooldown:
_usage: 0
_value: 0
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_subMachine: {fileID: 0}
- _id:
_usage: 0
_value: GameOver
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_cooldown:
_usage: 0
_value: 0
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_subMachine: {fileID: 0}
_transitions:
_list:
- _fromState:
_usage: 0
_value: InGame
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_toState:
_usage: 0
_value: GameOver
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_command:
_usage: 0
_value: SetGameOver
_constant: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
_testCondition: {fileID: 0}
_raiseEventToCompleteTransition: 0

View File

@ -1,27 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a56d454e768904df09296076c1aaea26, type: 3}
m_Name: GameStateMachine
m_EditorClassIdentifier:
_developerDescription:
_value:
_initialValue:
_oldValue:
Changed: {fileID: 11400000, guid: 3979f8535cda044809f98bde2acbadcd, type: 2}
ChangedWithHistory: {fileID: 0}
_preChangeTransformers: []
_transitionStarted: {fileID: 0}
_completeCurrentTransition: {fileID: 0}
_states:
_list: []
_transitions:
_list: []

View File

@ -570,7 +570,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: bd63f3a800fe74586b3a87357d763173, type: 3}
m_Name:
m_EditorClassIdentifier:
_event: {fileID: 0}
_event: {fileID: 11400000, guid: b512e6d2a87f440c5883fe3546438a16, type: 2}
_eventWithGameObjectReference: {fileID: 0}
_selectGameObjectReference: {fileID: 0}
--- !u!114 &754865917
@ -603,6 +603,8 @@ GameObject:
- component: {fileID: 854994186}
- component: {fileID: 854994187}
- component: {fileID: 854994188}
- component: {fileID: 854994189}
- component: {fileID: 854994190}
m_Layer: 0
m_Name: Player
m_TagString: Untagged
@ -766,7 +768,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
_tags:
- {fileID: 11400000, guid: 23f42a77324bd4158a00555f04f7824e, type: 2}
- {fileID: 0}
--- !u!114 &854994186
MonoBehaviour:
m_ObjectHideFlags: 0
@ -807,7 +808,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 854994178}
m_Enabled: 0
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 050ff37b5550f4b7e9bdf8b985aff484, type: 3}
m_Name:
@ -815,7 +816,29 @@ MonoBehaviour:
_developerDescription:
_unityEventResponse:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 854994189}
m_MethodName: DispatchGameOverIfDead
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument: SetGameOver
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 854994187}
m_MethodName: DestroyIfZeroOfBelow
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
_actionResponses: []
_eventReference:
_usage: 0
@ -823,6 +846,36 @@ MonoBehaviour:
_eventInstancer: {fileID: 0}
_variable: {fileID: 0}
_variableInstancer: {fileID: 0}
--- !u!114 &854994189
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 854994178}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 578b39322c0ba4e3fb1c53fbaf8ea6df, type: 3}
m_Name:
m_EditorClassIdentifier:
_gameStateRef:
_usage: 0
_fsm: {fileID: 11400000, guid: 42e941e460d424571aa897b211cee70e, type: 2}
_fsmInstancer: {fileID: 0}
--- !u!114 &854994190
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 854994178}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 96934656608554bbdb024f928c94a23b, type: 3}
m_Name:
m_EditorClassIdentifier:
_variables:
- {fileID: 11400000, guid: 50206c000343d496fa82bd362e3b3b2b, type: 2}
--- !u!1 &1067648696
GameObject:
m_ObjectHideFlags: 0

View File

@ -1,13 +0,0 @@
using UnityAtoms.FSM;
using UnityEngine;
public class KillPlayer : MonoBehaviour
{
[SerializeField]
private FiniteStateMachineReference _gameStateRef;
void Awake()
{
// _gameStateRef.Machine.
}
}

View File

@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 04d35e2eb934747da9d77a3af62bb8ca, type: 3}
m_Name: PlayerOnAwake
m_EditorClassIdentifier:
_developerDescription:
_replayBufferSize: 1

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 140c1256568a7460e97b92aa7c774ba7
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,8 +3,5 @@ using UnityEngine.SceneManagement;
public class RestartCurrentScene : MonoBehaviour
{
public void Do()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void Do() => SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

View File

@ -14,8 +14,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
_developerDescription:
_value: 0
_initialValue: 100
_oldValue: 0
_initialValue: 20
_oldValue: 10
Changed: {fileID: 11400000, guid: fb1508338b66f42099fceb52aa22cf4c, type: 2}
ChangedWithHistory: {fileID: 0}
_preChangeTransformers: []

View File

@ -18,6 +18,7 @@
public static class ExecutionOrder
{
public const int VARIABLE_RESETTER = -200;
public const int VARIABLE_INSTANCER = -100;
}

View File

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
namespace UnityAtoms
{

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
[AddComponentMenu("Unity Atoms/Variable Resetter")]
[DefaultExecutionOrder(Runtime.ExecutionOrder.VARIABLE_RESETTER)]
public class VariableResetter : MonoBehaviour
{
public List<AtomBaseVariable> _variables = new List<AtomBaseVariable>();
void OnEnable()
{
for (var i = 0; i < _variables.Count; ++i)
{
_variables[i].Reset();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 96934656608554bbdb024f928c94a23b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,7 @@
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityAtoms.BaseAtoms;
using UnityAtoms;
@ -59,7 +61,7 @@ namespace UnityAtoms.FSM
/// </summary>
private string _currentFlatValue;
private void Awake()
private void OnEnable()
{
if (CompleteCurrentTransition != null && CompleteCurrentTransition.ReplayBufferSize > 0)
{
@ -67,11 +69,33 @@ namespace UnityAtoms.FSM
CompleteCurrentTransition.ReplayBufferSize = 0;
}
FiniteStateMachineMonoHook.GetInstance(createIfNotExist: true).OnUpdate -= OnUpdate;
FiniteStateMachineMonoHook.GetInstance().OnUpdate += OnUpdate;
_isUpdatingState = false;
_currentTransition = null;
_resetOnNextTransitionCompleted = false;
_triggerEventsOnNextReset = false;
FiniteStateMachineMonoHook.GetInstance().OnStart -= OnStart;
FiniteStateMachineMonoHook.GetInstance().OnStart += OnStart;
// Make sure application is playing
if (Application.isPlaying)
{
FiniteStateMachineMonoHook.GetInstance(createIfNotExist: true).OnUpdate -= OnUpdate;
FiniteStateMachineMonoHook.GetInstance().OnUpdate += OnUpdate;
FiniteStateMachineMonoHook.GetInstance().OnStart -= OnStart;
FiniteStateMachineMonoHook.GetInstance().OnStart += OnStart;
}
else
{
UnityAction<Scene, LoadSceneMode> handler = null;
handler = (scene, mode) =>
{
SceneManager.sceneLoaded -= handler;
FiniteStateMachineMonoHook.GetInstance(createIfNotExist: true).OnUpdate -= OnUpdate;
FiniteStateMachineMonoHook.GetInstance().OnUpdate += OnUpdate;
Reset();
};
SceneManager.sceneLoaded += handler;
}
}
private void OnDisable()
@ -230,10 +254,7 @@ namespace UnityAtoms.FSM
{
// State doesn't exist in this FSM, propagate down to all sub FSMs.
var state = GetState(_currentFlatValue);
if (state.SubMachine != null)
{
state.SubMachine.Dispatch(command);
}
state?.SubMachine?.Dispatch(command);
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
@ -19,7 +20,21 @@ namespace UnityAtoms.Tags
/// Get the tags associated with this GameObject as `StringConstants` in a `ReadOnlyList&lt;T&gt;`.
/// </summary>
/// <value>The tags associated with this GameObject as `StringConstants` in a `ReadOnlyList&lt;T&gt;`.</value>
public ReadOnlyList<StringConstant> Tags { get; private set; }
public ReadOnlyList<StringConstant> Tags
{
get
{
if (_readOnlyTags == null || _readOnlyTags.Count != _sortedTags.Values.Count)
{
_readOnlyTags = new ReadOnlyList<StringConstant>(_sortedTags.Values);
}
return _readOnlyTags;
}
private set => _readOnlyTags = value;
}
private ReadOnlyList<StringConstant> _readOnlyTags;
[SerializeField]
private List<StringConstant> _tags = new List<StringConstant>();
@ -31,7 +46,7 @@ namespace UnityAtoms.Tags
private static readonly Dictionary<GameObject, AtomTags> TagInstances
= new Dictionary<GameObject, AtomTags>();
private static Action _onInitialization;
#region Serialization
@ -75,21 +90,32 @@ namespace UnityAtoms.Tags
#region Lifecycles
private void Awake()
{
Tags = new ReadOnlyList<StringConstant>(_sortedTags.Values);
}
private void OnEnable()
{
if (!TagInstances.ContainsKey(gameObject)) TagInstances.Add(gameObject, this);
for (var i = 0; i < Tags.Count; i++)
if (!IsInitalized(gameObject))
{
var stringConstant = Tags[i];
if (stringConstant == null) continue;
var tag = stringConstant.Value;
if (!TaggedGameObjects.ContainsKey(tag)) TaggedGameObjects.Add(tag, new List<GameObject>());
TaggedGameObjects[tag].Add(gameObject);
TaggedGameObjects.Clear();
TagInstances.Clear();
var _atomTagsInScene = GameObject.FindObjectsOfType<AtomTags>();
for (var i = 0; i < _atomTagsInScene.Length; ++i)
{
var atomTags = _atomTagsInScene[i];
var tagCount = atomTags.Tags.Count;
var go = _atomTagsInScene[i].gameObject;
if (!TagInstances.ContainsKey(go)) TagInstances.Add(go, atomTags);
for (var y = 0; y < tagCount; ++y)
{
var stringConstant = atomTags.Tags[y];
if (stringConstant == null) continue;
var tag = stringConstant.Value;
if (!TaggedGameObjects.ContainsKey(tag)) TaggedGameObjects.Add(tag, new List<GameObject>());
TaggedGameObjects[tag].Add(go);
}
}
_onInitialization?.Invoke();
_onInitialization = null;
}
}
@ -107,6 +133,19 @@ namespace UnityAtoms.Tags
#endregion
public static void OnInitialization(Action handler)
{
var atomTags = GameObject.FindObjectOfType<AtomTags>();
if (atomTags != null && !IsInitalized(atomTags.gameObject))
{
_onInitialization += handler;
}
else
{
handler();
}
}
/// <summary>
/// Check if the tag provided is associated with this `GameObject`.
/// </summary>
@ -214,5 +253,7 @@ namespace UnityAtoms.Tags
var tags = TagInstances[go];
return tags.Tags;
}
private static bool IsInitalized(GameObject go) => TagInstances.ContainsKey(go);
}
}