Example #1
0
        public void UpdateAndRenderShape(BoardLogic board)
        {
            Assert.IsTrue(!this.wasHitByExplosive);
            Assert.IsTrue(!board.createShape);


            bool isHoldingShape = board.currentHotIndex >= 0;

            bool turnSolid = false;

            TimerReturnInfo timerInfo = this.moveTimer.updateTimer(this.moveTime);

            if (timerInfo.finished)
            {
                this.moveTimer.turnTimerOn();
                this.moveTimer.value = timerInfo.residue;
                if (!this.MoveShape(board, MoveType.MOVE_DOWN))
                {
                    turnSolid = true;
                }
            }
            this.wasHitByExplosive = false;
            if (turnSolid)
            {
                solidfyShape(board);
                board.createShape      = true;
                this.wasHitByExplosive = false;
                ResetMouseUI(board);
            }
            else
            {
                int hotBlockIndex = -1;
                for (int i = 0; i < this.count; ++i)
                {
                    //NOTE: Render the hover indications & check for hot player block
                    Vector2   pos         = this.blocks[i].pos;
                    TwoColors alienColors = GetAlienHoverColor(i);

                    Vector4 color       = Color.white;
                    Bounds  blockBounds = new Bounds(new Vector3(pos.x, pos.y, keyStates.mouseInWorldSpace.z), new Vector3(1, 1, 1));

                    if (blockBounds.Contains(keyStates.mouseInWorldSpace))
                    {
                        hotBlockIndex = i;
                        if (board.currentHotIndex < 0)
                        {
                            color = alienColors.color1;
                        }
                    }

                    if (hotBlockIndex >= 0 && board.currentHotIndex < 0)
                    {
                        if (IsMirrorPartnerIndex(board, hotBlockIndex, i, false))
                        {
                            color = alienColors.color1;
                        }
                    }

                    if (board.currentHotIndex == i)
                    {
                        Assert.IsTrue(keyStates.isDown(ButtonType.BUTTON_LEFT_MOUSE));
                        color = alienColors.color2;
                    }

                    if (IsMirrorPartnerIndex(board, board.currentHotIndex, i, true))
                    {
                        color = alienColors.color2;
                    }

                    BoardValue val = board.GetBoardValue(pos);
                    Assert.IsTrue(val.valid);
                    val.color = color;
                }

                //NOTE: Have to do this afterwards since we the block can be before for the mirrorHotIndex
                if (board.currentHotIndex < 0 && hotBlockIndex >= 0 && board.isMirrorLevel)
                {
                    int mirrorOffsetCount = board.shapeSizes[0];
                    int mirrorIndexAt     = hotBlockIndex < mirrorOffsetCount ? (hotBlockIndex + mirrorOffsetCount) : (hotBlockIndex - mirrorOffsetCount);

                    if (HasMirrorPartner(board, hotBlockIndex, mirrorIndexAt)) //same size
                    {
                        Assert.IsTrue(mirrorIndexAt >= 0 && mirrorIndexAt < GetTotalNumberOfShapeBlocks(board));

                        Vector2    pos = this.blocks[mirrorIndexAt].pos;
                        BoardValue val = board.GetBoardValue(pos);
                        Assert.IsTrue(val.valid);
                        val.color = GetAlienHoverColor(hotBlockIndex).color1;
                    }
                    else
                    {
                        // printf("%s\n", "no mirror partner");
                        Assert.IsTrue(board.shapeSizes[0] != board.shapeSizes[1]);
                    }
                }

                if (keyStates.wasPressed(ButtonType.BUTTON_LEFT_MOUSE) && hotBlockIndex >= 0)
                {
                    board.currentHotIndex = hotBlockIndex;
                }

                if (board.currentHotIndex >= 0)
                {
                    //We are holding onto a block
                    Vector2 boardPosAt = keyStates.mouseInWorldSpace;
                    boardPosAt.x = (int)(Mathf.Clamp(boardPosAt.x, 0, board.boardWidth - 1) + 0.5f);
                    boardPosAt.y = (int)(Mathf.Clamp(boardPosAt.y, 0, board.boardHeight - 1) + 0.5f);

                    int  hotIndex = board.currentHotIndex;
                    bool okToMove = shapeStillConnected(hotIndex, boardPosAt, board);

                    Vector2 boardPosAtMirror = new Vector2(0, 0); //not used unless is a mirror level
                    int     mirrorIndex      = -1;
                    bool    isEvenSize       = true;
                    if (board.isMirrorLevel)
                    {
                        int mirrorOffsetCount = board.shapeSizes[0];
                        mirrorIndex = hotIndex < mirrorOffsetCount? (hotIndex + mirrorOffsetCount) : (hotIndex - mirrorOffsetCount);
                        if (HasMirrorPartner(board, board.currentHotIndex, mirrorIndex)) //
                        {
                            Vector2 mirrorOffset = boardPosAt - this.blocks[hotIndex].pos;
                            boardPosAtMirror = this.blocks[mirrorIndex].pos + mirrorOffset;

                            okToMove &= shapeStillConnected(mirrorIndex, boardPosAtMirror, board);
                        }
                        else
                        {
                            Assert.IsTrue(board.shapeSizes[0] != board.shapeSizes[1]);
                            isEvenSize = false;
                        }
                    }

                    int       mirCount   = (board.isMirrorLevel && isEvenSize) ? 2 : 1;
                    int[]     hotIndexes = new int[] { hotIndex, mirrorIndex };
                    Vector2[] newPoses   = new Vector2[] { boardPosAt, boardPosAtMirror };

                    if (okToMove)
                    {
                        //play the sound once
                        board.playArrangeSound();
                        for (int m = 0; m < mirCount; ++m)
                        {
                            int thisHotIndex = hotIndexes[m];

                            Vector2 oldPos = this.blocks[thisHotIndex].pos;
                            Vector2 newPos = newPoses[m];

                            Assert.IsTrue(board.GetBoardState(oldPos) == BoardState.BOARD_SHAPE);
                            Assert.IsTrue(board.GetBoardState(newPos) == BoardState.BOARD_NULL);
                            board.SetBoardState(oldPos, BoardState.BOARD_NULL, BoardValType.BOARD_VAL_NULL);
                            board.SetBoardState(newPos, BoardState.BOARD_SHAPE, this.blocks[thisHotIndex].type);
                            this.blocks[thisHotIndex].pos = newPos;
                            Assert.IsTrue(board.GetBoardState(newPos) == BoardState.BOARD_SHAPE);
                        }
                    }
                }
            }
        }