Entity vs Entity collision

This commit is contained in:
Asecave
2021-06-30 15:47:17 +02:00
parent a5eed792e7
commit c035253790
7 changed files with 53 additions and 23 deletions

View File

@@ -1,7 +1,6 @@
package main; package main;
import ea.Game; import ea.Game;
import ea.Sound;
import main.HUD.HUD; import main.HUD.HUD;
import main.worlds.TestWorld; import main.worlds.TestWorld;
import main.worlds.World; import main.worlds.World;

View File

@@ -49,12 +49,13 @@ public abstract class Entity implements Ticker {
/** /**
* Checkt collision mit tiles von der map. Fertig, clean, nie wieder ändern. * 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 x = (int) (posX - 2); x < posX + 2; x++) {
for (int y = (int) (posY - 2); y < posY + 2; y++) { for (int y = (int) (posY - 2); y < posY + 2; y++) {
if (x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight()) { if (x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight()) {
if (map.getTile(x, y).isCollidable()) { 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 distN = Math.abs((y + 1 + height / 2) - posY);
float distE = Math.abs((x + 1 + width / 2) - posX); float distE = Math.abs((x + 1 + width / 2) - posX);
float distS = Math.abs((y - height / 2) - posY); 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. * Gehört mit zur collision detection. Hier werden kollisionen aufgelöst.
*/ */
@@ -123,7 +141,8 @@ public abstract class Entity implements Ticker {
* Generiert einen vektor von diesm Entity zu einem anderen * Generiert einen vektor von diesm Entity zu einem anderen
*/ */
public Vektor vectorToEntity(Entity e) { 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) { if (v.x == 0 && v.y == 0) {
return new Vektor(0.0000001f, 0.0000001f); return new Vektor(0.0000001f, 0.0000001f);
} }
@@ -152,6 +171,7 @@ public abstract class Entity implements Ticker {
/** /**
* Gibt den velX Wert zurück. * Gibt den velX Wert zurück.
*
* @return gibt den velX Wert als float zurück. * @return gibt den velX Wert als float zurück.
*/ */
public float getVelX() { public float getVelX() {
@@ -160,6 +180,7 @@ public abstract class Entity implements Ticker {
/** /**
* Gibt den velY Wert zurück. * Gibt den velY Wert zurück.
*
* @return gibt den velY Wert als float zurück. * @return gibt den velY Wert als float zurück.
*/ */
public float getVelY() { public float getVelY() {
@@ -168,6 +189,7 @@ public abstract class Entity implements Ticker {
/** /**
* Setzt eine neue velX velocity. * Setzt eine neue velX velocity.
*
* @param velX neuer float velX Wert. * @param velX neuer float velX Wert.
*/ */
public void setVelX(float velX) { public void setVelX(float velX) {
@@ -176,6 +198,7 @@ public abstract class Entity implements Ticker {
/** /**
* Setzt eine neue Y velocity. * Setzt eine neue Y velocity.
*
* @param velY neuer float velY Wert. * @param velY neuer float velY Wert.
*/ */
public void setVelY(float velY) { public void setVelY(float velY) {
@@ -202,4 +225,12 @@ public abstract class Entity implements Ticker {
public boolean isReadyToDelete() { public boolean isReadyToDelete() {
return deleteEntity; return deleteEntity;
} }
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
} }

View File

@@ -5,6 +5,7 @@ import java.awt.Color;
import ea.ActionFigur; import ea.ActionFigur;
import ea.Figur; import ea.Figur;
import ea.internal.gra.PixelFeld; import ea.internal.gra.PixelFeld;
import main.Main;
import main.worlds.World; import main.worlds.World;
/* /*
@@ -68,6 +69,9 @@ public abstract class LivingEntity extends Entity {
} }
} }
checkTileCollision(Main.instance.getWorld().getCurrentMap());
checkEntityCollision(Main.instance.getWorld().getCurrentMap());
// Packt das Sprite an die richtige Stelle // Packt das Sprite an die richtige Stelle
actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR)); actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR));
float offsetX = width / 2 + spriteOffsetX - width / 2; float offsetX = width / 2 + spriteOffsetX - width / 2;

View File

@@ -68,7 +68,6 @@ public class Snake extends LivingEntity {
zustandSetzen("walk"); zustandSetzen("walk");
} }
} }
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
super.update(); super.update();
} }

View File

@@ -56,7 +56,6 @@ public class Spider extends LivingEntity {
zustandSetzen("walk"); zustandSetzen("walk");
} }
} }
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
super.update(); super.update();
} }

View File

@@ -83,9 +83,6 @@ public class Player extends LivingEntity {
} }
onlyAttackOnceTrigger = false; onlyAttackOnceTrigger = false;
} }
// auf Kollisionen prüfen
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
// LivingEntity auch updaten lassen // LivingEntity auch updaten lassen
super.update(); super.update();
} else { } else {
@@ -93,9 +90,6 @@ public class Player extends LivingEntity {
velY = 0; velY = 0;
} }
// auf Kollisionen prüfen
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) { if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) {
if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe") if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe")
&& actionFigur.aktuelleFigur().aktuellesBild() == 1) { && actionFigur.aktuelleFigur().aktuellesBild() == 1) {

View File

@@ -11,7 +11,11 @@ public class TestWorld extends World {
Map m2 = maps[3].clone(); Map m2 = maps[3].clone();
getCurrentMap().connectDoors(getCurrentMap().leftDoor, m2.leftDoor); 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);
}
} }
} }