performace

This commit is contained in:
Tim 2021-06-28 09:49:21 +02:00
parent a6726b5e25
commit 4f76c1eaa8
8 changed files with 102 additions and 77 deletions

View File

@ -26,6 +26,31 @@ public class Tile extends Knoten {
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;
static {
images = new Bild[8];
for (int i = 0; i < images.length; i++) {
try {
BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(i)));
// Gras hat 8 verschiedene Texturen von denen eine zufällig ausgewählt werden
// muss.
if (i == GRASS) {
buff = buff.getSubimage(16 * (int) (Math.random() * 8), 0, 16, 16);
}
// if (id == STONE_FLOOR) {
// buff = buff.getSubimage(16 * (int) (Math.random() * 10), 0, 16, 16);
// }
// Skalieren
BufferedImage scaled = new BufferedImage(World.SCALE, World.SCALE, BufferedImage.TYPE_INT_RGB);
scaled.getGraphics().drawImage(buff, 0, 0, World.SCALE, World.SCALE, null);
images[i] = new Bild(0, 0, scaled);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param id
@ -38,25 +63,8 @@ public class Tile extends Knoten {
posY = y;
if (id != VOID) {
// Das Bild laden
try {
BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(id)));
// Gras hat 8 verschiedene Texturen von denen eine zufällig ausgewählt werden
// muss.
if (id == GRASS) {
buff = buff.getSubimage(16 * (int) (Math.random() * 8), 0, 16, 16);
}
// if (id == STONE_FLOOR) {
// buff = buff.getSubimage(16 * (int) (Math.random() * 10), 0, 16, 16);
// }
// Skalieren
BufferedImage scaled = new BufferedImage(World.SCALE, World.SCALE, BufferedImage.TYPE_INT_RGB);
scaled.getGraphics().drawImage(buff, 0, 0, World.SCALE, World.SCALE, null);
img = new Bild(x * World.SCALE, y * World.SCALE, scaled);
} catch (IOException e) {
e.printStackTrace();
}
img = images[id].clone();
img.positionSetzen(posX, posY);
// Bild zu EA hinzufügen.
add(img);
}
@ -66,7 +74,7 @@ public class Tile extends Knoten {
/**
* @return den Pfad der zu der Id gehört.
*/
private String getPathFromId(int id) {
private static String getPathFromId(int id) {
switch (id) {
case GRASS:
return "/res/images/tiles/grass.png";
@ -133,4 +141,9 @@ public class Tile extends Knoten {
public int getID() {
return id;
}
@Override
public Tile clone() {
return new Tile(id, posX, posY);
}
}

View File

@ -7,28 +7,28 @@ public class Corridor extends Map {
public Corridor() {
super(15, 11);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
// Wand
if ((y == 0 || x == 5 || y == 10 || x == 9) && x != 7 && x >= 5 && x <= 9) {
map[x][y] = new Tile(Tile.STONE_WALL, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(Tile.STONE_WALL, x, y);
add(tiles[x][y]);
}
// 3D-Wand
else if (y == 1 && (x != 0 || x != 4) && x != 7 && x >= 6 && x <= 8) {
map[x][y] = new Tile(Tile.STONE_WALL_BOTTOM, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(Tile.STONE_WALL_BOTTOM, x, y);
add(tiles[x][y]);
}
// Steinboden
else if (x >= 6 && x <= 8) {
if (x == 7 && (y == 0 || y == map[0].length - 1)) {
if (x == 7 && (y == 0 || y == tiles[0].length - 1)) {
} else {
map[x][y] = new Tile(Tile.STONE_FLOOR, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(Tile.STONE_FLOOR, x, y);
add(tiles[x][y]);
}
} else {
map[x][y] = new Tile(Tile.VOID, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(Tile.VOID, x, y);
add(tiles[x][y]);
}
}
}

View File

@ -13,19 +13,17 @@ import main.Tile;
public class ImageMap extends Map {
private static PixelTile[] pixels = {
new PixelTile(Tile.STONE_WALL, new Color(100, 100, 100)),
new PixelTile(Tile.STONE_FLOOR, new Color(127, 127, 127)),
new PixelTile(Tile.VOID, new Color(0, 0, 0)),
private static PixelTile[] pixels = { new PixelTile(Tile.STONE_WALL, new Color(100, 100, 100)),
new PixelTile(Tile.STONE_FLOOR, new Color(127, 127, 127)), new PixelTile(Tile.VOID, new Color(0, 0, 0)),
new PixelTile(Tile.DOOR, new Color(255, 0, 0)) };
public DoorTile topDoor;
public DoorTile leftDoor;
public DoorTile rightDoor;
public DoorTile bottomDoor;
private static ImageMap[] maps;
static {
maps = new ImageMap[8];
for (int i = 0; i < maps.length; i++) {
@ -53,7 +51,7 @@ public class ImageMap extends Map {
}
if (id == Tile.DOOR) {
DoorTile door = new DoorTile(x, y, this);
map[x][y] = door;
tiles[x][y] = door;
if (x == 7) {
if (y < 5) {
topDoor = door;
@ -73,9 +71,9 @@ public class ImageMap extends Map {
}
}
} else {
map[x][y] = new Tile(id, x, y);
tiles[x][y] = new Tile(id, x, y);
}
add(map[x][y]);
add(tiles[x][y]);
}
}
} catch (IOException e) {
@ -83,6 +81,15 @@ public class ImageMap extends Map {
}
}
public ImageMap(ImageMap map) {
super(map.getWidth(), map.getHeight());
for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
tiles[x][y] = map.getTile(x, y).clone();
}
}
}
private static class PixelTile {
public final int c;
public final int id;
@ -130,7 +137,7 @@ public class ImageMap extends Map {
}
private ImageMap nextMap() {
return new ImageMap("/res/images/maps/map" + ((int) (Math.random() * 6) + 3) + ".png");
return maps[(int) (Math.random() * maps.length)].clone();
}
private boolean connectDoors(DoorTile door, ImageMap otherMap) {
@ -181,7 +188,7 @@ public class ImageMap extends Map {
Main.instance.manager.anmelden(rightDoor, 50);
}
}
public void stop() {
if (topDoor != null) {
Main.instance.manager.abmelden(topDoor);
@ -196,4 +203,9 @@ public class ImageMap extends Map {
Main.instance.manager.abmelden(rightDoor);
}
}
@Override
protected ImageMap clone() {
return null;
}
}

View File

@ -14,12 +14,12 @@ import main.entities.LivingEntity;
*/
public abstract class Map extends Knoten implements Ticker {
protected Tile[][] map; // Die Tiles der map in einem 2D Array.
protected Tile[][] tiles; // Die Tiles der map in einem 2D Array.
private ArrayList<Entity> entities;
public Map(int width, int height) {
map = new Tile[width][height];
tiles = new Tile[width][height];
entities = new ArrayList<>(100);
@ -31,8 +31,8 @@ public abstract class Map extends Knoten implements Ticker {
* existiert)
*/
public Tile getTile(int x, int y) {
if (x >= 0 && x < map.length && y >= 0 && y < map[0].length) {
return map[x][y];
if (x >= 0 && x < tiles.length && y >= 0 && y < tiles[0].length) {
return tiles[x][y];
}
return null;
}
@ -52,11 +52,11 @@ public abstract class Map extends Knoten implements Ticker {
}
public int getWidth() {
return map.length;
return tiles.length;
}
public int getHeight() {
return map[0].length;
return tiles[0].length;
}
public ArrayList<Entity> getEntities() {

View File

@ -6,17 +6,17 @@ public class Map01 extends Map {
public Map01 () {
super (15,11);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
//Wand
if((x == 4 || y == 9 || x == 10) && x > 3 && y > 0 && x < 11 && y < 10 && x != 7 ) {
map[x][y] = new Tile(3, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(3, x, y);
add(tiles[x][y]);
}
//Boden
else if(x > 3 && y > 0 && x < 11 && y < 10) {
map[x][y] = new Tile(5, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(5, x, y);
add(tiles[x][y]);
}
}
}

View File

@ -7,30 +7,30 @@ public class Map02 extends Map {
public Map02() {
super(15, 11);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
// Wand
if (y == 0 || x == 0 || y == 10 || x == 14) {
// Übergang, Tür
if (y == 10) {
if (x > 4 && x < 10) {
map[x][y] = new Tile(5, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(5, x, y);
add(tiles[x][y]);
continue;
}
}
map[x][y] = new Tile(3, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(3, x, y);
add(tiles[x][y]);
}
// 3D-Wand
else if(y == 1 ) {
map[x][y] = new Tile(4, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(4, x, y);
add(tiles[x][y]);
}
// Steinboden
else {
map[x][y] = new Tile(5, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(5, x, y);
add(tiles[x][y]);
}
}
}

View File

@ -7,10 +7,10 @@ public class TestMap extends Map {
public TestMap() {
super(15, 11);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
map[x][y] = new Tile(Math.random() * 5 < 1 ? 1 : 0, x, y);
add(map[x][y]);
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
tiles[x][y] = new Tile(Math.random() * 5 < 1 ? 1 : 0, x, y);
add(tiles[x][y]);
}
}
}

View File

@ -8,22 +8,22 @@ public class TutorialMap extends Map {
public TutorialMap() {
super(15, 11);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
//Wand, Sichtschutz
if (y == 0 ||y == map[0].length - 1 || x == 0 || x == map.length - 1 || ((y == 4|| y == 5) && x == 10 )) {
map[x][y] = new Tile (1, x, y);
add(map [x][y]);
if (y == 0 ||y == tiles[0].length - 1 || x == 0 || x == tiles.length - 1 || ((y == 4|| y == 5) && x == 10 )) {
tiles[x][y] = new Tile (1, x, y);
add(tiles [x][y]);
}
//3D-Wand, 3F-Sichtschutz
else if(y == 1 || ( y == 6 && x == 10)) {
map[x][y] = new Tile (2, x, y);
add(map [x][y]);
tiles[x][y] = new Tile (2, x, y);
add(tiles [x][y]);
}
//Grasboden
else{
map[x][y] = new Tile(0, x, y);
add(map[x][y]);
tiles[x][y] = new Tile(0, x, y);
add(tiles[x][y]);
}
}
}