From d5c8e7fa42efb31f88be014848613bf337e6d5c0 Mon Sep 17 00:00:00 2001 From: Asecave Date: Tue, 22 Jun 2021 18:12:12 +0200 Subject: [PATCH] collision detection (CLEAN) working --- Zoelda/src/main/Entity.java | 55 ++++++++++++++++++++++++++++++- Zoelda/src/main/LivingEntity.java | 14 +------- Zoelda/src/main/Player.java | 4 +++ 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/Zoelda/src/main/Entity.java b/Zoelda/src/main/Entity.java index 42af625..961b17e 100644 --- a/Zoelda/src/main/Entity.java +++ b/Zoelda/src/main/Entity.java @@ -8,7 +8,7 @@ public abstract class Entity implements Ticker { protected float velX, velY; protected float accelleration = 0.012f; protected float friction = 0.2f; - protected float width, height; + protected float width = 1f, height = 1f; @Override public void tick() { @@ -27,4 +27,57 @@ public abstract class Entity implements Ticker { 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; + } + } + } diff --git a/Zoelda/src/main/LivingEntity.java b/Zoelda/src/main/LivingEntity.java index 9b6a55e..fb5b5ee 100644 --- a/Zoelda/src/main/LivingEntity.java +++ b/Zoelda/src/main/LivingEntity.java @@ -1,10 +1,6 @@ package main; -import java.awt.Color; -import java.awt.Graphics2D; - import ea.ActionFigur; -import ea.BoundingRechteck; import ea.Figur; public class LivingEntity extends Entity { @@ -14,15 +10,7 @@ public class LivingEntity extends Entity { protected float spriteOffsetX, spriteOffsetY; public LivingEntity(Figur figur, String 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); - } - }; + actionFigur = new ActionFigur(figur, name); } @Override diff --git a/Zoelda/src/main/Player.java b/Zoelda/src/main/Player.java index d33c361..febec2f 100644 --- a/Zoelda/src/main/Player.java +++ b/Zoelda/src/main/Player.java @@ -14,6 +14,8 @@ public class Player extends LivingEntity { width = 0.7f; height = 0.8f; spriteOffsetY = -0.14f; + posX = 4f; + posY = 4f; loader.getPlayer(0).animationsGeschwindigkeitSetzen(200); loader.getPlayer(1).animationsGeschwindigkeitSetzen(50); @@ -27,6 +29,7 @@ public class Player extends LivingEntity { @Override protected void update() { + System.out.println(posX); if (!((actionFigur.aktuellesVerhalten().equals("strike")) && actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) { if (!((actionFigur.aktuellesVerhalten().equals("swipe")) @@ -56,6 +59,7 @@ public class Player extends LivingEntity { zustandSetzen("strike"); } } + checkTileCollisions(main.getWorld().getCurrentMap()); super.update(); } else { velX = 0;