public Gem Spawn(Enums.GemId gem, Vector2 position) { Gem spawned = _pools[gem].Get().GetComponent <Gem>(); spawned.transform.position = new Vector2(position.x, _spawnHeight); spawned.gameObject.SetActive(true); spawned.Move(position); return(spawned); }
bool CheckMatchGem(Vector2Int gem) { if (_grid[gem.x, gem.y] == null) { return(false); } Enums.GemId gemId = _grid[gem.x, gem.y].Id; bool hasHoriMatch = false; bool hasVertMatch = false; // number of matching gems in positive/negative direction. int matchHoriPos = 0; int matchHoriNeg = 0; int matchVertPos = 0; int matchVertNeg = 0; // Check horizontal match. for (int i = gem.x + 1; i < _gridSize.x; i++) { if (_grid[i, gem.y] == null) { break; } else if (_grid[i, gem.y].Id != gemId) { break; } else { matchHoriPos++; } } for (int i = gem.x - 1; i >= 0; i--) { if (_grid[i, gem.y] == null) { break; } else if (_grid[i, gem.y].Id != gemId) { break; } else { matchHoriNeg++; } } if (matchHoriPos + matchHoriNeg >= 2) { hasHoriMatch = true; } // Check vertical match. for (int i = gem.y + 1; i < _gridSize.y; i++) { if (_grid[gem.x, i] == null) { break; } else if (_grid[gem.x, i].Id != gemId) { break; } else { matchVertPos++; } } for (int i = gem.y - 1; i >= 0; i--) { if (_grid[gem.x, i] == null) { break; } else if (_grid[gem.x, i].Id != gemId) { break; } else { matchVertNeg++; } } if (matchVertPos + matchVertNeg >= 2) { hasVertMatch = true; } // Verify check results. if (hasHoriMatch || hasVertMatch) { // Store the match data. if (hasHoriMatch && hasVertMatch) { int[] dir = { matchHoriPos, matchHoriNeg, matchVertPos, matchVertNeg }; _matches.Add(gem, dir); } else if (hasHoriMatch) { int[] dir = { matchHoriPos, matchHoriNeg, 0, 0 }; _matches.Add(gem, dir); } else if (hasVertMatch) { int[] dir = { 0, 0, matchVertPos, matchVertNeg }; _matches.Add(gem, dir); } return(true); } else { return(false); } }