diff --git a/src/dk/itu/mario/level/MyNewLevel.java b/src/dk/itu/mario/level/MyNewLevel.java index 9138dde..c6e7a6f 100644 --- a/src/dk/itu/mario/level/MyNewLevel.java +++ b/src/dk/itu/mario/level/MyNewLevel.java @@ -1,5 +1,6 @@ package dk.itu.mario.level; +import java.util.ArrayList; import java.util.Random; import dk.itu.mario.MarioInterface.GamePlay; @@ -8,20 +9,25 @@ import dk.itu.mario.engine.sprites.Enemy; import dk.itu.mario.engine.sprites.SpriteTemplate; public class MyNewLevel extends Level { + public enum MazeLevel { + BOT, MID, TOP + } + public static long lastSeed; private static Random levelSeedRandom = new Random(); public int BLOCKS_COINS = 0; // the number of coin blocks public int BLOCKS_EMPTY = 0; // the number of empty blocks - public int BLOCKS_POWER = 0; // the number of power blocks + public int BLOCKS_POWER = 0; // the number of power blocks public int COINS = 0; // These are the coins in boxes that Mario collect + // Store information about the level public int ENEMIES = 0; // the number of enemies the level contains private int difficulty; - private int gaps; private int type; + Random random; public MyNewLevel(int width, int height) { @@ -68,13 +74,12 @@ public class MyNewLevel extends Level { // create the start location int length = buildStraight(0, width, true); - length += buildPipe(length, width - length, false); - length += buildStraight(length, width, true); - length += buildPipe(length, width - length, true); + // length += buildPipeJump(length, width - length); + length += buildMaze(length, width - length); // create all of the medium sections while (length < width - 64) { - length += buildStraight(length, width, true); + length += buildStraight(length, width - length, true); } // set the end piece @@ -225,60 +230,227 @@ public class MyNewLevel extends Level { } } - private int buildPipe(int xo, int maxLength, boolean pits) { - int numPipes = 4; - int floor = height - 1; - int length = numPipes * 4; - int height; + private int buildMaze(int xo, int maxLength) { + int length = random.nextInt(maxLength - 19) + 20; + int soFar = 0; + int next; + MazeLevel last = MazeLevel.BOT; - int[] pitLens = new int[numPipes]; - int[] pipeGrad = new int[numPipes]; + class Stretch { + public int len; + public MyNewLevel.MazeLevel lvl; - for (int i = 0; i < numPipes; i++) { - pitLens[i] = random.nextInt(6) + 1; - pipeGrad[i] = random.nextInt(5) - 2; + public Stretch(int lngth) { + len = lngth; - length += pitLens[i]; + switch (random.nextInt(3)) { + case 0: + lvl = MazeLevel.TOP; + break; + case 1: + lvl = MazeLevel.MID; + break; + default: + lvl = MazeLevel.BOT; + } + } } - if (length > maxLength) { - return 0; + ArrayList maze = new ArrayList(); + + while (soFar < length - 3) { + next = random.nextInt(length / 2) + 1; + + if (soFar + next > length - 3) { + next = length - soFar; + } + + maze.add(new Stretch(next)); + + soFar += next; } - length = 0; - height = pipeGrad[0]; + soFar = 0; + setBlock(xo, this.height - 1, Level.GROUND); + setBlock(xo + 1, this.height - 1, Level.GROUND); + setBlock(xo + 2, this.height - 1, Level.GROUND); - for (int i = 0; i < numPipes; i++) { - if (height < 1) { - height = 1; - } + for (Stretch str : maze) { + for (int x = 0; x < str.len; x++) { + setBlock(xo + 3 + soFar + x, this.height - 1, Level.GROUND); + setBlock(xo + 3 + soFar + x, this.height - 4, Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 7, Level.BLOCK_EMPTY); - setBlock(xo + length, floor, pits ? Level.HILL_TOP_LEFT - : Level.GROUND); - setBlock(xo + length + 1, floor, pits ? Level.HILL_TOP_RIGHT - : Level.GROUND); + if (x == str.len / 2) { - for (int h = 0; h < height; h++) { - setBlock(xo + length, floor - 1 - h, Level.TUBE_SIDE_LEFT); - setBlock(xo + length + 1, floor - 1 - h, Level.TUBE_SIDE_RIGHT); - } - - setBlock(xo + length, floor - 1 - height, Level.TUBE_TOP_LEFT); - setBlock(xo + length + 1, floor - 1 - height, Level.TUBE_TOP_RIGHT); - - for (int j = 0; j < pitLens[i]; j++) { - if (!pits) { - setBlock(xo + length + 2 + j, floor, Level.GROUND); + if (str.lvl != MazeLevel.BOT) { + setBlock(xo + 3 + soFar + x, this.height - 2, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 3, + Level.BLOCK_EMPTY); + } + if (str.lvl != MazeLevel.MID) { + setBlock(xo + 3 + soFar + x, this.height - 5, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 6, + Level.BLOCK_EMPTY); + } + if (str.lvl != MazeLevel.TOP) { + setBlock(xo + 3 + soFar + x, this.height - 8, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 9, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 10, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 11, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 12, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 13, + Level.BLOCK_EMPTY); + setBlock(xo + 3 + soFar + x, this.height - 14, + Level.BLOCK_EMPTY); + } } } - length += (2 + pitLens[i]); - height += (i == numPipes - 1) ? 0 : pipeGrad[i + 1]; + last = str.lvl; } return length; } + // private int buildPlatformJump(int xo, int maxLength) { + // int length = random.nextInt(maxLength + 1); + // int soFar; + // int segment; + // + // for (soFar = 0; soFar < length;) { + // int + // } + // + // return length; + // } + + private int buildPipeJump(int xo, int maxLength) { + int numPipes = 4; + int length = numPipes * 2; + int lastHeight = 4; + int localHeight; + int pipeHeight; + int gap = 0; + + while (length > maxLength) { + numPipes--; + length = numPipes * 2; + } + + if (length == 0) { + return length; + } + + for (int i = 0; i < numPipes && length < maxLength; i++) { + length += random.nextInt(4); + } + + if (length > maxLength) { + length = maxLength; + } + + for (int soFar = 0; numPipes > 0; numPipes--) { + localHeight = (soFar == 0) ? random.nextInt(2) + 4 : random + .nextInt(7) + 4; + + while (Math.abs(localHeight - lastHeight) > 3) { + localHeight += localHeight > lastHeight ? -1 : 1; + } + + lastHeight = localHeight; + pipeHeight = localHeight > 5 ? 4 + random.nextInt(localHeight - 4) + : 4; + + for (int y = 0; y < localHeight; y++) { + setBlock(xo + soFar, this.height - 1 - y, Level.GROUND); + setBlock(xo + soFar + 1, this.height - 1 - y, Level.GROUND); + + if (y == localHeight - 1) { + setBlock(xo + soFar, this.height - 1 - y, + Level.TUBE_TOP_LEFT); + setBlock(xo + soFar + 1, this.height - 1 - y, + Level.TUBE_TOP_RIGHT); + } else if (y > localHeight - pipeHeight) { + setBlock(xo + soFar, this.height - 1 - y, + Level.TUBE_SIDE_LEFT); + setBlock(xo + soFar + 1, this.height - 1 - y, + Level.TUBE_SIDE_RIGHT); + } + } + + gap = random.nextInt(4); + while (soFar + gap + 2 > length && gap >= 0) { + gap--; + } + + soFar += (2 + gap); + } + + return length; + + // int numPipes = 4; + // int floor = height - 1 - random.nextInt(2); + // int length = numPipes * 4; + // int height; + // + // int[] pitLens = new int[numPipes]; + // int[] pipeGrad = new int[numPipes]; + // + // for (int i = 0; i < numPipes; i++) { + // pitLens[i] = random.nextInt(6) + 1; + // pipeGrad[i] = random.nextInt(4); + // + // length += pitLens[i]; + // } + // + // if (length > maxLength) { + // return 0; + // } + // + // length = 0; + // height = pipeGrad[0]; + // + // for (int i = 0; i < numPipes; i++) { + // + // height = (height == 0) ? 1 : (height < 1) ? height * -1 : height; + // + // setBlock(xo + length, floor, pits ? Level.HILL_TOP_LEFT + // : Level.GROUND); + // setBlock(xo + length + 1, floor, pits ? Level.HILL_TOP_RIGHT + // : Level.GROUND); + // + // for (int h = 0; h < height; h++) { + // setBlock(xo + length, floor - 1 - h, Level.TUBE_SIDE_LEFT); + // setBlock(xo + length + 1, floor - 1 - h, Level.TUBE_SIDE_RIGHT); + // } + // + // setBlock(xo + length, floor - 1 - height, Level.TUBE_TOP_LEFT); + // setBlock(xo + length + 1, floor - 1 - height, Level.TUBE_TOP_RIGHT); + // + // if (!pits) { + // for (int y = floor; y < pitLens[i]; y++) { + // for (int x = xo + length + 2; x < xo + length + 2 + // + pitLens[i]; x++) { + // setBlock(x, y, Level.HILL_FILL); + // } + // } + // + // length += (2 + pitLens[i]); + // height += (i == numPipes - 1) ? 0 : pipeGrad[i + 1]; + // } + // } + // + // return length; + } + private int buildStraight(int xo, int maxLength, boolean safe) { int length = random.nextInt(10) + 2;