From 927d98c9778fc951b8eb9a1fbfcbb2cb69227dd3 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 25 Feb 2020 03:37:38 +0100 Subject: [PATCH] Mob subclasses --- .../ynerant/leveleditor/game/GameFrame.java | 13 ++-- .../java/fr/ynerant/leveleditor/game/Mob.java | 59 ++++++++++++++++++- .../fr/ynerant/leveleditor/game/Mob1.java | 23 ++++++++ .../fr/ynerant/leveleditor/game/Mob2.java | 23 ++++++++ .../ynerant/leveleditor/game/MobCancer.java | 23 ++++++++ 5 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 src/main/java/fr/ynerant/leveleditor/game/Mob1.java create mode 100644 src/main/java/fr/ynerant/leveleditor/game/Mob2.java create mode 100644 src/main/java/fr/ynerant/leveleditor/game/MobCancer.java diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index 6bdcf5b..70e8fe2 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -66,15 +66,20 @@ public class GameFrame extends JFrame implements WindowListener { if (mobs.isEmpty() && round < 4) { ++round; for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { - Mob mob = new Mob(); + Mob mob = Mob.getRandomMob(); mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); mobs.add(mob); } } - for (Mob mob : mobs) { - Sprite s = mob.getSprite(); - g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); + for (Mob mob : new ArrayList<>(mobs)) { + mob.tick(); + if (mob.getX() < 0 || mob.isDead()) + mobs.remove(mob); + else { + Sprite s = mob.getSprite(); + g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); + } } repaint(); diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob.java b/src/main/java/fr/ynerant/leveleditor/game/Mob.java index 1249ac7..b201ca5 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob.java @@ -7,18 +7,30 @@ import java.util.Arrays; import java.util.List; import java.util.Random; -public class Mob { +public abstract class Mob { private static final Random RANDOM = new Random(); - private static final List MOB_SPRITES = Arrays.asList("mob1", "mob2", "mobcancer"); private Sprite sprite; private int x; private int y; + private int hp; + private long tickRemains; public Mob() { - this.sprite = SpriteRegister.getCategory(MOB_SPRITES.get(RANDOM.nextInt(3))).getSprites().get(0); + this.hp = getMaxHP(); + this.tickRemains = getSlowness(); } + public abstract int getMaxHP(); + + public abstract long getSlowness(); + + public abstract int getReward(); + + public abstract String getName(); + public Sprite getSprite() { + if (sprite == null) + sprite = SpriteRegister.getCategory(getName()).getSprites().get(0); return sprite; } @@ -35,6 +47,36 @@ public class Mob { this.y = y; } + public int getHP() { + return hp; + } + + public boolean isDead() { + return hp <= 0; + } + + public void setHP(int hp) { + this.hp = hp; + } + + public boolean hit() { + if (!isDead()) { + --this.hp; + return true; + } + + return false; + } + + public void tick() { + if (tickRemains > 0) + --tickRemains; + else { + tickRemains = getSlowness(); + move(getX() - 1, getY()); + } + } + @Override public String toString() { return "Mob{" + @@ -43,4 +85,15 @@ public class Mob { ", y=" + y + '}'; } + + public static Mob getRandomMob() { + switch (RANDOM.nextInt(3)) { + case 1: + return new Mob1(); + case 2: + return new Mob2(); + default: + return new MobCancer(); + } + } } diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob1.java b/src/main/java/fr/ynerant/leveleditor/game/Mob1.java new file mode 100644 index 0000000..2a6fe6d --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob1.java @@ -0,0 +1,23 @@ +package fr.ynerant.leveleditor.game; + +public class Mob1 extends Mob { + @Override + public int getMaxHP() { + return 2; + } + + @Override + public long getSlowness() { + return 100; + } + + @Override + public int getReward() { + return 10; + } + + @Override + public String getName() { + return "mob1"; + } +} diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob2.java b/src/main/java/fr/ynerant/leveleditor/game/Mob2.java new file mode 100644 index 0000000..593213e --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob2.java @@ -0,0 +1,23 @@ +package fr.ynerant.leveleditor.game; + +public class Mob2 extends Mob { + @Override + public int getMaxHP() { + return 6; + } + + @Override + public long getSlowness() { + return 60; + } + + @Override + public int getReward() { + return 20; + } + + @Override + public String getName() { + return "mob2"; + } +} diff --git a/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java new file mode 100644 index 0000000..8164ec8 --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java @@ -0,0 +1,23 @@ +package fr.ynerant.leveleditor.game; + +public class MobCancer extends Mob { + @Override + public int getMaxHP() { + return 50; + } + + @Override + public long getSlowness() { + return 10000; + } + + @Override + public int getReward() { + return 100; + } + + @Override + public String getName() { + return "mobcancer"; + } +}