예제 #1
0
    public IEnumerator Moving(Vector2Int direction)
    {
        stateName = "Moving";

        List <PlayerController> relasePlayers = new List <PlayerController>();

        unHandledAction = (playerController) => {
            if (!relasePlayers.Contains(playerController))
            {
                relasePlayers.Add(playerController);
            }
        };
        PlayerHundle.isPushingHouse   = true;
        PlayerKeyboard.isPushingHouse = true;
        if (!PlayerHundle.EnergyCost() || !PlayerKeyboard.EnergyCost())
        {
            unHandledAction               = null;
            PlayerHundle.isPushingHouse   = false;
            PlayerKeyboard.isPushingHouse = false;
            ChangeState(Idle());
        }

        while (true)
        {
            foreach (var player in handledPlayers)
            {
                player.UseEnerge();
            }
            foreach (var grid in gridPositions)
            {
                grid.rounded = false;
            }
            var     start    = unitController.mapUnit.GetOriginPointInt();
            var     end      = start + direction;
            Vector2 housePos = start;
            float   moveTime = 1 / (movingSpeed + handledPlayers[0].player.PushingSpeed);
            {
                float timeCount = 0;
                while (timeCount < moveTime)
                {
                    housePos = Vector2.Lerp(start, end, timeCount / moveTime);
                    unitController.mapUnit.SetPosition(housePos);
                    yield return(null);

                    timeCount += Time.deltaTime;
                }
            }
            unitController.mapUnit.SetPositionInt(end);
            foreach (var grid in gridPositions)
            {
                grid.rounded = true;
            }
            if (relasePlayers.Count > 0)
            {
                foreach (var player in relasePlayers)
                {
                    player.ChangeState(player.Idle());
                    handledPlayers.Remove(player);
                }
                relasePlayers.Clear();
                break;
            }
            else if (!MapEmpty(direction))
            {
                break;
            }
            foreach (var player in handledPlayers)
            {
                if (!player.CouldMoveHouse())
                {
                    player.ChangeState(player.Idle());
                    handledPlayers.Remove(player);
                    break;
                }
            }
            if (!PlayerHundle.EnergyCost() || !PlayerKeyboard.EnergyCost())
            {
                break;
            }
        }
        unHandledAction               = null;
        PlayerHundle.isPushingHouse   = false;
        PlayerKeyboard.isPushingHouse = false;
        ChangeState(Idle());
    }