diff --git a/src/main/java/fr/ynerant/leveleditor/api/editor/RawCase.java b/src/main/java/fr/ynerant/leveleditor/api/editor/RawCase.java index 5274deb..3169474 100644 --- a/src/main/java/fr/ynerant/leveleditor/api/editor/RawCase.java +++ b/src/main/java/fr/ynerant/leveleditor/api/editor/RawCase.java @@ -53,4 +53,8 @@ public class RawCase { public Collision getCollision() { return collision; } + + public void setCollision(Collision collision) { + this.collision = collision; + } } diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index 154f228..3b5f9bb 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -1,5 +1,6 @@ package fr.ynerant.leveleditor.game; +import fr.ynerant.leveleditor.api.editor.Collision; import fr.ynerant.leveleditor.api.editor.RawCase; import fr.ynerant.leveleditor.api.editor.RawMap; import fr.ynerant.leveleditor.api.editor.sprites.Sprite; @@ -107,12 +108,25 @@ public class GameFrame extends JFrame { return map; } + public RawCase getCase(int x, int y) { + for (RawCase c : getMap().getCases()) { + if (c.getPosX() == x && c.getPosY() == y) + return c; + } + + return null; + } + public void tick() { if (mobs.isEmpty() && round < 4) { ++round; for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { Mob mob = Mob.getRandomMob(); - mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); + do { + mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); + } + while (getCase(mob.getX(), mob.getY()).getCollision() != Collision.ANY); + getCase(mob.getX(), mob.getY()).setCollision(Collision.PARTIAL); mobs.add(mob); } } @@ -123,7 +137,7 @@ public class GameFrame extends JFrame { } for (Mob mob : new ArrayList<>(mobs)) { - mob.tick(); + mob.tick(this); if (mob.getX() < 0 || mob.isDead()) { mobs.remove(mob); if (mob.getX() < 0) { @@ -139,16 +153,15 @@ public class GameFrame extends JFrame { } } - if (round == 4 && mobs.isEmpty()) { - winLabel.setForeground(Color.green.darker()); - winLabel.setText("Vous avez gagné !"); - return; - } - waveLabel.setText("Vague " + round); nbMobsLabel.setText(mobs.size() + " mob" + (mobs.size() > 1 ? "s" : "") + " restant" + (mobs.size() > 1 ? "s" : "")); hpLabel.setText("Points de vie : " + hp); rewardLabel.setText("Butin : " + reward); + + if (round == 4 && mobs.isEmpty()) { + winLabel.setForeground(Color.green.darker()); + winLabel.setText("Vous avez gagné !"); + } } private class Grid extends JComponent implements MouseListener { @@ -205,6 +218,11 @@ public class GameFrame extends JFrame { if (tower == null || tower.getPrice() > reward) return; + RawCase c = getCase(x, y); + if (c == null || c.getCollision() != Collision.ANY) + return; + c.setCollision(Collision.FULL); + reward -= tower.getPrice(); towers.add(tower); } diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob.java b/src/main/java/fr/ynerant/leveleditor/game/Mob.java index 89412a0..daea566 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob.java @@ -1,5 +1,7 @@ package fr.ynerant.leveleditor.game; +import fr.ynerant.leveleditor.api.editor.Collision; +import fr.ynerant.leveleditor.api.editor.RawCase; import fr.ynerant.leveleditor.api.editor.sprites.Sprite; import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister; @@ -66,12 +68,19 @@ public abstract class Mob { return false; } - public void tick() { + public void tick(GameFrame game) { if (tickRemains > 0) --tickRemains; else { tickRemains = getSlowness(); - move(getX() - 1, getY()); + RawCase c = game.getCase(getX(), getY()); + RawCase other = game.getCase(getX() - 1, getY()); + if (other == null || other.getCollision() == Collision.ANY) { + c.setCollision(Collision.ANY); + if (other != null) + other.setCollision(Collision.PARTIAL); + move(getX() - 1, getY()); + } } } diff --git a/src/main/java/fr/ynerant/leveleditor/game/NullTower.java b/src/main/java/fr/ynerant/leveleditor/game/NullTower.java index 798b924..8774dc7 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/NullTower.java +++ b/src/main/java/fr/ynerant/leveleditor/game/NullTower.java @@ -25,7 +25,7 @@ public class NullTower extends Tower { @Override public int getPrice() { - return 0; + return 5; } @Override