forked from hengineer/CaptainsMess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamplePlayerScript.cs
More file actions
161 lines (136 loc) · 3.38 KB
/
ExamplePlayerScript.cs
File metadata and controls
161 lines (136 loc) · 3.38 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.Collections;
public class ExamplePlayerScript : CaptainsMessPlayer
{
public Image image;
public Text nameField;
public Text readyField;
public Text rollResultField;
public Text totalPointsField;
[SyncVar]
public Color myColour;
// Simple game states for a dice-rolling game
[SyncVar]
public int rollResult;
[SyncVar]
public int totalPoints;
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
// Send custom player info
// This is an example of sending additional information to the server that might be needed in the lobby (eg. colour, player image, personal settings, etc.)
myColour = UnityEngine.Random.ColorHSV(0,1,1,1,1,1);
CmdSetCustomPlayerInfo(myColour);
}
[Command]
public void CmdSetCustomPlayerInfo(Color aColour)
{
myColour = aColour;
}
[Command]
public void CmdRollDie()
{
rollResult = UnityEngine.Random.Range(1, 7);
}
[Command]
public void CmdPlayAgain()
{
ExampleGameSession.instance.PlayAgain();
}
public override void OnClientEnterLobby()
{
base.OnClientEnterLobby();
// Brief delay to let SyncVars propagate
Invoke("ShowPlayer", 0.5f);
}
public override void OnClientReady(bool readyState)
{
if (readyState)
{
readyField.text = "READY!";
readyField.color = Color.green;
}
else
{
readyField.text = "not ready";
readyField.color = Color.red;
}
}
void ShowPlayer()
{
transform.SetParent(GameObject.Find("Canvas/PlayerContainer").transform, false);
image.color = myColour;
nameField.text = deviceName;
readyField.gameObject.SetActive(true);
rollResultField.gameObject.SetActive(false);
totalPointsField.gameObject.SetActive(false);
OnClientReady(IsReady());
}
public void Update()
{
totalPointsField.text = "Points: " + totalPoints.ToString();
if (rollResult > 0) {
rollResultField.text = "Roll: " + rollResult.ToString();
} else {
rollResultField.text = "";
}
}
[ClientRpc]
public void RpcOnStartedGame()
{
readyField.gameObject.SetActive(false);
rollResultField.gameObject.SetActive(true);
totalPointsField.gameObject.SetActive(true);
}
void OnGUI()
{
if (isLocalPlayer)
{
GUILayout.BeginArea(new Rect(0, Screen.height * 0.8f, Screen.width, 100));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
ExampleGameSession gameSession = ExampleGameSession.instance;
if (gameSession)
{
if (gameSession.gameState == GameState.Lobby ||
gameSession.gameState == GameState.Countdown)
{
if (GUILayout.Button(IsReady() ? "Not ready" : "Ready", GUILayout.Width(Screen.width * 0.3f), GUILayout.Height(100)))
{
if (IsReady()) {
SendNotReadyToBeginMessage();
} else {
SendReadyToBeginMessage();
}
}
}
else if (gameSession.gameState == GameState.WaitingForRolls)
{
if (rollResult == 0)
{
if (GUILayout.Button("Roll Die", GUILayout.Width(Screen.width * 0.3f), GUILayout.Height(100)))
{
CmdRollDie();
}
}
}
else if (gameSession.gameState == GameState.GameOver)
{
if (isServer)
{
if (GUILayout.Button("Play Again", GUILayout.Width(Screen.width * 0.3f), GUILayout.Height(100)))
{
CmdPlayAgain();
}
}
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
}