示例#1
0
    private static bool IsChipsAdjacent(ChipBehaviour firstChip, ChipBehaviour secondChip)
    {
        int xRange = Math.Abs(firstChip.col - secondChip.col);
        int yRange = Math.Abs(firstChip.row - secondChip.row);

        return(xRange + yRange == 1);
    }
    public void TransferChip(ChipBehaviour chip)
    {
        if (destinationChips.Contains(chip))
        {
            Destroy(chip.gameObject);
            destinationChips.Remove(chip);
            ReorderChips(destinationChips);
            SendHint();
            return;
        }

        if (!CanMoveToDest(chip))
        {
            return;
        }

        var chipNew = Instantiate(chip.gameObject).GetComponent <ChipBehaviour>();

        chipNew.Init(this, panel.gameObject, destinationSlots[destinationChips.Count]);

        destinationChips.Add(chipNew.GetComponent <ChipBehaviour>());
        ReorderChips(destinationChips);
        UpdateSendButton();
        SendHint();
    }
示例#3
0
    //immediate chip destroying, after animation
    internal void destroyChip(ChipBehaviour chip)
    {
        destroyWaiting--;

        for (int i = chip.row; i < MAX_ROWS - 1; i++)
        {
            chipArray[i, chip.col] = chipArray[i + 1, chip.col];
            //can be null for deleted chips
            if (chipArray[i, chip.col])
            {
                chipArray[i, chip.col].row = i;
            }
        }

        chipArray[MAX_ROWS - 1, chip.col] = null;

        Debug.Log("destroyChip, destroyWaiting: " + destroyWaiting);
        if (destroyWaiting <= 0)
        {
            Debug.Log("Turn physics on");
            FillNewChips();
            SetPhysics(true);
            for (int i = 0; i < MAX_ROWS; i++)
            {
                for (int j = 0; j < MAX_COLS; j++)
                {
                    if (chipArray[i, j] && chipArray[i, j].rigidbody)
                    {
                        previousPositionY[i, j] = chipArray[i, j].rigidbody.transform.position.y;
                    }
                }
            }
            isWaitingChipsFall = true;
        }
    }
    public bool CanMoveToDest(ChipBehaviour chip)
    {
        // We want to insert action and command already have action
        if (chip.chip.type == Chip.Type.Action)
        {
            if (destinationChips.FirstOrDefault(c => c.chip.type == Chip.Type.Action) != null)
            {
                return(false);
            }
        }

        // We want to insert number and command is already full
        if (chip.chip.type == Chip.Type.Number)
        {
            if (destinationChips.Count == 7)
            {
                return(false);
            }
            // or command doesn't have action yet
            if (destinationChips.FirstOrDefault(c => c.chip.type == Chip.Type.Action) == null)
            {
                return(false);
            }
        }
        return(true);
    }
示例#5
0
 private void AddRoads(ChipBehaviour c, List <Vector3> points)
 {
     for (int i = 0, h = points.Count - 1; i < h; i++)
     {
         CreateRoad(points[i], points[i + 1]).transform.parent = c.transform;
     }
 }
示例#6
0
    public ChipBehaviour Create(Vector3 v)
    {
        ChipBehaviour c = AssembleParts();

        c.transform.localPosition = v;
        c.transform.parent        = chipPool.transform;
        return(c);
    }
示例#7
0
    public ChipBehaviour CreateFrom(ChipBehaviour origin, Vector2 v)
    {
        Vector3       locate = origin.transform.position + ExchangeVector2To3(v);
        ChipBehaviour c      = Create(locate);

        origin.PushNextChip(c);
        return(c);
    }
示例#8
0
    public ChipBehaviour Init()
    {
        //initial chip
        ChipBehaviour c = chipFactory.Create(new Vector3(0, 0, 2));

        BuildMapFrom(c);
        return(c);
    }
示例#9
0
    private ChipBehaviour AssembleParts()
    {
        ChipBehaviour c = Instantiate(body);

        Instantiate(tables.Random()).transform.parent  = c.transform;
        Instantiate(symbols.Random()).transform.parent = c.transform;
        c.SetEvent(events.Random());
        c.SetManager(manager);
        return(c);
    }
示例#10
0
 public void ManagerListener(ChipBehaviour c)
 {
     Debug.Log($"player transition. {nowChip.ToStringDebug()} => {c}");
     if (nowChip.IsNextChip(c))
     {
         SetChip(c);
         nowChip.InvokeEvent(p);
         nowChip.PublishMessageLog(p.Command("UPDATE"));
     }
 }
示例#11
0
    //let 'em BOOM
    private void ActivateBonus(ChipBehaviour chip, int pairType)
    {
        //prevent duplicate activations
        if (chip.isDestroying)
        {
            return;
        }
        chip.StartDestroy();
        if (chip.Type == (int)ChipBehaviour.chipTypes.bomb)
        {
            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    if (chip.row + i >= 0 && chip.col + j >= 0 && chip.row + i < MAX_ROWS && chip.col + j < MAX_COLS &&
                        chipArray[chip.row + i, chip.col + j])
                    {
                        if (chipArray[chip.row + i, chip.col + j].Type > BASE_CHIP_TYPES)
                        {
                            ActivateBonus(chipArray[chip.row + i, chip.col + j], pairType);
                        }
                        chipArray[chip.row + i, chip.col + j].StartDestroy();
                    }
                }
            }
        }

        if (chip.Type == (int)ChipBehaviour.chipTypes.rainbow)
        {
            for (int i = 0; i < MAX_ROWS; i++)
            {
                for (int j = 0; j < MAX_COLS; j++)
                {
                    if (chipArray[i, j] && chipArray[i, j].Type == pairType)
                    {
                        chipArray[i, j].StartDestroy();
                    }
                }
            }
            chip.StartDestroy();
        }

        if (chip.Type == (int)ChipBehaviour.chipTypes.rocket)
        {
            for (int i = chip.row; i >= 0; i--)
            {
                chipArray[i, chip.col].StartDestroy();
                if (chipArray[i, chip.col].Type > BASE_CHIP_TYPES)
                {
                    ActivateBonus(chipArray[i, chip.col], pairType);
                }
                chipArray[i, chip.col].StartDestroy();
            }
        }
    }
示例#12
0
    //used both for direct and back moves
    internal void OnMovingEnd()
    {
        movingCounter++;
        if (movingCounter < 2)
        {
            return;
        }

        //shuffle end
        if (!selectedChip)
        {
            if (!FullMatchCheck())
            {
                EnablePlayerControl(true);
            }
            return;
        }

        int transitRow = selectedChip.row;
        int transitCol = selectedChip.col;

        selectedChip.row = secondChip.row;
        selectedChip.col = secondChip.col;
        secondChip.row   = transitRow;
        secondChip.col   = transitCol;

        chipArray[selectedChip.row, selectedChip.col] = selectedChip;
        chipArray[secondChip.row, secondChip.col]     = secondChip;

        if (isMovingBack)
        {
            isMovingBack = false;
            selectedChip = null;
            secondChip   = null;
            EnablePlayerControl(true);
            return;
        }

        //evade lazy boolean evaluation
        bool firstSuccessful  = СheckAndDestroyForChip(selectedChip, secondChip.Type);
        bool secondSuccessful = СheckAndDestroyForChip(secondChip, selectedChip.Type);

        if (firstSuccessful || secondSuccessful)
        {
            audioSource.PlayOneShot(matchSound);
            TurnsLeft--;
        }
        else
        {
            MoveChipsBack();
        }
    }
示例#13
0
    //three predefined points for chips, other place at random
    private void Shuffle()
    {
        selectedChip = null;
        SetPhysics(false);

        Debug.Log("Shuffle");
        var sameChips = GetAnySameChips(0);
        var excludes  = new List <int>();

        //linear array is much better to randomize
        var linear = new ChipBehaviour[MAX_ROWS * MAX_COLS];

        //move 3 found close to each other to make sure of one turn
        linear[GetLinearIndex(1, 0)] = sameChips[0];
        excludes.Add(GetLinearIndex(1, 0));

        linear[GetLinearIndex(1, 1)] = sameChips[1];
        excludes.Add(GetLinearIndex(1, 1));

        linear[GetLinearIndex(2, 2)] = sameChips[2];
        excludes.Add(GetLinearIndex(2, 2));

        for (int i = 0; i < MAX_ROWS; i++)
        {
            for (int j = 0; j < MAX_COLS; j++)
            {
                if (sameChips.Contains(chipArray[i, j]))
                {
                    continue;
                }
                int randomPlace = GetRandomWithExcludes(MAX_ROWS * MAX_COLS, excludes);
                linear[randomPlace] = chipArray[i, j];
                excludes.Add(randomPlace);
            }
        }

        //move
        for (int i = 0; i < linear.Length; i++)
        {
            linear[i].row = GetRowOfLinear(i);
            linear[i].col = GetColOfLinear(i);
            linear[i].MoveTo(chipArray[GetRowOfLinear(i), GetColOfLinear(i)].gameObject.transform.position);
        }

        //move linear array back to 2D
        for (int i = 0; i < linear.Length; i++)
        {
            chipArray[GetRowOfLinear(i), GetColOfLinear(i)] = linear[i];
        }
    }
示例#14
0
    private void OnMouseUp()
    {
        Debug.Log("OnMouseUp");
        //some animation or moving in process, skip click
        if (!GameBehaviour.instance.isFieldActive)
        {
            return;
        }
        if (startSwipeChip != lastMouseEnterSprite)
        {
            Debug.Log("OnMouseUp");
            GameBehaviour.instance.selectedChip = startSwipeChip;
            if (IsChipsAdjacent(startSwipeChip, lastMouseEnterSprite))
            {
                GameBehaviour.instance.TrySwipeWith(lastMouseEnterSprite);
            }
            return;
        }

        //uncheck chip
        if (GameBehaviour.instance.selectedChip == this)
        {
            startSwipeChip = null;
            halo.enabled   = false;
            GameBehaviour.instance.selectedChip = null;
        }
        //no previous checks
        else if (!GameBehaviour.instance.selectedChip)
        {
            halo.enabled = true;
            GameBehaviour.instance.selectedChip = this;
            Debug.Log("Selected chip row:" + row + "; col: " + col);
        }
        //try to change places?
        else if (IsChipsAdjacent(GameBehaviour.instance.selectedChip, this))
        {
            halo.enabled = false;
            GameBehaviour.instance.selectedChip.halo.enabled = false;
            GameBehaviour.instance.TrySwipeWith(this);
        }
        //not adjacent, lets check another chip
        else
        {
            GameBehaviour.instance.selectedChip.halo.enabled = false;
            halo.enabled = true;
            GameBehaviour.instance.selectedChip = this;
            Debug.Log("Selected chip row:" + row + "; col: " + col);
        }
    }
示例#15
0
    public void BuildChipSet(ChipBehaviour origin, ChipSet chipSet)
    {
        Debug.Log(chipSet);
        var instances = new Dictionary <int, ChipBehaviour>();

        build(0, origin);
        roadFactory.DecorateRoad(origin);

        void build(int i, ChipBehaviour source)
        {
            if (i >= chipSet.Count)
            {
                return;
            }

            //既に存在する時生成しない
            if (instances.ContainsKey(i))
            {
                source.PushNextChip(instances[i]);
                return;
            }

            (Vector2 location, string attr, List <int> content) = chipSet[i];

            ChipBehaviour self = chipFactory.CreateFrom(source, location);

            instances.Add(i, self);

            if (attr == "end")
            {
                return;
            }

            if (content.Count == 0)
            {
                build(i + 1, self);
            }
            else
            {
                foreach (var index in content)
                {
                    build(index, self);
                }
            }

            roadFactory.DecorateRoad(self);
        }
    }
示例#16
0
    public void DecorateRoad(ChipBehaviour c)
    {
        Vector3 pos = c.transform.position;

        foreach (var target in c.NextPositions())
        {
            Vector3 dist   = target - pos;
            var     points = new List <Vector3>();

            points.Add(pos);
            if (dist.z != 0 && dist.x != 0)
            {
                points.Add(pos + new Vector3(dist.x / 2, 0, 0));
                points.Add(pos + new Vector3(dist.x / 2, 0, dist.z));
            }
            points.Add(target);

            AddRoads(c, points);
        }
    }
示例#17
0
 internal void TrySwipeWith(ChipBehaviour secondChip)
 {
     Debug.Log("TrySwipe");
     for (int i = 0; i < MAX_ROWS; i++)
     {
         for (int j = 0; j < MAX_COLS; j++)
         {
             if (chipArray[i, j])
             {
                 chipArray[i, j].halo.enabled = false;
             }
         }
     }
     bonusRow      = new int[MAX_COLS];
     isFieldActive = false;
     SetPhysics(false);
     this.secondChip = secondChip;
     movingCounter   = 0;
     selectedChip.MoveTo(secondChip.gameObject.transform.position);
     secondChip.MoveTo(selectedChip.gameObject.transform.position);
 }
示例#18
0
 public void BuildMapFrom(ChipBehaviour origin)
 => factory.BuildMapFrom(origin);
示例#19
0
    // Use this for initialization
    void Start()
    {
        ChipBehaviour c = factory.Init();

        player.SetChip(c);
    }
示例#20
0
 public void BuildMapFrom(ChipBehaviour origin)
 {
     BuildChipSet(origin, chipsetContainer.Random());
 }
示例#21
0
 private void OnMouseDown()
 {
     startSwipeChip = this;
 }
示例#22
0
    //is there any lines with this chip? If so, collect it
    private bool СheckAndDestroyForChip(ChipBehaviour chip, int pairType)
    {
        if (chip.Type > BASE_CHIP_TYPES)
        {
            ActivateBonus(chip, pairType);
            return(true);
        }

        var horizontalLine = new List <ChipBehaviour>();
        var verticalLine   = new List <ChipBehaviour>();

        //first chip is it itself
        horizontalLine.Add(chip);
        verticalLine.Add(chip);

        int row = chip.row;
        int col = chip.col;

        Debug.Log("row:" + row + "; col:" + col);


        int currentType = SafeGetType(chip.row, chip.col);

        //UP
        if (currentType == SafeGetType(row + 1, col))
        {
            verticalLine.Add(chipArray[row + 1, col]);
            if (currentType == SafeGetType(row + 2, col))
            {
                verticalLine.Add(chipArray[row + 2, col]);
            }
        }

        //DOWN
        if (currentType == SafeGetType(row - 1, col))
        {
            verticalLine.Add(chipArray[row - 1, col]);
            if (currentType == SafeGetType(row - 2, col))
            {
                verticalLine.Add(chipArray[row - 2, col]);
            }
        }

        //LEFT
        if (currentType == SafeGetType(row, col - 1))
        {
            horizontalLine.Add(chipArray[row, col - 1]);
            if (currentType == SafeGetType(row, col - 2))
            {
                horizontalLine.Add(chipArray[row, col - 2]);
            }
        }

        //RIGHT
        if (currentType == SafeGetType(row, col + 1))
        {
            horizontalLine.Add(chipArray[row, col + 1]);
            if (currentType == SafeGetType(row, col + 2))
            {
                horizontalLine.Add(chipArray[row, col + 2]);
            }
        }

        if (horizontalLine.Count >= 3)
        {
            Debug.Log("It's horizontalLine line in (" + row + ";" + col + ")");
            if (horizontalLine.Count >= 4)
            {
                bonusRow[col] = (int)ChipBehaviour.chipTypes.bomb;
            }
            CollectLine(horizontalLine);
        }

        if (verticalLine.Count >= 3)
        {
            Debug.Log("It's vertical line in (" + row + ";" + col + ")");
            if (verticalLine.Count >= 4)
            {
                bonusRow[col] = (int)ChipBehaviour.chipTypes.rocket;
            }
            CollectLine(verticalLine);
        }

        if (verticalLine.Count >= 3 && horizontalLine.Count >= 3)
        {
            bonusRow[col] = (int)ChipBehaviour.chipTypes.rainbow;
        }

        return(verticalLine.Count >= 3 || horizontalLine.Count >= 3);
    }
示例#23
0
 private void OnMouseEnter()
 {
     lastMouseEnterSprite = this;
 }
示例#24
0
 public void SetChip(ChipBehaviour c)
 {
     target  = c.transform.position;
     nowChip = c;
 }