Exemplo n.º 1
0
 public BlockPiece GetBlockPiece(int x, int y, int z)
 {
     foreach (Block b in allBlocks)
     {
         BlockPiece p = b.GetBlockPiece(x, y, z);
         if (p != null)
         {
             return(p);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
    public void AddPiece(int x, int y, int z)
    {
        BlockPiece p = LevelController.instance.GetBlockPiece(x, y, z);

        if (p == null)
        {
            p = (Instantiate(LevelController.instance.blockPiecePrefab, transform) as GameObject).GetComponent <BlockPiece>();
            p.Init(x, y, z);
        }
        else
        {
            Debug.Log("Adding block piece failed. Position is occupied!");
        }
    }
Exemplo n.º 3
0
 public void AddPiece(int x, int y, int z)
 {
     if (LevelController.instance.IsWithinArea(x, y, z))
     {
         BlockPiece p = LevelController.instance.GetBlockPiece(x, y, z);
         if (p == null)
         {
             p = (Instantiate(LevelController.instance.blockPiecePrefab, transform) as GameObject).GetComponent <BlockPiece>();
             p.Init(this, x, y, z);
             p.SetMaterial(material);
             pieces.Add(p);
         }
         else
         {
             Debug.Log("Adding block piece failed. Position is occupied!");
         }
     }
     else
     {
         Debug.LogError("Adding block piece failed. Position is out of bounds!");
     }
 }
Exemplo n.º 4
0
    public void GetBlockItemData()
    {
        List <Dictionary <string, object> > item = CSVReader.Read("item");

        for (int i = 0; i < item.Count; i++)
        {
            string type = item[i]["type"].ToString();
            if (type == "blockPiece")
            {
                string name         = item[i]["name"].ToString();
                string info         = item[i]["info"].ToString();
                int    block_powder = int.Parse(item[i]["block_powder"].ToString());

                string path = item[i]["path"].ToString();

                BlockPiece blockPiece = Instantiate(blockPiecePrefab);
                blockPiece.transform.SetParent(transform);
                blockPiece.Initialize(name, info, block_powder, path);
                blockPieces.Add(blockPiece);
            }
        }
    }
Exemplo n.º 5
0
        public static void BlockHandler(Game1 game, Mario mario, IBlock block, int CollisionSide)
        {
            int     SIXTEEN   = 16;
            int     SIX       = 6;
            int     THREE     = 3;
            int     THIRTYONE = 31;
            int     EIGHT     = 8;
            int     TWELVE    = 12;
            Vector2 newPosition;
            Game1   myGame = game;

            if (block.isVisible)
            {
                switch (CollisionSide)
                {
                case 1:     //top collision

                    newPosition.X  = mario.state.marioSprite.desRectangle.X;
                    newPosition.Y  = block.blockSprite.desRectangle.Y - mario.state.marioSprite.desRectangle.Height - SIX;
                    mario.position = newPosition;
                    break;

                case 2:     //right side collision

                    newPosition.X  = block.blockSprite.desRectangle.X + block.blockSprite.desRectangle.Width;
                    newPosition.Y  = mario.state.marioSprite.desRectangle.Y;
                    mario.position = newPosition;


                    break;

                case 3:     //bottom collision
                    mario.jump     = false;
                    newPosition.X  = mario.state.marioSprite.desRectangle.X;
                    newPosition.Y  = block.blockSprite.desRectangle.Y + block.blockSprite.desRectangle.Height + THREE;
                    mario.position = newPosition;
                    if (block is BrickBlock && block.blockSprite is BrickBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.BUMP);
                        if (mario.marioState != Mario.MARIO_SMALL)
                        {
                            MarioSoundManager.instance.playSound(MarioSoundManager.BREAKBLOCK);
                            block.Bump();
                            block.isVisible = false;
                            BlockPiece block1 = new BlockPiece(new Vector2(block.position.X, block.position.Y), GameConstants.BlockPieceOne);
                            BlockPiece block2 = new BlockPiece(new Vector2(block.position.X, block.position.Y + SIXTEEN), GameConstants.BlockPieceTwo);
                            BlockPiece block3 = new BlockPiece(new Vector2(block.position.X + SIXTEEN, block.position.Y), GameConstants.BlockPieceThree);
                            BlockPiece block4 = new BlockPiece(new Vector2(block.position.X + SIXTEEN, block.position.Y + SIXTEEN), GameConstants.BlockPieceFour);
                            block1.gravity = 0;
                            block2.gravity = 0;
                            block3.gravity = 0;
                            block4.gravity = 0;
                            myGame.level.itemElements.Add(block1);
                            myGame.level.itemElements.Add(block2);
                            myGame.level.itemElements.Add(block3);
                            myGame.level.itemElements.Add(block4);
                        }
                        else
                        {
                            if (!block.isBumped)
                            {
                                block.Bump();
                            }
                        }
                    }
                    else if (block is BrickBlockC && block.blockSprite is BrickBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.COIN);
                        block.Bump();
                        block.blockSprite = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();
                        Coin c = new Coin(new Vector2(block.position.X + EIGHT, block.position.Y - THIRTYONE));
                        c.jump = true;
                        myGame.level.itemElements.Add(c);
                        //score part
                        mario.isScored    = true;
                        mario.score       = GameConstants.Score2;
                        mario.coin       += 1;
                        mario.totalScore += mario.score;
                        Vector2 newP;
                        newP.X             = block.position.X + TWELVE;
                        newP.Y             = block.position.Y - THREE;
                        mario.textPosition = newP;
                    }
                    else if (block is BrickBlockCC && block.blockSprite is BrickBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.COIN);
                        BrickBlockCC b = block as BrickBlockCC;
                        if (b.coinCount > 0)
                        {
                            b.Bump();
                            Coin c = new Coin(new Vector2(block.position.X + EIGHT, block.position.Y - THIRTYONE));
                            c.jump = true;
                            myGame.level.itemElements.Add(c);
                            b.coinCount--;
                            //score part
                            mario.isScored    = true;
                            mario.score       = GameConstants.Score2;
                            mario.coin       += 1;
                            mario.totalScore += mario.score;
                            Vector2 newP;
                            newP.X             = b.blockSprite.desRectangle.X + TWELVE;
                            newP.Y             = b.blockSprite.desRectangle.Y - THREE;
                            mario.textPosition = newP;
                        }
                        else
                        {
                            block.Bump();
                            block.blockSprite = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();
                            Coin c = new Coin(new Vector2(block.position.X + EIGHT, block.position.Y - THIRTYONE));
                            c.jump = true;
                            myGame.level.itemElements.Add(c);
                            //score part
                            mario.isScored    = true;
                            mario.score       = GameConstants.Score2;
                            mario.coin       += 1;
                            mario.totalScore += mario.score;
                            Vector2 newP;
                            newP.X             = b.blockSprite.desRectangle.X + TWELVE;
                            newP.Y             = b.blockSprite.desRectangle.Y - THREE;
                            mario.textPosition = newP;
                        }
                    }
                    else if (block is BrickBlockS && block.blockSprite is BrickBlockSprite)
                    {
                        block.Bump();
                        block.blockSprite = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();
                        myGame.level.itemElements.Add(new Star(new Vector2(block.position.X, block.position.Y - GameConstants.SquareWidth)));
                    }
                    else if (block is QuestionBlockM && block.blockSprite is QuestionBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.POWERUPAPPEARS);
                        block.Bump();
                        block.blockSprite = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();

                        if (mario.marioState == Mario.MARIO_SMALL)
                        {
                            myGame.level.itemElements.Add(new RedMushroom(new Vector2(block.position.X, block.position.Y - GameConstants.SquareWidth)));
                        }
                        else if (mario.marioState == Mario.MARIO_BIG || mario.marioState == Mario.MARIO_FIRE)
                        {
                            myGame.level.itemElements.Add(new Flower(new Vector2(block.position.X, block.position.Y - GameConstants.SquareWidth)));
                        }
                    }
                    else if (block is QuestionBlockC && block.blockSprite is QuestionBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.COIN);
                        block.Bump();
                        //score part
                        mario.isScored    = true;
                        mario.score       = GameConstants.Score2;
                        mario.coin       += 1;
                        mario.totalScore += mario.score;
                        Vector2 newP;
                        newP.X             = block.blockSprite.desRectangle.X + TWELVE;
                        newP.Y             = block.blockSprite.desRectangle.Y - THREE;
                        mario.textPosition = newP;
                        block.blockSprite  = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();
                        Coin c = new Coin(new Vector2(block.position.X + EIGHT, block.position.Y - THIRTYONE));
                        c.jump = true;
                        myGame.level.itemElements.Add(c);
                    }
                    else if (block is HiddenBlock && block.blockSprite is HiddenBlockSprite)
                    {
                        MarioSoundManager.instance.playSound(MarioSoundManager.POWERUPAPPEARS);
                        block.Bump();
                        block.blockSprite = EnvironmentSpriteFactory.Instance.CreateUsedBlockSprite();
                        myGame.level.itemElements.Add(new GreenMushroom(new Vector2(block.position.X, block.position.Y - GameConstants.SquareWidth)));
                    }
                    break;

                case 4:     //left side collision
                    if (block is Pipe)
                    {
                        Pipe tempPipe = block as Pipe;
                        if (tempPipe.size == GameConstants.UnderPipe && mario.position.Y > block.position.Y - GameConstants.Three + GameConstants.Two)
                        {
                            mario.animated  = true;
                            mario.animation = GameConstants.UnderPipeAnimation;
                        }
                    }
                    newPosition.X  = block.blockSprite.desRectangle.X - mario.state.marioSprite.desRectangle.Width;
                    newPosition.Y  = mario.state.marioSprite.desRectangle.Y;
                    mario.position = newPosition;
                    break;
                }
            }
        }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        Ray        ray = camera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo, 100, LayerMask.GetMask("Block")) && dragBlock == null)
        {
            BlockPiece p = hitInfo.collider.GetComponent <BlockPiece>();
            if (p != null)
            {
                moveIndicator.transform.position = hitInfo.collider.gameObject.transform.position + hitInfo.normal * 0.51f;
                moveIndicator.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

                moveIndicator.gameObject.SetActive(true);

                if (Input.GetMouseButtonDown(0))
                {
                    dragBlock    = p.block;
                    dragNormal   = hitInfo.normal;
                    dragHitPoint = hitInfo.point;
                    dragblockRelativeToHitPoint     = dragBlock.transform.position - dragHitPoint;
                    dragBlock.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

                    dragBlock.gameObject.layer = 9; // moving block
                    foreach (BlockPiece piece in dragBlock.pieces)
                    {
                        piece.gameObject.layer = 9; // moving block
                    }
                }
            }
        }
        else
        {
            moveIndicator.gameObject.SetActive(false);
        }


        if (dragBlock != null)
        {
            Plane nPlane = new Plane(dragNormal, dragHitPoint);
            float dist   = -1;
            if (nPlane.Raycast(ray, out dist))
            {
            }
            ;

            if (dist != -1)
            {
                Vector3 newPos = ray.GetPoint(dist) + dragblockRelativeToHitPoint;
                MoveBlockTo(dragBlock, newPos);
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (dragBlock != null)
            {
                dragBlock.SnapBasedOnPosition();
                dragBlock.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                dragBlock.rigidbody.velocity    = Vector3.zero;

                dragBlock.gameObject.layer = 8; // block
                foreach (BlockPiece piece in dragBlock.pieces)
                {
                    piece.gameObject.layer = 8; // block
                }

                dragBlock = null;
            }
        }
    }