From c035253790d55b371e689b0e5f828da94b55929e Mon Sep 17 00:00:00 2001 From: Asecave Date: Wed, 30 Jun 2021 15:47:17 +0200 Subject: [PATCH] Entity vs Entity collision --- Zoelda/src/main/Main.java | 1 - Zoelda/src/main/entities/Entity.java | 55 ++++++++++++++++----- Zoelda/src/main/entities/LivingEntity.java | 4 ++ Zoelda/src/main/entities/Snake.java | 1 - Zoelda/src/main/entities/Spider.java | 1 - Zoelda/src/main/entities/player/Player.java | 6 --- Zoelda/src/main/worlds/TestWorld.java | 8 ++- 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Zoelda/src/main/Main.java b/Zoelda/src/main/Main.java index 13a87b6..deccdda 100644 --- a/Zoelda/src/main/Main.java +++ b/Zoelda/src/main/Main.java @@ -1,7 +1,6 @@ package main; import ea.Game; -import ea.Sound; import main.HUD.HUD; import main.worlds.TestWorld; import main.worlds.World; diff --git a/Zoelda/src/main/entities/Entity.java b/Zoelda/src/main/entities/Entity.java index 1d25735..ee12f9f 100644 --- a/Zoelda/src/main/entities/Entity.java +++ b/Zoelda/src/main/entities/Entity.java @@ -49,12 +49,13 @@ public abstract class Entity implements Ticker { /** * Checkt collision mit tiles von der map. Fertig, clean, nie wieder ändern. */ - public void checkTileCollisions(Map map) { + public void checkTileCollision(Map map) { for (int x = (int) (posX - 2); x < posX + 2; x++) { for (int y = (int) (posY - 2); y < posY + 2; y++) { if (x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight()) { if (map.getTile(x, y).isCollidable()) { - if (posY + height / 2 > y && posY - height / 2 < y + 1 && posX + width / 2 > x && posX - width / 2 < x + 1) { + if (posY + height / 2 > y && posY - height / 2 < y + 1 && posX + width / 2 > x + && posX - width / 2 < x + 1) { float distN = Math.abs((y + 1 + height / 2) - posY); float distE = Math.abs((x + 1 + width / 2) - posX); float distS = Math.abs((y - height / 2) - posY); @@ -75,6 +76,23 @@ public abstract class Entity implements Ticker { } } + public void checkEntityCollision(Map map) { + for (int i = 0; i < map.getEntities().size(); i++) { + Entity e = map.getEntities().get(i); + if (e != this) { + float esize = (e.getWidth() + e.getHeight()) / 2; + float size = (width + height) / 2; + float dist = dist(e); + float sizes = esize / 2 + size / 2; + if (dist < sizes) { + float overlap = dist - sizes; + posX += (overlap * (e.posX - posX)); + posY += (overlap * (e.posY - posY)); + } + } + } + } + /** * Gehört mit zur collision detection. Hier werden kollisionen aufgelöst. */ @@ -104,32 +122,33 @@ public abstract class Entity implements Ticker { posX = blockX - width / 2; } } - + /** * @return die entifernung zu diesem Entity */ public float dist(Entity e) { 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) { - Vektor v = 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) { Vektor direct = vectorToEntity(e).normiert(); direct = direct.teilen(4f); @@ -152,6 +171,7 @@ public abstract class Entity implements Ticker { /** * Gibt den velX Wert zurück. + * * @return gibt den velX Wert als float zurück. */ public float getVelX() { @@ -160,6 +180,7 @@ public abstract class Entity implements Ticker { /** * Gibt den velY Wert zurück. + * * @return gibt den velY Wert als float zurück. */ public float getVelY() { @@ -168,6 +189,7 @@ public abstract class Entity implements Ticker { /** * Setzt eine neue velX velocity. + * * @param velX neuer float velX Wert. */ public void setVelX(float velX) { @@ -176,30 +198,39 @@ public abstract class Entity implements Ticker { /** * Setzt eine neue Y velocity. + * * @param velY neuer float velY Wert. */ public void setVelY(float velY) { 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; } - + public boolean isReadyToDelete() { return deleteEntity; } + + public float getWidth() { + return width; + } + + public float getHeight() { + return height; + } } diff --git a/Zoelda/src/main/entities/LivingEntity.java b/Zoelda/src/main/entities/LivingEntity.java index 686a428..9405f7d 100644 --- a/Zoelda/src/main/entities/LivingEntity.java +++ b/Zoelda/src/main/entities/LivingEntity.java @@ -5,6 +5,7 @@ import java.awt.Color; import ea.ActionFigur; import ea.Figur; import ea.internal.gra.PixelFeld; +import main.Main; import main.worlds.World; /* @@ -67,6 +68,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)); diff --git a/Zoelda/src/main/entities/Snake.java b/Zoelda/src/main/entities/Snake.java index cb6230d..25ad686 100644 --- a/Zoelda/src/main/entities/Snake.java +++ b/Zoelda/src/main/entities/Snake.java @@ -68,7 +68,6 @@ public class Snake extends LivingEntity { 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 786d644..a9d108a 100644 --- a/Zoelda/src/main/entities/Spider.java +++ b/Zoelda/src/main/entities/Spider.java @@ -56,7 +56,6 @@ public class Spider extends LivingEntity { zustandSetzen("walk"); } } - this.checkTileCollisions(Main.instance.getWorld().getCurrentMap()); super.update(); } diff --git a/Zoelda/src/main/entities/player/Player.java b/Zoelda/src/main/entities/player/Player.java index bd4eff1..f471041 100644 --- a/Zoelda/src/main/entities/player/Player.java +++ b/Zoelda/src/main/entities/player/Player.java @@ -83,9 +83,6 @@ public class Player extends LivingEntity { } onlyAttackOnceTrigger = false; } - - // auf Kollisionen prüfen - checkTileCollisions(Main.instance.getWorld().getCurrentMap()); // LivingEntity auch updaten lassen super.update(); } else { @@ -93,9 +90,6 @@ public class Player extends LivingEntity { velY = 0; } - // auf Kollisionen prüfen - checkTileCollisions(Main.instance.getWorld().getCurrentMap()); - if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) { if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) { diff --git a/Zoelda/src/main/worlds/TestWorld.java b/Zoelda/src/main/worlds/TestWorld.java index 9b4f459..833e37f 100644 --- a/Zoelda/src/main/worlds/TestWorld.java +++ b/Zoelda/src/main/worlds/TestWorld.java @@ -7,11 +7,15 @@ public class TestWorld extends World { public TestWorld() { super(4); - + Map m2 = maps[3].clone(); getCurrentMap().connectDoors(getCurrentMap().leftDoor, m2.leftDoor); - getCurrentMap().addLivingEntity(new Snake()); + for (int i = 0; i < 5; i++) { + Snake s = new Snake(); + s.setPosX((float) (Math.random() * 5 + 2)); + getCurrentMap().addLivingEntity(s); + } } }