Add labels

This commit is contained in:
Yohann D'ANELLO 2020-02-25 05:03:36 +01:00
parent 3e348cf91e
commit d8111ef500
2 changed files with 91 additions and 67 deletions

View File

@ -8,67 +8,76 @@ import fr.ynerant.leveleditor.editor.CollidPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class GameFrame extends JFrame implements WindowListener {
public class GameFrame extends JFrame {
private final Random RANDOM = new Random();
private final RawMap map;
private int round = 0;
private int hp = 20;
private int hp = 5;
private int reward = 20;
private List<Mob> mobs = new ArrayList<>();
private List<Tower> towers = new ArrayList<>();
private JLabel waveLabel;
private JLabel nbMobsLabel;
private JLabel hpLabel;
private JLabel rewardLabel;
private JLabel winLabel;
public GameFrame(RawMap map) {
super("Jeu");
this.map = map;
this.setSize(600, 600);
this.setPreferredSize(getSize());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
this.addWindowListener(this);
this.setContentPane(new Grid());
JPanel root = new JPanel();
root.setLayout(new BorderLayout());
this.setContentPane(root);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(5, 1));
root.add(pane, BorderLayout.SOUTH);
Grid grid = new Grid();
grid.setSize(map.getWidth(), map.getHeight());
grid.setPreferredSize(grid.getSize());
grid.setMinimumSize(grid.getSize());
grid.setMaximumSize(grid.getSize());
root.add(grid, BorderLayout.CENTER);
waveLabel = new JLabel();
pane.add(waveLabel);
nbMobsLabel = new JLabel();
pane.add(nbMobsLabel);
hpLabel = new JLabel();
pane.add(hpLabel);
rewardLabel = new JLabel();
pane.add(rewardLabel);
winLabel = new JLabel();
pane.add(winLabel);
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);
}
}
while (hp > 0 && round < 4) {
tick();
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();
}
try {
Thread.sleep(50L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
@ -80,7 +89,51 @@ public class GameFrame extends JFrame implements WindowListener {
return map;
}
private class Grid extends JPanel {
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));
mobs.add(mob);
}
}
if (round == 4 && mobs.isEmpty()) {
winLabel.setForeground(Color.green.darker());
winLabel.setText("Vous avez gagné !");
return;
}
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;
if (hp == 0) {
winLabel.setForeground(Color.red);
winLabel.setText("Vous avez perdu !");
return;
}
}
else
reward += mob.getReward();
}
}
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);
}
private class Grid extends JComponent {
@Override
protected void paintComponent(Graphics _g) {
Graphics2D g = (Graphics2D) _g;
@ -109,33 +162,4 @@ public class GameFrame extends JFrame implements WindowListener {
repaint();
}
}
@Override
public void windowActivated(WindowEvent event) {
}
@Override
public void windowClosed(WindowEvent event) {
}
@Override
public void windowClosing(WindowEvent event) {
dispose();
}
@Override
public void windowDeactivated(WindowEvent event) {
}
@Override
public void windowDeiconified(WindowEvent event) {
}
@Override
public void windowIconified(WindowEvent event) {
}
@Override
public void windowOpened(WindowEvent event) {
}
}

View File

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