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.cockActionComps) { 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) return; Debug.Log("end battle"); ExecuteLastBattleObj(); _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 attackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.from); attackCockActionComp.SetHighJump(i == 0); var counterattackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.to); var counterattackPlayer = _gameCore.localPlayer.playerId == log.to ? _gameCore.localPlayer : _gameCore.antiPlayer; counterattackPlayer.runTimeHp -= log.value; if (i == 0 && log.value == 0) // 攻击落空了 { counterattackCockActionComp.CreateMiss(); } else if (i == 0 && log.value > 0) { var cockHpComp = _gameCore.GetCockHpCompByPlayerId(log.to); cockHpComp.reduceHp = log.value; } switch (log.type) { case 1: attackCockActionComp.curAction = attackCockActionComp.CockController.JumpAndAttack; break; case 2: attackCockActionComp.curAction = attackCockActionComp.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; } } }