Compare commits

..

2 Commits

Author SHA1 Message Date
Tim
1555acd781 Merge branch 'master' of https://git.ngb.schule/Tim/Zoelda 2021-06-28 09:49:41 +02:00
Tim
4f76c1eaa8 performace 2021-06-28 09:49:21 +02:00
8 changed files with 102 additions and 77 deletions

View File

@@ -27,6 +27,31 @@ public class Tile extends Knoten {
private int id; // Die id dises Tiles private int id; // Die id dises Tiles
protected float posX, posY; // Position dieses 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 * @param id
* @param x - X-Koordinate in units * @param x - X-Koordinate in units
@@ -38,25 +63,8 @@ public class Tile extends Knoten {
posY = y; posY = y;
if (id != VOID) { if (id != VOID) {
// Das Bild laden img = images[id].clone();
try { img.positionSetzen(posX, posY);
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();
}
// Bild zu EA hinzufügen. // Bild zu EA hinzufügen.
add(img); add(img);
} }
@@ -66,7 +74,7 @@ public class Tile extends Knoten {
/** /**
* @return den Pfad der zu der Id gehört. * @return den Pfad der zu der Id gehört.
*/ */
private String getPathFromId(int id) { private static String getPathFromId(int id) {
switch (id) { switch (id) {
case GRASS: case GRASS:
return "/res/images/tiles/grass.png"; return "/res/images/tiles/grass.png";
@@ -133,4 +141,9 @@ public class Tile extends Knoten {
public int getID() { public int getID() {
return id; 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() { public Corridor() {
super(15, 11); super(15, 11);
for (int x = 0; x < map.length; x++) { for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < map[0].length; y++) { for (int y = 0; y < tiles[0].length; y++) {
// Wand // Wand
if ((y == 0 || x == 5 || y == 10 || x == 9) && x != 7 && x >= 5 && x <= 9) { 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); tiles[x][y] = new Tile(Tile.STONE_WALL, x, y);
add(map[x][y]); add(tiles[x][y]);
} }
// 3D-Wand // 3D-Wand
else if (y == 1 && (x != 0 || x != 4) && x != 7 && x >= 6 && x <= 8) { 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); tiles[x][y] = new Tile(Tile.STONE_WALL_BOTTOM, x, y);
add(map[x][y]); add(tiles[x][y]);
} }
// Steinboden // Steinboden
else if (x >= 6 && x <= 8) { 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 { } else {
map[x][y] = new Tile(Tile.STONE_FLOOR, x, y); tiles[x][y] = new Tile(Tile.STONE_FLOOR, x, y);
add(map[x][y]); add(tiles[x][y]);
} }
} else { } else {
map[x][y] = new Tile(Tile.VOID, x, y); tiles[x][y] = new Tile(Tile.VOID, x, y);
add(map[x][y]); add(tiles[x][y]);
} }
} }
} }

View File

@@ -13,10 +13,8 @@ import main.Tile;
public class ImageMap extends Map { public class ImageMap extends Map {
private static PixelTile[] pixels = { private static PixelTile[] pixels = { new PixelTile(Tile.STONE_WALL, new Color(100, 100, 100)),
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.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)) }; new PixelTile(Tile.DOOR, new Color(255, 0, 0)) };
public DoorTile topDoor; public DoorTile topDoor;
@@ -53,7 +51,7 @@ public class ImageMap extends Map {
} }
if (id == Tile.DOOR) { if (id == Tile.DOOR) {
DoorTile door = new DoorTile(x, y, this); DoorTile door = new DoorTile(x, y, this);
map[x][y] = door; tiles[x][y] = door;
if (x == 7) { if (x == 7) {
if (y < 5) { if (y < 5) {
topDoor = door; topDoor = door;
@@ -73,9 +71,9 @@ public class ImageMap extends Map {
} }
} }
} else { } 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) { } 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 { private static class PixelTile {
public final int c; public final int c;
public final int id; public final int id;
@@ -130,7 +137,7 @@ public class ImageMap extends Map {
} }
private ImageMap nextMap() { 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) { private boolean connectDoors(DoorTile door, ImageMap otherMap) {
@@ -196,4 +203,9 @@ public class ImageMap extends Map {
Main.instance.manager.abmelden(rightDoor); 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 { 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; private ArrayList<Entity> entities;
public Map(int width, int height) { public Map(int width, int height) {
map = new Tile[width][height]; tiles = new Tile[width][height];
entities = new ArrayList<>(100); entities = new ArrayList<>(100);
@@ -31,8 +31,8 @@ public abstract class Map extends Knoten implements Ticker {
* existiert) * existiert)
*/ */
public Tile getTile(int x, int y) { public Tile getTile(int x, int y) {
if (x >= 0 && x < map.length && y >= 0 && y < map[0].length) { if (x >= 0 && x < tiles.length && y >= 0 && y < tiles[0].length) {
return map[x][y]; return tiles[x][y];
} }
return null; return null;
} }
@@ -52,11 +52,11 @@ public abstract class Map extends Knoten implements Ticker {
} }
public int getWidth() { public int getWidth() {
return map.length; return tiles.length;
} }
public int getHeight() { public int getHeight() {
return map[0].length; return tiles[0].length;
} }
public ArrayList<Entity> getEntities() { public ArrayList<Entity> getEntities() {

View File

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

View File

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

View File

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

View File

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