From d440ad8f6758574ca0cc5cce6b63355db2267adc Mon Sep 17 00:00:00 2001 From: Asecave Date: Wed, 30 Jun 2021 18:03:47 +0200 Subject: [PATCH] Coins can be collected --- Zoelda/src/main/entities/Coin.java | 22 ++++++++++++++++++---- Zoelda/src/main/entities/Entity.java | 25 ++++++++++++++++++++----- Zoelda/src/main/maps/Map.java | 5 ++--- Zoelda/src/main/worlds/TestWorld.java | 9 ++++++--- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Zoelda/src/main/entities/Coin.java b/Zoelda/src/main/entities/Coin.java index 79e2099..a66d229 100644 --- a/Zoelda/src/main/entities/Coin.java +++ b/Zoelda/src/main/entities/Coin.java @@ -1,27 +1,41 @@ package main.entities; import main.SheetLoader; +import main.entities.player.Player; public class Coin extends AnimatedEntity { - + private static SheetLoader loader = new SheetLoader("/res/images/coins.png"); - + public Coin() { - loader.generateFigures(8, 8, new int[] {8, 8, 1}); + loader.generateFigures(8, 8, new int[] { 8, 8, 1 }); + + loader.getFigur(0).animationsGeschwindigkeitSetzen(100); + loader.getFigur(1).animationsGeschwindigkeitSetzen(100); + loader.getFigur(2).animationsGeschwindigkeitSetzen(100); + actionFigur.neuerZustand(loader.getFigur(0), "gold"); actionFigur.neuerZustand(loader.getFigur(1), "silver"); actionFigur.neuerZustand(loader.getFigur(2), "bronze"); zustandSetzen("gold"); - + posX = 4f; posY = 4f; width = 0.375f; height = 0.25f; spriteOffsetY = -0.1f; + enablePushBack = false; } @Override protected void update() { super.update(); } + + @Override + public void onEntityCollision(Entity e) { + if (e instanceof Player) { + deleteEntity(); + } + } } diff --git a/Zoelda/src/main/entities/Entity.java b/Zoelda/src/main/entities/Entity.java index ee12f9f..d3b0fa3 100644 --- a/Zoelda/src/main/entities/Entity.java +++ b/Zoelda/src/main/entities/Entity.java @@ -18,6 +18,7 @@ public abstract class Entity implements Ticker { 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 + protected boolean enablePushBack = true; // Ob das Entity von anderen Entities weggedrückt werden kann. // Das ist die Ticker-Methode von ea; wird jeden frame ausgeführt (frameloop) @Override @@ -83,16 +84,30 @@ public abstract class Entity implements Ticker { float esize = (e.getWidth() + e.getHeight()) / 2; float size = (width + height) / 2; float dist = dist(e); - float sizes = esize / 2 + size / 2; + float sizes = esize / 2 + size / 2; if (dist < sizes) { - float overlap = dist - sizes; - posX += (overlap * (e.posX - posX)); - posY += (overlap * (e.posY - posY)); + if (enablePushBack && e.enablePushBack) { + float overlap = dist - sizes; + float xDist = e.posX - posX; + float yDist = e.posY - posY; + if (xDist == 0f) { + xDist = (float) (Math.random() - 0.5f) * 0.01f; + } + if (yDist == 0f) { + yDist = (float) (Math.random() - 0.5f) * 0.01f; + } + posX += (overlap * xDist); + posY += (overlap * yDist); + } + onEntityCollision(e); } } } } + public void onEntityCollision(Entity e) { + } + /** * Gehört mit zur collision detection. Hier werden kollisionen aufgelöst. */ @@ -144,7 +159,7 @@ public abstract class Entity implements Ticker { Vektor v = new Vektor(e.posX - posX, e.posY - posY); ; if (v.x == 0 && v.y == 0) { - return new Vektor(0.0000001f, 0.0000001f); + return new Vektor(0.001f, 0.001f); } return v; } diff --git a/Zoelda/src/main/maps/Map.java b/Zoelda/src/main/maps/Map.java index 3ab4384..6c0c7e3 100644 --- a/Zoelda/src/main/maps/Map.java +++ b/Zoelda/src/main/maps/Map.java @@ -9,7 +9,6 @@ import main.Main; import main.Tile; import main.entities.AnimatedEntity; import main.entities.Entity; -import main.entities.LivingEntity; import main.entities.player.Player; /** @@ -49,8 +48,8 @@ public abstract class Map extends Knoten implements Ticker { for (int i = 0; i < entities.size(); i++) { Entity e = entities.get(i); if (e.isReadyToDelete()) { - if (e instanceof LivingEntity) { - entfernen(((LivingEntity) e).actionFigur); + if (e instanceof AnimatedEntity) { + entfernen(((AnimatedEntity) e).actionFigur); } Main.instance.manager.abmelden(e); entities.remove(e); diff --git a/Zoelda/src/main/worlds/TestWorld.java b/Zoelda/src/main/worlds/TestWorld.java index 07eb04a..69c117a 100644 --- a/Zoelda/src/main/worlds/TestWorld.java +++ b/Zoelda/src/main/worlds/TestWorld.java @@ -17,8 +17,11 @@ public class TestWorld extends World { s.setPosX((float) (Math.random() * 5 + 2)); getCurrentMap().addAnimatedEntity(s); } - - getCurrentMap().addAnimatedEntity(new Coin()); - } + for (int i = 0; i < 15; i++) { + Coin c = new Coin(); + c.setPosX(i * 0.5f + 4); + getCurrentMap().addAnimatedEntity(c); + } + } }