// 加载场景 public IEnumerator LoadScene(string mapName) { _battleState = State.PREPARE; _currentSceneName = mapName; // 加载场景 yield return(SceneManager.LoadSceneAsync(mapName, LoadSceneMode.Additive)); // 设置当前场景(新创建的单位都创建在这个场景中) SceneManager.SetActiveScene(SceneManager.GetSceneByName(_currentSceneName)); _camera = Camera.main; _mapGrid = GameObject.FindObjectOfType <MapGrid>(); // 场景加载完毕,等待服务器确认游戏开始 if (_battleType == BattleType.PVE) { // pve开始战斗 UserID = 1; S2C_StartBattle data = new S2C_StartBattle(); data.UserID1 = UserID; data.UserID2 = UserID + 1; data.RandomSeed = 0; data.UserLevel1 = UserLevel; data.UserLevel2 = UserLevel; OnStartBattle(data); } else //(_battleType == BattleType.PVP) { // pvp战斗,先连接战斗服务器 ConnectToBattleServer(); } }
// 战斗开始(两个人都加入对应房间后,开启战斗,战斗服务器直接通知) private void OnMsgStartBattle(RPCResponse rpc) { if (rpc.ErrCode != ErrorCode.OK) { return; } S2C_StartBattle ret = Net.Deserialize <S2C_StartBattle>(rpc.MsgData); OnStartBattle(ret); }
// 服务器确认,开始战斗 public void OnStartBattle(S2C_StartBattle data) { // 设置随机数种子 UnityEngine.Random.seed = data.RandomSeed; _battleState = State.GAME; ServerTurnIndex = 0; ClientTurnIndex = 0; _accumilatedTime = 0; Mana = GameConfig.START_MANA * GameConfig.MANA_MUL; // 清理出牌卡组 DeckCardList.Clear(); DeckCardList.AddRange(CardList); // 创建塔 ArenaConfig cfg = ArenaConfigLoader.GetConfig(_arenaID); if (cfg == null) { return; } _enemyTower.Clear(); // 判定主客场 只有判定了主客场,才能开始整场战斗 if (data.UserID1 == UserID) { // 主场 我在左边 IsHomeCourt = true; // 敌人的id和等级 EnemyUserID = data.UserID2; EnemyUserLevel = data.UserLevel2; // 创建我方的塔 (我在左边) // 主场玩家左侧为我方塔,右侧为敌方塔,非主场玩家则相反。 将来统一坐标系后再考虑玩家统一在左方,现在双方玩家跟服务器坐标一致,通过塔的颜色来区分 _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, UserLevel, cfg.TowerLeft1, UserID)); _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, UserLevel, cfg.TowerLeft2, UserID)); _enemyTower.Add(CreateTower(GameConfig.KING_TOWER_ID, UserLevel, cfg.CastleLeft, UserID)); // 创建敌方的塔 _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, EnemyUserLevel, cfg.TowerRight1, EnemyUserID)); _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, EnemyUserLevel, cfg.TowerRight2, EnemyUserID)); _enemyTower.Add(CreateTower(GameConfig.KING_TOWER_ID, EnemyUserLevel, cfg.CastleRight, EnemyUserID)); } else { // 客场,我在右边 IsHomeCourt = false; // 敌人的id和等级 EnemyUserID = data.UserID1; EnemyUserLevel = data.UserLevel1; // 创建我方的塔 (我在右边) _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, UserLevel, cfg.TowerRight1, UserID)); _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, UserLevel, cfg.TowerRight2, UserID)); _enemyTower.Add(CreateTower(GameConfig.KING_TOWER_ID, UserLevel, cfg.CastleRight, UserID)); // 创建敌方的塔 _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, EnemyUserLevel, cfg.TowerLeft1, EnemyUserID)); _enemyTower.Add(CreateTower(GameConfig.TOWER_ID, EnemyUserLevel, cfg.TowerLeft2, EnemyUserID)); _enemyTower.Add(CreateTower(GameConfig.KING_TOWER_ID, EnemyUserLevel, cfg.CastleLeft, EnemyUserID)); } // 产生4张卡牌 const float TIME = 0.3f; PushNewCard(0, null, 0); PushNewCard(1, null, TIME * 1); PushNewCard(2, null, TIME * 2); PushNewCard(3, null, TIME * 3); // 生成下一张卡牌 GenerateCard(); }