169 lines
3.9 KiB
Java
169 lines
3.9 KiB
Java
package main;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.IOException;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import ea.Bild;
|
|
import ea.Knoten;
|
|
import main.worlds.World;
|
|
|
|
/**
|
|
* Ein feld auf der Map
|
|
*/
|
|
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 STONE_FLOOR_SHADOW_LEFT = 6;
|
|
public static final int DOOR_LEFT_BOTTOM = 7;
|
|
public static final int DOOR_LEFT_TOP = 8;
|
|
public static final int DISCO = 9;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
private static void loadImages() {
|
|
images = new Bild[9];
|
|
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);
|
|
}
|
|
// 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return den Pfad der zu der Id gehört.
|
|
*/
|
|
private static String getPathFromId(int id) {
|
|
switch (id) {
|
|
case GRASS:
|
|
return "/res/images/tiles/grass.png";
|
|
case WALL_TOP:
|
|
return "/res/images/tiles/wall_top.png";
|
|
case WALL_BOTTOM:
|
|
return "/res/images/tiles/wall_bottom.png";
|
|
case STONE_WALL:
|
|
return "/res/images/tiles/test.png";
|
|
case STONE_WALL_BOTTOM:
|
|
return "/res/images/tiles/stone_wall_bottom.png";
|
|
case STONE_FLOOR:
|
|
return "/res/images/tiles/TestTest.png";
|
|
case STONE_FLOOR_SHADOW_LEFT:
|
|
return "/res/images/tiles/stone_floor_shadow_left.png";
|
|
case DOOR_LEFT_BOTTOM:
|
|
return "/res/images/tiles/door_left_bottom.png";
|
|
case DOOR_LEFT_TOP:
|
|
return "/res/images/tiles/door_left_top.png";
|
|
case DISCO:
|
|
return "/res/images/tiles/disco_sprite_sheet.png";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return die Größe der Textur
|
|
*/
|
|
private static int getSize() {
|
|
return 16;
|
|
}
|
|
|
|
/**
|
|
* @return ob man durch das Tile durchgehen kann
|
|
*/
|
|
public boolean isCollidable() {
|
|
// Alle Tiles durch die man nicht laufen soll müssen hier true zurückgeben,
|
|
// sonst werden sie bei der Collisiondetection nicht berücksichtigt.
|
|
return id == WALL_TOP || id == STONE_WALL;
|
|
}
|
|
|
|
/**
|
|
* Kleine Hifsmethode un die koordinate der Top-Kante rurückzugenben.
|
|
*/
|
|
public int getTop() {
|
|
return positionY();
|
|
}
|
|
|
|
/**
|
|
* Kleine Hifsmethode un die koordinate der Bottom-Kante rurückzugenben.
|
|
*/
|
|
public int getBottom() {
|
|
return positionY() + getSize();
|
|
}
|
|
|
|
/**
|
|
* Kleine Hifsmethode un die koordinate der Left-Kante rurückzugenben.
|
|
*/
|
|
public int getLeft() {
|
|
return positionX();
|
|
}
|
|
|
|
/**
|
|
* Kleine Hifsmethode un die koordinate der Right-Kante rurückzugenben.
|
|
*/
|
|
public int getRight() {
|
|
return positionX() + getSize();
|
|
}
|
|
|
|
public int getID() {
|
|
return id;
|
|
}
|
|
|
|
public float getPosX() {
|
|
return posX;
|
|
}
|
|
|
|
public float getPosY() {
|
|
return posY;
|
|
}
|
|
|
|
@Override
|
|
public Tile clone() {
|
|
return new Tile(id, posX, posY);
|
|
}
|
|
}
|