Files
Zoelda/Zoelda/src/main/entities/LivingEntity.java
2021-06-25 22:57:31 +02:00

82 lines
2.5 KiB
Java

package main.entities;
import ea.ActionFigur;
import ea.Figur;
import main.World;
/*
* Alles was zustände hat und sich Bewegen kann, ist ein LivingEntity.
*/
public abstract class LivingEntity extends Entity {
protected boolean side; // true = gespiegelt, false = nicht
public ActionFigur actionFigur; // Sprite des Entities
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
*/
public LivingEntity(Figur figur, String name) {
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
protected void update() {
// Prüft zu welcher seite man guckt
if (velX < 0) {
side = true;
} else if (velX > 0) {
side = false;
}
if (hp <= 0) {
actionFigur.zustandSetzen(getDeathAnimationName());
velX = 0;
velY = 0;
if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
deleteEntity();
}
}
// Packt das Sprite an die richtige Stelle
actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR));
float offsetX = width / 2 + spriteOffsetX - width / 2;
float offsetY = height / 2 + spriteOffsetY - height / 2;
actionFigur.positionSetzen((posX + offsetX) * World.SCALE - actionFigur.getBreite() / 2, (posY + offsetY) * World.SCALE - actionFigur.getHoehe() / 2);
}
/**
* Spiegelt die figur autmatisch, wenn nötig.
*/
protected void zustandSetzen(String name) {
actionFigur.spiegelXSetzen(mirrored ? !side : side);
actionFigur.zustandSetzen(name);
}
protected float getHealthPoints() {
return hp;
}
public void takeDamage(float damage, Entity e) {
actionFigur.zustandSetzen(getDamageAnimationName());
hp -= damage;
}
public abstract String getDeathAnimationName();
public abstract String getDamageAnimationName();
}