corrent dist method

This commit is contained in:
Tim
2021-06-24 10:30:54 +02:00
parent 758c944ec6
commit 8bab8d02ed
5 changed files with 21 additions and 15 deletions

View File

@@ -11,8 +11,8 @@ public class Main extends Game {
private World world; private World world;
public static final int WIDTH = 1440; // Fensterbreite public static final int WIDTH = 1000; // Fensterbreite
public static final int HEIGHT = 1056; // Fensterhöhe public static final int HEIGHT = 800; // Fensterhöhe
public Main() { public Main() {
super(WIDTH, HEIGHT); super(WIDTH, HEIGHT);

View File

@@ -5,7 +5,6 @@ import main.entities.Entity;
import main.entities.Player; import main.entities.Player;
import main.entities.Snake; import main.entities.Snake;
import main.entities.Spider; import main.entities.Spider;
import main.maps.Corridor;
import main.maps.Map; import main.maps.Map;
import main.maps.TestMap; import main.maps.TestMap;
@@ -14,7 +13,7 @@ import main.maps.TestMap;
*/ */
public class World extends Knoten { public class World extends Knoten {
public static final int SCALE_FACTOR = 6; // Der Basis Zoomfaktor public static final int SCALE_FACTOR = 4; // Der Basis Zoomfaktor
public static final int SCALE = SCALE_FACTOR * Tile.getSize(); // Eine Gameunit ist so viele pixel lang public static final int SCALE = SCALE_FACTOR * Tile.getSize(); // Eine Gameunit ist so viele pixel lang
private Map currentMap; // Die Map die aktuell angezeigt werden soll. private Map currentMap; // Die Map die aktuell angezeigt werden soll.

View File

@@ -105,7 +105,7 @@ public abstract class Entity implements Ticker {
* @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 * e.posX + posY * posY); return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY));
} }
} }

View File

@@ -1,6 +1,5 @@
package main.entities; package main.entities;
import ea.Farbe;
import ea.Taste; import ea.Taste;
import main.Main; import main.Main;
import main.SheetLoader; import main.SheetLoader;

View File

@@ -9,7 +9,8 @@ import main.Tile;
public class Snake extends LivingEntity { public class Snake extends LivingEntity {
private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32, new int[] { 10, 10, 10, 10, 10 }); private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32,
new int[] { 10, 10, 10, 10, 10 });
public Snake() { public Snake() {
super(loader.getFigur(0), "idle"); super(loader.getFigur(0), "idle");
@@ -40,17 +41,24 @@ public class Snake extends LivingEntity {
} }
} }
if (nearestPlayer != null && lineOfSightClear(nearestPlayer)) { if (nearestPlayer != null && lineOfSightClear(nearestPlayer)) {
Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY); if (dist(nearestPlayer) < 1f) {
toPlayer = toPlayer.normiert(); zustandSetzen("attack");
velX += toPlayer.x * accelleration; } else {
velY += toPlayer.y * accelleration; Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY);
zustandSetzen("walk"); toPlayer = toPlayer.normiert();
} else { velX += toPlayer.x * accelleration;
velY += toPlayer.y * accelleration;
zustandSetzen("walk");
}
} else
{
if (actionFigur.aktuellesVerhalten().equals("walk")) { if (actionFigur.aktuellesVerhalten().equals("walk")) {
zustandSetzen("lost_sight"); zustandSetzen("lost_sight");
} }
if (actionFigur.aktuellesVerhalten().equals("lost_sight") if (actionFigur.aktuellesVerhalten().equals("lost_sight") && actionFigur.aktuelleFigur()
&& actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) { .aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
actionFigur.aktuelleFigur().animationsBildSetzen(0); actionFigur.aktuelleFigur().animationsBildSetzen(0);
zustandSetzen("idle"); zustandSetzen("idle");
} }