Marshall's new level generator.

This commit is contained in:
Woody Folsom
2012-03-13 11:40:58 -04:00
parent 903437b727
commit de5718ba36

View File

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