Repository navigation
Expand file tree
/
Copy pathFlowController.cs
More file actions
93 lines (82 loc) · 2.54 KB
/
Copy pathFlowController.cs
File metadata and controls
93 lines (82 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System.Collections.Generic;
using Meta.XR.Samples;
using UnityEngine;
namespace SpatialLingo.States
{
[MetaCodeSample("SpatialLingo")]
public class FlowContext
{
public object Data;
}
[MetaCodeSample("SpatialLingo")]
public class NextStateContext
{
public string Name;
public FlowState State;
public object Data;
}
[MetaCodeSample("SpatialLingo")]
public class FlowController : MonoBehaviour
{
private FlowState m_currentState;
private NextStateContext m_nextContext;
private Dictionary<string, FlowState> m_states = new();
public void AddState(string name, FlowState prefab)
{
m_states[name] = prefab;
}
private void StartFlow(string name, object data)
{
if (m_states.TryGetValue(name, out var state))
{
var context = new NextStateContext
{
State = state,
Name = name,
Data = data
};
m_nextContext = context;
}
}
internal virtual void OnSendFlowSignal(string name, object context)
{
// No op
}
public virtual void Update()
{
if (m_nextContext != null)
{
// Current state pre-stop
if (m_currentState != null)
{
m_currentState.WillLoseFocus(m_nextContext.Data);
}
// Next state pre-start
FlowState nextState = null;
var nextStatePrefab = m_nextContext.State;
if (nextStatePrefab != null)
{
nextState = Instantiate(nextStatePrefab);
nextState.SendFlowSignal += OnSendFlowSignal;
nextState.WillGetFocus(m_nextContext.Data);
}
// Switch states
m_nextContext = null;
var previousState = m_currentState;
m_currentState = nextState;
// Prev state stop
if (previousState != null)
{
previousState.DidLoseFocus();
Destroy(previousState);
}
// Next State Start
if (m_currentState != null)
{
m_currentState.DidGetFocus();
}
}
}
}
}