From 9493fa42d7605fa583ca4628d7a458cbd1a211a0 Mon Sep 17 00:00:00 2001 From: Adrian400811 Date: Thu, 13 Jun 2024 12:02:22 -0400 Subject: [PATCH] Fix mobs instant kill Fix top bar not updating Hp --- Bee.java | 1 + Level.java | 6 +++--- Level0.java | 4 ++-- Level1.java | 14 +++++++------- Mites.java | 4 ++-- Mobs.java | 40 +++++++++++++++++----------------------- Player.java | 11 ++++++----- Spider.java | 1 + levels/0.csv | 2 +- 9 files changed, 40 insertions(+), 43 deletions(-) diff --git a/Bee.java b/Bee.java index 9147e1f..72d56b2 100755 --- a/Bee.java +++ b/Bee.java @@ -19,6 +19,7 @@ public void addedToWorld(World w) { @Override public void act() { + attackAct++; attack(dmg); collision(); super.act(); diff --git a/Level.java b/Level.java index c322ee4..8e8db6c 100755 --- a/Level.java +++ b/Level.java @@ -90,9 +90,9 @@ public void followPlayer(ImgScroll scr, Player p) { } } - public void updateCoin(SuperDisplayLabel cl) { - cl.update("Coins: " + totalCoins + " HP: " + totalHP); - cl.setLocation(getWidth() / 2, 20); + public void updateCoin() { + coinLabel.update("Coins: " + totalCoins + " HP: " + totalHP); + coinLabel.setLocation(getWidth() / 2, 20); } public int[][] loadLevel(int level) { diff --git a/Level0.java b/Level0.java index 839889e..f14143d 100755 --- a/Level0.java +++ b/Level0.java @@ -23,7 +23,7 @@ public Level0() { addObject(player = new Player(), 100, 622); addObject(coinLabel, 1100, 10); addObject(saveButton, getWidth() - 100, 40); - updateCoin(coinLabel); + updateCoin(); resetCoin(); setHP(5); setLevel(0); @@ -34,7 +34,7 @@ public Level0() { public void act() { followPlayer(scroll, player); - updateCoin(coinLabel); + updateCoin(); saveButton.setLocation(getWidth() - 100, 40); checkSaveButton(); checkNext(); diff --git a/Level1.java b/Level1.java index 3b33002..a1cb022 100755 --- a/Level1.java +++ b/Level1.java @@ -23,7 +23,7 @@ public Level1() { addObject(player = new Player(), 130, 135); addObject(coinLabel, 1100, 10); addObject(saveButton, getWidth() - 100, 40); - updateCoin(coinLabel); + updateCoin(); // Individual Block Placement int[][] blockGeneration = loadLevel(1); @@ -32,17 +32,17 @@ public Level1() { public void act() { followPlayer(scroll, player); - updateCoin(coinLabel); - saveButton.setLocation(getWidth()-100, 40); + updateCoin(); + saveButton.setLocation(getWidth() - 100, 40); checkSaveButton(); loseLife(); checkNext(); } - - private void loseLife(){ - if (player.touchingSpike()){ + + private void loseLife() { + if (player.touchingSpike()) { player.changeHP(-5); - setHP(totalHP-5); + setHP(totalHP - 5); } } } diff --git a/Mites.java b/Mites.java index 644cd29..0e0e9a3 100755 --- a/Mites.java +++ b/Mites.java @@ -6,7 +6,6 @@ public class Mites extends Mobs { private final int dmg; private final int speed; private final GreenfootImage image; - private int direction = 1; private Level w; private int movementAct; private int jumpAct; @@ -27,8 +26,9 @@ public void addedToWorld(World w) { public void act() { movementAct--; jumpAct--; + attackAct++; movement(); - direction = bounceWall(direction, image); + bounceWall(image); collision(); fall(); attack(dmg); diff --git a/Mobs.java b/Mobs.java index bb3eb24..d22541f 100755 --- a/Mobs.java +++ b/Mobs.java @@ -4,22 +4,31 @@ import java.util.List; public abstract class Mobs extends SuperSmoothMover { - public World w; - private int hp; - private int speed = 2; - private int dmg; - private int direction = 1; + protected World w; + protected int attackAct = 0; + protected int direction = 1; + protected int hp; + protected int speed = 2; + protected int dmg; public Mobs() { enableStaticRotation(); } + protected void bounceWall(GreenfootImage image) { + if (getOneObjectAtOffset(direction * getImage().getWidth() + 2, 0, Tile.class) != null) { + direction *= -1; + image.mirrorHorizontally(); + } + } + public void addedToWorld(World w) { this.w = w; } public void act() { stepped(); + attackAct++; } private void gravity() { @@ -55,21 +64,6 @@ public void collision() { } } - protected int bounceWall(int dir) { - if (getOneObjectAtOffset(dir * getImage().getWidth() + 1, 0, Tile.class) != null) { - dir *= -1; - } - return dir; - } - - protected int bounceWall(int dir, GreenfootImage image) { - if (getOneObjectAtOffset(dir * getImage().getWidth() + 2, 0, Tile.class) != null) { - dir *= -1; - image.mirrorHorizontally(); - } - return dir; - } - protected void idle() { if (getWorld() == null) { return; @@ -123,10 +117,10 @@ public void stepped() { public void attack(int dmg) { Player p = (Player) getOneIntersectingObject(Player.class); - if (p == null) { - return; + if (p != null && attackAct > 60) { + attackAct = 0; + p.changeHP(-dmg); } - p.changeHP(-dmg); } public void changeHP(int deltaHP) { diff --git a/Player.java b/Player.java index 4c7767b..e61ee16 100755 --- a/Player.java +++ b/Player.java @@ -12,7 +12,7 @@ public class Player extends Actor { private static int speed; private final int dmg; private int hp; - private World w; + private Level w; private int jumpActs = 0; private int boostActs = 0; @@ -22,7 +22,7 @@ public Player() { } public void addedToWorld(World w) { - this.w = w; + this.w = (Level) w; } /** @@ -54,7 +54,7 @@ private void movement() { private void jump() { if (Greenfoot.isKeyDown("Space") && getOneObjectAtOffset(0, (getImage().getHeight() / 2) + 1, Brick.class) != null) { jumpActs = 30; - } + } if (jumpActs > 15) { setLocation(getX(), getY() - 8); } @@ -122,9 +122,10 @@ public int getSpeed() { public void changeHP(int deltaHP) { hp += deltaHP; + w.setHP(hp); } - - public boolean touchingSpike(){ + + public boolean touchingSpike() { return (isTouching(Spike.class)); } } diff --git a/Spider.java b/Spider.java index d66d5a4..9e02b03 100755 --- a/Spider.java +++ b/Spider.java @@ -27,6 +27,7 @@ public void addedToWorld(World w) { } public void act() { + attackAct++; if (checkBlock() || checkY()) { holdAct--; hold(); diff --git a/levels/0.csv b/levels/0.csv index c9d3455..ce4c7b7 100755 --- a/levels/0.csv +++ b/levels/0.csv @@ -131,7 +131,7 @@ 71,1,1 71,4,8 72,11,12 -72,2,1 +72,1,1 72,5,8 73,10,1 74,10,1