performance fix
This commit is contained in:
@@ -60,4 +60,12 @@ public class DoorTile extends Tile implements Ticker {
|
|||||||
public void waitForLeave() {
|
public void waitForLeave() {
|
||||||
waitForLeave = true;
|
waitForLeave = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Tile clone() {
|
||||||
|
DoorTile t = new DoorTile(posX, posY, map);
|
||||||
|
t.setConnectedDoor(connected);
|
||||||
|
t.side = side;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ import ea.Knoten;
|
|||||||
public class Tile extends Knoten {
|
public class Tile extends Knoten {
|
||||||
|
|
||||||
// IDs der Tiles
|
// IDs der Tiles
|
||||||
|
public static final int VOID = -1;
|
||||||
public static final int GRASS = 0;
|
public static final int GRASS = 0;
|
||||||
public static final int WALL_TOP = 1;
|
public static final int WALL_TOP = 1;
|
||||||
public static final int WALL_BOTTOM = 2;
|
public static final int WALL_BOTTOM = 2;
|
||||||
public static final int STONE_WALL = 3;
|
public static final int STONE_WALL = 3;
|
||||||
public static final int STONE_WALL_BOTTOM = 4;
|
public static final int STONE_WALL_BOTTOM = 4;
|
||||||
public static final int STONE_FLOOR = 5;
|
public static final int STONE_FLOOR = 5;
|
||||||
public static final int VOID = 6;
|
public static final int DOOR = 6;
|
||||||
public static final int DOOR = 7;
|
|
||||||
|
|
||||||
private Bild img; // Bild, das gerendert wird
|
private Bild img; // Bild, das gerendert wird
|
||||||
private int id; // Die id dises Tiles
|
private int id; // Die id dises Tiles
|
||||||
@@ -29,8 +29,31 @@ public class Tile extends Knoten {
|
|||||||
|
|
||||||
private static Bild[] images;
|
private static Bild[] images;
|
||||||
|
|
||||||
static {
|
/**
|
||||||
images = new Bild[8];
|
* @param id
|
||||||
|
* @param x - X-Koordinate in units
|
||||||
|
* @param y - Y-Koordinate in units
|
||||||
|
*/
|
||||||
|
public Tile(int id, float x, float y) {
|
||||||
|
|
||||||
|
if (images == null) {
|
||||||
|
loadImages();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = id;
|
||||||
|
posX = x;
|
||||||
|
posY = y;
|
||||||
|
|
||||||
|
if (id != VOID) {
|
||||||
|
img = images[id].clone();
|
||||||
|
img.positionSetzen(posX * World.SCALE, posY * World.SCALE);
|
||||||
|
// Bild zu EA hinzufügen.
|
||||||
|
add(img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void loadImages() {
|
||||||
|
images = new Bild[7];
|
||||||
for (int i = 0; i < images.length; i++) {
|
for (int i = 0; i < images.length; i++) {
|
||||||
try {
|
try {
|
||||||
BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(i)));
|
BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(i)));
|
||||||
@@ -52,25 +75,6 @@ public class Tile extends Knoten {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param id
|
|
||||||
* @param x - X-Koordinate in units
|
|
||||||
* @param y - Y-Koordinate in units
|
|
||||||
*/
|
|
||||||
public Tile(int id, float x, float y) {
|
|
||||||
this.id = id;
|
|
||||||
posX = x;
|
|
||||||
posY = y;
|
|
||||||
|
|
||||||
if (id != VOID) {
|
|
||||||
img = images[id].clone();
|
|
||||||
img.positionSetzen(posX, posY);
|
|
||||||
// Bild zu EA hinzufügen.
|
|
||||||
add(img);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return den Pfad der zu der Id gehört.
|
* @return den Pfad der zu der Id gehört.
|
||||||
*/
|
*/
|
||||||
@@ -97,7 +101,7 @@ public class Tile extends Knoten {
|
|||||||
/**
|
/**
|
||||||
* @return die Größe der Textur
|
* @return die Größe der Textur
|
||||||
*/
|
*/
|
||||||
public static int getSize() {
|
private static int getSize() {
|
||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import java.util.ArrayList;
|
|||||||
public class World extends Knoten {
|
public class World extends Knoten {
|
||||||
|
|
||||||
public static final int SCALE_FACTOR = 4; // Der Basis Zoomfaktor
|
public static final int SCALE_FACTOR = 4; // Der Basis Zoomfaktor
|
||||||
public static final int SCALE = SCALE_FACTOR * Tile.getSize(); // Eine Gameunit ist so viele pixel lang
|
public static final int SCALE = SCALE_FACTOR * 16; // Eine Gameunit ist so viele pixel lang
|
||||||
|
|
||||||
private ImageMap currentMap; // Die Map die aktuell angezeigt werden soll.
|
private ImageMap currentMap; // Die Map die aktuell angezeigt werden soll.
|
||||||
private ArrayList<ImageMap> dungeon;
|
private ArrayList<ImageMap> dungeon;
|
||||||
@@ -30,6 +30,7 @@ public class World extends Knoten {
|
|||||||
currentMap.start();
|
currentMap.start();
|
||||||
|
|
||||||
start.generate(dungeon, DoorTile.BOTTOM);
|
start.generate(dungeon, DoorTile.BOTTOM);
|
||||||
|
System.out.println("generated!");
|
||||||
|
|
||||||
|
|
||||||
player = new Player();
|
player = new Player();
|
||||||
|
|||||||
@@ -86,6 +86,23 @@ public class ImageMap extends Map {
|
|||||||
for (int x = 0; x < map.getWidth(); x++) {
|
for (int x = 0; x < map.getWidth(); x++) {
|
||||||
for (int y = 0; y < map.getHeight(); y++) {
|
for (int y = 0; y < map.getHeight(); y++) {
|
||||||
tiles[x][y] = map.getTile(x, y).clone();
|
tiles[x][y] = map.getTile(x, y).clone();
|
||||||
|
if (tiles[x][y] instanceof DoorTile) {
|
||||||
|
DoorTile dt = (DoorTile) tiles[x][y];
|
||||||
|
switch (dt.getSide()) {
|
||||||
|
case DoorTile.TOP:
|
||||||
|
topDoor = dt;
|
||||||
|
break;
|
||||||
|
case DoorTile.BOTTOM:
|
||||||
|
bottomDoor = dt;
|
||||||
|
break;
|
||||||
|
case DoorTile.LEFT:
|
||||||
|
leftDoor = dt;
|
||||||
|
break;
|
||||||
|
case DoorTile.RIGHT:
|
||||||
|
rightDoor = dt;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,76 +119,71 @@ public class ImageMap extends Map {
|
|||||||
|
|
||||||
public void generate(ArrayList<ImageMap> dungeon, int fromSide) {
|
public void generate(ArrayList<ImageMap> dungeon, int fromSide) {
|
||||||
if (topDoor != null && fromSide != DoorTile.TOP) {
|
if (topDoor != null && fromSide != DoorTile.TOP) {
|
||||||
ImageMap map;
|
ImageMap map = nextMap(DoorTile.BOTTOM);
|
||||||
do {
|
connectDoors(topDoor, map.bottomDoor);
|
||||||
map = nextMap();
|
|
||||||
} while (!connectDoors(topDoor, map));
|
|
||||||
dungeon.add(map);
|
dungeon.add(map);
|
||||||
map.generate(dungeon, DoorTile.BOTTOM);
|
map.generate(dungeon, DoorTile.BOTTOM);
|
||||||
}
|
}
|
||||||
if (bottomDoor != null && fromSide != DoorTile.BOTTOM) {
|
if (bottomDoor != null && fromSide != DoorTile.BOTTOM) {
|
||||||
ImageMap map;
|
ImageMap map = nextMap(DoorTile.TOP);
|
||||||
do {
|
connectDoors(bottomDoor, map.topDoor);
|
||||||
map = nextMap();
|
|
||||||
} while (!connectDoors(bottomDoor, map));
|
|
||||||
dungeon.add(map);
|
dungeon.add(map);
|
||||||
map.generate(dungeon, DoorTile.TOP);
|
map.generate(dungeon, DoorTile.TOP);
|
||||||
}
|
}
|
||||||
if (leftDoor != null && fromSide != DoorTile.LEFT) {
|
if (leftDoor != null && fromSide != DoorTile.LEFT) {
|
||||||
ImageMap map;
|
ImageMap map = nextMap(DoorTile.RIGHT);
|
||||||
do {
|
connectDoors(leftDoor, map.rightDoor);
|
||||||
map = nextMap();
|
|
||||||
} while (!connectDoors(leftDoor, map));
|
|
||||||
dungeon.add(map);
|
dungeon.add(map);
|
||||||
map.generate(dungeon, DoorTile.RIGHT);
|
map.generate(dungeon, DoorTile.RIGHT);
|
||||||
}
|
}
|
||||||
if (rightDoor != null && fromSide != DoorTile.RIGHT) {
|
if (rightDoor != null && fromSide != DoorTile.RIGHT) {
|
||||||
ImageMap map;
|
ImageMap map = nextMap(DoorTile.LEFT);
|
||||||
do {
|
connectDoors(rightDoor, map.leftDoor);
|
||||||
map = nextMap();
|
|
||||||
} while (!connectDoors(rightDoor, map));
|
|
||||||
dungeon.add(map);
|
dungeon.add(map);
|
||||||
map.generate(dungeon, DoorTile.LEFT);
|
map.generate(dungeon, DoorTile.LEFT);
|
||||||
}
|
}
|
||||||
System.out.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ImageMap nextMap() {
|
private ImageMap nextMap(int entrySide) {
|
||||||
return maps[(int) (Math.random() * maps.length)].clone();
|
float r = 1f;
|
||||||
|
if (r < 0.1f) {
|
||||||
|
return maps[0].clone();
|
||||||
|
}
|
||||||
|
if (r < 0.2f) {
|
||||||
|
return maps[1].clone();
|
||||||
|
}
|
||||||
|
if (r < 0.6f) {
|
||||||
|
ImageMap map;
|
||||||
|
do {
|
||||||
|
map = maps[(int) (Math.random() * 2) + 2];
|
||||||
|
} while (!map.hasEntry(entrySide));
|
||||||
|
return map.clone();
|
||||||
|
} else {
|
||||||
|
ImageMap map;
|
||||||
|
do {
|
||||||
|
map = maps[(int) (Math.random() * 4) + 4];
|
||||||
|
} while (!map.hasEntry(entrySide));
|
||||||
|
return map.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean connectDoors(DoorTile door, ImageMap otherMap) {
|
private boolean hasEntry(int entrySide) {
|
||||||
switch (door.getSide()) {
|
switch (entrySide) {
|
||||||
case DoorTile.TOP:
|
case DoorTile.TOP:
|
||||||
if (otherMap.bottomDoor == null) {
|
return topDoor != null;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
door.setConnectedDoor(otherMap.bottomDoor);
|
|
||||||
otherMap.bottomDoor.setConnectedDoor(door);
|
|
||||||
return true;
|
|
||||||
case DoorTile.BOTTOM:
|
case DoorTile.BOTTOM:
|
||||||
if (otherMap.topDoor == null) {
|
return bottomDoor != null;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
door.setConnectedDoor(otherMap.topDoor);
|
|
||||||
otherMap.topDoor.setConnectedDoor(door);
|
|
||||||
return true;
|
|
||||||
case DoorTile.LEFT:
|
case DoorTile.LEFT:
|
||||||
if (otherMap.rightDoor == null) {
|
return leftDoor != null;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
door.setConnectedDoor(otherMap.rightDoor);
|
|
||||||
otherMap.rightDoor.setConnectedDoor(door);
|
|
||||||
return true;
|
|
||||||
case DoorTile.RIGHT:
|
case DoorTile.RIGHT:
|
||||||
if (otherMap.leftDoor == null) {
|
return rightDoor != null;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
door.setConnectedDoor(otherMap.leftDoor);
|
|
||||||
otherMap.leftDoor.setConnectedDoor(door);
|
private void connectDoors(DoorTile door, DoorTile door2) {
|
||||||
return true;
|
door.setConnectedDoor(door2);
|
||||||
}
|
door2.setConnectedDoor(door);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -206,6 +218,6 @@ public class ImageMap extends Map {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ImageMap clone() {
|
protected ImageMap clone() {
|
||||||
return null;
|
return new ImageMap(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user