Move game in a seperate thread

This commit is contained in:
Yohann D'ANELLO 2020-02-25 04:07:05 +01:00
parent dc44220ea6
commit 3e348cf91e
5 changed files with 41 additions and 30 deletions

View File

@ -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,34 +101,10 @@ 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);
}
}
repaint();
}

View File

@ -8,7 +8,7 @@ public class Mob1 extends Mob {
@Override
public long getSlowness() {
return 100;
return 60;
}
@Override

View File

@ -8,7 +8,7 @@ public class Mob2 extends Mob {
@Override
public long getSlowness() {
return 60;
return 20;
}
@Override

View File

@ -8,7 +8,7 @@ public class MobCancer extends Mob {
@Override
public long getSlowness() {
return 10000;
return 200;
}
@Override

View File

@ -31,7 +31,7 @@ public abstract class Tower {
}
else {
remainingTicks = getPeriod();
return filterDetectedMobs(mobs);
return _filterDetectedMobs(mobs);
}
}