Entity vs Entity collision
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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.
|
||||||
*/
|
*/
|
||||||
@@ -104,32 +122,33 @@ public abstract class Entity implements Ticker {
|
|||||||
posX = blockX - width / 2;
|
posX = blockX - width / 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return die entifernung zu diesem Entity
|
* @return die entifernung zu diesem Entity
|
||||||
*/
|
*/
|
||||||
public float dist(Entity e) {
|
public float dist(Entity e) {
|
||||||
return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY));
|
return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return die entifernung zu dieser Koordinate
|
* @return die entifernung zu dieser Koordinate
|
||||||
*/
|
*/
|
||||||
public float dist(float x, float y) {
|
public float dist(float x, float y) {
|
||||||
return (float) Math.sqrt((x - posX) * (x - posX) + (y - posY) * (y - posY));
|
return (float) Math.sqrt((x - posX) * (x - posX) + (y - posY) * (y - posY));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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);
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean lineOfSightClear(Entity e) {
|
protected boolean lineOfSightClear(Entity e) {
|
||||||
Vektor direct = vectorToEntity(e).normiert();
|
Vektor direct = vectorToEntity(e).normiert();
|
||||||
direct = direct.teilen(4f);
|
direct = direct.teilen(4f);
|
||||||
@@ -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,30 +198,39 @@ 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) {
|
||||||
this.velY = velY;
|
this.velY = velY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosX(float x) {
|
public void setPosX(float x) {
|
||||||
posX = x;
|
posX = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosY(float y) {
|
public void setPosY(float y) {
|
||||||
posY = y;
|
posY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPos(float x, float y) {
|
public void setPos(float x, float y) {
|
||||||
posX = x;
|
posX = x;
|
||||||
posY = y;
|
posY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteEntity() {
|
public void deleteEntity() {
|
||||||
deleteEntity = true;
|
deleteEntity = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isReadyToDelete() {
|
public boolean isReadyToDelete() {
|
||||||
return deleteEntity;
|
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.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;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -67,6 +68,9 @@ public abstract class LivingEntity extends Entity {
|
|||||||
deleteEntity();
|
deleteEntity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ public class Snake extends LivingEntity {
|
|||||||
zustandSetzen("walk");
|
zustandSetzen("walk");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
|
||||||
super.update();
|
super.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ public class Spider extends LivingEntity {
|
|||||||
zustandSetzen("walk");
|
zustandSetzen("walk");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
|
||||||
super.update();
|
super.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -7,11 +7,15 @@ public class TestWorld extends World {
|
|||||||
|
|
||||||
public TestWorld() {
|
public TestWorld() {
|
||||||
super(4);
|
super(4);
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user