/// <summary> /// Creates a new Level filled with Empty blocks and a start and endPlatform /// </summary> /// <param name="width">must be at least 7</param> /// <param name="height">must be at least 7</param> /// <param name="name"></param> public Level_Data(int width, int height, string name) { if (width < MinDimension) { throw new InvalidOperationException($"Trying to create new {nameof(Level_Data)} with width of " + width + " but it must be at least " + MinDimension + "!"); } if (height < MinDimension) { throw new InvalidOperationException($"Trying to create new {nameof(Level_Data)} with height of " + height + " but it must be at least " + MinDimension + "!"); } this.Name = name; this.BlockMap = new Block_Data[width, height]; StartPlatformCoordinates = new Vector2Int(1, height / 2); GoalPlatformCoordinates = new Vector2Int(width - 2, height / 2); Block_Data emptyBlock = new EmptyBlock_Data(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == height / 2) { if (j < 3) { BlockMap[j, i] = new StartBlock_Data(); } else if (j > width - 4) { BlockMap[j, i] = new GoalBlock_Data(); } else { BlockMap[j, i] = emptyBlock; } } else { BlockMap[j, i] = emptyBlock; } } } Block_Data lockedEmptyBlock = new EmptyBlock_Data(); lockedEmptyBlock.IsReplaceable = false; for (int j = 1; j < 3; j++) { for (int i = 0; i < 3; i++) { BlockMap[i, j + height / 2] = lockedEmptyBlock; } } }
/// <summary> /// Removes oldStartPlatform -> unlocks old locked Blocks -> Sets new startPlatform -> locks blocks above it /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void SetStartPlatform(int x, int y) { x = Mathf.Clamp(x, platformWidth / 2, width - (platformWidth + 1) / 2); int oldX = levelData.StartPlatformCoordinates.x; int oldY = levelData.StartPlatformCoordinates.y; int oldlockedHeight = Mathf.Min(height - oldY, 3); int lockedHeight = Mathf.Min(height - y, 3); //Remove old StartPlatform EmptyBlock_Data emptyData = new EmptyBlock_Data(); for (int i = 0; i < platformWidth; i++) { SetBlock((oldX - platformWidth / 2 + i), oldY, emptyData); } //Release lock on blocks above StartPlatform for (int j = 1; j < oldlockedHeight; j++) { for (int i = 0; i < platformWidth; i++) { levelData.BlockMap[oldX - platformWidth / 2 + i, Mathf.Min(oldY + j, height - 1)].IsReplaceable = true; } } EmptyBlock_Data lockedEmptyData = new EmptyBlock_Data(); lockedEmptyData.IsReplaceable = false; //Place new StartPlatform StartBlock_Data startData = new StartBlock_Data(); for (int i = 0; i < platformWidth; i++) { SetBlock((x - platformWidth / 2 + i), y, startData); } //Lock blocks above startPlatform(empty or otherwise player stuck in block) for (int j = 1; j < lockedHeight; j++) { for (int i = 0; i < platformWidth; i++) { SetBlock((x - platformWidth / 2 + i), Mathf.Min(y + j, height - 1), lockedEmptyData); } } levelData.StartPlatformCoordinates = new Vector2Int(x, y); }