//所有成员开始游戏都会执行的相同操作 void StartGame() { //如果上局为赢家 ,隐藏 winbanner if (_IWin) { _IWin = false; AnimElement.Prepare() .InitUGUI(new Vector3(0, 300, 0), new Vector3(0, 110, 0), _winBanner, 0, 25) .SetReverseAnimation(true) .Play(); } //玩家头像移动到目标位置 for (int i = 0; i < _playerIcons.Count; i++) { Transform icon = _playerIcons[i]; icon.SetParent(_canvas.transform); AnimElement.Prepare() .InitLocal(icon.localPosition, // id从1开始 FixedPos4PlayerIcon[GetPlayerDeckIndex(i + 1)], icon, 0, 40) .AddScaleAnimation(icon.localScale, new Vector3(0.25f, 0.25f)) .Play(); } //隐藏掉等待界面Panel _readyPanel.SetActive(false); print("游戏开始 人数: " + _playerNum); //将世界放平 _planet.transform.localRotation = Quaternion.Euler(new Vector3()); //有空了用插值动画实现 //关掉kinematic foreach (var brick in _allBricks) { brick.GetComponent <Rigidbody>().isKinematic = false; } //开启重力 Physics.gravity = new Vector3(0, -30, 0); //定时启动kinematic Invoke("SetAllKinematic", 3); //游戏状态----InitialSelectBricks GameState.Instance.CurrentState = GameState.State.InitialSelectBricks; }
void Awake() { Application.runInBackground = true; //防止后台掉线 CustomTypes.Register(); _client = new PhotonClient(); _client.AppId = "49395063-2a0e-4e1f-a35d-3d4600790bc2"; //Turnbased 的 ID //_client.AppId="bc31b098-957d-4231-9ad5-b75adc3c6fb7";// RealTime 的ID bool connectInProcess = _client.ConnectToRegionMaster("asia"); _go_GuessBricks = GameObject.Find("GuessBricks").transform; MyDock = GameObject.Find("DockD").transform; PickPop = GameObject.Find("PickPop").transform; _planet = GameObject.Find("Planet"); _table = _planet.transform.GetChild(0).gameObject; _unOccupied = _table.transform.GetChild(5).gameObject; //--------------找到UI元素------------ Notify = GetComponent <Notification>(); Panel = GameObject.Find("Panel").transform; StartBtn = GameObject.Find("Start").transform; _canvas = GameObject.Find("Canvas"); _winBanner = GameObject.Find("Win").transform; _panel = GameObject.Find("Panel"); _readyPanel = GameObject.Find("ReadyPanel"); _overPanel = GameObject.Find("OverPanel"); DoneBtn = GameObject.Find("AdjustDone").transform; CancelBtn = GameObject.Find("Cancel").transform; ContinueBtn = GameObject.Find("ContinueGuess").transform; _loading = GameObject.Find("Loading").transform; //----------------------- if (GameControl._instance == null) { GameControl._instance = this; } DontDestroyOnLoad(gameObject); #region 注册UI事件 GameObject.Find("Create").GetComponent <Button>().onClick.AddListener( () => { if (_roomName != "") { _client.OpCreateRoom(_roomName, new RoomOptions() { MaxPlayers = _playerNum }, TypedLobby.Default); } }); GameObject.Find("Join").GetComponent <Button>().onClick.AddListener( () => { if (_roomName != "") { _client.OpJoinRoom(_roomName); } }); GameObject.Find("PlayerNum").GetComponent <Dropdown>().onValueChanged .AddListener(x => { _playerNum = (byte)(x + 2); }); GameObject.Find("RoomName").GetComponent <InputField>().onValueChanged .AddListener(x => { _roomName = x; }); GameObject.Find("Leave").GetComponent <Button>().onClick.AddListener( () => { //销毁玩家头像 foreach (var icon in _playerIcons) { Destroy(icon.gameObject); } _playerIcons.Clear(); //通知其他人 我离开了 _client.OpRaiseEvent(MyEventCode.OneLeave, _myID, true, RaiseEventOptions.Default); _client.OpLeaveRoom(false); _overPanel.SetActive(false); _panel.SetActive(true); }); GameObject.Find("Again").GetComponent <Button>().onClick.AddListener( () => { //currentID 为自己 CurrentID = _myID; //重新设置玩家头像的parent for (int i = 0; i < _playerNum; i++) { var icon = _playerIcons[i]; icon.SetParent(_readyPanel.transform); } //自己的头像放到readypos 并且通知别人 //playerIcons readyPos的顺序和id一样 注意ID从1 开始 var myIcon = _playerIcons[_myID - 1]; AnimElement.Prepare() .InitUGUI(myIcon.localPosition, readyPos[_myID - 1], myIcon, 0, 40) .AddScaleAnimation(myIcon.localScale, Vector3.one * 0.6f) .Play(); _client.OpRaiseEvent(MyEventCode.OnePlayAgain, _myID, true, RaiseEventOptions.Default); //载入等待面板 _overPanel.SetActive(false); _readyPanel.SetActive(true); EnterRoom(_playerNum); }); //房主开始游戏 GameObject.Find("Start").GetComponent <Button>().onClick.AddListener( () => { //洗牌 ShuffleDeck(); //----------------------------- //把所有的牌发送给其他玩家 //----------------------- _client.OpRaiseEvent(MyEventCode.SyncAllBricks, indexMap, true , RaiseEventOptions.Default); //放置牌到桌子上 PlaceAllBricksOnTable(indexMap); StartGame(); }); // 完成 按钮的点击逻辑 DoneBtn.GetComponent <Button>().onClick.AddListener(() => { // 玩家确认完成Adjust过程中的joker放置后 点击 if (GameState.Instance.CurrentState == GameState.State.Adjust) { //通知其他玩家在逻辑层更新我的Joker位置 int msgType = 0; int[] msg = new int[4]; msg[0] = _myID; if (_decks[_myDeckIndex].Contains(_jokerWhite)) { msgType = -1; msg[2] = _decks[_myDeckIndex].IndexOf(_jokerWhite); } if (_decks[_myDeckIndex].Contains(_jokerBlack)) { msgType = -msgType; msg[3] = _decks[_myDeckIndex].IndexOf(_jokerBlack); } msg[1] = msgType; _client.OpRaiseEvent(MyEventCode.SyncJoker, msg, true, RaiseEventOptions.Default); // LocalReadyToGo(); //隐藏所有arrows for joker _jokerBlack.GetComponent <Brick>().DestroyArrows(); _jokerWhite.GetComponent <Brick>().DestroyArrows(); } // 每一round中抽到joker的玩家确认完成joker的放置后点击 else if (GameState.Instance.CurrentState == GameState.State.PlacingJoker) { //进入Guess状态 GameState.Instance.CurrentState = GameState.State.GuessABrick; //视觉提示 让我猜牌 Notify.autoNtf("请猜牌", Vector2.zero, new Vector2(1, 1), 1); //隐藏所有arrows for joker _jokerBlack.GetComponent <Brick>().DestroyArrows(); _jokerWhite.GetComponent <Brick>().DestroyArrows(); } else if (GameState.Instance.CurrentState == GameState.State.GuessingABrick) { //不猜了 轮到下一个玩家 ToNextOne(); //隐藏按钮 ContinueBtn.gameObject.SetActive(false); DoneBtn.gameObject.SetActive(false); //切换状态 GameState.Instance.CurrentState = GameState.State.WaitForOthers; } DoneBtn.gameObject.SetActive(false); }); //取消猜牌按钮的点击逻辑 CancelBtn.GetComponent <Button>().onClick.AddListener(() => { CancelBtn.gameObject.SetActive(false); //隐藏所有PossibleBrick HidePossibleBricks(); //状态回到GuessABrick GameState.Instance.CurrentState = GameState.State.GuessABrick; }); //继续猜 ContinueBtn.GetComponent <Button>().onClick.AddListener(() => { GameState.Instance.CurrentState = GameState.State.GuessABrick; ContinueBtn.gameObject.SetActive(false); DoneBtn.gameObject.SetActive(false); }); #endregion //----初始化UI状态---------- _readyPanel.SetActive(false); DoneBtn.gameObject.SetActive(false); CancelBtn.gameObject.SetActive(false); ContinueBtn.gameObject.SetActive(false); Panel.gameObject.SetActive(false); StartBtn.gameObject.SetActive(false); _overPanel.SetActive(false); //-------------------------------------- }
// Use this for initialization void Start() { //--------------------------------------- #region 对client中的事件进行函数注册 //---------------------------------------- //成功连接到服务器 _client.ConnectedToServer += () => { Panel.gameObject.SetActive(true); //隐藏loading _loading.gameObject.SetActive(false); }; _client.DebugAnyEventDataReceived += e => { print("收到事件代码: " + e.Code); //打印出code }; _client.OtherJoin += e => { //显示房间中新加入的玩家 Transform a = Instantiate(Resources.Load("prefabs/Player" + _client.CurrentRoom.PlayerCount) as GameObject).transform; a.SetParent(_readyPanel.transform); a.GetComponent <RectTransform>().anchoredPosition3D = readyPos[_client.CurrentRoom.PlayerCount - 1]; //因为Player使用的Dictionary 值从1开始 a.GetChild(0).GetComponent <Text>().text = _client.CurrentRoom .Players[_client.CurrentRoom.PlayerCount].ID.ToString(); a.GetComponent <PlayerIcon>().playerID = _client.CurrentRoom.PlayerCount; _playerIcons.Add(a); }; _client.OtherLeave += e => { #region //这里还需要完善逻辑. 需要对服务器进行修改 #endregion /*StartCoroutine("FetchingInfoAfterEnter"); * print("m y new id "+_myID);*/ }; //某玩家在游戏结束后 点击了 离开按钮 _client.OneLeave += (id) => { }; //------------------ //更新牌的位置为房主洗牌后的位置 //------------------- _client.SyncAllBricks += newIndex => { //_readyPanel.SetActive(false); for (int i = 0; i < newIndex.Length; i++) { indexMap[i] = newIndex[i]; } PlaceAllBricksOnTable(indexMap); StartGame(); }; //---------------------------- // 别人Open了一张牌 //------------------------------ _client.OtherOpenABrick += (indexInAllBricks) => { _allBricks[indexInAllBricks].GetComponent <Brick>().OpenMe(); //如果open的是我的牌 if (_decks[_myDeckIndex].Contains(_allBricks[indexInAllBricks])) { //我已经没有牌剩余了 if (CheckMyRemanent() == 0) { IamDone(); GameState.Instance.CurrentState = GameState.State.Watching; } } }; //----------------------------------------- //每一轮别人捡起了一张牌 更新相关位置和从属关系 //-------------------------------- _client.OtherPickABrick += (id, indexInAllBricks, destIndexInDeck) => { //根据公式进行换算 因为每个人在自己那里都是 DockD为自己的Dock.. int userIndexOfDockAndDeck = GetPlayerDeckIndex(id); Transform brick = _allBricks[indexInAllBricks]; //设置Brick所有者的ID brick.GetComponent <Brick>().OwnerID = id; print("user:"******" picked " + indexInAllBricks); //从 UnOccupied中剔除 _unOccupiedBricks.Remove(brick); //加入该玩家的deck 如果不含有这张牌 才加入 ,因为有时候同一张牌 // 已经存在了,但是会被加入两次(应该只有 joker这种牌) if (!Decks[userIndexOfDockAndDeck].Contains(brick)) { Decks[userIndexOfDockAndDeck].Insert(destIndexInDeck, brick); } //先移动到别处改变好rotation brick.localPosition = new Vector3(0, -100, 0); //挂载到该玩家Dock下 brick.SetParent(_docks[userIndexOfDockAndDeck]); brick.localRotation = Quaternion.Euler(new Vector3(0, 0, 0)); //再根据在数组中的逻辑顺序放置到应该的位置 PlaceBricks(userIndexOfDockAndDeck); }; //成功进入房间 _client.JoinedRoom += () => { //获取到自己的ID _myID = _client.LocalPlayer.ID; //用yield进行循环检查 直到有信息为止 StartCoroutine("FetchingInfoAfterEnter"); print("my id is:" + _myID); //获取完ID后 关掉UI _panel.SetActive(false); _readyPanel.SetActive(true); StartBtn.gameObject.SetActive(false); EnterRoom(_playerNum); //CurrentID设置为自己 让自己在房间里晃动 CurrentID = _myID; }; //成功创建房间 _client.CreatedRoom += () => { //获取到自己的ID _myID = _client.LocalPlayer.ID; //房主为一号玩家 首先活动 CurrentID = _myID; //用yield进行循环检查 直到有信息为止 StartCoroutine("FetchingInfoAfterEnter"); print("my id is:" + _myID); //获取完ID后 关掉UI _panel.SetActive(false); _readyPanel.SetActive(true); StartBtn.gameObject.SetActive(true); EnterRoom(_playerNum); }; //-------------------------------------------- // 房主处理别人的InitialPickRequest- //-------------------------------------------- _client.RequestInitialPick += (id, index) => { //如果我是房主 if (_client.CurrentRoom.MasterClientId == _myID) { HandleInitialPickRequest(id, index); } }; //------------------------------------------ //收到房主 InitialPick的Confirmation // 房主不响应这条消息,因为 // 他发出消息时就进行了相关处理了 //------------------------------------------ _client.ConfirmInitialPick += (id, indexer) => { //如果我不是房主 if (_client.CurrentRoom.MasterClientId != _myID) { OnInitialPickConfirmation(id, indexer); } }; //------------------------------- // 房主收到其他人ReadyToGo // 保存当前ReadyToGo的人的ID //------------------------------ _client.OtherReadyToGo += (id) => { //如果我是房主才响应这条消息 if (_myID == _client.CurrentRoom.MasterClientId) { ReadyToGo(id); } }; //----------------- // 游戏开始 //---------------- _client.GO += GO; //-------------- // 轮到下一个玩家 //---------------- _client.NextOne += (id) => { //更新当前活动的那个玩家的ID CurrentID = id; //下一个玩家是我 print("轮到id " + id + " 我的id是 " + _myID); if (id == _myID) { //视觉提示 请取一张牌 以后改成状态机了 用状态机做这个提示 Notify.autoNtf("请取一张牌", Vector2.zero, new Vector2(1, 1), 1); //如果牌都取完了, 那么进入GuessABrick状态 if (UnOccupiedBricks.Count == 0) { GameState.Instance.CurrentState = GameState.State.GuessABrick; } //切换到取牌的状态 else { GameState.Instance.CurrentState = GameState.State.PickABrick; } } }; //-------------- //某玩家 选择继续玩 //--------------- _client.OnePlayAgain += (id) => { var icon = _playerIcons[id - 1]; AnimElement.Prepare() .InitUGUI(icon.localPosition, readyPos[id - 1], icon, 0, 40) .AddScaleAnimation(icon.localScale, Vector3.one * 0.6f) .Play(); }; //---------------- // 某玩家adjust完成后 // 其他玩家同步他的joker位置 //------------------------ _client.SyncJoker += (id, msgType, whiteDestIndex, blackDestIndex) => { // 只有jokerWhite if (msgType == -1) { _decks[GetPlayerDeckIndex(id)].Remove(_jokerWhite); _decks[GetPlayerDeckIndex(id)].Insert(whiteDestIndex, _jokerWhite); } //只有 jokerBlack else if (msgType == 0) { _decks[GetPlayerDeckIndex(id)].Remove(_jokerBlack); _decks[GetPlayerDeckIndex(id)].Insert(blackDestIndex, _jokerBlack); } // 两者都有 else if (msgType == 1) { //先放入index小的 if (blackDestIndex < whiteDestIndex) { _decks[GetPlayerDeckIndex(id)].Remove(_jokerBlack); _decks[GetPlayerDeckIndex(id)].Remove(_jokerWhite); _decks[GetPlayerDeckIndex(id)].Insert(blackDestIndex, _jokerBlack); _decks[GetPlayerDeckIndex(id)].Insert(whiteDestIndex, _jokerWhite); } else { _decks[GetPlayerDeckIndex(id)].Remove(_jokerBlack); _decks[GetPlayerDeckIndex(id)].Remove(_jokerWhite); _decks[GetPlayerDeckIndex(id)].Insert(whiteDestIndex, _jokerWhite); _decks[GetPlayerDeckIndex(id)].Insert(blackDestIndex, _jokerBlack); } } }; //----------------------------- // 对某个玩家的牌进行逻辑层排序 //--------------------------------- _client.OrderOnesBrick += (id) => { OrderBricks(GetPlayerDeckIndex(id)); }; _client.DebugResponse += (resp) => { print("Code is " + resp.OperationCode); print("result is " + resp.ReturnCode); }; // 玩家(id)的牌都翻开了 (不包括自己 _client.OneDone += (id) => { DonePlayer.Add(id); //检查没有done的玩家只剩自己 ,那么 我赢了 游戏结束 if (DonePlayer.Count + 1 == _playerNum) { _IWin = true; //显示我赢了 AnimElement.Prepare() .InitUGUI(new Vector3(0, 300, 0), new Vector3(0, 110, 0), _winBanner, 0, 25) .Play(); //赢家头像放大到中间 id从1开始 var icon = _playerIcons[_myID - 1]; AnimElement.Prepare() .InitUGUI(icon.localPosition, Vector3.up * 40, icon, 0.2f, 30) .AddScaleAnimation(icon.localScale, Vector3.one * 0.5f) .Play(); //通知其他玩家 _client.OpRaiseEvent(MyEventCode.GameOver, _myID, true, RaiseEventOptions.Default); //显示重新游戏面板 _overPanel.SetActive(true); //隐藏结束按钮 继续猜按钮 DoneBtn.gameObject.SetActive(false); ContinueBtn.gameObject.SetActive(false); //gameover GameState.Instance.CurrentState = GameState.State.GameOver; } //显示通知 id 玩家已经完蛋了 else { } }; //游戏结束 (赢家发出这条消息) _client.GameOver += (id) => { //显示通知 你输了 //gameover GameState.Instance.CurrentState = GameState.State.GameOver; //显示重新游戏面板 _overPanel.SetActive(true); //隐藏结束按钮 继续猜按钮 DoneBtn.gameObject.SetActive(false); ContinueBtn.gameObject.SetActive(false); }; #endregion //-.-.-.-.-.-..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-..-.-.-.-.-.-.-.-.-.-.--.-..---.-.-.- //-.-.-.-.-.-..-.-.-.-.-.-.-.-.-以上是各种事件注册-.-.-.-.-.--.-..---.-.-.- //-.-.-.-.-.-..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-..-.-.-.-.-.-.-.-.-.-.--.-..---.-.-.- InitGuessStuffs(); GetStartPositions(); LoadResources(); PlaceAllBricksOnTable(indexMap); }
private void Init() { if (_butClose != null) { return; } _bg = transform.Find("BG").GetComponent <Image>(); // _bg.fillAmount = 0.658f; _butClose = transform.Find("BG/ButClose").GetComponent <Button>(); // _butClose.transform.localScale = Vector3.zero; _butClose.gameObject.SetActive(false); _butAD = transform.Find("BG/ButAD").GetComponent <Button>();// _butAD.transform.localScale = Vector3.zero; _openNum = transform.Find("Time/Num").GetComponent <Text>();// _time = transform.Find("Time"); _time.localScale = Vector3.zero; Button _butItem; string path = string.Empty; for (int i = 0; i < 3; i++) { AnimElement animElement = new AnimElement(); path = "BG/Item"; if (i > 0) { path = path + i; } Debug.Log("Init path = " + path); animElement._itemPos = transform.Find(path); animElement._itemIcon = animElement._itemPos.Find("Icon").GetComponent <Image>(); animElement._itemCup = animElement._itemPos.Find("Cup").GetComponent <Image>(); animElement._itemNum = animElement._itemPos.Find("Icon/num").GetComponent <Text>(); _animElements.Add(animElement); _butItem = animElement._itemPos.Find("Cup").GetComponent <Button>(); _butItem.onClick.RemoveAllListeners(); switch (i) { case 0: _butItem.onClick.AddListener(OnClickCard0); break; case 1: _butItem.onClick.AddListener(OnClickCard1); break; case 2: _butItem.onClick.AddListener(OnClickCard2); break; } } _butClose.onClick.RemoveAllListeners(); _butClose.onClick.AddListener(OnClickCancel); _butAD.onClick.RemoveAllListeners(); _butAD.onClick.AddListener(OnClickAD); for (int i = 0; i < _animElements.Count; i++) { initCupPos.Add(_animElements[i]._itemPos.localPosition); } }