Exemplo n.º 1
0
    public IEnumerator Battle()
    {
        currentPiece = _piecesList.GetEnumerator();
        while (true)
        {
            if (state == GameState.Battle)
            {
                if (GetTeamAlive() == 0)
                {
                    yield return(new WaitForSeconds(5f));

                    state = GameState.End;
                    GameOver(0);
                }
                // If end of piece then  reset piece to start
                if (!currentPiece.MoveNext())
                {
                    currentPiece.Reset();
                }
                else
                {
                    var piece = currentPiece.Current;
                    // If piece alive then judge if it doesn't have the target enemy
                    if (piece.Alive)
                    {
                        if (piece.Target != null && !piece.Target.Alive)
                        {
                            piece.Target = null;
                        }
                        //if (piece.Target == null)
                        //{
                        //FindEnemy(piece);
                        map.FindEnemy(piece);
                        //}
                        if (GetTeamAlive(piece.Team ^ 1) == 0)
                        {
                            yield return(new WaitForSeconds(5f));

                            // update winner
                            state = GameState.End;
                            GameOver(piece.Team);
                        }
                        else if (piece.Target != null)
                        {
                            // If need to  move piece
                            if (piece.state == PieceController.PieceState.Idle && piece.Alive)
                            {
                                var dis = piece.IsRemoteAttack
                                    ? Vector2.Distance(piece.Target.TargetPos, piece.CurrentPosition)
                                    : Mathf.Abs(piece.Target.TargetPos.x - piece.CurrentPosition.x);
                                if ((dis > piece.AttackDistance + 6e-6f && piece.IsRemoteAttack) ||
                                    (!piece.IsRemoteAttack &&
                                     (piece.Target.TargetPos.y != piece.CurrentPosition.y ||
                                      dis > piece.AttackDistance +
                                      6e-6f))) // float number equal
                                {
                                    // Whether the surrounding movable grid is shorter
                                    var targetPath = map.FindPathToTarget(
                                        Vector3Int.RoundToInt(piece.CurrentPosition - piece.offset),
                                        Vector3Int.RoundToInt(piece.Target.TargetPos - piece.offset), piece);
                                    if (targetPath != null)
                                    {
                                        if (targetPath.Count > 0)
                                        {
                                            var target = new Vector3(targetPath[0].x, targetPath[0].y, 0);
                                            if (Vector2Int.RoundToInt(target) == Vector2Int.RoundToInt(piece.lastMove - piece.offset)) // 防止小数点造成的错误
                                            {
                                                Debug.Log(piece, piece.gameObject);
                                                piece.Target = null; // 防止重复回跳
                                            }
                                            else
                                            {
                                                if (map.CheckMoveable(Vector3Int.RoundToInt(target)))
                                                {
                                                    map.PutPiece(Vector3Int.RoundToInt(target),
                                                                 piece); // place the piece to target first
                                                    if (piece.Move(target + piece.offset))
                                                    {
                                                        map.PutPiece(
                                                            Vector3Int.RoundToInt(piece.CurrentPosition - piece.offset),
                                                            null);
                                                    }
                                                    else
                                                    {
                                                        map.PutPiece(Vector3Int.RoundToInt(target), null);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        piece.Target = null;
                                    }
                                }
                                else
                                {
                                    // Attack the piece
                                    if (piece.CanAttackDamage())
                                    {
                                        piece.Attack(piece.Target.ApplyAttacked);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            yield return(null);
        }
    }