From dc44220ea6a8a92a24bdc19539ba5bee979b9dbc Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 25 Feb 2020 03:59:31 +0100 Subject: [PATCH] Add towers --- .../ynerant/leveleditor/game/BasicTower.java | 38 ++++++++++++++++ .../ynerant/leveleditor/game/GameFrame.java | 15 ++++++- .../java/fr/ynerant/leveleditor/game/Mob.java | 2 - .../ynerant/leveleditor/game/NullTower.java | 30 +++++++++++++ .../ynerant/leveleditor/game/SuperTower.java | 29 ++++++++++++ .../fr/ynerant/leveleditor/game/Tower.java | 45 +++++++++++++++++++ 6 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 src/main/java/fr/ynerant/leveleditor/game/BasicTower.java create mode 100644 src/main/java/fr/ynerant/leveleditor/game/NullTower.java create mode 100644 src/main/java/fr/ynerant/leveleditor/game/SuperTower.java create mode 100644 src/main/java/fr/ynerant/leveleditor/game/Tower.java diff --git a/src/main/java/fr/ynerant/leveleditor/game/BasicTower.java b/src/main/java/fr/ynerant/leveleditor/game/BasicTower.java new file mode 100644 index 0000000..3ce72bc --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/BasicTower.java @@ -0,0 +1,38 @@ +package fr.ynerant.leveleditor.game; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class BasicTower extends Tower { + public BasicTower(int x, int y) { + super(x, y); + } + + @Override + public int getDamagePerShot() { + return 1; + } + + @Override + public long getPeriod() { + return 5; + } + + @Override + public int getPrice() { + return 10; + } + + @Override + Collection _filterDetectedMobs(Collection mobs) { + List filtered = new ArrayList<>(); + + for (Mob mob : mobs) { + if (mob.getX() == getX() || mob.getY() == getY()) + filtered.add(mob); + } + + return filtered; + } +} diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index 70e8fe2..cbca5b4 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -19,7 +19,10 @@ public class GameFrame extends JFrame implements WindowListener { private final RawMap map; private int round = 0; + private int hp = 20; + private int reward = 20; private List mobs = new ArrayList<>(); + private List towers = new ArrayList<>(); public GameFrame(RawMap map) { super("Jeu"); @@ -72,10 +75,20 @@ public class GameFrame extends JFrame implements WindowListener { } } + for (Tower tower : towers) { + for (Mob mob : tower.filterDetectedMobs(mobs)) + mob.hit(); + } + for (Mob mob : new ArrayList<>(mobs)) { mob.tick(); - if (mob.getX() < 0 || mob.isDead()) + if (mob.getX() < 0 || mob.isDead()) { mobs.remove(mob); + if (mob.getX() < 0) + --hp; + else + reward += mob.getReward(); + } else { Sprite s = mob.getSprite(); g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob.java b/src/main/java/fr/ynerant/leveleditor/game/Mob.java index b201ca5..89412a0 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob.java @@ -3,8 +3,6 @@ package fr.ynerant.leveleditor.game; import fr.ynerant.leveleditor.api.editor.sprites.Sprite; import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister; -import java.util.Arrays; -import java.util.List; import java.util.Random; public abstract class Mob { diff --git a/src/main/java/fr/ynerant/leveleditor/game/NullTower.java b/src/main/java/fr/ynerant/leveleditor/game/NullTower.java new file mode 100644 index 0000000..3ea628e --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/NullTower.java @@ -0,0 +1,30 @@ +package fr.ynerant.leveleditor.game; + +import java.util.ArrayList; +import java.util.Collection; + +public class NullTower extends Tower { + public NullTower(int x, int y) { + super(x, y); + } + + @Override + public int getDamagePerShot() { + return Integer.MAX_VALUE; + } + + @Override + public long getPeriod() { + return 1; + } + + @Override + public int getPrice() { + return 0; + } + + @Override + Collection _filterDetectedMobs(Collection mobs) { + return new ArrayList<>(); + } +} diff --git a/src/main/java/fr/ynerant/leveleditor/game/SuperTower.java b/src/main/java/fr/ynerant/leveleditor/game/SuperTower.java new file mode 100644 index 0000000..bd48827 --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/SuperTower.java @@ -0,0 +1,29 @@ +package fr.ynerant.leveleditor.game; + +import java.util.Collection; + +public class SuperTower extends Tower { + public SuperTower(int x, int y) { + super(x, y); + } + + @Override + public int getDamagePerShot() { + return 20; + } + + @Override + public long getPeriod() { + return 10; + } + + @Override + public int getPrice() { + return 142; + } + + @Override + Collection _filterDetectedMobs(Collection mobs) { + return mobs; + } +} diff --git a/src/main/java/fr/ynerant/leveleditor/game/Tower.java b/src/main/java/fr/ynerant/leveleditor/game/Tower.java new file mode 100644 index 0000000..295e5c9 --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/Tower.java @@ -0,0 +1,45 @@ +package fr.ynerant.leveleditor.game; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Random; + +public abstract class Tower { + private final int x; + private final int y; + private long remainingTicks; + + public Tower(int x, int y) { + this.x = x; + this.y = y; + } + + private static final Random RANDOM = new Random(); + + public abstract int getDamagePerShot(); + + public abstract long getPeriod(); + + public abstract int getPrice(); + + abstract Collection _filterDetectedMobs(Collection mobs); + + public Collection filterDetectedMobs(Collection mobs) { + if (remainingTicks > 0) { + --remainingTicks; + return new ArrayList<>(); + } + else { + remainingTicks = getPeriod(); + return filterDetectedMobs(mobs); + } + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } +}