new Entity help methods
line of sight and vector to entity
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package main.entities;
|
package main.entities;
|
||||||
|
|
||||||
import ea.Ticker;
|
import ea.Ticker;
|
||||||
|
import ea.Vektor;
|
||||||
|
import main.Main;
|
||||||
|
import main.Tile;
|
||||||
import main.maps.Map;
|
import main.maps.Map;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -107,5 +110,32 @@ public abstract class Entity implements Ticker {
|
|||||||
public float dist(Entity e) {
|
public float dist(Entity e) {
|
||||||
return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY));
|
return (float) Math.sqrt((e.posX - posX) * (e.posX - posX) + (e.posY - posY) * (e.posY - posY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generiert einen vektor von diesm Entity zu einem anderen
|
||||||
|
*/
|
||||||
|
public Vektor vectorToEntity(Entity e) {
|
||||||
|
return new Vektor(e.posX - posX, e.posY - posY);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean lineOfSightClear(Entity e) {
|
||||||
|
Vektor direct = vectorToEntity(e).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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import main.World;
|
|||||||
/*
|
/*
|
||||||
* Alles was zustände hat und sich Bewegen kann, ist ein LivingEntity.
|
* Alles was zustände hat und sich Bewegen kann, ist ein LivingEntity.
|
||||||
*/
|
*/
|
||||||
public class LivingEntity extends Entity {
|
public abstract class LivingEntity extends Entity {
|
||||||
|
|
||||||
protected boolean side; // true = gespiegelt, false = nicht
|
protected boolean side; // true = gespiegelt, false = nicht
|
||||||
public ActionFigur actionFigur; // Sprite des Entities
|
public ActionFigur actionFigur; // Sprite des Entities
|
||||||
@@ -60,4 +60,8 @@ public class LivingEntity extends Entity {
|
|||||||
protected float getHealthPoints() {
|
protected float getHealthPoints() {
|
||||||
return hp;
|
return hp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void takeDamage(float damage) {
|
||||||
|
hp -= damage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package main.entities;
|
package main.entities;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import ea.Taste;
|
import ea.Taste;
|
||||||
import main.Main;
|
import main.Main;
|
||||||
import main.SheetLoader;
|
import main.SheetLoader;
|
||||||
@@ -73,6 +75,15 @@ public class Player extends LivingEntity {
|
|||||||
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
||||||
zustandSetzen("die");
|
zustandSetzen("die");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) {
|
||||||
|
ArrayList<Entity> entities = Main.instance.getWorld().getCurrentMap().getEntities();
|
||||||
|
for (Entity e : entities) {
|
||||||
|
if (e instanceof LivingEntity) {
|
||||||
|
((LivingEntity) e).takeDamage(0.1f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// auf Kollisionen prüfen
|
// auf Kollisionen prüfen
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ 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,
|
private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32,
|
||||||
new int[] { 10, 10, 10, 10, 10 });
|
new int[] { 10, 10, 10, 10, 10, 8});
|
||||||
|
|
||||||
public Snake() {
|
public Snake() {
|
||||||
super(loader.getFigur(0), "idle");
|
super(loader.getFigur(0), "idle");
|
||||||
@@ -27,6 +27,7 @@ public class Snake extends LivingEntity {
|
|||||||
actionFigur.neuerZustand(loader.getFigur(2), "walk");
|
actionFigur.neuerZustand(loader.getFigur(2), "walk");
|
||||||
actionFigur.neuerZustand(loader.getFigur(3), "attack");
|
actionFigur.neuerZustand(loader.getFigur(3), "attack");
|
||||||
actionFigur.neuerZustand(loader.getFigur(4), "die");
|
actionFigur.neuerZustand(loader.getFigur(4), "die");
|
||||||
|
actionFigur.neuerZustand(loader.getFigur(5), "damage");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -44,16 +45,16 @@ public class Snake extends LivingEntity {
|
|||||||
if (dist(nearestPlayer) < 1f) {
|
if (dist(nearestPlayer) < 1f) {
|
||||||
zustandSetzen("attack");
|
zustandSetzen("attack");
|
||||||
} else {
|
} else {
|
||||||
|
if (actionFigur.aktuellesVerhalten().equals("attack")) {
|
||||||
|
actionFigur.aktuelleFigur().animationsBildSetzen(0);
|
||||||
|
}
|
||||||
Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY);
|
Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY);
|
||||||
toPlayer = toPlayer.normiert();
|
toPlayer = toPlayer.normiert();
|
||||||
velX += toPlayer.x * accelleration;
|
velX += toPlayer.x * accelleration;
|
||||||
velY += toPlayer.y * accelleration;
|
velY += toPlayer.y * accelleration;
|
||||||
zustandSetzen("walk");
|
zustandSetzen("walk");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
} else
|
|
||||||
|
|
||||||
{
|
|
||||||
if (actionFigur.aktuellesVerhalten().equals("walk")) {
|
if (actionFigur.aktuellesVerhalten().equals("walk")) {
|
||||||
zustandSetzen("lost_sight");
|
zustandSetzen("lost_sight");
|
||||||
}
|
}
|
||||||
@@ -66,24 +67,4 @@ public class Snake extends LivingEntity {
|
|||||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||||
super.update();
|
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: 3.0 KiB After Width: | Height: | Size: 2.3 KiB |
Reference in New Issue
Block a user