Esempio n. 1
0
        private void UpdateGhostBubble(bool valid, Vector3 endPoint)
        {
            var ghostBubble = _contexts.game.GetGroup(GameMatcher.Ghost).GetSingleEntity();

            if (ghostBubble == null)
            {
                ghostBubble = BubbleCreatorService.CreateGhostBubble(new AxialCoord {
                    Q = 0, R = 0
                });
            }

            if (!valid)
            {
                ghostBubble.isDestroyed = true;
                return;
            }

            var hex = HexHelperService.PointToHex(endPoint);
            var neighbourAxialCoord = HexHelperService.FindNearestNeighbour(hex, endPoint);

            if (ghostBubble.axialCoord.Value.Q != hex.Q || ghostBubble.axialCoord.Value.R != hex.R)
            {
                ghostBubble.ReplaceAxialCoord(neighbourAxialCoord);
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        private void ShootBall(List <Vector3> trajectory, GameEntity ghostBubble)
        {
            var shotBubble = _contexts.game.GetGroup(GameMatcher.Shot).GetSingleEntity();

            if (shotBubble != null)
            {
                return;
            }

            var finalAxialPos = ghostBubble.axialCoord.Value;
            var finalPos      = HexHelperService.HexToPoint(finalAxialPos);

            trajectory[trajectory.Count - 1] = finalPos;

            var shooterBubble = _contexts.game.GetGroup(GameMatcher.WillBeShotNext).GetSingleEntity();
            var bubbleId      = shooterBubble.id.Value;

            shooterBubble.AddShot(trajectory.ToArray(), () =>
            {
                var bubble = _contexts.game.GetEntityWithId(bubbleId);
                if (bubble == null)
                {
                    return;
                }

                bubble.RemoveShot();
                bubble.isWillBeShotNext = false;
                bubble.RemovePosition();
                bubble.AddAxialCoord(finalAxialPos);
                bubble.isMergeDirty = true;
            });

            ghostBubble.isDestroyed = true;
            _contexts.game.RemoveShootingTrajectory();
        }
Esempio n. 4
0
        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]);
        }
Esempio n. 5
0
        protected override void Execute(List <GameEntity> entities)
        {
            _contexts.game.isBubbleShiftDirty = false;
            var indented = _contexts.game.boardOffset.Indented;

            _contexts.game.ReplaceBoardOffset(!indented);

            var hexMap = HexStorageService.UpdateHexMap();

            foreach (var bubble in hexMap)
            {
                if (bubble == null)
                {
                    continue;
                }
                var bubblePos = bubble.position.Value;
                bubble.ReplaceAxialCoord(HexHelperService.PointToHex(bubblePos));
                bubble.RemovePosition();
            }

            hexMap = HexStorageService.UpdateHexMap();

            var boardSize = _contexts.game.boardSize.Value;
            var width     = boardSize.x;
            var height    = boardSize.y;
            var r         = height - 1;

            RemoveRow(r, width, hexMap);
            RemoveRow(r - 1, width, hexMap);
            FillRow(r - 2, width, hexMap);
            FillRow(r - 3, width, hexMap);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        public void OnAxialCoord(GameEntity entity, AxialCoord hex)
        {
            transform.position = HexHelperService.HexToPoint(hex);

            if (entity.isGhost)
            {
                GhostBubbleAppear();
            }
        }
Esempio n. 8
0
        public void OnNudged(GameEntity entity, AxialCoord from, Action callback)
        {
            var pos            = (Vector2)transform.position;
            var nudgePos       = HexHelperService.HexToPoint(from);
            var nudgeDirection = pos - nudgePos;

            nudgeDirection = nudgeDirection.normalized;

            transform.DOMove(pos + nudgeDirection * 0.2f, 0.2f).SetEase(Ease.OutSine).SetLoops(2, LoopType.Yoyo)
            .onComplete += () => callback();
        }
Esempio n. 9
0
        public void OnMergeTo(GameEntity entity, AxialCoord spot, Action callback)
        {
            var movement = transform.DOMove(HexHelperService.HexToPoint(spot), MergeDuration);

            DoWait.WaitSeconds(0.05f, () =>
                               CreateBubbleParticle(transform.position, spriteRenderer.color, mergeParticle));
            var seq = DOTween.Sequence();

            seq.AppendInterval(0.05f);
            seq.Append(movement);
            seq.onComplete += () => callback();
        }
Esempio n. 10
0
 private void ShiftBubbles(int direction)
 {
     foreach (var bubble in _bubbleGroup)
     {
         var bubblePos = HexHelperService.HexToPoint(bubble.axialCoord.Value);
         bubblePos.y += ShiftAmount * direction;
         bubble.AddShiftTo(bubblePos, () =>
         {
             bubble.RemoveShiftTo();
             bubble.AddPosition(bubblePos);
             _contexts.game.isBubbleShiftDirty = true;
         });
     }
 }
Esempio n. 11
0
        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);
                }
            }
        }
Esempio n. 12
0
        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;
            }
        }
Esempio n. 13
0
        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;
            });
        }
Esempio n. 14
0
        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());
            }
        }
Esempio n. 15
0
        protected override void Execute(List <GameEntity> entities)
        {
            _contexts.input.isInputDisabled = false;
            var shouldShiftDown = true;

            foreach (var bubble in _bubbleGroup)
            {
                var point = HexHelperService.HexToPoint(bubble.axialCoord.Value);
                if (point.y <= UpShitLimit)
                {
                    shouldShiftDown = false;
                }
                if (point.y <= DownShiftLimit)
                {
                    ShiftUp();
                    return;
                }
            }

            if (shouldShiftDown)
            {
                ShiftDown();
            }
        }
Esempio n. 16
0
 public void OnAxialCoord(GameEntity entity, AxialCoord hex)
 {
     transform.position = HexHelperService.HexToPoint(hex);
 }
        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);
        }
Esempio n. 18
0
        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);
        }