Ejemplo n.º 1
0
    //Initializes every Block as a GameObject
    private void InitBlocks()
    {
        //To make the edges of the dirt look nice a padded map is added where there is one layer of air around all 4 edges
        //Currently only used by Dirt.InitializeSprite()

        //Initializes the empty padded map
        PrimitiveBlock[][] paddedMap     = new PrimitiveBlock[BIOME_WIDTH + 2][];
        PrimitiveBlock[][] paddedBackMap = new PrimitiveBlock[BIOME_WIDTH + 2][];
        for (int i = 0; i < BIOME_WIDTH + 2; i++)
        {
            paddedMap[i]     = new PrimitiveBlock[BIOME_HEIGHT + 2];
            paddedBackMap[i] = new PrimitiveBlock[BIOME_HEIGHT + 2];
        }

        //Fills the padded map
        for (int x = 0; x < BIOME_WIDTH + 2; x++)
        {
            for (int y = 0; y < BIOME_HEIGHT + 2; y++)
            {
                //If it's a piece of padding to be added
                if (x == 0 || x == BIOME_WIDTH + 1 || y == 0 || y == BIOME_HEIGHT + 1)
                {
                    paddedMap[x][y]     = new PrimitiveBlock(x, y);
                    paddedBackMap[x][y] = new PrimitiveBlock(x, y);
                }
                //If the block is a trench block, when initializing dirt sprites a Trench block is considered a Dirt block
                else if (map[x - 1][y - 1].bt == BlockType.TRENCH)
                {
                    paddedMap[x][y] = new PrimitiveBlock(x, y, BlockType.DIRT);
                }
                //If it's a block that's also present in map
                else
                {
                    paddedMap[x][y]     = map[x - 1][y - 1];
                    paddedBackMap[x][y] = backMap[x - 1][y - 1];
                }
            }
        }

        GameObject     go;
        PrimitiveBlock pb;

        //Initializes every block
        for (int x = 0; x < BIOME_WIDTH; x++)
        {
            for (int y = 0; y < BIOME_HEIGHT; y++)
            {
                pb = map[x][y];
                //Adds the correct scripts (if any) to
                switch (pb.bt)
                {
                case BlockType.DIRT:
                    go = Instantiate <GameObject>(baseDirt);
                    Dirt dirt = go.GetComponent <Dirt>();
                    dirt.SetPosition(pb.x, pb.y);
                    dirt.InitializeSprite(paddedMap);
                    break;

                case BlockType.BRIDGE:
                    go = Instantiate <GameObject>(baseBridge);
                    Bridge bridge = go.GetComponent <Bridge>();
                    bridge.SetPosition(pb.x, pb.y);
                    bridge.InitializeSprite(map);
                    break;

                case BlockType.LADDER:
                    go = Instantiate <GameObject>(baseLadder);
                    Ladder ladder = go.GetComponent <Ladder>();
                    ladder.SetPosition(pb.x, pb.y);
                    ladder.InitializeSprite(map);
                    break;

                case BlockType.TRENCH:
                    go = Instantiate <GameObject>(baseTrench);
                    Trench trench = go.GetComponent <Trench>();
                    trench.SetPosition(pb.x, pb.y);
                    trench.InitializeSprite(map);
                    break;

                case BlockType.PLATFORM:
                    go = Instantiate <GameObject>(basePlatform);
                    Platform platform = go.GetComponent <Platform>();
                    platform.SetPosition(pb.x, pb.y);
                    platform.InitializeSprite(map);
                    break;

                case BlockType.ROOF:
                    go = Instantiate <GameObject>(baseRoof);
                    Roof roof = go.GetComponent <Roof>();
                    roof.SetPosition(pb.x, pb.y);
                    roof.InitializeSprite(map);
                    break;

                case BlockType.BOX:
                    go = Instantiate <GameObject>(baseBox);
                    Box box = go.GetComponent <Box>();
                    box.SetPosition(pb.x, pb.y);
                    box.InitializeSprite(map);
                    break;

                case BlockType.EXPLOSIVE:
                    go = Instantiate(baseExplosive);
                    Explosive e = go.GetComponent <Explosive>();
                    e.SetPosition(pb.x, pb.y);
                    break;
                }

                pb = backMap[x][y];
                BackgroundBlock bb;
                switch (pb.bt)
                {
                case BlockType.BACKGROUND_BLOCK:
                    go = Instantiate(baseBackgroundBlock);
                    bb = go.GetComponent <BackgroundBlock>();
                    bb.SetPosition(pb.x, pb.y);
                    bb.InitializeSprite(backMap);
                    //go.isStatic = true;
                    break;

                case BlockType.DIRT:
                    go = Instantiate(baseBackgroundBlock);
                    bb = go.GetComponent <BackgroundBlock>();
                    bb.SetPosition(pb.x, pb.y);
                    Dirt.InitializeSprite(paddedBackMap, go.GetComponent <SpriteRenderer>(), pb.x, pb.y);
                    //go.isStatic = true;
                    break;
                }
            }
        }
    }