Skip to content
Snippets Groups Projects
Commit cc5458b5 authored by Heine, Eric R's avatar Heine, Eric R
Browse files

Added a class that fires game events based on its current state.

parent fb6eceab
No related branches found
No related tags found
1 merge request!4Merge Request for Issue/6
fileFormatVersion: 2
guid: a51ef5da8031fd141a078805d54ccb66
folderAsset: yes
timeCreated: 1510599145
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
{
"name": "Shared.StateManager",
"references": [
"GUID:6735fe1fe1dbd994a96eb9d888b81400"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
\ No newline at end of file
fileFormatVersion: 2
guid: bf6ca905532d08647bc1c0964551b0f5
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
using Shared.ScriptableVariables;
namespace Shared.StateManager {
// A scriptable object that represents one state a StateManager can be in
[CreateAssetMenu(menuName = "Scriptable Objects/State")]
public class State : ScriptableObject {
[Tooltip("The GameEvent that will switch a StateManager into this State")]
public GameEvent enterEvent;
[Tooltip("The list of GameEvents to fire when this State gets activated")]
public List<GameEvent> inEvents = new List<GameEvent>();
[Tooltip("The list of GameEvents to fire when this State gets deactivated")]
public List<GameEvent> outEvents = new List<GameEvent>();
// ------------------------------------------------------------------------
public void EnterState() {
foreach (var inEvent in inEvents) {
inEvent?.Raise();
}
}
// ------------------------------------------------------------------------
public void ExitState() {
foreach (var outEvent in outEvents) {
outEvent?.Raise();
}
}
}
}
fileFormatVersion: 2
guid: f4ee41ed730737741b3c93effc4fb5a9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Shared.ScriptableVariables;
namespace Shared.StateManager {
// Manages what State is currently active in a given scene
public class StateManager : MonoBehaviour {
[Tooltip("The state we should start out with when this manager is enabled")]
public State startingState;
[Tooltip("The list of possible states this component is managing")]
public List<State> possibleStates = new List<State>();
[Tooltip("The currently active state for this manager")]
[SerializeField]
private State currentState;
// A list of delegates the possible states are listening to for entering so
// we can stop listening on disable
private Dictionary<State, GameEvent.GameEventRaised> stateListeners = new Dictionary<State, GameEvent.GameEventRaised>();
// ------------------------------------------------------------------------
void OnEnable() {
StartCoroutine(ActivateStartingState());
// Start listening to the events that will change our state
foreach (var state in possibleStates) {
GameEvent.GameEventRaised listener = delegate() { ActivateState(state); };
state.enterEvent.OnRaised += listener;
stateListeners.Add(state, listener);
}
}
// ------------------------------------------------------------------------
void OnDisable() {
if (currentState != null) {
DeactivateState(currentState);
}
// Stop listening to the events that will change our state
foreach (var state in possibleStates) {
state.enterEvent.OnRaised -= stateListeners[state];
}
stateListeners.Clear();
}
// ------------------------------------------------------------------------
private IEnumerator ActivateStartingState() {
// Wait one frame to make sure all starting listeners have a chance to setup
yield return null;
ActivateState(startingState);
}
// ------------------------------------------------------------------------
private void ActivateState(State state) {
// Can't transition into a state we're already in
if (currentState != state) {
if (currentState != null) {
DeactivateState(currentState);
}
currentState = state;
state.EnterState();
}
}
// ------------------------------------------------------------------------
private void DeactivateState(State state) {
if (state == currentState) {
currentState = null;
}
state.ExitState();
}
}
}
fileFormatVersion: 2
guid: a55df0166f51f7941954952e3c47dfa0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -2,7 +2,7 @@
"name": "com.futuretech.shared",
"displayName": "FutureTech Shared",
"description": "Contains shared items such as the Scriptable Variables.",
"version": "0.1.23",
"version": "0.1.24",
"unity": "2019.2",
"license": "MIT",
"repository": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment