Mob subclasses

This commit is contained in:
Yohann D'ANELLO 2020-02-25 03:37:38 +01:00
parent 07221a09f2
commit 927d98c977
5 changed files with 134 additions and 7 deletions

View File

@ -66,16 +66,21 @@ public class GameFrame extends JFrame implements WindowListener {
if (mobs.isEmpty() && round < 4) { if (mobs.isEmpty() && round < 4) {
++round; ++round;
for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { 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)); mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16));
mobs.add(mob); mobs.add(mob);
} }
} }
for (Mob mob : mobs) { for (Mob mob : new ArrayList<>(mobs)) {
mob.tick();
if (mob.getX() < 0 || mob.isDead())
mobs.remove(mob);
else {
Sprite s = mob.getSprite(); Sprite s = mob.getSprite();
g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null);
} }
}
repaint(); repaint();
} }

View File

@ -7,18 +7,30 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class Mob { public abstract class Mob {
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
private static final List<String> MOB_SPRITES = Arrays.asList("mob1", "mob2", "mobcancer");
private Sprite sprite; private Sprite sprite;
private int x; private int x;
private int y; private int y;
private int hp;
private long tickRemains;
public Mob() { 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() { public Sprite getSprite() {
if (sprite == null)
sprite = SpriteRegister.getCategory(getName()).getSprites().get(0);
return sprite; return sprite;
} }
@ -35,6 +47,36 @@ public class Mob {
this.y = y; 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 @Override
public String toString() { public String toString() {
return "Mob{" + return "Mob{" +
@ -43,4 +85,15 @@ public class Mob {
", y=" + y + ", y=" + y +
'}'; '}';
} }
public static Mob getRandomMob() {
switch (RANDOM.nextInt(3)) {
case 1:
return new Mob1();
case 2:
return new Mob2();
default:
return new MobCancer();
}
}
} }

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -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";
}
}