moved entities from word to map

This commit is contained in:
Asecave
2021-06-23 20:56:19 +02:00
parent 9b1f3be694
commit 43a6aecd2e
11 changed files with 109 additions and 64 deletions

View File

@@ -3,6 +3,6 @@ package main;
public class Launcher { public class Launcher {
public static void main(String[] args) { public static void main(String[] args) {
new Malin(); new Main();
} }
} }

View File

@@ -5,18 +5,21 @@ import ea.Game;
/** /**
* Von hier wird alles initialisiert. Das ist die höchste Klasse. * Von hier wird alles initialisiert. Das ist die höchste Klasse.
*/ */
public class Malin extends Game { public class Main extends Game {
public static Main instance;
private World world; private World world;
public static final int WIDTH = 1440; // Fensterbreite public static final int WIDTH = 1440; // Fensterbreite
public static final int HEIGHT = 1056; // Fensterhöhe public static final int HEIGHT = 1056; // Fensterhöhe
public Malin() { public Main() {
super(WIDTH, HEIGHT); super(WIDTH, HEIGHT);
instance = this;
// Welt initialisieren und Spieler hinzufügen // Welt initialisieren und Spieler hinzufügen
world = new World(this); world = new World();
// die Welt zu EA hinzufügen // die Welt zu EA hinzufügen
wurzel.add(world); wurzel.add(world);
} }

View File

@@ -1,10 +1,9 @@
package main; package main;
import java.util.ArrayList;
import ea.Knoten; import ea.Knoten;
import main.entities.Entity; import main.entities.Entity;
import main.entities.Player; import main.entities.Player;
import main.entities.Snake;
import main.entities.Spider; import main.entities.Spider;
import main.maps.Map; import main.maps.Map;
import main.maps.TestMap; import main.maps.TestMap;
@@ -14,15 +13,12 @@ import main.maps.TestMap;
*/ */
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.
private ArrayList<Entity> entities;
public World(Malin main) { public World() {
entities = new ArrayList<>(100);
// Map initialisieren // Map initialisieren
currentMap = new TestMap(); currentMap = new TestMap();
@@ -30,18 +26,22 @@ public class World extends Knoten {
add(currentMap); add(currentMap);
// und Entities auch // und Entities auch
Player player = new Player(main); Player player = new Player();
add(player.actionFigur); add(player.actionFigur);
entities.add(player); currentMap.getEntities().add(player);
Spider spider = new Spider(main); Spider spider = new Spider();
// und Spinnen auch // und Spinnen auch
add(spider.actionFigur); add(spider.actionFigur);
entities.add(spider); 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 : entities) { for (Entity e : currentMap.getEntities()) {
main.manager.anmelden(e, 20); Main.instance.manager.anmelden(e, 20);
} }
} }

View File

@@ -100,5 +100,12 @@ public abstract class Entity implements Ticker {
posX = blockX - width / 2; posX = blockX - width / 2;
} }
} }
/**
* @return die entifernung zu diesem Entity
*/
public float dist(Entity e) {
return (float) Math.sqrt(e.posX * e.posX + posY * posY);
}
} }

View File

@@ -1,6 +1,10 @@
package main.entities; package main.entities;
import java.awt.Color;
import java.awt.Graphics2D;
import ea.ActionFigur; import ea.ActionFigur;
import ea.BoundingRechteck;
import ea.Figur; import ea.Figur;
import main.World; import main.World;
@@ -22,13 +26,13 @@ public class LivingEntity extends Entity {
actionFigur = new ActionFigur(figur, name) { actionFigur = new ActionFigur(figur, name) {
// DEBUG: render boxes // DEBUG: render boxes
// @Override @Override
// public void zeichnen(Graphics2D g, BoundingRechteck r) { public void zeichnen(Graphics2D g, BoundingRechteck r) {
// g.setColor(Color.GREEN); g.setColor(Color.GREEN);
// g.drawRect((int) actionFigur.positionX(), (int) actionFigur.positionY(), (int) actionFigur.getBreite(), (int) actionFigur.getHoehe()); 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)); 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); super.zeichnen(g, r);
// } }
}; };
} }
@@ -48,22 +52,6 @@ public class LivingEntity extends Entity {
actionFigur.positionSetzen((posX + offsetX) * World.SCALE - actionFigur.getBreite() / 2, (posY + offsetY) * World.SCALE - actionFigur.getHoehe() / 2); actionFigur.positionSetzen((posX + offsetX) * World.SCALE - actionFigur.getBreite() / 2, (posY + offsetY) * World.SCALE - actionFigur.getHoehe() / 2);
} }
protected void moveLeft() {
velX -= accelleration;
}
protected void moveRight() {
velX += accelleration;
}
protected void moveUp() {
velY -= accelleration;
}
protected void moveDown() {
velY += accelleration;
}
/** /**
* Spiegelt die figur autmatisch, wenn nötig. * Spiegelt die figur autmatisch, wenn nötig.
*/ */

View File

@@ -1,17 +1,15 @@
package main.entities; package main.entities;
import ea.Taste; import ea.Taste;
import main.Malin; import main.Main;
import main.SheetLoader; import main.SheetLoader;
public class Player extends LivingEntity { public class Player extends LivingEntity {
private Malin main; // Referenz auf die main Klasse. Wird später für den Keyboardinput verwendet
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});
public Player(Malin main) { public Player() {
super(loader.getFigur(0), "idle_left"); super(loader.getFigur(0), "idle");
this.main = main;
// Entity-Eigenschaften werden festgelegt // Entity-Eigenschaften werden festgelegt
width = 0.7f; width = 0.7f;
@@ -45,17 +43,17 @@ public class Player extends LivingEntity {
&& actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) { && actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) {
// wasd movement // wasd movement
if (main.tasteGedrueckt(Taste.A)) { if (Main.instance.tasteGedrueckt(Taste.A)) {
moveLeft(); velX -= accelleration;
} }
if (main.tasteGedrueckt(Taste.D)) { if (Main.instance.tasteGedrueckt(Taste.D)) {
moveRight(); velX += accelleration;
} }
if (main.tasteGedrueckt(Taste.W)) { if (Main.instance.tasteGedrueckt(Taste.W)) {
moveUp(); velY -= accelleration;
} }
if (main.tasteGedrueckt(Taste.S)) { if (Main.instance.tasteGedrueckt(Taste.S)) {
moveDown(); velY += accelleration;
} }
// Auf idle stellen wenn man sich nicht bewegt // Auf idle stellen wenn man sich nicht bewegt
@@ -66,19 +64,19 @@ public class Player extends LivingEntity {
} }
// Attacken // Attacken
if (main.tasteGedrueckt(Taste.LEERTASTE)) { if (Main.instance.tasteGedrueckt(Taste.LEERTASTE)) {
zustandSetzen("swipe"); zustandSetzen("swipe");
} }
if (main.tasteGedrueckt(Taste.Q)) { if (Main.instance.tasteGedrueckt(Taste.Q)) {
zustandSetzen("strike"); zustandSetzen("strike");
} }
if (main.tasteGedrueckt(Taste.F)) { if (Main.instance.tasteGedrueckt(Taste.F)) {
zustandSetzen("die"); zustandSetzen("die");
} }
} }
// auf Kollisionen prüfen // auf Kollisionen prüfen
checkTileCollisions(main.getWorld().getCurrentMap()); checkTileCollisions(Main.instance.getWorld().getCurrentMap());
// LivingEntity auch updaten lassen // LivingEntity auch updaten lassen
super.update(); super.update();
} else { } else {

View File

@@ -0,0 +1,41 @@
package main.entities;
import java.util.ArrayList;
import ea.Vektor;
import main.Main;
import main.SheetLoader;
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 });
public Snake() {
super(loader.getFigur(0), "idle");
posX = 9f;
posY = 9f;
accelleration = 0.008f;
}
@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) {
Vektor toPlayer = new Vektor(nearestPlayer.posX - posX, nearestPlayer.posY - posY);
toPlayer = toPlayer.normiert();
velX += toPlayer.x * accelleration;
velY += toPlayer.y * accelleration;
}
this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
super.update();
}
}

View File

@@ -1,17 +1,15 @@
package main.entities; package main.entities;
import main.Malin; import main.Main;
import main.SheetLoader; import main.SheetLoader;
public class Spider extends LivingEntity { public class Spider extends LivingEntity {
private Malin main; // Referenz auf die main Klasse. Wird später für den Keyboardinput verwendet
private static SheetLoader loader = new SheetLoader("/res/images/spider_sprite_sheet.png", 32, 32, private static SheetLoader loader = new SheetLoader("/res/images/spider_sprite_sheet.png", 32, 32,
new int[] { 5, 6, 9, 1, 4, 3, 9, 6, 5, 6, 9, 1, 4, 3, 9, 6 }); new int[] { 5, 6, 9, 1, 4, 3, 9, 6, 5, 6, 9, 1, 4, 3, 9, 6 });
public Spider(Malin main) { public Spider() {
super(loader.getFigur(0), "walk"); super(loader.getFigur(0), "walk");
this.main = main;
width = 0.7f; width = 0.7f;
height = 0.8f; height = 0.8f;
@@ -24,7 +22,7 @@ public class Spider extends LivingEntity {
protected void update() { protected void update() {
// auf Kollisionen prüfen // auf Kollisionen prüfen
checkTileCollisions(main.getWorld().getCurrentMap()); checkTileCollisions(Main.instance.getWorld().getCurrentMap());
// LivingEntity auch updaten lassen // LivingEntity auch updaten lassen
super.update(); super.update();
velX = 0; velX = 0;

View File

@@ -1,7 +1,10 @@
package main.maps; package main.maps;
import java.util.ArrayList;
import ea.Knoten; import ea.Knoten;
import main.Tile; import main.Tile;
import main.entities.Entity;
/** /**
* Auf der Map sind alle Entities, sowie die Tiles gespiechert. * Auf der Map sind alle Entities, sowie die Tiles gespiechert.
@@ -9,9 +12,12 @@ import main.Tile;
public abstract class Map extends Knoten { public abstract class Map extends Knoten {
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;
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);
} }
/** /**
@@ -31,4 +37,8 @@ public abstract class Map extends Knoten {
public int getHeight() { public int getHeight() {
return map[0].length; return map[0].length;
} }
public ArrayList<Entity> getEntities() {
return entities;
}
} }

View File

@@ -5,11 +5,11 @@ import main.Tile;
public class TestMap extends Map { public class TestMap extends Map {
public TestMap() { public TestMap() {
super(20, 10); super(15, 11);
for (int x = 0; x < map.length; x++) { for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) { for (int y = 0; y < map[0].length; y++) {
map[x][y] = new Tile(0, x, y); map[x][y] = new Tile(Math.random() * 5 < 1 ? 1 : 0, x, y);
add(map[x][y]); add(map[x][y]);
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB