collision detection (CLEAN) working
This commit is contained in:
@@ -8,7 +8,7 @@ public abstract class Entity implements Ticker {
|
|||||||
protected float velX, velY;
|
protected float velX, velY;
|
||||||
protected float accelleration = 0.012f;
|
protected float accelleration = 0.012f;
|
||||||
protected float friction = 0.2f;
|
protected float friction = 0.2f;
|
||||||
protected float width, height;
|
protected float width = 1f, height = 1f;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
@@ -27,4 +27,57 @@ public abstract class Entity implements Ticker {
|
|||||||
|
|
||||||
protected abstract void update();
|
protected abstract void update();
|
||||||
|
|
||||||
|
public void checkTileCollisions(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) {
|
||||||
|
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);
|
||||||
|
float distW = Math.abs((x - width / 2) - posX);
|
||||||
|
if (distN <= distE && distN < distS && distN < distW) {
|
||||||
|
onBlockCollision(new boolean[] { true, false, false, false }, x, y);
|
||||||
|
} else if (distE <= distS && distE < distN && distE < distW) {
|
||||||
|
onBlockCollision(new boolean[] { false, true, false, false }, x, y);
|
||||||
|
} else if (distS <= distW && distS < distN && distS < distE) {
|
||||||
|
onBlockCollision(new boolean[] { false, false, true, false }, x, y);
|
||||||
|
} else if (distW <= distS && distW < distN && distW < distE) {
|
||||||
|
onBlockCollision(new boolean[] { false, false, false, true }, x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onBlockCollision(boolean[] sides, int blockX, int blockY) {
|
||||||
|
if (sides[0]) {
|
||||||
|
if (velY < 0) {
|
||||||
|
velY = 0;
|
||||||
|
}
|
||||||
|
posY = blockY + 1 + height / 2;
|
||||||
|
}
|
||||||
|
if (sides[1]) {
|
||||||
|
if (velX < 0) {
|
||||||
|
velX = 0;
|
||||||
|
}
|
||||||
|
posX = blockX + 1 + width / 2;
|
||||||
|
}
|
||||||
|
if (sides[2]) {
|
||||||
|
if (velY > 0) {
|
||||||
|
velY = 0;
|
||||||
|
}
|
||||||
|
posY = blockY - height / 2;
|
||||||
|
}
|
||||||
|
if (sides[3]) {
|
||||||
|
if (velX > 0) {
|
||||||
|
velX = 0;
|
||||||
|
}
|
||||||
|
posX = blockX - width / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Graphics2D;
|
|
||||||
|
|
||||||
import ea.ActionFigur;
|
import ea.ActionFigur;
|
||||||
import ea.BoundingRechteck;
|
|
||||||
import ea.Figur;
|
import ea.Figur;
|
||||||
|
|
||||||
public class LivingEntity extends Entity {
|
public class LivingEntity extends Entity {
|
||||||
@@ -14,15 +10,7 @@ public class LivingEntity extends Entity {
|
|||||||
protected float spriteOffsetX, spriteOffsetY;
|
protected float spriteOffsetX, spriteOffsetY;
|
||||||
|
|
||||||
public LivingEntity(Figur figur, String name) {
|
public LivingEntity(Figur figur, String name) {
|
||||||
actionFigur = new ActionFigur(figur, name) {
|
actionFigur = new ActionFigur(figur, name);
|
||||||
@Override
|
|
||||||
public void zeichnen(Graphics2D g, BoundingRechteck r) {
|
|
||||||
g.setColor(Color.GREEN);
|
|
||||||
g.drawRect((int) actionFigur.positionX(), (int) actionFigur.positionY(), (int) actionFigur.getBreite(), (int) actionFigur.getHoehe());
|
|
||||||
g.drawRect((int) (posX * Tile.getSize()), (int) (posY * Tile.getSize()), (int) (width * Tile.getSize()), (int) (height * Tile.getSize()));
|
|
||||||
super.zeichnen(g, r);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ public class Player extends LivingEntity {
|
|||||||
width = 0.7f;
|
width = 0.7f;
|
||||||
height = 0.8f;
|
height = 0.8f;
|
||||||
spriteOffsetY = -0.14f;
|
spriteOffsetY = -0.14f;
|
||||||
|
posX = 4f;
|
||||||
|
posY = 4f;
|
||||||
|
|
||||||
loader.getPlayer(0).animationsGeschwindigkeitSetzen(200);
|
loader.getPlayer(0).animationsGeschwindigkeitSetzen(200);
|
||||||
loader.getPlayer(1).animationsGeschwindigkeitSetzen(50);
|
loader.getPlayer(1).animationsGeschwindigkeitSetzen(50);
|
||||||
@@ -27,6 +29,7 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update() {
|
protected void update() {
|
||||||
|
System.out.println(posX);
|
||||||
if (!((actionFigur.aktuellesVerhalten().equals("strike"))
|
if (!((actionFigur.aktuellesVerhalten().equals("strike"))
|
||||||
&& actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) {
|
&& actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) {
|
||||||
if (!((actionFigur.aktuellesVerhalten().equals("swipe"))
|
if (!((actionFigur.aktuellesVerhalten().equals("swipe"))
|
||||||
@@ -56,6 +59,7 @@ public class Player extends LivingEntity {
|
|||||||
zustandSetzen("strike");
|
zustandSetzen("strike");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
checkTileCollisions(main.getWorld().getCurrentMap());
|
||||||
super.update();
|
super.update();
|
||||||
} else {
|
} else {
|
||||||
velX = 0;
|
velX = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user