private void OnDragBeganOnDotHandler(object grid)
        {
            var cellView = (GridCellView)grid;

            _currentDotOrigin = cellView as DotView;
            var gridDatum = cellView.GridDatum;

            if (_lineLookup.ContainsKey(gridDatum.ColorData.ColorId))
            {
                _currentLineRenderer = _lineLookup[gridDatum.ColorData.ColorId];
                if (_completedLines.ContainsKey(gridDatum.ColorData.ColorId))
                {
                    ClearLine(gridDatum.ColorData.ColorId);
                }
            }
            else
            {
                _currentLineRenderer            = Instantiate(_linePrefab, transform, false);
                _currentLineRenderer.startColor = _currentLineRenderer.endColor = gridDatum.ColorData.UnityColor;
                _lineLookup.Add(gridDatum.ColorData.ColorId, _currentLineRenderer);
            }

            var lineData = new List <Vector3Int>();
            var gridData = new List <GridDatum>();

            lineData.Add(GridUtility.GetLocalPositionFromIndices(gridDatum.PosX, gridDatum.PosY));
            UpdateLine(_currentLineRenderer, lineData);
            _currentGridDatum     = gridDatum;
            _currentLineColorData = gridDatum.ColorData;
            _currentLineData      = lineData;
            _currentLineGridData  = gridData;
            _lineDrawState        = LineDrawState.Drawing;
        }
 private void FinishDrawing()
 {
     _currentDotOrigin    = null;
     _currentGridDatum    = null;
     _currentLineData     = null;
     _currentLineRenderer = null;
     _currentLineData     = null;
     _lineDrawState       = LineDrawState.None;
 }
        private void ExtendCurrentLine(GridDatum gridDatum, bool isDot)
        {
            var position = GridUtility.GetLocalPositionFromIndices(gridDatum.PosX, gridDatum.PosY);

            _currentGridDatum = gridDatum;
            _currentLineData.Add(position);
            if (!isDot)
            {
                gridDatum.ColorData = _currentLineColorData;
                _currentLineGridData.Add(gridDatum);
            }
            UpdateLine(_currentLineRenderer, _currentLineData);
        }
Esempio n. 4
0
 private void PopulateCells(LevelDataModel levelData)
 {
     for (int i = 0; i < levelData.GridSize; i++)
     {
         for (int j = 0; j < levelData.GridSize; j++)
         {
             var position = GridUtility.GetLocalPositionFromIndices(i, j, levelData.GridSize);
             var cell     = Instantiate(gridCellPrefab, transform);
             cell.transform.localPosition = position;
             var gridDatum = new GridDatum(i, j);
             cell.Initialize(gridDatum);
         }
     }
 }
 public override void Initialize(GridDatum gridDatum)
 {
     if (_spriteRenderer == null)
     {
         _spriteRenderer = GetComponent <SpriteRenderer>();
     }
     GridDatum = gridDatum;
     if (ColorUtility.TryParseHtmlString(GridDatum.ColorData.ColorHexString, out var colorObj))
     {
         _spriteRenderer.color = colorObj;
     }
     else
     {
         Debug.LogError($"Invalid color code passed! - {GridDatum.ColorData.ColorHexString}");
     }
 }
Esempio n. 6
0
 public virtual void Initialize(GridDatum gridDatum)
 {
     GridDatum = gridDatum;
 }
        private void OnGridCellEnterHandler(object grid)
        {
            if (_lineDrawState != LineDrawState.Drawing)
            {
                return;
            }

            var cellView = (GridCellView)grid;

            if (cellView == null)
            {
                throw new InvalidEnumArgumentException();
            }
            var gridDatum = cellView.GridDatum;
            var isDot     = (cellView is DotView);

            Debug.Log($"IS DOT :{isDot}");
            if (!isDot)
            {
                if (gridDatum.ColorData != null)
                {
                    Debug.Log($"gridDatum.ColorData!=null=> {gridDatum.ColorData.ColorId!= _currentLineColorData.ColorId}");

                    if (gridDatum.ColorData.ColorId != _currentLineColorData.ColorId && AreAdjacent(gridDatum, _currentGridDatum)) // Clear only if im going to continue along
                    {
                        Debug.Log($"Clearing line {gridDatum.ColorData.ColorId}");
                        ClearLine(gridDatum.ColorData.ColorId);
                    }
                }
                else
                {
                    Debug.Log("Color Data is null");
                }
                if (IsOverlappingCurrentLine(gridDatum))
                {
                    return;
                }
            }
            else if (cellView.GridDatum.ColorData.ColorId != _currentLineColorData.ColorId)
            {
                return;
            }
            else if (cellView == _currentDotOrigin)
            {
                return;
            }

            if (_currentGridDatum != null && !AreAdjacent(gridDatum, _currentGridDatum))
            {
                return;
            }
            _currentGridDatum = gridDatum;
            ExtendCurrentLine(gridDatum, isDot);

            if (!(cellView is DotView) || cellView == _currentDotOrigin ||
                cellView.GridDatum.ColorData?.ColorId != _currentLineColorData.ColorId)
            {
                return;
            }

            EventManager.Raise(CustomEventType.OnLineCompleted, _currentLineColorData.ColorId);
            _completedLines.Add(_currentLineColorData.ColorId, _currentLineData);
            _completedLineGridDatum.Add(_currentLineColorData.ColorId, _currentLineGridData);
            FinishDrawing();
        }
 private bool IsOverlappingCurrentLine(GridDatum grid)
 {
     return(_currentLineData
            .FindIndex((position) => (position.x == grid.PosX) && (position.y == grid.PosY)) > 0);
 }
 private bool AreAdjacent(GridDatum g1, GridDatum g2)
 {
     return(Mathf.Abs(g1.PosX - g2.PosX) + Mathf.Abs(g1.PosY - g2.PosY) <= 1);
 }
 public static Vector3  GetLocalPositionFromIndices(this GridDatum gridDatum)
 {
     return(new Vector3(gridDatum.PosX, gridDatum.PosY, 0f));
 }