From 4f76c1eaa88511e5042e077cf703f3fb7dd612ed Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 28 Jun 2021 09:49:21 +0200 Subject: [PATCH] performace --- Zoelda/src/main/Tile.java | 53 +++++++++++++++++---------- Zoelda/src/main/maps/Corridor.java | 22 +++++------ Zoelda/src/main/maps/ImageMap.java | 34 +++++++++++------ Zoelda/src/main/maps/Map.java | 12 +++--- Zoelda/src/main/maps/Map01.java | 12 +++--- Zoelda/src/main/maps/Map02.java | 20 +++++----- Zoelda/src/main/maps/TestMap.java | 8 ++-- Zoelda/src/main/maps/TutorialMap.java | 18 ++++----- 8 files changed, 102 insertions(+), 77 deletions(-) diff --git a/Zoelda/src/main/Tile.java b/Zoelda/src/main/Tile.java index 036763c..bf3ba7a 100644 --- a/Zoelda/src/main/Tile.java +++ b/Zoelda/src/main/Tile.java @@ -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); + } } diff --git a/Zoelda/src/main/maps/Corridor.java b/Zoelda/src/main/maps/Corridor.java index 957d88b..ff4c306 100644 --- a/Zoelda/src/main/maps/Corridor.java +++ b/Zoelda/src/main/maps/Corridor.java @@ -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]); } } } diff --git a/Zoelda/src/main/maps/ImageMap.java b/Zoelda/src/main/maps/ImageMap.java index 18beec1..f070a75 100644 --- a/Zoelda/src/main/maps/ImageMap.java +++ b/Zoelda/src/main/maps/ImageMap.java @@ -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; + } } diff --git a/Zoelda/src/main/maps/Map.java b/Zoelda/src/main/maps/Map.java index cd6ed88..57cb1a3 100644 --- a/Zoelda/src/main/maps/Map.java +++ b/Zoelda/src/main/maps/Map.java @@ -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 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 getEntities() { diff --git a/Zoelda/src/main/maps/Map01.java b/Zoelda/src/main/maps/Map01.java index 811e5f8..2688320 100644 --- a/Zoelda/src/main/maps/Map01.java +++ b/Zoelda/src/main/maps/Map01.java @@ -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]); } } } diff --git a/Zoelda/src/main/maps/Map02.java b/Zoelda/src/main/maps/Map02.java index 54bfe25..69d660f 100644 --- a/Zoelda/src/main/maps/Map02.java +++ b/Zoelda/src/main/maps/Map02.java @@ -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]); } } } diff --git a/Zoelda/src/main/maps/TestMap.java b/Zoelda/src/main/maps/TestMap.java index f9465d3..2451d4e 100644 --- a/Zoelda/src/main/maps/TestMap.java +++ b/Zoelda/src/main/maps/TestMap.java @@ -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]); } } } diff --git a/Zoelda/src/main/maps/TutorialMap.java b/Zoelda/src/main/maps/TutorialMap.java index 1e0e716..a6cc7e0 100644 --- a/Zoelda/src/main/maps/TutorialMap.java +++ b/Zoelda/src/main/maps/TutorialMap.java @@ -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]); } } }