fixing snake
This commit is contained in:
@@ -12,11 +12,8 @@ public class Main extends Game {
|
||||
private World world;
|
||||
private HUD hud;
|
||||
|
||||
public static final int WIDTH = 1000; // Fensterbreite
|
||||
public static final int HEIGHT = 800; // Fensterhöhe
|
||||
|
||||
public Main() {
|
||||
super(WIDTH, HEIGHT);
|
||||
super(World.SCALE * 15, World.SCALE * 11);
|
||||
instance = this;
|
||||
|
||||
// Welt initialisieren und Spieler hinzufügen
|
||||
|
||||
@@ -2,15 +2,18 @@ package main;
|
||||
|
||||
import ea.Knoten;
|
||||
import main.entities.Entity;
|
||||
import main.maps.Corridor;
|
||||
import main.entities.Snake;
|
||||
import main.entities.Spider;
|
||||
import main.entities.player.Player;
|
||||
import main.maps.Map;
|
||||
import main.maps.TestMap;
|
||||
|
||||
/**
|
||||
* Hier werden alle Maps gespeichert.
|
||||
*/
|
||||
public class World extends Knoten {
|
||||
|
||||
public static final int SCALE_FACTOR = 4; // Der Basis Zoomfaktor
|
||||
public static final int SCALE_FACTOR = 6; // Der Basis Zoomfaktor
|
||||
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.
|
||||
@@ -18,11 +21,24 @@ public class World extends Knoten {
|
||||
public World() {
|
||||
|
||||
// Map initialisieren
|
||||
currentMap = new Corridor();
|
||||
currentMap = new TestMap();
|
||||
// Map zu EA hinzufügen
|
||||
add(currentMap);
|
||||
|
||||
// und Entities auch
|
||||
Player player = new Player();
|
||||
add(player.actionFigur);
|
||||
currentMap.getEntities().add(player);
|
||||
currentMap.setPlayer(player);
|
||||
|
||||
Spider spider = new Spider();
|
||||
// und Spinnen auch
|
||||
add(spider.actionFigur);
|
||||
currentMap.getEntities().add(spider);
|
||||
|
||||
Snake snake = new Snake();
|
||||
add(snake.actionFigur);
|
||||
currentMap.getEntities().add(snake);
|
||||
|
||||
// Alle Entities als ticker registrieren (triggert dann die update methoden)
|
||||
for (Entity e : currentMap.getEntities()) {
|
||||
|
||||
@@ -17,6 +17,7 @@ public abstract class Entity implements Ticker {
|
||||
protected float accelleration = 0.012f; // Beschleunigung des entities in gameunits pro frame.
|
||||
protected float friction = 0.2f; // Entschleunigung des entities in gameunits pro frame.
|
||||
protected float width = 1f, height = 1f; // Breite und Höhe der Hitbox.
|
||||
private boolean deleteEntity; // Wenn true wird dieses Entity von der map gelöscht
|
||||
|
||||
// Das ist die Ticker-Methode von ea; wird jeden frame ausgeführt (frameloop)
|
||||
@Override
|
||||
@@ -169,4 +170,12 @@ public abstract class Entity implements Ticker {
|
||||
public void setVelY(float velY) {
|
||||
this.velY = velY;
|
||||
}
|
||||
|
||||
public void deleteEntity() {
|
||||
deleteEntity = true;
|
||||
}
|
||||
|
||||
public boolean isReadyToDelete() {
|
||||
return deleteEntity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ public abstract class LivingEntity extends Entity {
|
||||
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
|
||||
@@ -42,6 +43,16 @@ public abstract class LivingEntity extends Entity {
|
||||
side = false;
|
||||
}
|
||||
|
||||
if (hp <= 0) {
|
||||
actionFigur.zustandSetzen(getDeathAnimationName());
|
||||
velX = 0;
|
||||
velY = 0;
|
||||
if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
|
||||
System.out.println("adasd");
|
||||
deleteEntity();
|
||||
}
|
||||
}
|
||||
|
||||
// Packt das Sprite an die richtige Stelle
|
||||
actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR));
|
||||
float offsetX = width / 2 + spriteOffsetX - width / 2;
|
||||
@@ -61,7 +72,11 @@ public abstract class LivingEntity extends Entity {
|
||||
return hp;
|
||||
}
|
||||
|
||||
public void takeDamage(float damage) {
|
||||
public void takeDamage(float damage, Entity e) {
|
||||
actionFigur.zustandSetzen(getDamageAnimationName());
|
||||
hp -= damage;
|
||||
}
|
||||
|
||||
public abstract String getDeathAnimationName();
|
||||
public abstract String getDamageAnimationName();
|
||||
}
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package main.entities;
|
||||
|
||||
import ea.Vektor;
|
||||
import main.Main;
|
||||
import main.SheetLoader;
|
||||
import main.entities.player.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
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, 8 });
|
||||
private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32, new int[] { 10, 10, 10, 10, 13, 8 });
|
||||
|
||||
public Snake() {
|
||||
super(loader.getFigur(0), "idle");
|
||||
@@ -26,54 +22,28 @@ public class Snake extends LivingEntity {
|
||||
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");
|
||||
actionFigur.neuerZustand(loader.getFigur(5), "damage");
|
||||
actionFigur.neuerZustand(loader.getFigur(4), getDeathAnimationName());
|
||||
actionFigur.neuerZustand(loader.getFigur(5), getDamageAnimationName());
|
||||
|
||||
loader.getFigur(3).animationsGeschwindigkeitSetzen(80);
|
||||
loader.getFigur(5).animationsGeschwindigkeitSetzen(40);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void update() {
|
||||
ArrayList<Entity> entities = Main.instance.getWorld().getCurrentMap().getEntities();
|
||||
Entity nearestPlayer = null;
|
||||
for (Entity e : entities) {
|
||||
if (e instanceof Player) {
|
||||
if (nearestPlayer == null || e.dist(this) < nearestPlayer.dist(this)) {
|
||||
nearestPlayer = e;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nearestPlayer != null && lineOfSightClear(nearestPlayer) && !actionFigur.aktuellesVerhalten().equals("damage")) {
|
||||
if (dist(nearestPlayer) < 1f) {
|
||||
zustandSetzen("attack");
|
||||
} else {
|
||||
if (actionFigur.aktuellesVerhalten().equals("attack")) {
|
||||
actionFigur.aktuelleFigur().animationsBildSetzen(0);
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
if (actionFigur.aktuellesVerhalten().equals("damage") && actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1){
|
||||
zustandSetzen("walk");
|
||||
}
|
||||
Player player = Main.instance.getWorld().getCurrentMap().getPlayer();
|
||||
|
||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
super.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeDamage(float damage) {
|
||||
zustandSetzen("damage");
|
||||
super.takeDamage(damage);
|
||||
public String getDeathAnimationName() {
|
||||
return "die";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDamageAnimationName() {
|
||||
return "damage";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,4 +30,14 @@ public class Spider extends LivingEntity {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeathAnimationName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDamageAnimationName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import main.SheetLoader;
|
||||
import main.entities.Entity;
|
||||
import main.entities.LivingEntity;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Player extends LivingEntity {
|
||||
|
||||
private static SheetLoader loader = new SheetLoader("/res/images/player_sprite_sheet.png", 64, 32, new int[] { 5, 8, 7, 6, 2, 5, 4, 7 });
|
||||
private boolean onlyAttackOnceTrigger;
|
||||
|
||||
public Player() {
|
||||
super(loader.getFigur(0), "idle");
|
||||
@@ -35,7 +37,8 @@ public class Player extends LivingEntity {
|
||||
actionFigur.neuerZustand(loader.getFigur(1), "walk");
|
||||
actionFigur.neuerZustand(loader.getFigur(2), "strike");
|
||||
actionFigur.neuerZustand(loader.getFigur(3), "swipe");
|
||||
actionFigur.neuerZustand(loader.getFigur(7), "die");
|
||||
actionFigur.neuerZustand(loader.getFigur(6), getDamageAnimationName());
|
||||
actionFigur.neuerZustand(loader.getFigur(7), getDeathAnimationName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,15 +81,7 @@ public class Player extends LivingEntity {
|
||||
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
onlyAttackOnceTrigger = false;
|
||||
}
|
||||
|
||||
// auf Kollisionen prüfen
|
||||
@@ -101,18 +96,31 @@ public class Player extends LivingEntity {
|
||||
// auf Kollisionen prüfen
|
||||
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
|
||||
if (actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) {
|
||||
if (!onlyAttackOnceTrigger && actionFigur.aktuellesVerhalten().equals("swipe") && actionFigur.aktuelleFigur().aktuellesBild() == 1) {
|
||||
onlyAttackOnceTrigger = true;
|
||||
ArrayList<Entity> entities = Main.instance.getWorld().getCurrentMap().getEntities();
|
||||
for (Entity e : entities) {
|
||||
if (e instanceof LivingEntity && e != this) {
|
||||
if (e instanceof LivingEntity && e != this && e.dist(this) <= 1f) {
|
||||
LivingEntity le = (LivingEntity) e;
|
||||
Vektor toE = vectorToEntity(le);
|
||||
toE = toE.normiert();
|
||||
if ((toE.x > 0 && !side) || (toE.x <= 0 && side)) {
|
||||
le.setVelX(le.getVelX() + toE.x * 0.05f);
|
||||
le.setVelY(le.getVelY()+toE.x * 0.05f);
|
||||
le.takeDamage(0.1f);
|
||||
le.setVelY(le.getVelY() + toE.y * 0.05f);
|
||||
le.takeDamage(0.1f, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeathAnimationName() {
|
||||
return "die";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDamageAnimationName() {
|
||||
return "damage";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,28 @@ package main.maps;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import ea.Knoten;
|
||||
import ea.Ticker;
|
||||
import main.Main;
|
||||
import main.Tile;
|
||||
import main.entities.Entity;
|
||||
import main.entities.LivingEntity;
|
||||
import main.entities.player.Player;
|
||||
|
||||
/**
|
||||
* Auf der Map sind alle Entities, sowie die Tiles gespiechert.
|
||||
*/
|
||||
public abstract class Map extends Knoten {
|
||||
public abstract class Map extends Knoten implements Ticker {
|
||||
|
||||
protected Tile[][] map; // Die Tiles der map in einem 2D Array.
|
||||
private ArrayList<Entity> entities;
|
||||
private Player player;
|
||||
|
||||
public Map(int width, int height) {
|
||||
map = new Tile[width][height];
|
||||
|
||||
entities = new ArrayList<>(100);
|
||||
|
||||
Main.instance.manager.anmelden(this, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,6 +37,19 @@ public abstract class Map extends Knoten {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
for (int i = 0; i < entities.size(); i++) {
|
||||
Entity e = entities.get(i);
|
||||
if (e instanceof LivingEntity) {
|
||||
if (e.isReadyToDelete()) {
|
||||
Main.instance.manager.abmelden(e);
|
||||
entities.remove(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return map.length;
|
||||
}
|
||||
@@ -41,4 +61,12 @@ public abstract class Map extends Knoten {
|
||||
public ArrayList<Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public void setPlayer(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 4.0 KiB |
Reference in New Issue
Block a user