performance fix

This commit is contained in:
Asecave 2021-06-28 15:45:48 +02:00
parent 1555acd781
commit 23eba51e5c
4 changed files with 103 additions and 78 deletions

View File

@ -60,4 +60,12 @@ public class DoorTile extends Tile implements Ticker {
public void waitForLeave() {
waitForLeave = true;
}
@Override
public Tile clone() {
DoorTile t = new DoorTile(posX, posY, map);
t.setConnectedDoor(connected);
t.side = side;
return t;
}
}

View File

@ -14,23 +14,46 @@ import ea.Knoten;
public class Tile extends Knoten {
// IDs der Tiles
public static final int VOID = -1;
public static final int GRASS = 0;
public static final int WALL_TOP = 1;
public static final int WALL_BOTTOM = 2;
public static final int STONE_WALL = 3;
public static final int STONE_WALL_BOTTOM = 4;
public static final int STONE_FLOOR = 5;
public static final int VOID = 6;
public static final int DOOR = 7;
public static final int DOOR = 6;
private Bild img; // Bild, das gerendert wird
private int id; // Die id dises Tiles
protected float posX, posY; // Position dieses Tiles
private static Bild[] images;
/**
* @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);
}
}
static {
images = new Bild[8];
private static void loadImages() {
images = new Bild[7];
for (int i = 0; i < images.length; i++) {
try {
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.
*/
@ -97,7 +101,7 @@ public class Tile extends Knoten {
/**
* @return die Größe der Textur
*/
public static int getSize() {
private static int getSize() {
return 16;
}

View File

@ -13,7 +13,7 @@ import java.util.ArrayList;
public class World extends Knoten {
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 ArrayList<ImageMap> dungeon;
@ -30,6 +30,7 @@ public class World extends Knoten {
currentMap.start();
start.generate(dungeon, DoorTile.BOTTOM);
System.out.println("generated!");
player = new Player();

View File

@ -86,6 +86,23 @@ public class ImageMap extends Map {
for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
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) {
if (topDoor != null && fromSide != DoorTile.TOP) {
ImageMap map;
do {
map = nextMap();
} while (!connectDoors(topDoor, map));
ImageMap map = nextMap(DoorTile.BOTTOM);
connectDoors(topDoor, map.bottomDoor);
dungeon.add(map);
map.generate(dungeon, DoorTile.BOTTOM);
}
if (bottomDoor != null && fromSide != DoorTile.BOTTOM) {
ImageMap map;
do {
map = nextMap();
} while (!connectDoors(bottomDoor, map));
ImageMap map = nextMap(DoorTile.TOP);
connectDoors(bottomDoor, map.topDoor);
dungeon.add(map);
map.generate(dungeon, DoorTile.TOP);
}
if (leftDoor != null && fromSide != DoorTile.LEFT) {
ImageMap map;
do {
map = nextMap();
} while (!connectDoors(leftDoor, map));
ImageMap map = nextMap(DoorTile.RIGHT);
connectDoors(leftDoor, map.rightDoor);
dungeon.add(map);
map.generate(dungeon, DoorTile.RIGHT);
}
if (rightDoor != null && fromSide != DoorTile.RIGHT) {
ImageMap map;
do {
map = nextMap();
} while (!connectDoors(rightDoor, map));
ImageMap map = nextMap(DoorTile.LEFT);
connectDoors(rightDoor, map.leftDoor);
dungeon.add(map);
map.generate(dungeon, DoorTile.LEFT);
}
System.out.println();
}
private ImageMap nextMap() {
return maps[(int) (Math.random() * maps.length)].clone();
}
private boolean connectDoors(DoorTile door, ImageMap otherMap) {
switch (door.getSide()) {
case DoorTile.TOP:
if (otherMap.bottomDoor == null) {
return false;
}
door.setConnectedDoor(otherMap.bottomDoor);
otherMap.bottomDoor.setConnectedDoor(door);
return true;
case DoorTile.BOTTOM:
if (otherMap.topDoor == null) {
return false;
}
door.setConnectedDoor(otherMap.topDoor);
otherMap.topDoor.setConnectedDoor(door);
return true;
case DoorTile.LEFT:
if (otherMap.rightDoor == null) {
return false;
}
door.setConnectedDoor(otherMap.rightDoor);
otherMap.rightDoor.setConnectedDoor(door);
return true;
case DoorTile.RIGHT:
if (otherMap.leftDoor == null) {
return false;
}
door.setConnectedDoor(otherMap.leftDoor);
otherMap.leftDoor.setConnectedDoor(door);
return true;
private ImageMap nextMap(int entrySide) {
float r = 1f;
if (r < 0.1f) {
return maps[0].clone();
}
return true;
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 hasEntry(int entrySide) {
switch (entrySide) {
case DoorTile.TOP:
return topDoor != null;
case DoorTile.BOTTOM:
return bottomDoor != null;
case DoorTile.LEFT:
return leftDoor != null;
case DoorTile.RIGHT:
return rightDoor != null;
}
return false;
}
private void connectDoors(DoorTile door, DoorTile door2) {
door.setConnectedDoor(door2);
door2.setConnectedDoor(door);
}
public void start() {
@ -206,6 +218,6 @@ public class ImageMap extends Map {
@Override
protected ImageMap clone() {
return null;
return new ImageMap(this);
}
}