Esempio n. 1
0
    private void OnDirectionPressed(Direction direction)
    {
        bool anyEntityShifted = true;

        Vector2Int directionVector = directionVectors[direction];

        IterateOverGrid(direction, (x, y, entity) =>
        {
            Vector2Int shiftedIndeces = new Vector2Int(x + directionVector.x, y + directionVector.y);

            bool positionValid           = IsPositionValid(shiftedIndeces.x, shiftedIndeces.y);
            EntityBase shiftTargetEntity = positionValid ? Grid[shiftedIndeces.x, shiftedIndeces.y] : null;

            bool shiftSpaceEmpty = positionValid &&
                                   shiftTargetEntity == null;
            if (entity != null && shiftSpaceEmpty && entity.movedThisTurn == false)
            {
                anyEntityShifted = true;
                MoveEntityToIndeces(entity, shiftedIndeces.x, shiftedIndeces.y, true);
                entity.movedThisTurn = true;
            }
            else if (entity != null && positionValid && shiftTargetEntity != null)
            {
                InteractionResult result = InteractionSystem.Handle(entity, shiftTargetEntity);


                switch (result)
                {
                case InteractionResult.NONE:
                    InteractionResult swappedResult = InteractionSystem.Handle(shiftTargetEntity, entity);
                    switch (swappedResult)
                    {
                    case InteractionResult.TARGET_DEATH:
                        entity.Die();
                        break;

                    case InteractionResult.ACTOR_DEATH:
                        shiftTargetEntity.Die();
                        break;
                    }
                    break;

                case InteractionResult.TARGET_DEATH:
                    shiftTargetEntity.Die();
                    break;

                case InteractionResult.ACTOR_DEATH:
                    entity.Die();
                    break;
                }

                if (entity != null && entity.morphingInto.HasValue)
                {
                    entity.Die();
                }
                else if (shiftTargetEntity != null && shiftTargetEntity.morphingInto.HasValue)
                {
                }
            }
        });

        IterateOverGrid(direction, (x, y, e) =>
        {
            if (e != null)
            {
                e.movedThisTurn = false;
            }
        });

        if (anyEntityShifted)
        {
            //Get cell for spawn
            List <Vector2Int> emptyEdgeCells = GetEdgeCellsForDirection(direction);

            if (emptyEdgeCells.Count == 0)
            {
                ResetGrid();
            }
            else
            {
                Vector2Int emptyCell = emptyEdgeCells[UnityEngine.Random.Range(0, emptyEdgeCells.Count)];

                EntityType randomEntityType = autspawnEntities[UnityEngine.Random.Range(0, autspawnEntities.Length)];
                CreateNewEntity(randomEntityType, emptyCell.x, emptyCell.y);
            }
        }
    }