moved entities from word to map
This commit is contained in:
@@ -3,6 +3,6 @@ package main;
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Malin();
|
||||
new Main();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,21 @@ import ea.Game;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
public static final int WIDTH = 1440; // Fensterbreite
|
||||
public static final int HEIGHT = 1056; // Fensterhöhe
|
||||
|
||||
public Malin() {
|
||||
public Main() {
|
||||
super(WIDTH, HEIGHT);
|
||||
instance = this;
|
||||
|
||||
// Welt initialisieren und Spieler hinzufügen
|
||||
world = new World(this);
|
||||
world = new World();
|
||||
// die Welt zu EA hinzufügen
|
||||
wurzel.add(world);
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package main;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import ea.Knoten;
|
||||
import main.entities.Entity;
|
||||
import main.entities.Player;
|
||||
import main.entities.Snake;
|
||||
import main.entities.Spider;
|
||||
import main.maps.Map;
|
||||
import main.maps.TestMap;
|
||||
@@ -14,15 +13,12 @@ import main.maps.TestMap;
|
||||
*/
|
||||
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.
|
||||
private ArrayList<Entity> entities;
|
||||
|
||||
public World(Malin main) {
|
||||
|
||||
entities = new ArrayList<>(100);
|
||||
public World() {
|
||||
|
||||
// Map initialisieren
|
||||
currentMap = new TestMap();
|
||||
@@ -30,18 +26,22 @@ public class World extends Knoten {
|
||||
add(currentMap);
|
||||
|
||||
// und Entities auch
|
||||
Player player = new Player(main);
|
||||
Player player = new Player();
|
||||
add(player.actionFigur);
|
||||
entities.add(player);
|
||||
currentMap.getEntities().add(player);
|
||||
|
||||
Spider spider = new Spider(main);
|
||||
Spider spider = new Spider();
|
||||
// und Spinnen auch
|
||||
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)
|
||||
for (Entity e : entities) {
|
||||
main.manager.anmelden(e, 20);
|
||||
for (Entity e : currentMap.getEntities()) {
|
||||
Main.instance.manager.anmelden(e, 20);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,4 +101,11 @@ public abstract class Entity implements Ticker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return die entifernung zu diesem Entity
|
||||
*/
|
||||
public float dist(Entity e) {
|
||||
return (float) Math.sqrt(e.posX * e.posX + posY * posY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package main.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import ea.ActionFigur;
|
||||
import ea.BoundingRechteck;
|
||||
import ea.Figur;
|
||||
import main.World;
|
||||
|
||||
@@ -22,13 +26,13 @@ public class LivingEntity extends Entity {
|
||||
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
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package main.entities;
|
||||
|
||||
import ea.Taste;
|
||||
import main.Malin;
|
||||
import main.Main;
|
||||
import main.SheetLoader;
|
||||
|
||||
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});
|
||||
|
||||
public Player(Malin main) {
|
||||
super(loader.getFigur(0), "idle_left");
|
||||
this.main = main;
|
||||
public Player() {
|
||||
super(loader.getFigur(0), "idle");
|
||||
|
||||
// Entity-Eigenschaften werden festgelegt
|
||||
width = 0.7f;
|
||||
@@ -45,17 +43,17 @@ public class Player extends LivingEntity {
|
||||
&& actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) {
|
||||
|
||||
// wasd movement
|
||||
if (main.tasteGedrueckt(Taste.A)) {
|
||||
moveLeft();
|
||||
if (Main.instance.tasteGedrueckt(Taste.A)) {
|
||||
velX -= accelleration;
|
||||
}
|
||||
if (main.tasteGedrueckt(Taste.D)) {
|
||||
moveRight();
|
||||
if (Main.instance.tasteGedrueckt(Taste.D)) {
|
||||
velX += accelleration;
|
||||
}
|
||||
if (main.tasteGedrueckt(Taste.W)) {
|
||||
moveUp();
|
||||
if (Main.instance.tasteGedrueckt(Taste.W)) {
|
||||
velY -= accelleration;
|
||||
}
|
||||
if (main.tasteGedrueckt(Taste.S)) {
|
||||
moveDown();
|
||||
if (Main.instance.tasteGedrueckt(Taste.S)) {
|
||||
velY += accelleration;
|
||||
}
|
||||
|
||||
// Auf idle stellen wenn man sich nicht bewegt
|
||||
@@ -66,19 +64,19 @@ public class Player extends LivingEntity {
|
||||
}
|
||||
|
||||
// Attacken
|
||||
if (main.tasteGedrueckt(Taste.LEERTASTE)) {
|
||||
if (Main.instance.tasteGedrueckt(Taste.LEERTASTE)) {
|
||||
zustandSetzen("swipe");
|
||||
}
|
||||
if (main.tasteGedrueckt(Taste.Q)) {
|
||||
if (Main.instance.tasteGedrueckt(Taste.Q)) {
|
||||
zustandSetzen("strike");
|
||||
}
|
||||
if (main.tasteGedrueckt(Taste.F)) {
|
||||
if (Main.instance.tasteGedrueckt(Taste.F)) {
|
||||
zustandSetzen("die");
|
||||
}
|
||||
}
|
||||
|
||||
// auf Kollisionen prüfen
|
||||
checkTileCollisions(main.getWorld().getCurrentMap());
|
||||
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
// LivingEntity auch updaten lassen
|
||||
super.update();
|
||||
} else {
|
||||
|
||||
41
Zoelda/src/main/entities/Snake.java
Normal file
41
Zoelda/src/main/entities/Snake.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
package main.entities;
|
||||
|
||||
import main.Malin;
|
||||
import main.Main;
|
||||
import main.SheetLoader;
|
||||
|
||||
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,
|
||||
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");
|
||||
this.main = main;
|
||||
|
||||
width = 0.7f;
|
||||
height = 0.8f;
|
||||
@@ -24,7 +22,7 @@ public class Spider extends LivingEntity {
|
||||
protected void update() {
|
||||
|
||||
// auf Kollisionen prüfen
|
||||
checkTileCollisions(main.getWorld().getCurrentMap());
|
||||
checkTileCollisions(Main.instance.getWorld().getCurrentMap());
|
||||
// LivingEntity auch updaten lassen
|
||||
super.update();
|
||||
velX = 0;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package main.maps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import ea.Knoten;
|
||||
import main.Tile;
|
||||
import main.entities.Entity;
|
||||
|
||||
/**
|
||||
* Auf der Map sind alle Entities, sowie die Tiles gespiechert.
|
||||
@@ -9,9 +12,12 @@ import main.Tile;
|
||||
public abstract class Map extends Knoten {
|
||||
|
||||
protected Tile[][] map; // Die Tiles der map in einem 2D Array.
|
||||
private ArrayList<Entity> entities;
|
||||
|
||||
public Map(int width, int height) {
|
||||
map = new Tile[width][height];
|
||||
|
||||
entities = new ArrayList<>(100);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,4 +37,8 @@ public abstract class Map extends Knoten {
|
||||
public int getHeight() {
|
||||
return map[0].length;
|
||||
}
|
||||
|
||||
public ArrayList<Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import main.Tile;
|
||||
public class TestMap extends Map {
|
||||
|
||||
public TestMap() {
|
||||
super(20, 10);
|
||||
super(15, 11);
|
||||
|
||||
for (int x = 0; x < map.length; x++) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Zoelda/src/res/images/snake_spritesheet_calciumtrice.png
Normal file
BIN
Zoelda/src/res/images/snake_spritesheet_calciumtrice.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.0 KiB |
Reference in New Issue
Block a user