Skip to content

Commit 2454077

Browse files
committed
Commit PusherManager.cs
1 parent bf656ad commit 2454077

File tree

1 file changed

+19
-31
lines changed
  • Examples/1-realtime-controlled-2d-game/RealtimeControlled2dGame/Assets

1 file changed

+19
-31
lines changed

Examples/1-realtime-controlled-2d-game/RealtimeControlled2dGame/Assets/PusherManager.cs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
public enum State
77
{
8-
IDLE,
9-
RUNRIGHT,
108
RUNLEFT,
9+
RUNRIGHT,
10+
IDLE,
1111
ATTACK
1212
}
1313

@@ -17,9 +17,9 @@ public class PusherManager : MonoBehaviour
1717
public static PusherManager instance = null;
1818
private Pusher _pusher;
1919
private Channel _channel;
20-
private State _currentState = State.IDLE;
2120
private const string APP_KEY = "APP_KEY";
2221
private const string APP_CLUSTER = "APP_CLUSTER";
22+
private State _state = State.IDLE;
2323

2424
async Task Start()
2525
{
@@ -63,38 +63,26 @@ private async Task InitialisePusher()
6363

6464
private void PusherOnConnected(object sender)
6565
{
66-
Debug.Log("Connected");
67-
68-
// Bind to game control events
69-
_channel.Bind("move_right", (dynamic data) =>
66+
_channel.Bind("run-left", (dynamic data) =>
7067
{
71-
Debug.Log("move_right received");
72-
SetCurrentState(State.RUNRIGHT);
68+
_state = State.RUNLEFT;
7369
});
7470

75-
_channel.Bind("move_left", (dynamic data) =>
71+
_channel.Bind("run-right", (dynamic data) =>
7672
{
77-
Debug.Log("move_left received");
78-
SetCurrentState(State.RUNLEFT);
79-
});
80-
81-
_channel.Bind("attack", (dynamic data) =>
82-
{
83-
Debug.Log("attack received");
84-
SetCurrentState(State.ATTACK);
73+
_state = State.RUNRIGHT;
8574
});
8675

8776
_channel.Bind("idle", (dynamic data) =>
8877
{
89-
Debug.Log("idle received");
90-
SetCurrentState(State.IDLE);
78+
_state = State.IDLE;
9179
});
9280

93-
// Keep the original event binding for compatibility
94-
_channel.Bind("my-event", (dynamic data) =>
81+
_channel.Bind("attack", (dynamic data) =>
9582
{
96-
Debug.Log("my-event received");
83+
_state = State.ATTACK;
9784
});
85+
Debug.Log("Connected");
9886
}
9987

10088
private void PusherOnConnectionStateChanged(object sender, ConnectionState state)
@@ -112,21 +100,21 @@ private void OnChannelOnSubscribed(object s, Channel channel)
112100
Debug.Log("Subscribed");
113101
}
114102

115-
async Task OnApplicationQuit()
103+
public void Message(string message)
116104
{
117-
if (_pusher != null)
118-
{
119-
await _pusher.DisconnectAsync();
120-
}
105+
_channel?.Trigger("time has occured", message);
121106
}
122107

123108
public State CurrentState()
124109
{
125-
return _currentState;
110+
return _state;
126111
}
127112

128-
public void SetCurrentState(State newState)
113+
async Task OnApplicationQuit()
129114
{
130-
_currentState = newState;
115+
if (_pusher != null)
116+
{
117+
await _pusher.DisconnectAsync();
118+
}
131119
}
132120
}

0 commit comments

Comments
 (0)