From 3e348cf91e71a7c18dc6308ac32a448cbff27858 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 25 Feb 2020 04:07:05 +0100 Subject: [PATCH] Move game in a seperate thread --- .../ynerant/leveleditor/game/GameFrame.java | 63 +++++++++++-------- .../fr/ynerant/leveleditor/game/Mob1.java | 2 +- .../fr/ynerant/leveleditor/game/Mob2.java | 2 +- .../ynerant/leveleditor/game/MobCancer.java | 2 +- .../fr/ynerant/leveleditor/game/Tower.java | 2 +- 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index cbca5b4..f4a1505 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -38,6 +38,41 @@ public class GameFrame extends JFrame implements WindowListener { setVisible(true); + new Thread(() -> { + while (true) { + 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)); + mobs.add(mob); + } + } + + 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()) { + mobs.remove(mob); + if (mob.getX() < 0) + --hp; + else + reward += mob.getReward(); + } + + try { + Thread.sleep(20L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }).start(); + repaint(); } @@ -66,33 +101,9 @@ public class GameFrame extends JFrame implements WindowListener { g.drawImage(s3.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, null, null); } - 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)); - mobs.add(mob); - } - } - - 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()) { - 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); - } + 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/Mob1.java b/src/main/java/fr/ynerant/leveleditor/game/Mob1.java index 2a6fe6d..8b00f22 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob1.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob1.java @@ -8,7 +8,7 @@ public class Mob1 extends Mob { @Override public long getSlowness() { - return 100; + return 60; } @Override diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob2.java b/src/main/java/fr/ynerant/leveleditor/game/Mob2.java index 593213e..4ee511a 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Mob2.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob2.java @@ -8,7 +8,7 @@ public class Mob2 extends Mob { @Override public long getSlowness() { - return 60; + return 20; } @Override diff --git a/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java index 8164ec8..42954a5 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java +++ b/src/main/java/fr/ynerant/leveleditor/game/MobCancer.java @@ -8,7 +8,7 @@ public class MobCancer extends Mob { @Override public long getSlowness() { - return 10000; + return 200; } @Override diff --git a/src/main/java/fr/ynerant/leveleditor/game/Tower.java b/src/main/java/fr/ynerant/leveleditor/game/Tower.java index 295e5c9..305949c 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/Tower.java +++ b/src/main/java/fr/ynerant/leveleditor/game/Tower.java @@ -31,7 +31,7 @@ public abstract class Tower { } else { remainingTicks = getPeriod(); - return filterDetectedMobs(mobs); + return _filterDetectedMobs(mobs); } }