diff --git a/Zoelda/lib/Engine.Alpha.jar b/Zoelda/lib/Engine.Alpha.jar index a3d7fc2..efbc5d0 100644 Binary files a/Zoelda/lib/Engine.Alpha.jar and b/Zoelda/lib/Engine.Alpha.jar differ diff --git a/Zoelda/src/main/DoorTile.java b/Zoelda/src/main/DoorTile.java new file mode 100644 index 0000000..4df59f1 --- /dev/null +++ b/Zoelda/src/main/DoorTile.java @@ -0,0 +1,65 @@ +package main; + +import ea.Ticker; +import main.entities.player.Player; +import main.maps.ImageMap; + +public class DoorTile extends Tile implements Ticker { + + public static final int TOP = 0; + public static final int BOTTOM = 1; + public static final int LEFT = 2; + public static final int RIGHT = 3; + + private int side; + private DoorTile connected; + private ImageMap map; + private boolean waitForLeave; + + public DoorTile(float x, float y, ImageMap map) { + super(Tile.DOOR, x, y); + this.map = map; + + Main.instance.tickerAnmelden(this, 50); + } + + @Override + public void tick() { + if (Main.instance.getWorld() != null) { + Player player = Main.instance.getWorld().getPlayer(); + float dist = player.dist(posX + 0.5f, posY + 0.5f); + if (dist < 0.5f) { + if (!waitForLeave) { + Main.instance.getWorld().changeMap(this); + waitForLeave = true; + } + } else { + waitForLeave = false; + } + } + } + + public int getSide() { + return side; + } + + public void setSide(int side) { + this.side = side; + } + + public void setConnectedDoor(DoorTile door) { + connected = door; + } + + public DoorTile getConnectedDoor() { + return connected; + } + + public ImageMap getMap() { + return map; + } + + public void waitForLeave() { + waitForLeave = true; + } +} diff --git a/Zoelda/src/main/HUD.java b/Zoelda/src/main/HUD.java index 0f51778..c8e5db6 100644 --- a/Zoelda/src/main/HUD.java +++ b/Zoelda/src/main/HUD.java @@ -1,89 +1,72 @@ package main; -import java.awt.geom.AffineTransform; +import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; - import javax.imageio.ImageIO; - -import ea.Bild; +import ea.BoundingRechteck; import ea.Knoten; import ea.Ticker; public class HUD extends Knoten implements Ticker { - private Bild[] volles_Herz; - private Bild halbes_Herz; - private Bild[] leeres_Herz; + private BufferedImage volles_Herz; + private BufferedImage halbes_Herz; + private BufferedImage leeres_Herz; private BufferedImage hauptBild; - private Bild[] HPLeiste; + private BufferedImage[] HPLeiste; private final static int SCALE = 50; public HUD() { - // inizialisieren der Arrays, laden der Bilder und für den ticker anmelden - volles_Herz = new Bild[3]; - leeres_Herz = new Bild[3]; - HPLeiste = new Bild[3]; - Main.instance.manager.anmelden(this, 20); + // inizialisieren der Variablen, laden der Bilder und für den ticker anmelden + HPLeiste = new BufferedImage[3]; + Main.instance.manager.anmelden(this, 50); try { hauptBild = ImageIO.read(HUD.class.getResourceAsStream("/res/images/icons.png")); } catch (IOException e) { e.printStackTrace(); } + BufferedImage buff; // volles_Herz: - BufferedImage buff = skalieren(hauptBild.getSubimage(16, 0, 16, 16)); - volles_Herz[0] = new Bild(0, 0, buff); - volles_Herz[1] = new Bild(48, 0, buff); - volles_Herz[2] = new Bild(96, 0, buff); + buff = hauptBild.getSubimage(16, 0, 16, 16); + volles_Herz = buff; // halbes_Herz: - buff = skalieren(hauptBild.getSubimage(32, 0, 16, 16)); - halbes_Herz = new Bild(48, 0, buff); + buff = hauptBild.getSubimage(32, 0, 16, 16); + halbes_Herz = buff; // leeres_Herz - buff = skalieren(hauptBild.getSubimage(48, 0, 16, 16)); - leeres_Herz[0] = new Bild(0, 0, buff); - leeres_Herz[1] = new Bild(48, 0, buff); - leeres_Herz[2] = new Bild(96, 0, buff); - } - - private BufferedImage skalieren(BufferedImage buff) { - BufferedImage scaled = new BufferedImage(SCALE, SCALE, BufferedImage.TYPE_INT_RGB); - scaled.getGraphics().drawImage(buff, 0, 0, SCALE, SCALE, null); - return scaled; + buff = hauptBild.getSubimage(48, 0, 16, 16); + leeres_Herz = buff; + HPLeiste[0] = volles_Herz; + HPLeiste[1] = volles_Herz; + HPLeiste[2] = volles_Herz; } + //wird alle 50ms ausgeführt: @Override public void tick() { - //Prototyp, deshalb erstmal nur lokale Hp und außerdem hardgecoded (bleibt wahrscheinlich nicht so) - float localeHP = 1f; - //erstes Herz: - if (localeHP < 0.165f) { - HPLeiste[0] = leeres_Herz[0]; - } else if (localeHP < 0.33f) { - halbes_Herz.setX(0); - HPLeiste[0] = halbes_Herz; - } else { - HPLeiste[0] = volles_Herz[0]; + // teilt den Wert der HP in Teile auf, welche der Anzahl der Herzen entspricht + // und prüft dann für den eigenen Teil des Herzens, ob das jeweilige Herz voll, + // halb oder leer ist (BIG BRAIN): + float localeHP; + for (int i = 0; i < HPLeiste.length; i++) { + localeHP = Main.instance.getWorld().getPlayer().getHP(); + ; + localeHP -= ((float) i) * (1f / HPLeiste.length); + if (localeHP <= 0f) { + HPLeiste[i] = leeres_Herz; + } else if (localeHP < (1 / (float) (2 * HPLeiste.length))) { + HPLeiste[i] = halbes_Herz; + } else if (localeHP > 1 / (float) (2 * HPLeiste.length)) { + HPLeiste[i] = volles_Herz; + } } - //zweites Herz: - if (localeHP < 0.495f) { - HPLeiste[1] = leeres_Herz[1]; - } else if (localeHP < 0.66f) { - halbes_Herz.setX(48); - HPLeiste[1] = halbes_Herz; - } else { - HPLeiste[1] = volles_Herz[1]; + } + + //Zeichnet alle Herzen im Array HPLeiste[] auf den Bildschirm + @Override + public void zeichnen(Graphics2D g, BoundingRechteck r) { + for (int i = 0; i < HPLeiste.length; i++) { + g.drawImage(HPLeiste[i], i * SCALE, 0, SCALE, SCALE, null); } - //drittes Herz: - if (localeHP < 0.825f) { - HPLeiste[2] = leeres_Herz[2]; - } else if (localeHP < 0.99f) { - halbes_Herz.setX(96); - HPLeiste[2] = halbes_Herz; - } else { - HPLeiste[2] = volles_Herz[2]; - } - add(HPLeiste[0]); - add(HPLeiste[1]); - add(HPLeiste[2]); } } diff --git a/Zoelda/src/main/Main.java b/Zoelda/src/main/Main.java index 0a70962..ce4b004 100644 --- a/Zoelda/src/main/Main.java +++ b/Zoelda/src/main/Main.java @@ -13,7 +13,7 @@ public class Main extends Game { private HUD hud; public Main() { - super(World.SCALE * 15, World.SCALE * 11); + super(World.SCALE * 15, World.SCALE * 11, 16); instance = this; // Welt initialisieren und Spieler hinzufügen diff --git a/Zoelda/src/main/SheetLoader.java b/Zoelda/src/main/SheetLoader.java index 26feaca..17acd13 100644 --- a/Zoelda/src/main/SheetLoader.java +++ b/Zoelda/src/main/SheetLoader.java @@ -11,7 +11,7 @@ import ea.internal.gra.PixelFeld; public class SheetLoader { - private static Figur[] figures; + private Figur[] figures; /** * Macht aus einem Spritesheet Figuren. diff --git a/Zoelda/src/main/Tile.java b/Zoelda/src/main/Tile.java index 4717e5c..036763c 100644 --- a/Zoelda/src/main/Tile.java +++ b/Zoelda/src/main/Tile.java @@ -20,40 +20,47 @@ public class Tile extends Knoten { 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; private Bild img; // Bild, das gerendert wird private int id; // Die id dises Tiles + protected float posX, posY; // Position dieses Tiles /** * @param id - * @param x - X-Koordinate in units - * @param y - Y-Koordinate in units + * @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; - // 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(); + 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(); + } + // Bild zu EA hinzufügen. + add(img); } - // Bild zu EA hinzufügen. - add(img); + } /** @@ -72,7 +79,9 @@ public class Tile extends Knoten { case STONE_WALL_BOTTOM: return "/res/images/tiles/stone_wall_bottom.png"; case STONE_FLOOR: - return "/res/images/tiles/Stone_floor.png"; + return "/res/images/tiles/TestTest.png"; + case DOOR: + return "/res/images/tiles/door.png"; } return null; } @@ -90,7 +99,7 @@ public class Tile extends Knoten { 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 ; + return id == WALL_TOP || id == STONE_WALL; } /** @@ -120,4 +129,8 @@ public class Tile extends Knoten { public int getRight() { return positionX() + getSize(); } + + public int getID() { + return id; + } } diff --git a/Zoelda/src/main/World.java b/Zoelda/src/main/World.java index 19b2e71..bbf2589 100644 --- a/Zoelda/src/main/World.java +++ b/Zoelda/src/main/World.java @@ -1,14 +1,11 @@ package main; +import java.util.ArrayList; + import ea.Knoten; -import main.entities.Entity; -import main.entities.Snake; -import main.entities.Spider; -import main.maps.Corridor; import main.entities.player.Player; +import main.maps.ImageMap; import main.maps.Map; -import main.maps.TestMap; -import main.maps.TutorialMap; /** * Hier werden alle Maps gespeichert. @@ -19,39 +16,46 @@ public class World extends Knoten { public static final int SCALE = SCALE_FACTOR * Tile.getSize(); // Eine Gameunit ist so viele pixel lang private Map currentMap; // Die Map die aktuell angezeigt werden soll. + private ArrayList dungeon; + + private Player player; public World() { - // Map initialisieren - currentMap = new TutorialMap(); - // Map zu EA hinzuf�gen - add(currentMap); - - // und Entities auch - Player player = new Player(); - add(player.actionFigur); + dungeon = new ArrayList<>(50); + + ImageMap start = new ImageMap("/res/images/maps/map2.png"); + dungeon.add(start); + currentMap = start; + + start.generate(dungeon, DoorTile.BOTTOM); + + + player = new Player(); currentMap.getEntities().add(player); - - // und Spinnen auch - Spider spider = new Spider(); - add(spider.actionFigur); - currentMap.getEntities().add(spider); + currentMap.add(player.actionFigur); + Main.instance.manager.anmelden(player, 20); - //und - Snake snake = new Snake(); - add(snake.actionFigur); - currentMap.getEntities().add(snake); - - currentMap.setPlayer(player); + add(currentMap); - - // Alle Entities als ticker registrieren (triggert dann die update methoden) - for (Entity e : currentMap.getEntities()) { - Main.instance.manager.anmelden(e, 20); - } } public Map getCurrentMap() { return currentMap; } + + public Player getPlayer() { + return player; + } + + public void changeMap(DoorTile door) { + currentMap.entfernen(player.actionFigur); + entfernen(currentMap); + System.out.println(door); + currentMap = door.getConnectedDoor().getMap(); + currentMap.add(player.actionFigur); + add(currentMap); + door.getConnectedDoor().waitForLeave(); + player.setPos(door.getConnectedDoor().posX + 0.5f, door.getConnectedDoor().posY + 0.5f); + } } diff --git a/Zoelda/src/main/entities/Entity.java b/Zoelda/src/main/entities/Entity.java index b8b5051..1d25735 100644 --- a/Zoelda/src/main/entities/Entity.java +++ b/Zoelda/src/main/entities/Entity.java @@ -112,11 +112,22 @@ public abstract class Entity implements Ticker { return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY)); } + /** + * @return die entifernung zu dieser Koordinate + */ + public float dist(float x, float y) { + return (float) Math.sqrt((x - posX) * (x - posX) + (y - posY) * (y - posY)); + } + /** * Generiert einen vektor von diesm Entity zu einem anderen */ public Vektor vectorToEntity(Entity e) { - return new Vektor(e.posX - posX, e.posY - posY); + Vektor v = new Vektor(e.posX - posX, e.posY - posY);; + if (v.x == 0 && v.y == 0) { + return new Vektor(0.0000001f, 0.0000001f); + } + return v; } protected boolean lineOfSightClear(Entity e) { @@ -171,6 +182,19 @@ public abstract class Entity implements Ticker { this.velY = velY; } + public void setPosX(float x) { + posX = x; + } + + public void setPosY(float y) { + posY = y; + } + + public void setPos(float x, float y) { + posX = x; + posY = y; + } + public void deleteEntity() { deleteEntity = true; } diff --git a/Zoelda/src/main/entities/LivingEntity.java b/Zoelda/src/main/entities/LivingEntity.java index 75b4b7d..b3e977a 100644 --- a/Zoelda/src/main/entities/LivingEntity.java +++ b/Zoelda/src/main/entities/LivingEntity.java @@ -48,7 +48,6 @@ public abstract class LivingEntity extends Entity { velX = 0; velY = 0; if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { - System.out.println("adasd"); deleteEntity(); } } @@ -65,15 +64,20 @@ public abstract class LivingEntity extends Entity { */ protected void zustandSetzen(String name) { actionFigur.spiegelXSetzen(mirrored ? !side : side); - actionFigur.zustandSetzen(name); + if (!actionFigur.aktuellesVerhalten().equals(name)) { + actionFigur.zustandSetzen(name); + actionFigur.aktuelleFigur().animationsBildSetzen(0); + } } - protected float getHealthPoints() { + + public float getHP() { return hp; } public void takeDamage(float damage, Entity e) { - actionFigur.zustandSetzen(getDamageAnimationName()); + zustandSetzen(getDamageAnimationName()); + actionFigur.aktuelleFigur().animationsBildSetzen(0); hp -= damage; } diff --git a/Zoelda/src/main/entities/Snake.java b/Zoelda/src/main/entities/Snake.java index a31d937..9a0ca7e 100644 --- a/Zoelda/src/main/entities/Snake.java +++ b/Zoelda/src/main/entities/Snake.java @@ -1,12 +1,14 @@ package main.entities; +import ea.Vektor; import main.Main; import main.SheetLoader; import main.entities.player.Player; public class Snake extends LivingEntity { - private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32, new int[] { 10, 10, 10, 10, 13, 8 }); + private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32, + new int[] { 10, 10, 10, 10, 13, 8 }); public Snake() { super(loader.getFigur(0), "idle"); @@ -26,13 +28,35 @@ public class Snake extends LivingEntity { actionFigur.neuerZustand(loader.getFigur(5), getDamageAnimationName()); loader.getFigur(3).animationsGeschwindigkeitSetzen(80); - loader.getFigur(5).animationsGeschwindigkeitSetzen(40); + loader.getFigur(5).animationsGeschwindigkeitSetzen(60); } @Override protected void update() { - Player player = Main.instance.getWorld().getCurrentMap().getPlayer(); - + Player player = Main.instance.getWorld().getPlayer(); + if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) { + if (lineOfSightClear(player)) { + if (dist(player) < 1f) { + zustandSetzen("attack"); + } else { + Vektor toPlayer = vectorToEntity(player).normiert(); + velX += toPlayer.x * accelleration; + velY += toPlayer.y * accelleration; + zustandSetzen("walk"); + } + } else { + if (!actionFigur.aktuellesVerhalten().equals("idle")) { + zustandSetzen("lost_sight"); + if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { + zustandSetzen("idle"); + } + } + } + } else { + if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { + zustandSetzen("walk"); + } + } this.checkTileCollisions(Main.instance.getWorld().getCurrentMap()); super.update(); } diff --git a/Zoelda/src/main/entities/Spider.java b/Zoelda/src/main/entities/Spider.java index b817a26..4cd2782 100644 --- a/Zoelda/src/main/entities/Spider.java +++ b/Zoelda/src/main/entities/Spider.java @@ -1,5 +1,6 @@ package main.entities; +import ea.Vektor; import main.Main; import main.SheetLoader; import main.entities.player.Player; @@ -17,6 +18,7 @@ public class Spider extends LivingEntity { spriteScale = 0.8f; posX = 4f; posY = 4f; + mirrored = true; actionFigur.neuerZustand(loader.getFigur(1), "lost_sight"); actionFigur.neuerZustand(loader.getFigur(2), "walk"); @@ -29,11 +31,32 @@ public class Spider extends LivingEntity { } protected void update() { - Player player = Main.instance.getWorld().getCurrentMap().getPlayer(); - + Player player = Main.instance.getWorld().getPlayer(); + if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) { + if (lineOfSightClear(player)) { + if (dist(player) < 1f) { + zustandSetzen("attack"); + } else { + Vektor toPlayer = vectorToEntity(player).normiert(); + velX += toPlayer.x * accelleration; + velY += toPlayer.y * accelleration; + zustandSetzen("walk"); + } + } else { + if (!actionFigur.aktuellesVerhalten().equals("idle")) { + zustandSetzen("lost_sight"); + if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { + zustandSetzen("idle"); + } + } + } + } else { + if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { + zustandSetzen("walk"); + } + } this.checkTileCollisions(Main.instance.getWorld().getCurrentMap()); super.update(); - } @Override diff --git a/Zoelda/src/main/entities/player/Player.java b/Zoelda/src/main/entities/player/Player.java index 8baa6a5..5097ccd 100644 --- a/Zoelda/src/main/entities/player/Player.java +++ b/Zoelda/src/main/entities/player/Player.java @@ -1,5 +1,7 @@ package main.entities.player; +import java.util.ArrayList; + import ea.Taste; import ea.Vektor; import main.Main; @@ -7,13 +9,12 @@ import main.SheetLoader; import main.entities.Entity; import main.entities.LivingEntity; -import java.awt.event.ActionListener; -import java.util.ArrayList; - public class Player extends LivingEntity { - private static SheetLoader loader = new SheetLoader("/res/images/player_sprite_sheet.png", 64, 32, new int[] { 5, 8, 7, 6, 2, 5, 4, 7 }); + private static SheetLoader loader = new SheetLoader("/res/images/player_sprite_sheet.png", 64, 32, + new int[] { 5, 8, 7, 6, 2, 5, 4, 7 }); private boolean onlyAttackOnceTrigger; + private float range = 1.2f; // die reichweite der Attacke public Player() { super(loader.getFigur(0), "idle"); @@ -23,8 +24,8 @@ public class Player extends LivingEntity { height = 0.8f; spriteOffsetY = -0.14f; spriteScale = 0.8f; - posX = 4f; - posY = 4f; + posX = 7.5f; + posY = 5.5f; // unterschiedliche Animationsgeschwindigkeiten // für idle @@ -47,8 +48,8 @@ public class Player extends LivingEntity { if (!((actionFigur.aktuellesVerhalten().equals("strike")) && actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) { // Bei diser soll man sich nicht bewegen können aber weiter rutschen - if (!((actionFigur.aktuellesVerhalten().equals("swipe")) - && actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) { + if (!((actionFigur.aktuellesVerhalten().equals("swipe")) && actionFigur.aktuelleFigur() + .aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) { // wasd movement if (Main.instance.tasteGedrueckt(Taste.A)) { @@ -96,18 +97,21 @@ public class Player extends LivingEntity { // auf Kollisionen prüfen checkTileCollisions(Main.instance.getWorld().getCurrentMap()); - if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) { + if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe") + && actionFigur.aktuelleFigur().aktuellesBild() == 1) { onlyAttackOnceTrigger = true; ArrayList entities = Main.instance.getWorld().getCurrentMap().getEntities(); for (Entity e : entities) { - if (e instanceof LivingEntity && e != this && e.dist(this) <= 1f) { + if (e instanceof LivingEntity && e != this && e.dist(this) <= range) { LivingEntity le = (LivingEntity) e; - Vektor toE = vectorToEntity(le); - toE = toE.normiert(); - if ((toE.x > 0 && !side) || (toE.x <= 0 && side)) { - le.setVelX(le.getVelX() + toE.x * 0.05f); - le.setVelY(le.getVelY() + toE.y * 0.05f); - le.takeDamage(0.1f, this); + if (le.getHP() > 0) { + Vektor toE = vectorToEntity(le); + toE = toE.normiert(); + if ((toE.x > 0 && !side) || (toE.x <= 0 && side)) { + le.setVelX(le.getVelX() + toE.x * 0.1f); + le.setVelY(le.getVelY() + toE.y * 0.1f); + le.takeDamage(0.1f, this); + } } } } diff --git a/Zoelda/src/main/maps/Corridor.java b/Zoelda/src/main/maps/Corridor.java index b398bdd..957d88b 100644 --- a/Zoelda/src/main/maps/Corridor.java +++ b/Zoelda/src/main/maps/Corridor.java @@ -2,31 +2,37 @@ package main.maps; import main.Tile; -import main.entities.Snake; - public class Corridor extends Map { public Corridor() { - super(5, 10); - + super(15, 11); + for (int x = 0; x < map.length; x++) { for (int y = 0; y < map[0].length; y++) { - //Wand - if((y == 0 || x == 0 || y == 9 || x == 4) && x != 2 ) { - map[x][y] = new Tile(3, x, y); - add(map[x][y]); - } - //3D-Wand - else if(y == 1 && (x != 0 || x != 4)&& x != 2 ) { - map[x][y] = new Tile(4, x, 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]); } - //Steinboden - else { - map[x][y] = new Tile(5, 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]); + } + // Steinboden + else if (x >= 6 && x <= 8) { + if (x == 7 && (y == 0 || y == map[0].length - 1)) { + } else { + map[x][y] = new Tile(Tile.STONE_FLOOR, x, y); + add(map[x][y]); + } + } else { + map[x][y] = new Tile(Tile.VOID, x, y); add(map[x][y]); } } } + + registerEntities(); } } diff --git a/Zoelda/src/main/maps/ImageMap.java b/Zoelda/src/main/maps/ImageMap.java new file mode 100644 index 0000000..ba2980a --- /dev/null +++ b/Zoelda/src/main/maps/ImageMap.java @@ -0,0 +1,165 @@ +package main.maps; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.ArrayList; + +import javax.imageio.ImageIO; + +import main.DoorTile; +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)), + new PixelTile(Tile.DOOR, new Color(255, 0, 0)) }; + + public DoorTile topDoor; + public DoorTile leftDoor; + public DoorTile rightDoor; + public DoorTile bottomDoor; + + public ImageMap(String path) { + super(15, 11); + + try { + BufferedImage img = ImageIO.read(ImageMap.class.getResourceAsStream(path)); + if (img.getWidth() != 15 || img.getHeight() != 11) { + System.err.println("Odd map size! Use 15x11"); + } + for (int x = 0; x < img.getWidth(); x++) { + for (int y = 0; y < img.getHeight(); y++) { + int id = Tile.VOID; + int rgb = img.getRGB(x, y); + for (PixelTile t : pixels) { + if (t.c == rgb) { + id = t.id; + break; + } + } + if (id == Tile.DOOR) { + DoorTile door = new DoorTile(x, y, this); + map[x][y] = door; + if (x == 7) { + if (y < 5) { + topDoor = door; + door.setSide(DoorTile.TOP); + } else { + bottomDoor = door; + door.setSide(DoorTile.BOTTOM); + } + } + if (y == 5) { + if (x < 7) { + leftDoor = door; + door.setSide(DoorTile.LEFT); + } else { + rightDoor = door; + door.setSide(DoorTile.RIGHT); + } + } + } else { + map[x][y] = new Tile(id, x, y); + } + add(map[x][y]); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static class PixelTile { + public final int c; + public final int id; + + public PixelTile(int id, Color c) { + this.c = c.getRGB(); + this.id = id; + } + } + + public void generate(ArrayList dungeon, int fromSide) { + if (topDoor != null && fromSide != DoorTile.TOP) { + ImageMap map; + do { + map = nextMap(); + } while (!connectDoors(topDoor, map)); + dungeon.add(map); + System.out.println("TOP"); + map.generate(dungeon, DoorTile.BOTTOM); + } + if (bottomDoor != null && fromSide != DoorTile.BOTTOM) { + ImageMap map; + do { + map = nextMap(); + } while (!connectDoors(bottomDoor, map)); + dungeon.add(map); + System.out.println("BOTTOM"); + map.generate(dungeon, DoorTile.TOP); + } + if (leftDoor != null && fromSide != DoorTile.LEFT) { + ImageMap map; + do { + map = nextMap(); + } while (!connectDoors(leftDoor, map)); + dungeon.add(map); + System.out.println("LEFT"); + map.generate(dungeon, DoorTile.RIGHT); + } + if (rightDoor != null && fromSide != DoorTile.RIGHT) { + ImageMap map; + do { + map = nextMap(); + } while (!connectDoors(rightDoor, map)); + dungeon.add(map); + System.out.println("RIGHT"); + map.generate(dungeon, DoorTile.LEFT); + } + System.out.println(); + } + + private ImageMap nextMap() { + return new ImageMap("/res/images/maps/map" + ((int) (Math.random() * 4) + 5) + ".png"); + } + + 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); + System.out.println(door.getConnectedDoor()); + System.out.println(otherMap.bottomDoor.getConnectedDoor()); + 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; + } + return true; + } +} diff --git a/Zoelda/src/main/maps/Map.java b/Zoelda/src/main/maps/Map.java index 4c57e1e..cd6ed88 100644 --- a/Zoelda/src/main/maps/Map.java +++ b/Zoelda/src/main/maps/Map.java @@ -8,7 +8,6 @@ import main.Main; import main.Tile; import main.entities.Entity; import main.entities.LivingEntity; -import main.entities.player.Player; /** * Auf der Map sind alle Entities, sowie die Tiles gespiechert. @@ -17,18 +16,19 @@ public abstract class Map extends Knoten implements Ticker { protected Tile[][] map; // Die Tiles der map in einem 2D Array. private ArrayList entities; - private Player player; + public Map(int width, int height) { map = new Tile[width][height]; entities = new ArrayList<>(100); - + Main.instance.manager.anmelden(this, 20); } - + /** - * Gibt das Tile-Objekt an der gegebenen Koordinate zurück. (nur wenn es existiert) + * Gibt das Tile-Objekt an der gegebenen Koordinate zurück. (nur wenn es + * existiert) */ public Tile getTile(int x, int y) { if (x >= 0 && x < map.length && y >= 0 && y < map[0].length) { @@ -36,24 +36,25 @@ public abstract class Map extends Knoten implements Ticker { } return null; } - + @Override public void tick() { for (int i = 0; i < entities.size(); i++) { Entity e = entities.get(i); - if (e instanceof LivingEntity) { - if (e.isReadyToDelete()) { - Main.instance.manager.abmelden(e); - entities.remove(e); + if (e.isReadyToDelete()) { + if (e instanceof LivingEntity) { + entfernen(((LivingEntity) e).actionFigur); } + Main.instance.manager.abmelden(e); + entities.remove(e); } } } - + public int getWidth() { return map.length; } - + public int getHeight() { return map[0].length; } @@ -61,12 +62,10 @@ public abstract class Map extends Knoten implements Ticker { public ArrayList getEntities() { return entities; } - - public Player getPlayer() { - return player; - } - public void setPlayer(Player player) { - this.player = player; + public void registerEntities() { + for (Entity e : getEntities()) { + Main.instance.manager.anmelden(e, 20); + } } } diff --git a/Zoelda/src/main/maps/Map01.java b/Zoelda/src/main/maps/Map01.java new file mode 100644 index 0000000..811e5f8 --- /dev/null +++ b/Zoelda/src/main/maps/Map01.java @@ -0,0 +1,25 @@ +package main.maps; + +import main.Tile; + +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++) { + //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]); + } + //Boden + else if(x > 3 && y > 0 && x < 11 && y < 10) { + map[x][y] = new Tile(5, x, y); + add(map[x][y]); + } + } + } + } + +} diff --git a/Zoelda/src/main/maps/Map02.java b/Zoelda/src/main/maps/Map02.java new file mode 100644 index 0000000..54bfe25 --- /dev/null +++ b/Zoelda/src/main/maps/Map02.java @@ -0,0 +1,38 @@ +package main.maps; + +import main.Tile; + +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++) { + // 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]); + continue; + } + } + map[x][y] = new Tile(3, x, y); + add(map[x][y]); + } + // 3D-Wand + else if(y == 1 ) { + map[x][y] = new Tile(4, x, y); + add(map[x][y]); + } + // Steinboden + else { + map[x][y] = new Tile(5, x, y); + add(map[x][y]); + } + } + } + } +} diff --git a/Zoelda/src/main/maps/TutorialMap.java b/Zoelda/src/main/maps/TutorialMap.java index 097b998..1e0e716 100644 --- a/Zoelda/src/main/maps/TutorialMap.java +++ b/Zoelda/src/main/maps/TutorialMap.java @@ -1,6 +1,8 @@ package main.maps; import main.Tile; +import main.entities.Snake; +import main.entities.Spider; public class TutorialMap extends Map { @@ -25,5 +27,18 @@ public class TutorialMap extends Map { } } } + + // und Spinnen auch + Spider spider = new Spider(); + add(spider.actionFigur); + getEntities().add(spider); + + //und + Snake snake = new Snake(); + add(snake.actionFigur); + getEntities().add(snake); + + // Alle Entities als ticker registrieren (triggert dann die update methoden) + registerEntities(); } } diff --git a/Zoelda/src/res/images/herzen.png b/Zoelda/src/res/images/herzen.png new file mode 100644 index 0000000..c64d5e7 Binary files /dev/null and b/Zoelda/src/res/images/herzen.png differ diff --git a/Zoelda/src/res/images/maps/map1.png b/Zoelda/src/res/images/maps/map1.png new file mode 100644 index 0000000..bea5452 Binary files /dev/null and b/Zoelda/src/res/images/maps/map1.png differ diff --git a/Zoelda/src/res/images/maps/map2.png b/Zoelda/src/res/images/maps/map2.png new file mode 100644 index 0000000..5361062 Binary files /dev/null and b/Zoelda/src/res/images/maps/map2.png differ diff --git a/Zoelda/src/res/images/maps/map3.png b/Zoelda/src/res/images/maps/map3.png new file mode 100644 index 0000000..8182b79 Binary files /dev/null and b/Zoelda/src/res/images/maps/map3.png differ diff --git a/Zoelda/src/res/images/maps/map4.png b/Zoelda/src/res/images/maps/map4.png new file mode 100644 index 0000000..a3e5eee Binary files /dev/null and b/Zoelda/src/res/images/maps/map4.png differ diff --git a/Zoelda/src/res/images/maps/map5.png b/Zoelda/src/res/images/maps/map5.png new file mode 100644 index 0000000..2c0ed6d Binary files /dev/null and b/Zoelda/src/res/images/maps/map5.png differ diff --git a/Zoelda/src/res/images/maps/map6.png b/Zoelda/src/res/images/maps/map6.png new file mode 100644 index 0000000..0c17146 Binary files /dev/null and b/Zoelda/src/res/images/maps/map6.png differ diff --git a/Zoelda/src/res/images/maps/map7.png b/Zoelda/src/res/images/maps/map7.png new file mode 100644 index 0000000..fd4ad1e Binary files /dev/null and b/Zoelda/src/res/images/maps/map7.png differ diff --git a/Zoelda/src/res/images/maps/map8.png b/Zoelda/src/res/images/maps/map8.png new file mode 100644 index 0000000..0f3cc49 Binary files /dev/null and b/Zoelda/src/res/images/maps/map8.png differ diff --git a/Zoelda/src/res/images/tiles/TestTest.png b/Zoelda/src/res/images/tiles/TestTest.png new file mode 100644 index 0000000..c439029 Binary files /dev/null and b/Zoelda/src/res/images/tiles/TestTest.png differ diff --git a/Zoelda/src/res/images/tiles/door.png b/Zoelda/src/res/images/tiles/door.png new file mode 100644 index 0000000..92edddf Binary files /dev/null and b/Zoelda/src/res/images/tiles/door.png differ diff --git a/Zoelda/src/res/images/tiles/stone_floor.png b/Zoelda/src/res/images/tiles/stone_floor.png deleted file mode 100644 index 70eadc3..0000000 Binary files a/Zoelda/src/res/images/tiles/stone_floor.png and /dev/null differ diff --git a/Zoelda/src/res/images/tiles/stone_wall.png b/Zoelda/src/res/images/tiles/stone_wall.png deleted file mode 100644 index b0842b0..0000000 Binary files a/Zoelda/src/res/images/tiles/stone_wall.png and /dev/null differ diff --git a/Zoelda/src/res/images/tiles/stone_wall_bottom.png b/Zoelda/src/res/images/tiles/stone_wall_bottom.png index 5878183..5279d0f 100644 Binary files a/Zoelda/src/res/images/tiles/stone_wall_bottom.png and b/Zoelda/src/res/images/tiles/stone_wall_bottom.png differ