From aab49eed1dc38f3f349ef180d5c5fd3b6ccb9023 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 24 Nov 2018 19:53:58 +0100 Subject: Rename MenuScene to MainMenuScene and minor code cleanup --- src/subconscious/Actor.java | 9 ++-- src/subconscious/Map.java | 17 ------ src/subconscious/graphics/GameWindow.java | 19 +++---- src/subconscious/graphics/MainMenuScene.java | 74 ++++++++++++++++++++++++++ src/subconscious/graphics/MapScene.java | 8 +-- src/subconscious/graphics/MenuScene.java | 74 -------------------------- src/subconscious/graphics/Scene.java | 7 +-- src/subconscious/graphics/widget/Button.java | 1 + src/subconscious/graphics/widget/Dynamic.java | 4 ++ src/subconscious/graphics/widget/PerfView.java | 5 ++ src/subconscious/graphics/widget/Widget.java | 1 + 11 files changed, 100 insertions(+), 119 deletions(-) create mode 100644 src/subconscious/graphics/MainMenuScene.java delete mode 100644 src/subconscious/graphics/MenuScene.java (limited to 'src') diff --git a/src/subconscious/Actor.java b/src/subconscious/Actor.java index 8280fe8..38e83ff 100644 --- a/src/subconscious/Actor.java +++ b/src/subconscious/Actor.java @@ -3,7 +3,7 @@ package subconscious; public class Actor { private final String name; - // TODO: enemy should not be binary (ex clans / groups / factions) + // TODO: enemy should not be binary (ex clans / groups / factions) private boolean isEnemy; private int hp; @@ -21,8 +21,8 @@ public class Actor { private Weapon weapon; private int actionsLeft; // TODO: make final - private int actions = 2; - // TODO: make bonus / power-ups structure + private final int actions = 2; + // TODO: make bonus / power-ups structure => use EnumSet public Actor(String name, int hp, boolean isEnemy, int agility) { this.name = name; @@ -32,8 +32,7 @@ public class Actor { this.skills = new SkillSet(); this.skills.agility = agility; - // TODO: puch should have infinite durability - this.weapon = new Weapon("fist", 1, 1, 10000000); + this.weapon = new Weapon("fist", 1, 1, -1); this.resetActions(); } diff --git a/src/subconscious/Map.java b/src/subconscious/Map.java index 4402790..21b9ae5 100644 --- a/src/subconscious/Map.java +++ b/src/subconscious/Map.java @@ -77,23 +77,6 @@ public class Map { return out; } - public void update(ArrayList actorsList, Tile[] tileGrid) { - // TODO: if this is needed for something, implement copy constructors for Actor and Tile - // and delete this code - for (Actor actor : actorsList) { - Actor newActor = new Actor(actor.getName(), actor.getHP(), actor.isEnemy(), actor.getSkills().agility); - newActor.place(actor.getX(), actor.getY()); - this.actors.add(newActor); - } - - for (int x = 0; x < this.size.width; x++) { - for (int y = 0; y < this.size.height; y++) { - Tile.Type oldTileType = tileGrid[x * this.size.width + y].getType(); - this.grid[x * this.size.width + y] = new Tile(oldTileType, x, y); - } - } - } - // TODO: refractor to resetActorActions(); public void resetActors() { for (Actor actor : this.actors) { diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index eb360f5..b26d6a4 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -32,14 +32,12 @@ public class GameWindow extends Frame implements WindowListener { private volatile Game game; - // TODO: remove map editor, start directly on Battle mode public GameWindow(Game g) { super("Subconscious"); - + this.game = g; // set up JFrame - // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WINDOW_SIZE); this.setPreferredSize(WINDOW_SIZE); this.setLocationRelativeTo(null); @@ -61,7 +59,7 @@ public class GameWindow extends Frame implements WindowListener { // ovserver of this.game private void loop() { // load the first scene - this.loadScene(new MenuScene(this.game)); + this.loadScene(new MainMenuScene(this.game)); while (this.game.isRunning()) { // check if the scene has requested a new scene @@ -80,9 +78,7 @@ public class GameWindow extends Frame implements WindowListener { Game.State newState = this.game.getState(); Game.State lastState = this.game.getLastState(); - if (lastState == newState) { - throw new IllegalStateException(); - } + assert newState != lastState; // MAIN_MENU is always the first scene if (newState == Game.State.MAIN_MENU) { @@ -130,12 +126,6 @@ public class GameWindow extends Frame implements WindowListener { this.unloadScene(); } - // private void unloadScenes(int count) { - // for (int i = 0; i < count; i++) { - // this.unloadScene(); - // } - // } - private void unloadScene() { // close old scene this.scene.stop(); @@ -167,6 +157,9 @@ public class GameWindow extends Frame implements WindowListener { this.setVisible(false); this.dispose(); + // check that all scene threads have been killed + assert this.loadedScenes.empty(); + System.exit(0); } diff --git a/src/subconscious/graphics/MainMenuScene.java b/src/subconscious/graphics/MainMenuScene.java new file mode 100644 index 0000000..9526b17 --- /dev/null +++ b/src/subconscious/graphics/MainMenuScene.java @@ -0,0 +1,74 @@ +package subconscious.graphics; + +import subconscious.Game; + +import java.awt.Graphics2D; +import java.awt.GridLayout; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Button; + +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + + + +@SuppressWarnings("serial") +public class MainMenuScene extends Scene implements ActionListener { + + public MainMenuScene(Game g) { + this(g, ""); + } + + public MainMenuScene(Game g, String uniqueName) { + super(g, uniqueName); + + this.game = g; + + // build Menu card + this.setLayout(new GridLayout(3, 1)); + + Button editor = new Button("Editor"); + editor.setActionCommand("btn-editor"); + editor.addActionListener(this); + + Button battle = new Button("Battle"); + battle.setActionCommand("btn-battle"); + battle.addActionListener(this); + + Button exit = new Button("Exit"); + exit.setActionCommand("btn-exit"); + exit.addActionListener(this); + + this.add(editor); + this.add(battle); + this.add(exit); + } + + @Override + public void build() {} + @Override + public void update(long deltaNanoTime) {} + @Override + public void render(Graphics2D g) {} + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().startsWith("btn")) { + if (e.getActionCommand().equals("btn-exit")) { + this.game.quit(); + return; + } + + if (e.getActionCommand().equals("btn-editor")) { + // TODO: fix editor + // this.loadScene(new MapEditorScene()); + } else if (e.getActionCommand().equals("btn-battle")) { + this.game.start(); + // TODO: change + this.requestScene(new BattleScene(this.game, "demo")); + } + + } + } +} \ No newline at end of file diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index 149d03d..77e0c92 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -57,13 +57,7 @@ public abstract class MapScene extends Scene { this.game = g; this.map = this.game.getMap(); - if (this.canvasSize.width < this.canvasSize.height) { - this.shorterCanvasLenght = this.canvasSize.width; - } else { - this.shorterCanvasLenght = this.canvasSize.height; - } - - this.tileSize = this.shorterCanvasLenght / 10; + this.updateCanvasSize(this.canvasSize); } protected void renderTiles(Graphics2D g) { diff --git a/src/subconscious/graphics/MenuScene.java b/src/subconscious/graphics/MenuScene.java deleted file mode 100644 index 32d9569..0000000 --- a/src/subconscious/graphics/MenuScene.java +++ /dev/null @@ -1,74 +0,0 @@ -package subconscious.graphics; - -import subconscious.Game; - -import java.awt.Graphics2D; -import java.awt.GridLayout; -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.Button; - -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; - - - -@SuppressWarnings("serial") -public class MenuScene extends Scene implements ActionListener { - - public MenuScene(Game g) { - this(g, ""); - } - - public MenuScene(Game g, String uniqueName) { - super(g, uniqueName); - - this.game = g; - - // build Menu card - this.setLayout(new GridLayout(3, 1)); - - Button editor = new Button("Editor"); - editor.setActionCommand("btn-editor"); - editor.addActionListener(this); - - Button battle = new Button("Battle"); - battle.setActionCommand("btn-battle"); - battle.addActionListener(this); - - Button exit = new Button("Exit"); - exit.setActionCommand("btn-exit"); - exit.addActionListener(this); - - this.add(editor); - this.add(battle); - this.add(exit); - } - - @Override - public void build() {} - @Override - public void update(long deltaNanoTime) {} - @Override - public void render(Graphics2D g) {} - - @Override - public void actionPerformed(ActionEvent e) { - if (e.getActionCommand().startsWith("btn")) { - if (e.getActionCommand().equals("btn-exit")) { - this.game.quit(); - return; - } - - if (e.getActionCommand().equals("btn-editor")) { - // TODO: fix editor - // this.loadScene(new MapEditorScene()); - } else if (e.getActionCommand().equals("btn-battle")) { - this.game.start(); - // TODO: change - this.requestScene(new BattleScene(this.game, "demo")); - } - - } - } -} \ No newline at end of file diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 466aa03..50ef323 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -195,7 +195,7 @@ public abstract class Scene extends Panel // initialize canvas buffer // the condition checks because the while above can be interrupted by // changing this.running - // TODO: this is because of the MenuScene which hides the canvas + // TODO: this is because of the MainMenuScene which hides the canvas // when the scene will be fixed this if can be removed if (this.canvas.isDisplayable()) { this.canvas.createBufferStrategy(2); @@ -244,8 +244,8 @@ public abstract class Scene extends Panel } else { try { Thread.sleep((this.DESIRED_DELTA_LOOP - deltaLoop)/(1000*1000)); - } catch (InterruptedException e ) { - + } catch (InterruptedException ex) { + ex.printStackTrace(); } } @@ -304,6 +304,7 @@ public abstract class Scene extends Panel /* mouse listener */ @Override public void mouseClicked(MouseEvent e) { + // could be cached for (Widget w : this.widgets) { if (w instanceof Clickable) { ((Clickable) w).mouseClick(e.getPoint()); diff --git a/src/subconscious/graphics/widget/Button.java b/src/subconscious/graphics/widget/Button.java index 47ba810..ea774b9 100644 --- a/src/subconscious/graphics/widget/Button.java +++ b/src/subconscious/graphics/widget/Button.java @@ -3,6 +3,7 @@ package subconscious.graphics.widget; import java.awt.Graphics2D; import java.awt.Point; + public class Button extends Widget implements Clickable { public Button(String uniqueName, int x, int y, int width, int height) { diff --git a/src/subconscious/graphics/widget/Dynamic.java b/src/subconscious/graphics/widget/Dynamic.java index ca0029a..dc26305 100644 --- a/src/subconscious/graphics/widget/Dynamic.java +++ b/src/subconscious/graphics/widget/Dynamic.java @@ -1,5 +1,9 @@ package subconscious.graphics.widget; + +/* Dynamic interface for Widgets + * If a Widget is dynamic, it is updated on every tick of the game. + */ public interface Dynamic { public void update(long deltaNanoTime); } \ No newline at end of file diff --git a/src/subconscious/graphics/widget/PerfView.java b/src/subconscious/graphics/widget/PerfView.java index 8aa7c7e..94c68ab 100644 --- a/src/subconscious/graphics/widget/PerfView.java +++ b/src/subconscious/graphics/widget/PerfView.java @@ -3,6 +3,11 @@ package subconscious.graphics.widget; import java.awt.Graphics2D; +/* PerfView + * Simple example implementation that shows the performance of the game, + * it was mainly created for debugging, but could be used to show FPS in the + * final game. + */ public class PerfView extends Widget implements Dynamic { public static final int WIDTH = 120; diff --git a/src/subconscious/graphics/widget/Widget.java b/src/subconscious/graphics/widget/Widget.java index 31edc9a..0a9e5f6 100644 --- a/src/subconscious/graphics/widget/Widget.java +++ b/src/subconscious/graphics/widget/Widget.java @@ -3,6 +3,7 @@ package subconscious.graphics.widget; import java.awt.Graphics2D; import java.awt.Rectangle; + public abstract class Widget { public final String UNIQUE_NAME; -- cgit v1.2.1