From a5e222a02f04fbe34c2e269db42fb3c3c69be8bb Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Sun, 3 Jan 2021 23:57:21 +0100 Subject: [PATCH] pythagoras is saving our ass --- core/src/com/trs/main/Enemy.java | 7 +++++-- core/src/com/trs/main/FightScreen.java | 26 +++++++++++++------------- core/src/com/trs/main/Hostile.java | 2 +- core/src/com/trs/main/MovingNpc.java | 2 +- core/src/com/trs/main/StaticMath.java | 25 +++++-------------------- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/core/src/com/trs/main/Enemy.java b/core/src/com/trs/main/Enemy.java index 2f962d6..0cfb1db 100644 --- a/core/src/com/trs/main/Enemy.java +++ b/core/src/com/trs/main/Enemy.java @@ -15,6 +15,9 @@ public class Enemy extends FightObject{ public void act(FightPlayer player){ if(POI == null && !move){ double distance = StaticMath.calculateDistance(x, y, player.x, player.y); + System.out.println("PLayer pos " + player.x + " " + player.y); + System.out.println("Meine pos " + x + " " + y); + System.out.println("ich bin " + isMelee + "mit Distanz " + distance); if(isMelee) { if(distance <= 32f) { @@ -59,10 +62,10 @@ public class Enemy extends FightObject{ float deltaY = player.y - y; if(Math.abs(deltaX) >= Math.abs(deltaY)) { - tempX += -(deltaX / Math.abs(deltaX)) * 32; + tempX -= (deltaX / Math.abs(deltaX)) * 32; } else { - tempY += -(deltaY / Math.abs(deltaY)) * 32; + tempY -= (deltaY / Math.abs(deltaY)) * 32; } POI = new Vector2(tempX, tempY); diff --git a/core/src/com/trs/main/FightScreen.java b/core/src/com/trs/main/FightScreen.java index 8edc7c5..073242e 100644 --- a/core/src/com/trs/main/FightScreen.java +++ b/core/src/com/trs/main/FightScreen.java @@ -15,14 +15,14 @@ public class FightScreen { final int gridHeight = 12; Batch batch; - ShapeRenderer renderer; + ShapeRenderer renderer; FightObject[] objects; Rectangle[] collisionRects; - FightDialogue fightDialogue; - - Vector2 gridPos; - + FightDialogue fightDialogue; + + Vector2 gridPos; + // 0: positioning all Objects on the grid 1: player turn, 2: enemy turn, 3: fight ends int state = 0; @@ -30,14 +30,14 @@ public class FightScreen { this.batch = batch; this.objects = objects; this.collisionRects = collisionRects; - this.renderer = new ShapeRenderer(); - this.fightDialogue = new FightDialogue(camX, camY); - - gridPos = new Vector2(); - - gridPos.x = (float)(Math.ceil((double)(camX-Main.CAMERA_WIDTH/2)/32.0) * 32.0) + 64; - gridPos.y = (float)(Math.ceil((double)(camY-Main.CAMERA_HEIGHT/2)/32.0) * 32.0) + 32; - + this.renderer = new ShapeRenderer(); + this.fightDialogue = new FightDialogue(camX, camY); + + gridPos = new Vector2(); + + gridPos.x = (float)(Math.ceil((double)(camX-Main.CAMERA_WIDTH/2)/32.0) * 32.0) + 64; + gridPos.y = (float)(Math.ceil((double)(camY-Main.CAMERA_HEIGHT/2)/32.0) * 32.0) + 32; + // SORTING OBJECTS BY INITIATIVE STAT diff --git a/core/src/com/trs/main/Hostile.java b/core/src/com/trs/main/Hostile.java index 7299dff..d5249bf 100644 --- a/core/src/com/trs/main/Hostile.java +++ b/core/src/com/trs/main/Hostile.java @@ -85,7 +85,7 @@ public class Hostile extends Actor { facing = 3; } - if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y, movement.angleRad()) < 1f) { + if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 1f) { movement.x = 0; movement.y = 0; } diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/MovingNpc.java index dca1c73..19cefe8 100644 --- a/core/src/com/trs/main/MovingNpc.java +++ b/core/src/com/trs/main/MovingNpc.java @@ -153,7 +153,7 @@ public class MovingNpc extends Actor{ facing = 3; } - if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y, movement.angleRad()) < 10f) { + if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 10f) { movementX = 0; movementY = 0; } diff --git a/core/src/com/trs/main/StaticMath.java b/core/src/com/trs/main/StaticMath.java index 0ed3005..d91fc60 100644 --- a/core/src/com/trs/main/StaticMath.java +++ b/core/src/com/trs/main/StaticMath.java @@ -42,36 +42,21 @@ public class StaticMath { return (float) alpha; } - public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2, float angle){ - float deltaX = xPos2 - xPos1; - float deltaY = yPos2 - yPos1; - - double distance; - - if(angle == 0) { - distance = deltaX; - } - else { - distance = Math.abs((deltaY / Math.sin(angle))); - } - - return (float) distance; - } - public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2){ float deltaX = xPos2 - xPos1; float deltaY = yPos2 - yPos1; - double distance; + float distance; double angle = calculateAngle(xPos1, yPos1, xPos2, yPos2); - if(angle == 0) { + if(angle == 0 || angle == Math.PI) { distance = deltaX; } else { - distance = Math.abs((deltaY / Math.sin(angle))); + distance = Math.abs(deltaX / (float) Math.cos(angle)); } - return (float) distance; + return (float) Math.sqrt((float) Math.pow(deltaX, 2) + (float) Math.pow(deltaY, 2)); + //return distance; } }