using System.Timers; using Comp; using HttpApi; using UnityEngine; namespace Game { public class GameBattleState : GameState { private Timer _battleTimer; private bool _runBattle = false; public GameBattleState(GameCore gameCore) : base(gameCore) { } public override void PlayBattle() { // 啥都不做 } public override void BattleStart() { Debug.Log("battle start"); if (_battleTimer == null) { Debug.Log("battle timer start"); foreach (var cockActionComp in _gameCore.cockDict) { if (cockActionComp.Key == _gameCore.localPlayer.playerId) { cockActionComp.Value.gameObject.GetComponent().rightForward = true; } else { cockActionComp.Value.gameObject.GetComponent().rightForward = false; } cockActionComp.Value.gameObject.GetComponent().CockController.JumpAndAttack(); } var followObject = _gameCore.GetCockActionCompByPlayerId(_gameCore.localPlayer.playerId).gameObject; _gameCore.camera.AddComponent().SetTarget(followObject.transform); _battleTimer = new Timer(); _battleTimer.Interval = 1000; _battleTimer.Enabled = true; _battleTimer.Elapsed += OnBattleTimer; _battleTimer.Start(); } } public override void LiftCock() { if (_gameCore.liftTimes > 0) _gameCore.TransitionToState(_gameCore.liftState); } public override void ExitBattle() { _runBattle = false; if (_battleTimer != null) { _battleTimer.Dispose(); _battleTimer = null; } } public override void EndBattle() { } public override void EnterState() { _runBattle = true; } public override void ExitState() { _runBattle = false; } private void OnBattleTimer(object o, ElapsedEventArgs args) { if (!_runBattle) return; if (_gameCore.battleDetailObjs == null || _gameCore.battleDetailObjs.Count < 0) return; ExecuteBattleObj(_gameCore.battleDetailObjs.Last.Value); _gameCore.battleDetailObjs.RemoveLast(); // if (_gameCore.battleDetailObjs.Count < 0) // { // Debug.Log("end battle"); // _gameCore.battleMainComp.end = true; // _battleTimer.Dispose(); // } // else if (_gameCore.battleDetailObjs.Count == 0) { ExecuteLastBattleObj(); Debug.Log("end battle"); _gameCore.battleMainComp.end = true; _battleTimer.Dispose(); _battleTimer = null; } } private void ExecuteBattleObj(BattleDetailObj battleDetailObj) { for (int i = 0; i <= battleDetailObj.logs.Length - 1; i++) { var log = battleDetailObj.logs[i]; var fromComp = _gameCore.GetCockActionCompByPlayerId(log.from); fromComp.SetHighJump(i == 0); if (i == 0 && log.value == 0) // 攻击落空了 { var toComp = _gameCore.GetCockActionCompByPlayerId(log.to); toComp.CreateMiss(); } switch (log.type) { case 1: fromComp.curAction = fromComp.CockController.JumpAndAttack; break; case 2: fromComp.curAction = fromComp.CockController.Attack; break; } } } private void ExecuteLastBattleObj() { var fromComp = _gameCore.GetCockActionCompByPlayerId(_gameCore.winPlayerId); int toPlayerId; if (_gameCore.winPlayerId == _gameCore.localPlayer.playerId) { toPlayerId = _gameCore.antiPlayer.playerId; } else { toPlayerId = _gameCore.localPlayer.playerId; } var toComp = _gameCore.GetCockActionCompByPlayerId(toPlayerId); fromComp.SetHighJump(true); toComp.SetHighJump(false); fromComp.curAction = fromComp.CockController.JumpAndAttack; toComp.curAction = toComp.CockController.Die; } } }