Пример #1
0
    void OnTouchReleased(Vector3 position)
    {
        if (_isPressed)
        {
            // Check if items is null
            if (_items == null)
            {
                _items = new MapItem[rows, columns];
            }

            int row    = -1;
            int column = -1;

            if (GetCell(position, ref row, ref column))
            {
                if (_selectedRow == row && _selectedColumn == column)
                {
                    if (_itemType.IsFrog())
                    {
                        MapItem mapItem = _items[row, column];

                        if (mapItem != null)
                        {
                            // Get direction
                            Direction direction = _itemType.GetDirection();

                            // Find a frog
                            GameObject frog = mapItem.item.FindChild("Frog");

                            if (frog != null)
                            {
                                Destroy(frog);

                                if (direction == _direction)
                                {
                                    // Reset start cell
                                    _startRow    = -1;
                                    _startColumn = -1;
                                }
                                else
                                {
                                    // Replace frog
                                    frog      = Instantiate(_itemPrefab) as GameObject;
                                    frog.name = "Frog";
                                    frog.transform.SetParent(mapItem.item.transform);
                                    frog.transform.localPosition = Vector3.zero;

                                    // Set direction
                                    _direction = direction;
                                }
                            }
                            else
                            {
                                if (_startRow >= 0 && _startColumn >= 0)
                                {
                                    GameObject foothold = _items[_startRow, _startColumn].item;

                                    // There is only one frog
                                    if (foothold != null)
                                    {
                                        foothold.DestroyChild("Frog");
                                    }
                                }

                                // Create frog
                                frog      = Instantiate(_itemPrefab) as GameObject;
                                frog.name = "Frog";
                                frog.transform.SetParent(mapItem.item.transform);
                                frog.transform.localPosition = Vector3.zero;

                                // Set start cell
                                _startRow    = row;
                                _startColumn = column;

                                // Set direction
                                _direction = direction;
                            }
                        }
                    }
                    else
                    {
                        // Add item
                        if (_items[row, column] == null)
                        {
                            AddItem(_itemType, _itemPrefab, row, column);
                        }
                        else
                        {
                            Destroy(_items[row, column].item);

                            // Remove item
                            if (_items[row, column].type == _itemType)
                            {
                                _items[row, column] = null;
                            }
                            // Replace item
                            else
                            {
                                AddItem(_itemType, _itemPrefab, row, column);
                            }

                            if (row == _startRow && column == _startColumn)
                            {
                                // Reset start cell
                                _startRow    = -1;
                                _startColumn = -1;
                            }
                        }
                    }
                }
            }
        }
    }