public BoardDesc Copy() { BoardDesc copy = this; copy.colors = new Color[colors.Length]; System.Array.Copy(colors, copy.colors, colors.Length); return(copy); }
void Generate() { Debug.Log("generating board " + boardDesc); var props = new MaterialPropertyBlock(); int _ColorID = Shader.PropertyToID("_Color"); System.Array.Resize(ref cells, boardDesc.size.x * boardDesc.size.y); int layer = LayerMask.NameToLayer("Board"); gameObject.layer = layer; Util.DestroyChildren(transform); foreach (var coord in Coords()) { var obj = Instantiate <GameObject>(cellPrefab); obj.layer = layer; obj.name = "board_" + coord; obj.layer = gameObject.layer; obj.transform.SetParent(transform); // Position the block obj.transform.localPosition = new Vector3( coord.x * boardDesc.cellSize.x, Random.value * boardDesc.yNoise, coord.y * boardDesc.cellSize.y); var index = (coord.x + coord.y) % 2 == 0 ? 1 : 0; //var index = (int)Mathf.Repeat((float)count, boardDesc.colors.Length); var baseColor = boardDesc.colors[index]; props.SetColor(_ColorID, boardDesc.colors[index]); obj.GetComponent <Renderer>().SetPropertyBlock(props); var cell = cells[CoordToIndex(coord)] = obj.GetComponent <Cell>().Init(this, coord, baseColor); if (boardDesc.BorderWalls) { cell.SetWall(Direction.North, coord.y == boardDesc.size.y - 1); cell.SetWall(Direction.East, coord.x == boardDesc.size.x - 1); cell.SetWall(Direction.South, coord.y == 0); cell.SetWall(Direction.West, coord.x == 0); } } lastDesc = boardDesc.Copy(); }