From ca40e4c01a6fd891ba160ebb4ccb88a4db1a12e6 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 1 Jul 2021 10:47:37 +0200 Subject: [PATCH 1/2] Coin collect animation --- Zoelda/src/main/HUD/Inventory.java | 12 +++++++----- Zoelda/src/main/entities/Coin.java | 20 +++++++++++++++++--- Zoelda/src/main/entities/Entity.java | 4 ++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Zoelda/src/main/HUD/Inventory.java b/Zoelda/src/main/HUD/Inventory.java index d834b05..bf131ac 100644 --- a/Zoelda/src/main/HUD/Inventory.java +++ b/Zoelda/src/main/HUD/Inventory.java @@ -10,6 +10,7 @@ import ea.Knoten; import ea.Ticker; import main.Main; import main.entities.player.PlayerInventory; +import main.worlds.World; public class Inventory extends Knoten implements Ticker { @@ -26,21 +27,22 @@ public class Inventory extends Knoten implements Ticker { } catch (IOException e) { e.printStackTrace(); } - coinCounterIcon = coinCounterIcon.getSubimage(0,20,7,4); + coinCounterIcon = coinCounterIcon.getSubimage(0, 20, 7, 4); } - public void style(Graphics2D g){ + public void style(Graphics2D g) { Font currentFont = g.getFont(); - Font newFont = currentFont.deriveFont(Font.BOLD,20); + Font newFont = currentFont.deriveFont(Font.BOLD, 20); g.setFont(newFont); } public void zeichnen(Graphics2D g) { style(g); g.drawImage(inventoryFrame, 0, 0, SCALE * 2, SCALE * 2, null); - g.drawString(String.valueOf(inventory.getCoins()),SCALE+10,145); - g.drawImage(coinCounterIcon,0,125,SCALE,SCALE/2,null); + g.drawString(String.valueOf(inventory.getCoins()), SCALE + 10, 145); + g.drawImage(coinCounterIcon, 0, 125, coinCounterIcon.getWidth() * World.SCALE_FACTOR, + coinCounterIcon.getHeight() * World.SCALE_FACTOR, null); } // wird alle 50ms ausgef�hrt: diff --git a/Zoelda/src/main/entities/Coin.java b/Zoelda/src/main/entities/Coin.java index 3f36e1e..e9295a8 100644 --- a/Zoelda/src/main/entities/Coin.java +++ b/Zoelda/src/main/entities/Coin.java @@ -7,6 +7,9 @@ import main.entities.player.Player; public class Coin extends AnimatedEntity { private static SheetLoader loader = new SheetLoader("/res/images/coins.png"); + private boolean moveToCounterAnimation; + private float animationDestX = 0.5f; + private float animationDestY = 2f; public Coin() { loader.generateFigures(8, 8, new int[] { 8, 8, 1 }); @@ -31,13 +34,24 @@ public class Coin extends AnimatedEntity { @Override protected void update() { super.update(); + + if (moveToCounterAnimation) { + float distX = animationDestX - posX; + float distY = animationDestY - posY; + posX += distX / 10f; + posY += distY / 10f; + if (distX < 0.05f && distX > -0.05f && distY < 0.05f && distY > -0.05f) { + deleteEntity(); + Main.instance.getWorld().getPlayer().getInventory().addCoins(1); + } + } } @Override public void onEntityCollision(Entity e) { - if (e instanceof Player) { - deleteEntity(); - Main.instance.getWorld().getPlayer().getInventory().addCoins(1); + if (e instanceof Player && !moveToCounterAnimation) { + moveToCounterAnimation = true; + enableTileCollisions = false; } } } diff --git a/Zoelda/src/main/entities/Entity.java b/Zoelda/src/main/entities/Entity.java index 82fbb11..42a883f 100644 --- a/Zoelda/src/main/entities/Entity.java +++ b/Zoelda/src/main/entities/Entity.java @@ -19,6 +19,7 @@ public abstract class Entity implements Ticker { 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. + protected boolean enableTileCollisions = true; // Ob das Entity von Wänden abprallen soll // Das ist die Ticker-Methode von ea; wird jeden frame ausgeführt (frameloop) @Override @@ -51,6 +52,9 @@ public abstract class Entity implements Ticker { * Checkt collision mit tiles von der map. Fertig, clean, nie wieder ändern. */ public void checkTileCollision(Map map) { + if (!enableTileCollisions) { + return; + } for (int x = (int) (posX - 2); x < posX + 2; x++) { for (int y = (int) (posY - 2); y < posY + 2; y++) { if (x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight()) { From 5f0b827fab6e5556232b60517196259743db82e7 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 1 Jul 2021 10:58:42 +0200 Subject: [PATCH 2/2] better coin alignment --- Zoelda/src/main/entities/Coin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zoelda/src/main/entities/Coin.java b/Zoelda/src/main/entities/Coin.java index e9295a8..5c06992 100644 --- a/Zoelda/src/main/entities/Coin.java +++ b/Zoelda/src/main/entities/Coin.java @@ -8,8 +8,8 @@ public class Coin extends AnimatedEntity { private static SheetLoader loader = new SheetLoader("/res/images/coins.png"); private boolean moveToCounterAnimation; - private float animationDestX = 0.5f; - private float animationDestY = 2f; + private float animationDestX = 0.2f; + private float animationDestY = 2.0f; public Coin() { loader.generateFigures(8, 8, new int[] { 8, 8, 1 });