pythagoras is saving our ass

master
Jonathan Hager 5 years ago
parent 85164681f2
commit a5e222a02f

@ -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);

@ -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

@ -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;
}

@ -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;
}

@ -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;
}
}

Loading…
Cancel
Save