private AxialCoord FindMergeSpot(List <AxialCoord> cluster) { var rootIndices = HexHelperService.GetArrayIndices(cluster[0]); var rootBubble = _hexMap[rootIndices.x, rootIndices.y]; var bubbleNumber = rootBubble.bubbleNumber.Value; var exponent = (int)Math.Log(bubbleNumber, 2); bubbleNumber = (int)Mathf.Pow(2, exponent + cluster.Count - 1); var maxRIndex = 0; for (var i = 0; i < cluster.Count; i++) { var axialCoord = cluster[i]; var neighbours = GetNeighboursWithBubbleNumber(axialCoord, bubbleNumber); if (neighbours.Count > 0) { return(axialCoord); } if (axialCoord.R > cluster[maxRIndex].R) { maxRIndex = i; } } return(cluster[maxRIndex]); }
private List <AxialCoord> GetNeighboursWithBubbleNumber(AxialCoord testedBubbleCoord, int bubbleNumber) { var neighbourAxialCoords = HexHelperService.GetNeighbours(testedBubbleCoord); var canBeMergedNeighbourAxialCoords = new List <AxialCoord>(); foreach (var axialCoord in neighbourAxialCoords) { var arrayIndices = HexHelperService.GetArrayIndices(axialCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } //number check if (_hexMap[arrayIndices.x, arrayIndices.y].bubbleNumber.Value != bubbleNumber) { continue; } canBeMergedNeighbourAxialCoords.Add(axialCoord); } return(canBeMergedNeighbourAxialCoords); }
public static GameEntity[,] UpdateHexMap() { var contexts = Contexts.sharedInstance; var boardSize = contexts.game.boardSize.Value; var hexMap = new GameEntity[boardSize.x, boardSize.y]; var bubbleGroup = contexts.game.GetGroup(GameMatcher.AllOf(GameMatcher.Bubble, GameMatcher.AxialCoord) .NoneOf(GameMatcher.Destroyed, GameMatcher.Ghost, GameMatcher.WillBeShotNext)); for (var x = 0; x < boardSize.x; x++) { for (var y = 0; y < boardSize.y; y++) { hexMap[x, y] = null; } } //get the bubbles in an array foreach (var bubble in bubbleGroup) { var bubbleAxialCoord = bubble.axialCoord.Value; var arrayIndices = HexHelperService.GetArrayIndices(bubbleAxialCoord); hexMap[arrayIndices.x, arrayIndices.y] = bubble; } return(hexMap); }
protected override void Execute(List <GameEntity> entities) { var boardSize = _contexts.game.boardSize.Value; var hexMap = HexStorageService.UpdateHexMap(); for (var x = 0; x < boardSize.x; x++) { for (var y = 0; y < boardSize.y; y++) { var bubble = hexMap[x, y]; if (bubble == null) { continue; } var bubbleNumber = bubble.bubbleNumber.Value; if (bubbleNumber < 2048) { continue; } var bubbleCoord = bubble.axialCoord.Value; var neighbours = HexHelperService.GetNeighbours(bubbleCoord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= boardSize.x || arrayIndices.y >= boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } ExplodeBubble(hexMap[arrayIndices.x, arrayIndices.y], false); } ExplodeBubble(bubble, true); } } }
private void RemoveRow(int r, int width, GameEntity[,] hexMap) { var rOffset = r / 2; for (var q = -rOffset; q < width - rOffset; q++) { var axialCoord = new AxialCoord { Q = q, R = r }; var indices = HexHelperService.GetArrayIndices(axialCoord); if (hexMap[indices.x, indices.y] == null) { return; } var bubble = hexMap[indices.x, indices.y]; bubble.isDestroyed = true; } }
private void StartMerge(AxialCoord mergeSpot, List <AxialCoord> rest) { foreach (var axialCoord in rest) { var indices = HexHelperService.GetArrayIndices(axialCoord); var bubble = _hexMap[indices.x, indices.y]; var bubbleId = bubble.id.Value; bubble.AddMergeTo(mergeSpot, () => { var b = _contexts.game.GetEntityWithId(bubbleId); if (b == null) { return; } b.isDestroyed = true; }); } var masterIndices = HexHelperService.GetArrayIndices(mergeSpot); var master = _hexMap[masterIndices.x, masterIndices.y]; var masterId = master.id.Value; master.AddMergeTo(mergeSpot, () => { var b = _contexts.game.GetEntityWithId(masterId); if (b == null) { return; } b.isDestroyed = true; var bubbleNumber = b.bubbleNumber.Value; var exponent = (int)Math.Log(bubbleNumber, 2); bubbleNumber = (int)Mathf.Pow(2, exponent + rest.Count); bubbleNumber = Mathf.Min(2048, bubbleNumber); var nb = BubbleCreatorService.CreateBoardBubble(mergeSpot, bubbleNumber); nb.isMergeDirty = true; }); }
private void FillRow(int r, int width, GameEntity[,] hexMap) { var rOffset = r / 2; for (var q = -rOffset; q < width - rOffset; q++) { var axialCoord = new AxialCoord { Q = q, R = r }; var indices = HexHelperService.GetArrayIndices(axialCoord); if (hexMap[indices.x, indices.y] != null) { continue; } BubbleCreatorService.CreateBoardBubble(axialCoord, BubbleCreatorService.GenerateRandomBubbleNumber()); } }
private bool IsConnected(AxialCoord rootCoord) { var ceilingCoords = _contexts.game.ceilingCoords.Value; var visited = new bool[_boardSize.x, _boardSize.y]; for (var x = 0; x < _boardSize.x; x++) { for (var y = 0; y < _boardSize.y; y++) { visited[x, y] = false; } } var queue = new Queue <AxialCoord>(); queue.Enqueue(rootCoord); var rootIndices = HexHelperService.GetArrayIndices(rootCoord); visited[rootIndices.x, rootIndices.y] = true; while (queue.Count > 0) { var testCord = queue.Dequeue(); if (ceilingCoords.Any(c => c.Q == testCord.Q && c.R == testCord.R)) { return(true); } var neighbours = HexHelperService.GetNeighbours(testCord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //visited check if (visited[arrayIndices.x, arrayIndices.y]) { continue; } visited[arrayIndices.x, arrayIndices.y] = true; //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } queue.Enqueue(neighbourCoord); } } return(false); }
private List <AxialCoord> GetBubbleCluster(AxialCoord bubbleCoord) { var visited = new bool[_boardSize.x, _boardSize.y]; for (var x = 0; x < _boardSize.x; x++) { for (var y = 0; y < _boardSize.y; y++) { visited[x, y] = false; } } var cluster = new List <AxialCoord>(); var queue = new Queue <AxialCoord>(); var rootIndices = HexHelperService.GetArrayIndices(bubbleCoord); var rootBubble = _hexMap[rootIndices.x, rootIndices.y]; var bubbleNumber = rootBubble.bubbleNumber.Value; cluster.Add(bubbleCoord); queue.Enqueue(bubbleCoord); visited[rootIndices.x, rootIndices.y] = true; while (queue.Count > 0) { var testCord = queue.Dequeue(); var neighbours = HexHelperService.GetNeighbours(testCord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } //visited check if (visited[arrayIndices.x, arrayIndices.y]) { continue; } visited[arrayIndices.x, arrayIndices.y] = true; //number check if (_hexMap[arrayIndices.x, arrayIndices.y].bubbleNumber.Value != bubbleNumber) { continue; } queue.Enqueue(neighbourCoord); cluster.Add(neighbourCoord); } } return(cluster); }