chenxin
2020-12-02 e6a2684b79e1b66844e37f99a18d17468455ee9e
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
using Core.Input;
using TowerDefense.UI.HUD;
using State = TowerDefense.UI.HUD.GameUI.State;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// TD Specific input switcher that also disables controls when the game is paused
    /// </summary>
    public class TowerDefenseInputSchemeSwitcher : InputSchemeSwitcher
    {
        /// <summary>
        /// Gets whether the game is in a paused state
        /// </summary>
        public bool isPaused
        {
            get { return GameUI.instance.state == State.Paused; }
        }
 
        /// <summary>
        /// Register GameUI's stateChanged event
        /// </summary>
        protected virtual void Start()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.stateChanged += OnUIStateChanged;
            }
        }
 
        /// <summary>
        /// Do nothing when game is paused
        /// </summary>
        protected override void Update()
        {
            if (isPaused)
            {
                return;
            }
            
            base.Update();
        }
 
        /// <summary>
        /// Unregister from GameUI's stateChanged event
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.stateChanged -= OnUIStateChanged;
            }
        }
 
        /// <summary>
        /// Activate or deactivate the current input scheme when the game pauses/unpauses
        /// </summary>
        void OnUIStateChanged(State oldState, State newState)
        {
            if (m_CurrentScheme == null)
            {
                return;
            }
            if (newState == State.Paused)
            {
                m_CurrentScheme.Deactivate(null);
            }
            else
            {
                m_CurrentScheme.Activate(null);
            }
        }
    }
}