diff --git a/Zoelda/src/main/entities/Entity.java b/Zoelda/src/main/entities/Entity.java index 3a8dd90..719ed11 100644 --- a/Zoelda/src/main/entities/Entity.java +++ b/Zoelda/src/main/entities/Entity.java @@ -1,6 +1,9 @@ package main.entities; import ea.Ticker; +import ea.Vektor; +import main.Main; +import main.Tile; import main.maps.Map; /* @@ -107,5 +110,32 @@ public abstract class Entity implements Ticker { public float dist(Entity e) { return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY)); } + + /** + * Generiert einen vektor von diesm Entity zu einem anderen + */ + public Vektor vectorToEntity(Entity e) { + return new Vektor(e.posX - posX, e.posY - posY); + } + + protected boolean lineOfSightClear(Entity e) { + Vektor direct = vectorToEntity(e).normiert(); + direct = direct.teilen(4f); + Vektor loc = new Vektor(posX, posY); + float lastDist; + float dist = Float.MAX_VALUE; + boolean clear = true; + do { + loc = loc.summe(direct); + lastDist = dist; + dist = Math.abs(e.posX - loc.x) + Math.abs(e.posY - loc.y); + Tile t = Main.instance.getWorld().getCurrentMap().getTile((int) loc.x, (int) loc.y); + if (t != null && t.isCollidable()) { + clear = false; + break; + } + } while (dist < lastDist); + return clear; + } } diff --git a/Zoelda/src/main/entities/LivingEntity.java b/Zoelda/src/main/entities/LivingEntity.java index a280da0..91ac26c 100644 --- a/Zoelda/src/main/entities/LivingEntity.java +++ b/Zoelda/src/main/entities/LivingEntity.java @@ -7,7 +7,7 @@ import main.World; /* * Alles was zustände hat und sich Bewegen kann, ist ein LivingEntity. */ -public class LivingEntity extends Entity { +public abstract class LivingEntity extends Entity { protected boolean side; // true = gespiegelt, false = nicht public ActionFigur actionFigur; // Sprite des Entities @@ -60,4 +60,8 @@ public class LivingEntity extends Entity { protected float getHealthPoints() { return hp; } + + public void takeDamage(float damage) { + hp -= damage; + } } diff --git a/Zoelda/src/main/entities/Player.java b/Zoelda/src/main/entities/Player.java index fe18eb1..261bd72 100644 --- a/Zoelda/src/main/entities/Player.java +++ b/Zoelda/src/main/entities/Player.java @@ -1,5 +1,7 @@ package main.entities; +import java.util.ArrayList; + import ea.Taste; import main.Main; import main.SheetLoader; @@ -73,6 +75,15 @@ public class Player extends LivingEntity { if (Main.instance.tasteGedrueckt(Taste.F)) { zustandSetzen("die"); } + + if (actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) { + ArrayList entities = Main.instance.getWorld().getCurrentMap().getEntities(); + for (Entity e : entities) { + if (e instanceof LivingEntity) { + ((LivingEntity) e).takeDamage(0.1f); + } + } + } } // auf Kollisionen prüfen diff --git a/Zoelda/src/main/entities/Snake.java b/Zoelda/src/main/entities/Snake.java index 5ff1cbf..24f612e 100644 --- a/Zoelda/src/main/entities/Snake.java +++ b/Zoelda/src/main/entities/Snake.java @@ -10,7 +10,7 @@ import main.Tile; 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, 10 }); + new int[] { 10, 10, 10, 10, 10, 8}); public Snake() { super(loader.getFigur(0), "idle"); @@ -27,6 +27,7 @@ public class Snake extends LivingEntity { actionFigur.neuerZustand(loader.getFigur(2), "walk"); actionFigur.neuerZustand(loader.getFigur(3), "attack"); actionFigur.neuerZustand(loader.getFigur(4), "die"); + actionFigur.neuerZustand(loader.getFigur(5), "damage"); } @Override @@ -44,16 +45,16 @@ public class Snake extends LivingEntity { if (dist(nearestPlayer) < 1f) { zustandSetzen("attack"); } else { + if (actionFigur.aktuellesVerhalten().equals("attack")) { + actionFigur.aktuelleFigur().animationsBildSetzen(0); + } Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY); toPlayer = toPlayer.normiert(); velX += toPlayer.x * accelleration; velY += toPlayer.y * accelleration; zustandSetzen("walk"); } - - } else - - { + } else { if (actionFigur.aktuellesVerhalten().equals("walk")) { zustandSetzen("lost_sight"); } @@ -66,24 +67,4 @@ public class Snake extends LivingEntity { this.checkTileCollisions(Main.instance.getWorld().getCurrentMap()); super.update(); } - - private boolean lineOfSightClear(Entity e) { - Vektor direct = new Vektor(e.posX - posX, e.posY - posY).normiert(); - direct = direct.teilen(4f); - Vektor loc = new Vektor(posX, posY); - float lastDist; - float dist = Float.MAX_VALUE; - boolean clear = true; - do { - loc = loc.summe(direct); - lastDist = dist; - dist = Math.abs(e.posX - loc.x) + Math.abs(e.posY - loc.y); - Tile t = Main.instance.getWorld().getCurrentMap().getTile((int) loc.x, (int) loc.y); - if (t != null && t.isCollidable()) { - clear = false; - break; - } - } while (dist < lastDist); - return clear; - } } diff --git a/Zoelda/src/res/images/snake_spritesheet_calciumtrice.png b/Zoelda/src/res/images/snake_spritesheet_calciumtrice.png index 0417b5e..ceaf5fe 100644 Binary files a/Zoelda/src/res/images/snake_spritesheet_calciumtrice.png and b/Zoelda/src/res/images/snake_spritesheet_calciumtrice.png differ