Entity vs Entity collision
This commit is contained in:
parent
a5eed792e7
commit
c035253790
|
@ -1,7 +1,6 @@
|
|||
package main;
|
||||
|
||||
import ea.Game;
|
||||
import ea.Sound;
|
||||
import main.HUD.HUD;
|
||||
import main.worlds.TestWorld;
|
||||
import main.worlds.World;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -68,7 +68,6 @@ public class Snake extends LivingEntity {
|
|||
zustandSetzen("walk");
|
||||
}
|
||||
}
|
||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
super.update();
|
||||
}
|
||||
|
||||
|
|
|
@ -56,7 +56,6 @@ public class Spider extends LivingEntity {
|
|||
zustandSetzen("walk");
|
||||
}
|
||||
}
|
||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
super.update();
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue