Snake following player

This commit is contained in:
Asecave
2021-06-23 22:32:50 +02:00
parent 67a427e2d0
commit a6ea7a5a39
4 changed files with 52 additions and 16 deletions

View File

@@ -4,9 +4,9 @@ import ea.Knoten;
import main.entities.Entity;
import main.entities.Player;
import main.entities.Snake;
import main.maps.Corridor;
import main.entities.Spider;
import main.maps.Map;
import main.maps.TestMap;
/**
* Hier werden alle Maps gespeichert.
@@ -21,8 +21,8 @@ public class World extends Knoten {
public World() {
// Map initialisieren
currentMap = new Corridor();
// Map zu EA hinzuf<EFBFBD>gen
currentMap = new TestMap();
// Map zu EA hinzufügen
add(currentMap);
// und Entities auch

View File

@@ -1,10 +1,6 @@
package main.entities;
import java.awt.Color;
import java.awt.Graphics2D;
import ea.ActionFigur;
import ea.BoundingRechteck;
import ea.Figur;
import main.World;
@@ -18,6 +14,7 @@ public class LivingEntity extends Entity {
protected float spriteScale = 1f; // Skalierung des Sprites
protected float spriteOffsetX, spriteOffsetY; // Offset des Sprites. Hier kann man die relative render-Position nachjustieren.
protected float hp = 1f; //hp des Entitys
protected boolean mirrored;
/**
* @param figur - erstes (standart) Sprite
* @param name - name des Zustands
@@ -26,13 +23,13 @@ public class LivingEntity extends Entity {
actionFigur = new ActionFigur(figur, name) {
// DEBUG: render boxes
@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 - width / 2) * World.SCALE), (int) ((posY - height / 2) * World.SCALE), (int) (width * World.SCALE), (int) (height * World.SCALE));
super.zeichnen(g, r);
}
// @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 - width / 2) * World.SCALE), (int) ((posY - height / 2) * World.SCALE), (int) (width * World.SCALE), (int) (height * World.SCALE));
// super.zeichnen(g, r);
// }
};
}
@@ -56,7 +53,7 @@ public class LivingEntity extends Entity {
* Spiegelt die figur autmatisch, wenn nötig.
*/
protected void zustandSetzen(String name) {
actionFigur.spiegelXSetzen(side);
actionFigur.spiegelXSetzen(mirrored ? !side : side);
actionFigur.zustandSetzen(name);
}

View File

@@ -5,6 +5,7 @@ import java.util.ArrayList;
import ea.Vektor;
import main.Main;
import main.SheetLoader;
import main.Tile;
public class Snake extends LivingEntity {
@@ -16,6 +17,15 @@ public class Snake extends LivingEntity {
posX = 9f;
posY = 9f;
accelleration = 0.008f;
spriteOffsetY = -0.6f;
width = 0.65f;
height = 0.8f;
mirrored = true;
actionFigur.neuerZustand(loader.getFigur(1), "lost_sight");
actionFigur.neuerZustand(loader.getFigur(2), "walk");
actionFigur.neuerZustand(loader.getFigur(3), "attack");
actionFigur.neuerZustand(loader.getFigur(4), "die");
}
@Override
@@ -29,13 +39,42 @@ public class Snake extends LivingEntity {
}
}
}
if (nearestPlayer != null) {
if (nearestPlayer != null && lineOfSightClear(nearestPlayer)) {
Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY);
toPlayer = toPlayer.normiert();
velX += toPlayer.x * accelleration;
velY += toPlayer.y * accelleration;
zustandSetzen("walk");
} else {
if (actionFigur.aktuellesVerhalten().equals("walk")) {
zustandSetzen("lost_sight");
}
if (actionFigur.aktuellesVerhalten().equals("lost_sight") && actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
actionFigur.aktuelleFigur().animationsBildSetzen(0);
zustandSetzen("idle");
}
}
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
super.update();
}
private boolean lineOfSightClear(Entity e) {
Vektor direct = new Vektor(e.posX - posX, e.posY - posY).normiert();
direct = direct.teilen(4f);
Vektor loc = new Vektor(posX, posY);
float lastDist;
float dist = Float.MAX_VALUE;
boolean clear = true;
do {
loc = loc.summe(direct);
lastDist = dist;
dist = Math.abs(e.posX - loc.x) + Math.abs(e.posY - loc.y);
Tile t = Main.instance.getWorld().getCurrentMap().getTile((int) loc.x, (int) loc.y);
if (t != null && t.isCollidable()) {
clear = false;
break;
}
} while (dist < lastDist);
return clear;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB