Beispiel #1
0
    public void SetData(int level, Vector2Int logicPos, int typeInt)
    {
        gameObject.name = $"tile_{logicPos.x}_{logicPos.y}";

        //设置位置
        var levelData = ConfigDataHolder.levelDataDict[level];
        int matrixX   = levelData[0].Length;
        int matrixY   = levelData.Length;

        transform.position = PositionRelateMethods.CalcLevelBlockPosition(matrixX, matrixY, logicPos);

        this.level          = level;
        this.logicPos       = logicPos;
        this.tileType       = (TileType)(typeInt);
        spriteRender.sprite = GameMain.Instance.artResManager.FindTileSprite(typeInt);
        animator.runtimeAnimatorController = GameMain.Instance.artResManager.FindTileAnim(typeInt);
        if (this.tileType == TileType.RedEnd || this.tileType == TileType.YellowEnd)
        {
            fx = Instantiate(GameMain.Instance.receiveFx, this.transform);
            fx.SetActive(false);
            fx.transform.SetParent(this.transform);
        }
        if (this.tileType == TileType.Red)
        {
            fx = Instantiate(GameMain.Instance.paintFx, this.transform);
            fx.SetActive(false);
            fx.transform.SetParent(this.transform);
        }
    }
    //产生关卡
    public Level GenerateLevel(int level)
    {
        var newLevel = Instantiate(sampleLevel, transform);

        if (!ConfigDataHolder.levelDataDict.ContainsKey(level))
        {
            return(null);
        }
        var levelData = ConfigDataHolder.levelDataDict[level];
        int matrixX   = levelData[0].Length;
        int matrixY   = levelData.Length;

        var tileDatas = new Tile[matrixX][];
        var robots    = new List <Robot>();
        var boxes     = new List <Box>();

        for (int x = 0; x < matrixX; ++x)
        {
            var colxCells = new Tile[matrixY];
            for (int y = 0; y < matrixY; ++y)
            {
                int blockValue = PositionRelateMethods.GetLevelBlockValue(level, x, y);

                //创建地块
                var newTile     = Instantiate(sampleTile, newLevel.tilesTrans);
                int tileTypeInt = blockValue % 10;
                newTile.SetData(level, new Vector2Int(x, y), tileTypeInt);
                colxCells[y] = newTile;

                int objTypeInt = blockValue / 10;
                if (objTypeInt == 0)
                {
                    continue;
                }
                var objectType = (ObjectType)objTypeInt;
                if (objectType == ObjectType.Robot)
                {
                    //创建机器人
                    var newObj = Instantiate(sampleRobot, newLevel.robotsTrans);
                    newObj.SetData(level, new Vector2Int(x, y), objTypeInt, robots.Count);
                    robots.Add(newObj);
                }
                else
                {
                    //创建箱子
                    var newObj = Instantiate(sampleBox, newLevel.boxesTrans);
                    newObj.SetData(level, new Vector2Int(x, y), objTypeInt, boxes.Count);
                    boxes.Add(newObj);
                }
            }
            tileDatas[x] = colxCells;
        }

        newLevel.SetData(level, tileDatas, robots, boxes);

        return(newLevel);
    }
    private void SetData(int level, Vector2Int logicPos, int typeInt)
    {
        var objectType = (ObjectType)(typeInt);

        //设置位置
        var levelData = ConfigDataHolder.levelDataDict[level];
        int matrixX   = levelData[0].Length;
        int matrixY   = levelData.Length;

        transform.position = PositionRelateMethods.
                             CalcLevelBlockPosition(matrixX, matrixY, logicPos);

        this.level        = level;
        this.logicPos     = logicPos;
        this.initLogicPos = logicPos;
        this.objectType   = objectType;
    }