-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cs
298 lines (294 loc) · 12.1 KB
/
GameManager.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class GameManager : MonoBehaviourPunCallbacks
{
public GameObject ReadyButton;
public GameObject Dice;
public GameObject Canvas;
public GameObject EnterReadyButton;
[Header("UI")]
public GameObject UIObject;
public GameObject TurnUI;
public bool ConfirmButtonIsClicked = false;
public bool ConfirmButtonCanShow = false;
public GameObject turn;
[Header("Three Dice")]
public GameObject RollButton;
public GameObject DiceUI;
public GameObject Dice1;
public GameObject Dice2;
public GameObject Dice3;
public GameObject Dice1postion;
public GameObject Dice2postion;
public GameObject Dice3postion;
public GameObject ConfirmButton;
public GameObject Player1;
public GameObject Player2;
public GameObject Player3;
public GameObject yesButton;
public GameObject GameOverText;
public GameObject myCard;
[Header("TurnController")]
public int confirmNum;
public int turnNum;
public void ReadyToPlay()
{
ReadyButton.SetActive(false);
Debug.Log(PhotonNetwork.CurrentRoom.Name);
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
{
Player1 = PhotonNetwork.Instantiate("Player1", new Vector3(350,150, 0), Quaternion.identity, 0);
var clone = PhotonNetwork.Instantiate("Card", new Vector3(0, 0, 0), Quaternion.identity, 0);
clone.name = "Player1Card";
photonView.RPC("UpdatePlayer",RpcTarget.All);
Debug.Log("Player1Coming");
}
else if (PhotonNetwork.CurrentRoom.PlayerCount == 2)
{
Player2 = PhotonNetwork.Instantiate("Player2", new Vector3(350, 100, 0), Quaternion.identity, 0);
var clone = PhotonNetwork.Instantiate("Card", new Vector3(0, 0, 0), Quaternion.identity, 0);
clone.name = "Player2Card";
photonView.RPC("UpdatePlayer",RpcTarget.All);
Debug.Log("Player2Coming");
}
else if (PhotonNetwork.CurrentRoom.PlayerCount == 3)
{
Player3 = PhotonNetwork.Instantiate("Player3", new Vector3(350, 50, 0), Quaternion.identity, 0);
PhotonNetwork.Instantiate("Card", new Vector3(0, 0, 0), Quaternion.identity, 0).name = "Player3Card";
photonView.RPC("UpdatePlayer",RpcTarget.All);
Debug.Log("Player3Coming");
}
//StartNextTurn();
}
public void ClickOnThisTurnOverButtton()
{
DiceUI.SetActive(false);
GameObject[] cardList = GameObject.FindGameObjectsWithTag("card");
foreach(GameObject card in cardList)
{
if (card.GetComponent<PhotonView>().IsMine)
{
Debug.Log(card.name);
UpdateCardNumbers(card);
}
}
}
public void UpdateCardNumbers(GameObject card)
{
foreach (Transform numChild in card.GetComponentsInChildren<Transform>(true))
{
Debug.Log(numChild.name);
if (numChild.tag == "CardNum")
{
numChild.GetComponent<PhotonView>().RPC("UpdateDice", RpcTarget.Others, numChild.GetComponent<Text>().text.ToString());
}
}
}
private void Awake()
{
UIObject.transform.SetAsLastSibling();
}
private void Start() {
}
private void Update()
{
if(myCard == null){
GameObject[] cardList = GameObject.FindGameObjectsWithTag("card");
foreach(GameObject card in cardList)
{
if (card.GetComponent<PhotonView>().IsMine)
{
myCard = card;
}
}
}//获取属于自己的卡片
CheckConfirmButton();
turn.GetComponent<Text>().text = confirmNum.ToString()+"Players have confirmed"; //多少人确认
TurnUI.GetComponent<Text>().text = "Player "+turnNum.ToString()+" turn"; //第几回合
CheckTurnOver();
}
public void Player1First()
{
GameObject.Find("Player1Card").GetComponent<Transform>().SetSiblingIndex(5);
}
public void Player2First()
{
GameObject.Find("Player2Card").GetComponent<Transform>().SetSiblingIndex(5);
}
public void Player3First()
{
GameObject.Find("Player3Card").GetComponent<Transform>().SetSiblingIndex(5);
}
public void setBiscuitActiveFalse(){
GameObject[] Biscuitlist=GameObject.FindGameObjectsWithTag("Eraser");
foreach(GameObject a in Biscuitlist){
if(a.GetComponentInChildren<PhotonView>().IsMine){
a.SetActive(false);
}
}
}
public void PressConfirmButton()
{
if (!ConfirmButtonIsClicked)
{
ConfirmButtonIsClicked = true;
GetComponent<PhotonView>().RPC("UpdateConfirmButton",RpcTarget.All);
}
}
public void CheckTurnOver(){
if(confirmNum == PhotonNetwork.CurrentRoom.PlayerCount){
Debug.Log("turnOver");
turnNum = turnNum%PhotonNetwork.CurrentRoom.PlayerCount+1;
GetComponent<PhotonView>().RPC("setDiceActive",RpcTarget.All);
ConfirmButtonIsClicked = false;
confirmNum = 0;
StartNextTurn();
}
}
public void ResetDices(GameObject dice,GameObject dicePostion){
dice.transform.position = dicePostion.transform.position;
dice.SetActive(true);
dice.GetComponentInChildren<PhotonView>().RPC("UpdateDice",RpcTarget.All,"?");
dice.GetComponentInChildren<PhotonView>().RPC("ResetDice",RpcTarget.All);
}
public void GameOverTest(){
gameObject.GetComponent<PhotonView>().RPC("GameOver",RpcTarget.All);
}
[PunRPC]
public void GameOver(){
GameObject[] scoreList = GameObject.FindGameObjectsWithTag("Score");
int max = -1;
GameObject maxCard = scoreList[0];
foreach(GameObject score in scoreList){
int sco = (int)float.Parse(score.GetComponent<Text>().text);
if(sco > max){
max = sco;
maxCard = score;
}
}
Debug.Log(maxCard.GetComponent<PhotonView>().Controller.NickName);
GameOverText.SetActive(true);
GameOverText.transform.SetAsLastSibling();
GameOverText.GetComponent<Text>().text = maxCard.GetComponent<PhotonView>().Controller.NickName+" Wins";
AudioManager.PauseBgm();
}
public void StartNextTurn(){
if(turnNum == Player1.GetComponent<PhotonView>().OwnerActorNr && Player1.GetComponent<PhotonView>().IsMine)
{
RollButton.SetActive(true);
}
else if(turnNum == Player2.GetComponent<PhotonView>().OwnerActorNr && Player2.GetComponent<PhotonView>().IsMine)
{
RollButton.SetActive(true);
}
else if(turnNum == Player3.GetComponent<PhotonView>().OwnerActorNr && Player3.GetComponent<PhotonView>().IsMine)
{
RollButton.SetActive(true);
}
}
[PunRPC]
public void setDiceActive()
{
Dice.SetActive(true);
ResetDices(Dice1,Dice1postion);
ResetDices(Dice2,Dice2postion);
ResetDices(Dice3,Dice3postion);
yesButton.SetActive(false);
}
[PunRPC]
void UpdateConfirmButton(){
confirmNum+=1;
}
public void UpdateName(string name1)
{
this.name = name1;
}
public void CheckConfirmButton()//检查确认键的出现情况
{
if (!EnterReadyButton.activeInHierarchy)
{
if (!Dice1.activeInHierarchy && !Dice2.activeInHierarchy && !Dice3.activeInHierarchy)
{
ConfirmButtonCanShow = true;
}
else if (!Dice1.activeInHierarchy && !Dice2.activeInHierarchy && Dice3.GetComponentInChildren<Text>().text == "?")
{
ConfirmButtonCanShow = true;
}
else if (!Dice2.activeInHierarchy && !Dice3.activeInHierarchy && Dice1.GetComponentInChildren<Text>().text == "?")
{
ConfirmButtonCanShow = true;
}
else if (!Dice1.activeInHierarchy && !Dice3.activeInHierarchy && Dice2.GetComponentInChildren<Text>().text == "?")
{
ConfirmButtonCanShow = true;
}
else if (!Dice1.activeInHierarchy && !Dice2.activeInHierarchy && Dice3.GetComponentInChildren<textUpdate>().isTrexDie ==true){
if(Dice3.GetComponentInChildren<Text>().text =="1"||Dice3.GetComponentInChildren<Text>().text =="2"){
if(myCard.GetComponent<Card>().Area12Full==true) ConfirmButtonCanShow = true;
}
else if(Dice3.GetComponentInChildren<Text>().text =="3"||Dice3.GetComponentInChildren<Text>().text =="4"){
if(myCard.GetComponent<Card>().Area34Full==true) ConfirmButtonCanShow = true;
}
else if(Dice3.GetComponentInChildren<Text>().text =="5"||Dice3.GetComponentInChildren<Text>().text =="6"){
if(myCard.GetComponent<Card>().Area56Full==true) ConfirmButtonCanShow = true;
}
}
else if (!Dice2.activeInHierarchy && !Dice3.activeInHierarchy && Dice1.GetComponentInChildren<textUpdate>().isTrexDie ==true){
if(Dice1.GetComponentInChildren<Text>().text =="1"||Dice1.GetComponentInChildren<Text>().text =="2"){
if(myCard.GetComponent<Card>().Area12Full==true) ConfirmButtonCanShow = true;
}
else if(Dice1.GetComponentInChildren<Text>().text =="3"||Dice1.GetComponentInChildren<Text>().text =="4"){
if(myCard.GetComponent<Card>().Area34Full==true) ConfirmButtonCanShow = true;
}
else if(Dice1.GetComponentInChildren<Text>().text =="5"||Dice1.GetComponentInChildren<Text>().text =="6"){
if(myCard.GetComponent<Card>().Area56Full==true) ConfirmButtonCanShow = true;
}
}
else if (!Dice1.activeInHierarchy && !Dice3.activeInHierarchy && Dice2.GetComponentInChildren<textUpdate>().isTrexDie ==true){
if(Dice2.GetComponentInChildren<Text>().text =="1"||Dice2.GetComponentInChildren<Text>().text =="2"){
if(myCard.GetComponent<Card>().Area12Full==true) ConfirmButtonCanShow = true;
}
else if(Dice2.GetComponentInChildren<Text>().text =="3"||Dice2.GetComponentInChildren<Text>().text =="4"){
if(myCard.GetComponent<Card>().Area34Full==true) ConfirmButtonCanShow = true;
}
else if(Dice2.GetComponentInChildren<Text>().text =="5"||Dice2.GetComponentInChildren<Text>().text =="6"){
if(myCard.GetComponent<Card>().Area56Full==true) ConfirmButtonCanShow = true;
}
}
else
{
ConfirmButtonCanShow = false;
}
}
if (ConfirmButtonCanShow == true && ConfirmButtonIsClicked == false )
{
ConfirmButton.SetActive(true);
}
else
{
ConfirmButton.SetActive(false);
}
}
[PunRPC]
public void UpdatePlayer(){
switch(PhotonNetwork.CurrentRoom.PlayerCount){
case 1:
this.Player1 = (GameObject.Find("Player1(Clone)"));
break;
case 2:
this.Player1 = (GameObject.Find("Player1(Clone)"));
this.Player2 = (GameObject.Find("Player2(Clone)"));
break;
case 3:
this.Player1 = (GameObject.Find("Player1(Clone)"));
this.Player2 = (GameObject.Find("Player2(Clone)"));
this.Player3 = (GameObject.Find("Player3(Clone)"));
break;
}
}
}