Skip to content

Commit

Permalink
Fix mobs instant kill
Browse files Browse the repository at this point in the history
Fix top bar not updating Hp
  • Loading branch information
reihoron committed Jun 13, 2024
1 parent c300912 commit 9493fa4
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 43 deletions.
1 change: 1 addition & 0 deletions Bee.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void addedToWorld(World w) {

@Override
public void act() {
attackAct++;
attack(dmg);
collision();
super.act();
Expand Down
6 changes: 3 additions & 3 deletions Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Level0.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -34,7 +34,7 @@ public Level0() {

public void act() {
followPlayer(scroll, player);
updateCoin(coinLabel);
updateCoin();
saveButton.setLocation(getWidth() - 100, 40);
checkSaveButton();
checkNext();
Expand Down
14 changes: 7 additions & 7 deletions Level1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions Mites.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
40 changes: 17 additions & 23 deletions Mobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +22,7 @@ public Player() {
}

public void addedToWorld(World w) {
this.w = w;
this.w = (Level) w;
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
}
1 change: 1 addition & 0 deletions Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void addedToWorld(World w) {
}

public void act() {
attackAct++;
if (checkBlock() || checkY()) {
holdAct--;
hold();
Expand Down
2 changes: 1 addition & 1 deletion levels/0.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9493fa4

Please sign in to comment.