diff --git a/Zoelda/src/main/Main.java b/Zoelda/src/main/Main.java index deccdda..5d59147 100644 --- a/Zoelda/src/main/Main.java +++ b/Zoelda/src/main/Main.java @@ -27,8 +27,8 @@ public class Main extends Game { wurzel.add(world); hud = new HUD(); wurzel.add(hud); - sounds = new Sounds(); - sounds.playSound(2); +// sounds = new Sounds(); +// sounds.playSound(2); } @Override diff --git a/Zoelda/src/main/entities/AnimatedEntity.java b/Zoelda/src/main/entities/AnimatedEntity.java new file mode 100644 index 0000000..1a91099 --- /dev/null +++ b/Zoelda/src/main/entities/AnimatedEntity.java @@ -0,0 +1,74 @@ +package main.entities; + +import java.awt.Color; +import java.awt.Graphics2D; + +import ea.ActionFigur; +import ea.BoundingRechteck; +import ea.Figur; +import ea.internal.gra.PixelFeld; +import main.Main; +import main.worlds.World; + +public abstract class AnimatedEntity extends Entity { + + public ActionFigur actionFigur; // Sprite des Entities + protected float spriteScale = 1f; // Skalierung des Sprites + protected float spriteOffsetX, spriteOffsetY; // Offset des Sprites. Hier kann man die relative render-Position + // nachjustieren. + protected boolean side; + + private static Figur noTexture = new Figur(); + + static { + PixelFeld[] img = new PixelFeld[1]; + img[0] = new PixelFeld(16, 16, 1); + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + img[0].farbeSetzen(x, y, Color.MAGENTA); + } + } + noTexture.animationSetzen(img); + } + + public AnimatedEntity() { + actionFigur = new ActionFigur(noTexture, "undefined") { + // DEBUG: render boxes + + @Override + public void zeichnen(Graphics2D g, BoundingRechteck r) { + g.setColor(Color.GREEN); + g.drawRect((int) actionFigur.positionX(), (int) actionFigur.positionY(), + (int) actionFigur.getBreite(), (int) actionFigur.getHoehe()); + g.drawRect((int) ((posX - width / 2) * World.SCALE), (int) ((posY - height / 2) * World.SCALE), + (int) (width * World.SCALE), (int) (height * World.SCALE)); + super.zeichnen(g, r); + } + }; + } + + @Override + protected void update() { + + checkTileCollision(Main.instance.getWorld().getCurrentMap()); + checkEntityCollision(Main.instance.getWorld().getCurrentMap()); + + // Packt das Sprite an die richtige Stelle + actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR)); + float offsetX = width / 2 + spriteOffsetX - width / 2; + float offsetY = height / 2 + spriteOffsetY - height / 2; + actionFigur.positionSetzen((posX + offsetX) * World.SCALE - actionFigur.getBreite() / 2, + (posY + offsetY) * World.SCALE - actionFigur.getHoehe() / 2); + } + + /** + * Spiegelt die figur autmatisch, wenn n�tig. + */ + protected void zustandSetzen(String name) { + actionFigur.spiegelXSetzen(side); + if (!actionFigur.aktuellesVerhalten().equals(name)) { + actionFigur.zustandSetzen(name); + actionFigur.aktuelleFigur().animationsBildSetzen(0); + } + } +} diff --git a/Zoelda/src/main/entities/LivingEntity.java b/Zoelda/src/main/entities/LivingEntity.java index 9405f7d..101a38d 100644 --- a/Zoelda/src/main/entities/LivingEntity.java +++ b/Zoelda/src/main/entities/LivingEntity.java @@ -1,63 +1,20 @@ package main.entities; -import java.awt.Color; - -import ea.ActionFigur; -import ea.Figur; -import ea.internal.gra.PixelFeld; -import main.Main; -import main.worlds.World; - /* * Alles was zustände hat und sich Bewegen kann, ist ein LivingEntity. */ -public abstract class LivingEntity extends Entity { +public abstract class LivingEntity extends AnimatedEntity { - protected boolean side; // true = gespiegelt, false = nicht - public ActionFigur actionFigur; // Sprite des Entities - protected float spriteScale = 1f; // Skalierung des Sprites - protected float spriteOffsetX, spriteOffsetY; // Offset des Sprites. Hier kann man die relative render-Position nachjustieren. + protected boolean mirrored; // true = gespiegelt, false = nicht protected float hp = 1f; //hp des Entitys - protected boolean mirrored; - - private static Figur noTexture = new Figur(); - - static { - PixelFeld[] img = new PixelFeld[1]; - img[0] = new PixelFeld(16, 16, 1); - for (int x = 0; x < 16; x++) { - for (int y = 0; y < 16; y++) { - img[0].farbeSetzen(x, y, Color.MAGENTA); - } - } - noTexture.animationSetzen(img); - } - - /** - * @param figur - erstes (standart) Sprite - * @param name - name des Zustands - */ - public LivingEntity() { - actionFigur = new ActionFigur(noTexture, "undefined") { - // DEBUG: render boxes - -// @Override -// public void zeichnen(Graphics2D g, BoundingRechteck r) { -// g.setColor(Color.GREEN); -// g.drawRect((int) actionFigur.positionX(), (int) actionFigur.positionY(), (int) actionFigur.getBreite(), (int) actionFigur.getHoehe()); -// g.drawRect((int) ((posX - width / 2) * World.SCALE), (int) ((posY - height / 2) * World.SCALE), (int) (width * World.SCALE), (int) (height * World.SCALE)); -// super.zeichnen(g, r); -// } - }; - } @Override protected void update() { // Prüft zu welcher seite man guckt if (velX < 0) { - side = true; + side = !mirrored; } else if (velX > 0) { - side = false; + side = mirrored; } if (hp <= 0) { @@ -68,29 +25,9 @@ public abstract class LivingEntity extends Entity { deleteEntity(); } } - - checkTileCollision(Main.instance.getWorld().getCurrentMap()); - checkEntityCollision(Main.instance.getWorld().getCurrentMap()); - - // Packt das Sprite an die richtige Stelle - actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR)); - float offsetX = width / 2 + spriteOffsetX - width / 2; - float offsetY = height / 2 + spriteOffsetY - height / 2; - actionFigur.positionSetzen((posX + offsetX) * World.SCALE - actionFigur.getBreite() / 2, (posY + offsetY) * World.SCALE - actionFigur.getHoehe() / 2); + super.update(); } - /** - * Spiegelt die figur autmatisch, wenn nötig. - */ - protected void zustandSetzen(String name) { - actionFigur.spiegelXSetzen(mirrored ? !side : side); - if (!actionFigur.aktuellesVerhalten().equals(name)) { - actionFigur.zustandSetzen(name); - actionFigur.aktuelleFigur().animationsBildSetzen(0); - } - } - - public float getHP() { return hp; } diff --git a/Zoelda/src/main/maps/Map.java b/Zoelda/src/main/maps/Map.java index 01590d1..3ab4384 100644 --- a/Zoelda/src/main/maps/Map.java +++ b/Zoelda/src/main/maps/Map.java @@ -7,6 +7,7 @@ import ea.Ticker; import main.DoorTile; import main.Main; import main.Tile; +import main.entities.AnimatedEntity; import main.entities.Entity; import main.entities.LivingEntity; import main.entities.player.Player; @@ -69,7 +70,7 @@ public abstract class Map extends Knoten implements Ticker { return entities; } - public void addLivingEntity(LivingEntity e) { + public void addAnimatedEntity(AnimatedEntity e) { entities.add(e); add(e.actionFigur); } diff --git a/Zoelda/src/main/maps/TutorialMap.java b/Zoelda/src/main/maps/TutorialMap.java index 19d8fed..e6852ca 100644 --- a/Zoelda/src/main/maps/TutorialMap.java +++ b/Zoelda/src/main/maps/TutorialMap.java @@ -28,8 +28,8 @@ public class TutorialMap extends Map { } } - addLivingEntity(new Spider()); - addLivingEntity(new Snake()); + addAnimatedEntity(new Spider()); + addAnimatedEntity(new Snake()); } @Override diff --git a/Zoelda/src/main/worlds/TestWorld.java b/Zoelda/src/main/worlds/TestWorld.java index 833e37f..07eb04a 100644 --- a/Zoelda/src/main/worlds/TestWorld.java +++ b/Zoelda/src/main/worlds/TestWorld.java @@ -1,5 +1,6 @@ package main.worlds; +import main.entities.Coin; import main.entities.Snake; import main.maps.Map; @@ -14,8 +15,10 @@ public class TestWorld extends World { for (int i = 0; i < 5; i++) { Snake s = new Snake(); s.setPosX((float) (Math.random() * 5 + 2)); - getCurrentMap().addLivingEntity(s); + getCurrentMap().addAnimatedEntity(s); } + + getCurrentMap().addAnimatedEntity(new Coin()); } } diff --git a/Zoelda/src/main/worlds/World.java b/Zoelda/src/main/worlds/World.java index d92d5a7..610dc56 100644 --- a/Zoelda/src/main/worlds/World.java +++ b/Zoelda/src/main/worlds/World.java @@ -61,7 +61,7 @@ public abstract class World extends Knoten { } public void start() { - currentMap.addLivingEntity(player); + currentMap.addAnimatedEntity(player); currentMap.start(); } }