Пример #1
0
    protected override bool OnExecuteState()
    {
        if (Input.GetMouseButton(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Unit"))
                {
                    Unit unit = hit.collider.GetComponent <Unit>();
                    if (player.IsUnitFromPlayer(unit))
                    {
                        if (!unit.HasAttacked)
                        {
                            player.SetSelectedUnit(unit);
                            nextState = PlayerFSM.PlayerStates.SELECT_UNIT;
                            return(true);
                        }
                    }
                }
            }
        }

        return(false);
    }
    protected override bool OnExecuteState()
    {
        if (Input.GetMouseButtonDown(0)) // TODO: Input manager should handle this
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Floor"))
                {
                    GridNode target = GameManager.Instance.gridManager.GetNodeFromWorldPosition(hit.transform.position);
                    if (target != null)
                    {
                        if (GameManager.Instance.IsNodeReachable(target))
                        {
                            nextState = PlayerFSM.PlayerStates.MOVE_UNIT;
                            GameManager.Instance.PathfinderCall(target, currentUnit);
                        }

                        else
                        {
                            nextState = PlayerFSM.PlayerStates.IDLE;
                            player.SetSelectedUnit(null);
                        }

                        return(true);
                    }
                }

                if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Unit"))
                {
                    Unit enemy = hit.collider.gameObject.GetComponent <Unit>();
                    if (!player.IsUnitFromPlayer(enemy))
                    {
                        if (currentUnit.IsEnemyInRange(enemy))
                        {
                            nextState = PlayerFSM.PlayerStates.ATTACK_UNIT;
                            currentUnit.CurrentEnemy = enemy;
                            return(true);
                        }

                        else
                        {
                            currentUnit.CanAttack = false;

                            if (currentUnit.HasMoved)
                            {
                                ((UnitFSM)currentUnit.fSM).ForceChangeState(UnitFSM.UnitStates.IDLE);
                                nextState = PlayerFSM.PlayerStates.IDLE;
                                ForceQuit = true;
                            }
                        }
                    }

                    else
                    {
                        ((UnitFSM)player.CurrentSelectedUnit.fSM).ForceChangeState(UnitFSM.UnitStates.IDLE);
                        nextState = PlayerFSM.PlayerStates.IDLE;
                        player.SetSelectedUnit(null);

                        if (enemy.HasMoved)
                        {
                            player.SetSelectedUnit(enemy);
                            nextState = PlayerFSM.PlayerStates.SELECT_UNIT;
                        }

                        ForceQuit = true;
                    }
                }
            }
        }
        return(false);
    }