fixing snake
This commit is contained in:
@@ -12,11 +12,8 @@ public class Main extends Game {
|
|||||||
private World world;
|
private World world;
|
||||||
private HUD hud;
|
private HUD hud;
|
||||||
|
|
||||||
public static final int WIDTH = 1000; // Fensterbreite
|
|
||||||
public static final int HEIGHT = 800; // Fensterhöhe
|
|
||||||
|
|
||||||
public Main() {
|
public Main() {
|
||||||
super(WIDTH, HEIGHT);
|
super(World.SCALE * 15, World.SCALE * 11);
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
||||||
// Welt initialisieren und Spieler hinzufügen
|
// Welt initialisieren und Spieler hinzufügen
|
||||||
|
|||||||
@@ -2,15 +2,18 @@ package main;
|
|||||||
|
|
||||||
import ea.Knoten;
|
import ea.Knoten;
|
||||||
import main.entities.Entity;
|
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.Map;
|
||||||
|
import main.maps.TestMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hier werden alle Maps gespeichert.
|
* Hier werden alle Maps gespeichert.
|
||||||
*/
|
*/
|
||||||
public class World extends Knoten {
|
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
|
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.
|
||||||
@@ -18,11 +21,24 @@ public class World extends Knoten {
|
|||||||
public World() {
|
public World() {
|
||||||
|
|
||||||
// Map initialisieren
|
// Map initialisieren
|
||||||
currentMap = new Corridor();
|
currentMap = new TestMap();
|
||||||
// Map zu EA hinzufügen
|
// Map zu EA hinzufügen
|
||||||
add(currentMap);
|
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)
|
// Alle Entities als ticker registrieren (triggert dann die update methoden)
|
||||||
for (Entity e : currentMap.getEntities()) {
|
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 accelleration = 0.012f; // Beschleunigung des entities in gameunits pro frame.
|
||||||
protected float friction = 0.2f; // Entschleunigung 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.
|
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)
|
// Das ist die Ticker-Methode von ea; wird jeden frame ausgeführt (frameloop)
|
||||||
@Override
|
@Override
|
||||||
@@ -169,4 +170,12 @@ public abstract class Entity implements Ticker {
|
|||||||
public void setVelY(float velY) {
|
public void setVelY(float velY) {
|
||||||
this.velY = 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 spriteOffsetX, spriteOffsetY; // Offset des Sprites. Hier kann man die relative render-Position nachjustieren.
|
||||||
protected float hp = 1f; //hp des Entitys
|
protected float hp = 1f; //hp des Entitys
|
||||||
protected boolean mirrored;
|
protected boolean mirrored;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param figur - erstes (standart) Sprite
|
* @param figur - erstes (standart) Sprite
|
||||||
* @param name - name des Zustands
|
* @param name - name des Zustands
|
||||||
@@ -42,6 +43,16 @@ public abstract class LivingEntity extends Entity {
|
|||||||
side = false;
|
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
|
// Packt das Sprite an die richtige Stelle
|
||||||
actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR));
|
actionFigur.faktorSetzen((int) (spriteScale * World.SCALE_FACTOR));
|
||||||
float offsetX = width / 2 + spriteOffsetX - width / 2;
|
float offsetX = width / 2 + spriteOffsetX - width / 2;
|
||||||
@@ -61,7 +72,11 @@ public abstract class LivingEntity extends Entity {
|
|||||||
return hp;
|
return hp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void takeDamage(float damage) {
|
public void takeDamage(float damage, Entity e) {
|
||||||
|
actionFigur.zustandSetzen(getDamageAnimationName());
|
||||||
hp -= damage;
|
hp -= damage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract String getDeathAnimationName();
|
||||||
|
public abstract String getDamageAnimationName();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
package main.entities;
|
package main.entities;
|
||||||
|
|
||||||
import ea.Vektor;
|
|
||||||
import main.Main;
|
import main.Main;
|
||||||
import main.SheetLoader;
|
import main.SheetLoader;
|
||||||
import main.entities.player.Player;
|
import main.entities.player.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
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, 13, 8 });
|
||||||
new int[] { 10, 10, 10, 10, 10, 8 });
|
|
||||||
|
|
||||||
public Snake() {
|
public Snake() {
|
||||||
super(loader.getFigur(0), "idle");
|
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(1), "lost_sight");
|
||||||
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), getDeathAnimationName());
|
||||||
actionFigur.neuerZustand(loader.getFigur(5), "damage");
|
actionFigur.neuerZustand(loader.getFigur(5), getDamageAnimationName());
|
||||||
|
|
||||||
|
loader.getFigur(3).animationsGeschwindigkeitSetzen(80);
|
||||||
|
loader.getFigur(5).animationsGeschwindigkeitSetzen(40);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update() {
|
protected void update() {
|
||||||
ArrayList<Entity> entities = Main.instance.getWorld().getCurrentMap().getEntities();
|
Player player = Main.instance.getWorld().getCurrentMap().getPlayer();
|
||||||
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");
|
|
||||||
}
|
|
||||||
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||||
super.update();
|
super.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void takeDamage(float damage) {
|
public String getDeathAnimationName() {
|
||||||
zustandSetzen("damage");
|
return "die";
|
||||||
super.takeDamage(damage);
|
}
|
||||||
|
|
||||||
|
@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.Entity;
|
||||||
import main.entities.LivingEntity;
|
import main.entities.LivingEntity;
|
||||||
|
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Player extends LivingEntity {
|
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 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() {
|
public Player() {
|
||||||
super(loader.getFigur(0), "idle");
|
super(loader.getFigur(0), "idle");
|
||||||
@@ -35,7 +37,8 @@ public class Player extends LivingEntity {
|
|||||||
actionFigur.neuerZustand(loader.getFigur(1), "walk");
|
actionFigur.neuerZustand(loader.getFigur(1), "walk");
|
||||||
actionFigur.neuerZustand(loader.getFigur(2), "strike");
|
actionFigur.neuerZustand(loader.getFigur(2), "strike");
|
||||||
actionFigur.neuerZustand(loader.getFigur(3), "swipe");
|
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
|
@Override
|
||||||
@@ -78,15 +81,7 @@ public class Player extends LivingEntity {
|
|||||||
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
||||||
zustandSetzen("die");
|
zustandSetzen("die");
|
||||||
}
|
}
|
||||||
|
onlyAttackOnceTrigger = false;
|
||||||
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
|
||||||
@@ -101,18 +96,31 @@ public class Player extends LivingEntity {
|
|||||||
// auf Kollisionen prüfen
|
// auf Kollisionen prüfen
|
||||||
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
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();
|
ArrayList<Entity> entities = Main.instance.getWorld().getCurrentMap().getEntities();
|
||||||
for (Entity e : entities) {
|
for (Entity e : entities) {
|
||||||
if (e instanceof LivingEntity && e != this) {
|
if (e instanceof LivingEntity && e != this && e.dist(this) <= 1f) {
|
||||||
LivingEntity le = (LivingEntity) e;
|
LivingEntity le = (LivingEntity) e;
|
||||||
Vektor toE = vectorToEntity(le);
|
Vektor toE = vectorToEntity(le);
|
||||||
toE = toE.normiert();
|
toE = toE.normiert();
|
||||||
le.setVelX(le.getVelX()+toE.x * 0.05f);
|
if ((toE.x > 0 && !side) || (toE.x <= 0 && side)) {
|
||||||
le.setVelY(le.getVelY()+toE.x * 0.05f);
|
le.setVelX(le.getVelX() + 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 java.util.ArrayList;
|
||||||
|
|
||||||
import ea.Knoten;
|
import ea.Knoten;
|
||||||
|
import ea.Ticker;
|
||||||
|
import main.Main;
|
||||||
import main.Tile;
|
import main.Tile;
|
||||||
import main.entities.Entity;
|
import main.entities.Entity;
|
||||||
|
import main.entities.LivingEntity;
|
||||||
|
import main.entities.player.Player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auf der Map sind alle Entities, sowie die Tiles gespiechert.
|
* 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.
|
protected Tile[][] map; // Die Tiles der map in einem 2D Array.
|
||||||
private ArrayList<Entity> entities;
|
private ArrayList<Entity> entities;
|
||||||
|
private Player player;
|
||||||
|
|
||||||
public Map(int width, int height) {
|
public Map(int width, int height) {
|
||||||
map = new Tile[width][height];
|
map = new Tile[width][height];
|
||||||
|
|
||||||
entities = new ArrayList<>(100);
|
entities = new ArrayList<>(100);
|
||||||
|
|
||||||
|
Main.instance.manager.anmelden(this, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,6 +37,19 @@ public abstract class Map extends Knoten {
|
|||||||
return null;
|
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() {
|
public int getWidth() {
|
||||||
return map.length;
|
return map.length;
|
||||||
}
|
}
|
||||||
@@ -41,4 +61,12 @@ public abstract class Map extends Knoten {
|
|||||||
public ArrayList<Entity> getEntities() {
|
public ArrayList<Entity> getEntities() {
|
||||||
return entities;
|
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