From caeb3588b53f5536a89fda8d54f35e7f40b13ee2 Mon Sep 17 00:00:00 2001 From: Shengyu JIANG <shengyu.jiang@student.unimelb.edu.au> Date: Wed, 7 Oct 2020 23:58:10 +1100 Subject: [PATCH] Optimize the readability of the code --- res/levelGenerateData.txt | 2 +- src/Animation/Animation.java | 7 +- src/Level/BeginnerPanel.java | 23 ++---- src/Level/Dialogue.java | 4 - src/Level/EndPanel.java | 21 ++--- src/Level/Level.java | 142 +++++++++++++++----------------- src/Level/LevelManager.java | 5 +- src/Level/MakeLevel.java | 86 ++++++++++--------- src/Level/MyLevelManager.java | 1 + src/Level/SelectButton.java | 12 +-- src/Level/SelectPanel.java | 80 +++++++++++------- src/Listener/MouseListener.java | 13 +-- src/Music/MusicPlayer.java | 8 +- src/Page/GameStage.java | 17 ++-- src/Page/LevelButton.java | 3 +- src/Page/LevelPage.java | 15 ++-- src/ShadowLifeGame.java | 61 +++++--------- src/UI/Button.java | 18 ++-- src/UI/Pane.java | 3 +- src/UI/TestButton.java | 15 ---- src/UI/TestPane.java | 26 ------ src/UI/TextBox.java | 14 ++-- src/actor/Actor.java | 32 ------- src/actor/Lightning.java | 4 +- src/actor/Portal.java | 2 +- src/actor/Projectile.java | 8 -- src/actor/Sign.java | 2 - src/actor/Skeleton.java | 5 -- src/actor/Skull.java | 6 +- src/actor/Spider.java | 6 +- src/actor/Trap.java | 7 +- src/actor/WallMaker.java | 2 +- src/actor/Witch.java | 6 +- src/myUtil/MyMath.java | 6 +- 34 files changed, 246 insertions(+), 416 deletions(-) delete mode 100644 src/Level/Dialogue.java delete mode 100644 src/UI/TestButton.java delete mode 100644 src/UI/TestPane.java diff --git a/res/levelGenerateData.txt b/res/levelGenerateData.txt index dec2bf5..b5045cc 100644 --- a/res/levelGenerateData.txt +++ b/res/levelGenerateData.txt @@ -1 +1 @@ -19 \ No newline at end of file +21 \ No newline at end of file diff --git a/src/Animation/Animation.java b/src/Animation/Animation.java index 719d785..987682f 100644 --- a/src/Animation/Animation.java +++ b/src/Animation/Animation.java @@ -1,8 +1,7 @@ package Animation; -import myUtil.Vec2D; -import bagel.Drawing; import bagel.Image; +import myUtil.Vec2D; import java.util.ArrayList; @@ -11,7 +10,7 @@ public class Animation { private static final int FRAME_REFRESH_TICK = 200; private static final int RESET = 0; - private ArrayList<Image> animation; + private final ArrayList<Image> animation; private long previousTime; private int elapsedTime; @@ -19,7 +18,7 @@ public class Animation { private int frame; public Animation() { - animation = new ArrayList<Image>(); + animation = new ArrayList<>(); elapsedTime = RESET; previousTime = System.currentTimeMillis(); frame = 0; diff --git a/src/Level/BeginnerPanel.java b/src/Level/BeginnerPanel.java index 27c6fbf..a148cde 100644 --- a/src/Level/BeginnerPanel.java +++ b/src/Level/BeginnerPanel.java @@ -1,8 +1,6 @@ package Level; -import Listener.MouseListener; -import UI.Pane; -import UI.TextBox; +import UI.*; import bagel.Image; import bagel.util.Colour; import myUtil.Vec2D; @@ -15,15 +13,15 @@ import java.util.ArrayList; public class BeginnerPanel extends Pane { private int dialogBox; - private String filename; + private final String filename; - private ArrayList<String> content; + private final ArrayList<String> content; private int currentIndex; + //text box TextBox textBox = new TextBox("", 600, new Vec2D(220, 120), Colour.WHITE); - - private Vec2D position = new Vec2D(50, 50); + private final Vec2D position = new Vec2D(50, 50); public BeginnerPanel(String filename) { super(); @@ -34,6 +32,7 @@ public class BeginnerPanel extends Pane { } public void load() { + //load script frame dialogBox = add(new Image("res/ActorExtensionPack/UI/dialogueBox.png")); try (BufferedReader br = new BufferedReader(new FileReader(filename))) { @@ -43,7 +42,7 @@ public class BeginnerPanel extends Pane { } } catch (IOException e) { - System.err.println(e); + System.err.println(e.toString()); } } private static final int FONT_SIZE = 40; @@ -58,14 +57,10 @@ public class BeginnerPanel extends Pane { public boolean response() { currentIndex++; - if (currentIndex == content.size()) { - return false; - } - return true; + return currentIndex != content.size(); } public BeginnerPanel clone() { - BeginnerPanel newBeginnerPanel = new BeginnerPanel(filename); - return newBeginnerPanel; + return new BeginnerPanel(filename); } } diff --git a/src/Level/Dialogue.java b/src/Level/Dialogue.java deleted file mode 100644 index a315bd3..0000000 --- a/src/Level/Dialogue.java +++ /dev/null @@ -1,4 +0,0 @@ -package Level; - -public class Dialogue { -} diff --git a/src/Level/EndPanel.java b/src/Level/EndPanel.java index 068b7d0..f303c1e 100644 --- a/src/Level/EndPanel.java +++ b/src/Level/EndPanel.java @@ -5,38 +5,29 @@ import Music.MusicPlayerManager; import Page.GameStage; import UI.Pane; import UI.TextBox; -import bagel.Image; import bagel.util.Colour; import myUtil.Vec2D; public class EndPanel extends Pane { Level castToLevel; - private int upPart; - private int middlePart; - private int bottomPart; + private final static int TITLE_SIZE = 100; - private static final Vec2D UP_POSITION = new Vec2D(220, 200); - private static final Vec2D MIDDLE_POSITION1 = new Vec2D(220, 300); - private static final Vec2D MIDDLE_POSITION2 = new Vec2D(220, 400); - private static final Vec2D BOTTOM_POSITION = new Vec2D(220, 500); + TextBox title = new TextBox("...", 1000, new Vec2D(150,380), Colour.WHITE); - private int deadSound; + private boolean playSound = false; + + private final int deadSound; public EndPanel(Level castToLevel) { this.castToLevel = castToLevel; deadSound = MusicPlayerManager.getInstance().addMusic(new MusicPlayer("res/ActorExtensionPack/Sound/wav/DeadSound.wav")); } - private final static int TITLE_SIZE = 100; - - TextBox title = new TextBox("...", 1000, new Vec2D(150,380), Colour.WHITE); - - private boolean playSound = false; - @Override public void display() { if (!playSound) { + //check for end status MusicPlayerManager.getInstance().stopAll(); if (castToLevel.state == Level.WIN) { MusicPlayerManager.getInstance().playMixed(GameStage.wining, MusicPlayer.NONE); diff --git a/src/Level/Level.java b/src/Level/Level.java index a70881a..59a2e7d 100644 --- a/src/Level/Level.java +++ b/src/Level/Level.java @@ -19,7 +19,6 @@ import java.util.Collections; public class Level { public static final int TILED_LENGTH = 64; - public static final int HALF_TILED_LENGTH = 32; //map identifier public static final String DUNGEON = "Dungeon"; public static final String GRASS = "Grass"; @@ -33,8 +32,6 @@ public class Level { //actor identifier protected static final String KNIGHT = "Knight"; protected static final String SKELETON = "Skeleton"; - protected static final String HORIZON = "Horizon"; - protected static final String VERTICAL = "Vertical"; protected static final String WALL_MAKER = "WallMaker"; protected static final String TRAP = "Trap"; protected static final String SKULL = "Skull"; @@ -47,22 +44,17 @@ public class Level { protected static final String SIGN_LEFT = "SignLeft"; protected static final String SIGN_RIGHT = "SignRight"; - //indexing for csv reader - private final int actorName = 0; private final int actorX = 1; private final int actorY = 2; private final int extraActorX = 3; private final int extraActorY = 4; private final int extraInfo0 = 3; private final int extraInfo1 = 4; - private final int extraInfo2 = 2; //refreshRate public static final int STEP_REFRESH = 63; public static final int HALF_REFRESH = 31; - public static final int OK = 0; - //Direction constant protected static final int UP = 0; protected static final int RIGHT = 1; @@ -71,12 +63,12 @@ public class Level { private int currentStage; private Image worldMap; - private ArrayList<Actor> actors; - private ArrayList<Actor> pending; + private final ArrayList<Actor> actors; + private final ArrayList<Actor> pending; - private String worldType; - private String filename; - private boolean[] playMode; + private final String worldType; + private final String filename; + private final boolean[] playMode; public static final int WIN = 1; public static final int CONTINUING = 0; @@ -228,7 +220,6 @@ public class Level { } private int endCheck() { - int checkState = CONTINUING; for (Actor actor : actors) { if (actor instanceof Knight) { if (((Knight) actor).isCarrying()) { @@ -267,8 +258,7 @@ public class Level { } state = endCheck(); - Collection<Actor> list = actors;; - list.removeIf(actor -> actor.isDestroyed()); + ((Collection<Actor>) actors).removeIf(Actor::isDestroyed); actors.addAll(pending); pending.clear(); @@ -314,7 +304,7 @@ public class Level { } public void loadCSV(String filename) { - String line = ""; + String line; try (BufferedReader br = new BufferedReader(new FileReader(filename))) { int lineNum = 1; @@ -322,35 +312,51 @@ public class Level { String[] actorInfo = line.split(","); //check for actor type and add to actor list - if (actorInfo[actorName].equals(KNIGHT)) { - readKnight(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(SKELETON)) { - readSkeleton(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(WALL_MAKER)) { - readWallMaker(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(TRAP)) { - readTrap(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(SKULL)) { - readSkull(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(PORTAL)) { - readPortal(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(WITCH)) { - readWitch(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(CHEST)) { - readChest(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(SPIDER)) { - readSpider(actorInfo, filename, lineNum); - } else if (actorInfo[actorName].equals(SIGN_UP)) { - readSign(actorInfo, filename, lineNum, UP); - } else if (actorInfo[actorName].equals(SIGN_DOWN)) { - readSign(actorInfo, filename, lineNum, DOWN); - } else if (actorInfo[actorName].equals(SIGN_LEFT)) { - readSign(actorInfo, filename, lineNum, LEFT); - } else if (actorInfo[actorName].equals(SIGN_RIGHT)) { - readSign(actorInfo, filename, lineNum, RIGHT); - } else { - System.out.println("error: in file \"" + filename +"\" at line " + lineNum); - System.exit(-1); + //indexing for csv reader + int actorName = 0; + switch (actorInfo[actorName]) { + case KNIGHT: + readKnight(actorInfo, filename, lineNum); + break; + case SKELETON: + readSkeleton(actorInfo, filename, lineNum); + break; + case WALL_MAKER: + readWallMaker(actorInfo, filename, lineNum); + break; + case TRAP: + readTrap(actorInfo, filename, lineNum); + break; + case SKULL: + readSkull(actorInfo, filename, lineNum); + break; + case PORTAL: + readPortal(actorInfo, filename, lineNum); + break; + case WITCH: + readWitch(actorInfo, filename, lineNum); + break; + case CHEST: + readChest(actorInfo, filename, lineNum); + break; + case SPIDER: + readSpider(actorInfo, filename, lineNum); + break; + case SIGN_UP: + readSign(actorInfo, filename, lineNum, UP); + break; + case SIGN_DOWN: + readSign(actorInfo, filename, lineNum, DOWN); + break; + case SIGN_LEFT: + readSign(actorInfo, filename, lineNum, LEFT); + break; + case SIGN_RIGHT: + readSign(actorInfo, filename, lineNum, RIGHT); + break; + default: + System.out.println("error: in file \"" + filename + "\" at line " + lineNum); + System.exit(-1); } lineNum++; } @@ -420,8 +426,8 @@ public class Level { spawnPoint2.set(checkInt(actorInfo[extraActorX], filename, lineNum) * TILED_LENGTH, checkInt(actorInfo[extraActorY], filename, lineNum) * TILED_LENGTH); //add wall maker - Actor wallMaker1 = new WallMaker(spawnPoint1); - Actor wallMaker2 = new WallMaker(spawnPoint2, ((WallMaker) wallMaker1).getRelateID()); + WallMaker wallMaker1 = new WallMaker(spawnPoint1); + Actor wallMaker2 = new WallMaker(spawnPoint2, wallMaker1.getRelateID()); add(wallMaker1); add(wallMaker2); //add lightning @@ -437,9 +443,9 @@ public class Level { for (int i = (int) (start + TILED_LENGTH); i < end; i += TILED_LENGTH) { if (lightningType == Lightning.VERTICAL) { - add(new Lightning(new Vec2D(spawnPoint1.x, i), lightningType, ((WallMaker) wallMaker1).getRelateID())); - } else if (lightningType == Lightning.HORIZON) { - add(new Lightning(new Vec2D(i, spawnPoint1.y), lightningType, ((WallMaker) wallMaker1).getRelateID())); + add(new Lightning(new Vec2D(spawnPoint1.x, i), lightningType, wallMaker1.getRelateID())); + } else { + add(new Lightning(new Vec2D(i, spawnPoint1.y), lightningType, wallMaker1.getRelateID())); } } } @@ -474,6 +480,7 @@ public class Level { checkInt(actorInfo[actorY], filename, lineNum) * TILED_LENGTH); //add attribute + int extraInfo2 = 2; add(new Spider(spawnPoint, checkInt(actorInfo[extraInfo0], filename, lineNum), checkInt(actorInfo[extraInfo1], filename, lineNum), checkInt(actorInfo[extraInfo2], filename, lineNum))); } @@ -484,8 +491,8 @@ public class Level { checkInt(actorInfo[actorY], filename, lineNum) * TILED_LENGTH); //add attribute - Actor skull = new Skull(spawnPoint, checkInt(actorInfo[extraInfo0], filename, lineNum), checkInt(actorInfo[extraInfo1], filename, lineNum)); - ((Skull) skull).setCastToLevel(this); + Skull skull = new Skull(spawnPoint, checkInt(actorInfo[extraInfo0], filename, lineNum), checkInt(actorInfo[extraInfo1], filename, lineNum)); + skull.setCastToLevel(this); add(skull); } @@ -497,8 +504,8 @@ public class Level { checkInt(actorInfo[actorY], filename, lineNum) * TILED_LENGTH); //add attribute - Actor witch = new Witch(spawnPoint, checkInt(actorInfo[extraInfo0], filename, lineNum), checkInt(actorInfo[extraInfo1], filename, lineNum)); - ((Witch) witch).setCastToLevel(this); + Witch witch = new Witch(spawnPoint, checkInt(actorInfo[extraInfo0], filename, lineNum), checkInt(actorInfo[extraInfo1], filename, lineNum)); + witch.setCastToLevel(this); add(witch); } @@ -507,10 +514,7 @@ public class Level { int result = 0; try { result = Integer.parseInt(integer); - } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("error: in file \"" + filename +"\" at line " + lineNum); - System.exit(-1); - } catch (NumberFormatException e) { + } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { System.out.println("error: in file \"" + filename +"\" at line " + lineNum); System.exit(-1); } @@ -529,11 +533,10 @@ public class Level { MusicPlayerManager.getInstance().playSingleTrack(GameStage.playMusic, MusicPlayer.LOOP_OPTION); endPanel.setPlaySound(false); loadCSV(filename); - Collection<Actor> list = actors;; - list.removeIf(actor -> actor instanceof Sign); + ((Collection<Actor>) actors).removeIf(actor -> actor instanceof Sign); } public Level clone() { - Level newLevel = null; + Level newLevel; if (hasBeginnerPanel) { newLevel = new Level(worldType, filename, beginnerFilename, playMode); } else { @@ -550,14 +553,6 @@ public class Level { return filename; } - public boolean[] getPlayMode() { - return playMode; - } - - public ArrayList<Actor> getPending() { - return pending; - } - public SelectPanel getSelectPanel() { return selectPanel; } @@ -566,11 +561,4 @@ public class Level { return state; } - public void setBeginnerPanel(BeginnerPanel beginnerPanel) { - this.beginnerPanel = beginnerPanel; - } - - public void setCurrentStage(int currentStage) { - this.currentStage = currentStage; - } } diff --git a/src/Level/LevelManager.java b/src/Level/LevelManager.java index 9a72d6c..380c9ad 100644 --- a/src/Level/LevelManager.java +++ b/src/Level/LevelManager.java @@ -7,14 +7,11 @@ public class LevelManager { ArrayList<Level> levels = new ArrayList<Level>(); - private static final String[] levelName = {"Easy", "Skeleton", "More Skeleton", "Spider", "Seapider", "MagicTower", - "Portal", "Where am I", "Watch out", "Feet with Holes", "Fire Bone", "Bones", "Magic", "Meteor Shower" - , "Monster Party"}; - public static final boolean[] PLAYING_INVENTORY = {false, false, false, false, false, false, false, false, true, true, true, true, false}; public static final boolean[] MAKING_INVENTORY = {true, true, true, true, true, true, true, true, true, true, true, true, true}; private LevelManager() { + //load map and help guide script levels.add(new Level(Level.DUNGEON, "res/ActorExtensionPack/Level/Easy.csv", "res/ActorExtensionPack/Level/Easy.txt", PLAYING_INVENTORY)); levels.add(new Level(Level.DUNGEON, "res/ActorExtensionPack/Level/Skeleton.csv", "res/ActorExtensionPack/Level/Skeleton.txt", PLAYING_INVENTORY)); levels.add(new Level(Level.DUNGEON, "res/ActorExtensionPack/Level/MoreSkeleton.csv", "res/ActorExtensionPack/Level/MoreSkeleton.txt", PLAYING_INVENTORY)); diff --git a/src/Level/MakeLevel.java b/src/Level/MakeLevel.java index 4823c9a..6fc8f36 100644 --- a/src/Level/MakeLevel.java +++ b/src/Level/MakeLevel.java @@ -23,7 +23,7 @@ public class MakeLevel extends Level { int newName = 0; try (BufferedReader reader = Files.newBufferedReader(file)) { - String line = null; + String line; if ((line = reader.readLine()) != null) { newName = Integer.parseInt(line.replace("\n", "")); } @@ -41,66 +41,72 @@ public class MakeLevel extends Level { file = Paths.get("res/ActorExtensionPack/Level/MyLevel/MyLevel" + newName + ".csv"); - byte data[] = makeLevelFileString().getBytes(); + byte[] data = makeLevelFileString().getBytes(); try (OutputStream out = new BufferedOutputStream( Files.newOutputStream(file, CREATE, APPEND))) { out.write(data, 0, data.length); } catch (IOException e) { - System.err.println(e); + System.err.println(e.toString()); System.exit(-1); } } @Override public MakeLevel clone() { + super.clone(); return new MakeLevel(getWorldType(), getFilename(), LevelManager.MAKING_INVENTORY); } public String makeLevelFileString() { - String s = ""; + StringBuilder s = new StringBuilder(); for (Actor actor : getSelectPanel().planning) { - if (actor instanceof Knight) { - s = s + KNIGHT + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; - } else if (actor instanceof Skeleton) { - s = s + SKELETON + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "," + actor.getDirection() + "," + ((Skeleton) actor).getWanderLoop() + "\n"; - } else if (actor instanceof Trap) { - s = s + TRAP + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "," + ((Trap)actor).getOn() + "\n"; - } else if (actor instanceof Spider) { - s = s + SPIDER + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "," + ((Spider) actor).getDirection0() + "," + ((Spider) actor).getDirection1() + "," + ((Spider) actor).getWanderLoop() + "\n"; - } else if (actor instanceof Skull) { - s = s + SKULL + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "," + actor.getDirection() + "," + ((Skull) actor).getWanderLoop() + "\n"; - } else if (actor instanceof Portal) { - s = s + PORTAL + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + ","; - for (Actor actor1 : getSelectPanel().planning) { - if (actor1 != actor && actor1 instanceof Portal && ((Portal) actor).getPairID() == ((Portal) actor1).getPairID()) { - s = s + (int)(actor1.getPosition().x/TILED_LENGTH) + "," + (int)(actor1.getPosition().y/TILED_LENGTH) + "\n"; + //generate actors into csv + if (!actor.isRemove()) { + if (actor instanceof Knight) { + s.append(KNIGHT).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); + } else if (actor instanceof Skeleton) { + s.append(SKELETON).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(",").append(actor.getDirection()).append(",").append(((Skeleton) actor).getWanderLoop()).append("\n"); + } else if (actor instanceof Trap) { + s.append(TRAP).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(",").append(((Trap) actor).getOn()).append("\n"); + } else if (actor instanceof Spider) { + s.append(SPIDER).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(",").append(((Spider) actor).getDirection0()).append(",").append(((Spider) actor).getDirection1()).append(",").append(((Spider) actor).getWanderLoop()).append("\n"); + } else if (actor instanceof Skull) { + s.append(SKULL).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(",").append(actor.getDirection()).append(",").append(((Skull) actor).getWanderLoop()).append("\n"); + } else if (actor instanceof Portal) { + s.append(PORTAL).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(","); + for (Actor actor1 : getSelectPanel().planning) { + if (actor1 != actor && actor1 instanceof Portal && ((Portal) actor).getPairID() == ((Portal) actor1).getPairID()) { + s.append((int) (actor1.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor1.getPosition().y / TILED_LENGTH)).append("\n"); + actor1.setRemove(true); + } } - } - } else if (actor instanceof WallMaker) { - s = s + WALL_MAKER + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + ","; - for (Actor actor1 : getSelectPanel().planning) { - if (actor1 != actor && actor1 instanceof WallMaker && ((WallMaker) actor).getRelateID() == ((WallMaker) actor1).getRelateID()) { - s = s + (int)(actor1.getPosition().x/TILED_LENGTH) + "," + (int)(actor1.getPosition().y/TILED_LENGTH) + "\n"; + } else if (actor instanceof WallMaker) { + s.append(WALL_MAKER).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(","); + for (Actor actor1 : getSelectPanel().planning) { + if (actor1 != actor && actor1 instanceof WallMaker && ((WallMaker) actor).getRelateID() == ((WallMaker) actor1).getRelateID()) { + s.append((int) (actor1.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor1.getPosition().y / TILED_LENGTH)).append("\n"); + actor1.setRemove(true); + } } + } else if (actor instanceof Witch) { + s.append(WITCH).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append(",").append(actor.getDirection()).append(",").append(((Witch) actor).getWanderLoop()).append("\n"); + } else if (actor instanceof Sign) { + if (actor.getDirection() == UP) { + s.append(SIGN_UP).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); + } else if (actor.getDirection() == RIGHT) { + s.append(SIGN_RIGHT).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); + } else if (actor.getDirection() == DOWN) { + s.append(SIGN_DOWN).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); + } else if (actor.getDirection() == LEFT) { + s.append(SIGN_LEFT).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); + } + } else if (actor instanceof Chest) { + s.append(CHEST).append(",").append((int) (actor.getPosition().x / TILED_LENGTH)).append(",").append((int) (actor.getPosition().y / TILED_LENGTH)).append("\n"); } - } else if (actor instanceof Witch) { - s = s + WITCH + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "," + actor.getDirection() + "," + ((Witch) actor).getWanderLoop() + "\n"; - } else if (actor instanceof Sign) { - if (actor.getDirection() == UP) { - s = s + SIGN_UP + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; - } else if (actor.getDirection() == RIGHT) { - s = s + SIGN_RIGHT + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; - } else if (actor.getDirection() == DOWN) { - s = s + SIGN_DOWN + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; - } else if (actor.getDirection() == LEFT) { - s = s + SIGN_LEFT + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; - } - } else if (actor instanceof Chest) { - s = s + CHEST + "," + (int)(actor.getPosition().x/TILED_LENGTH) + "," + (int)(actor.getPosition().y/TILED_LENGTH) + "\n"; } } - return s; + return s.toString(); } @Override diff --git a/src/Level/MyLevelManager.java b/src/Level/MyLevelManager.java index 6abb501..99c1c7a 100644 --- a/src/Level/MyLevelManager.java +++ b/src/Level/MyLevelManager.java @@ -13,6 +13,7 @@ public class MyLevelManager { ArrayList<Level> levels = new ArrayList<Level>(); private MyLevelManager() { + //read all data try (Stream<Path> paths = Files.walk(Paths.get("res/ActorExtensionPack/Level/MyLevel"))) { paths .filter(Files::isRegularFile) diff --git a/src/Level/SelectButton.java b/src/Level/SelectButton.java index 38694fb..5e31b1a 100644 --- a/src/Level/SelectButton.java +++ b/src/Level/SelectButton.java @@ -1,14 +1,8 @@ package Level; -import Listener.MouseListener; -import UI.AABB; import UI.Button; import UI.FontManager; import actor.Actor; -import actor.Knight; -import actor.Skeleton; -import actor.WallMaker; -import bagel.Drawing; import bagel.Image; import myUtil.Vec2D; @@ -17,7 +11,7 @@ public class SelectButton extends Button { private SelectPanel castToPanel; private Actor actor; private int amount; - private Vec2D position; + private final Vec2D position; public SelectButton(Image button, Vec2D position, Actor actor, int amount) { super(button, position); @@ -52,8 +46,4 @@ public class SelectButton extends Button { public void setAmount(int amount) { this.amount = amount; } - - public int getAmount() { - return amount; - } } diff --git a/src/Level/SelectPanel.java b/src/Level/SelectPanel.java index f9ac974..95c538b 100644 --- a/src/Level/SelectPanel.java +++ b/src/Level/SelectPanel.java @@ -16,7 +16,7 @@ public class SelectPanel extends Pane { //select component protected Actor selected = null; - private Level castToLevel; + private final Level castToLevel; //worked level Vec2D position = new Vec2D(0,640); @@ -32,7 +32,6 @@ public class SelectPanel extends Pane { //default value protected static int INVALID = -1; - protected static String VOID = ""; //default value private int start = 0; @@ -60,7 +59,13 @@ public class SelectPanel extends Pane { private static final int WIDTH = 1024; private static final int OFFSET = -5; - private ArrayList<Actor> selectActor; + //wall data + private static final int X_UPPERBOUND = 1024 - 64; + private static final int X_LOWER_BOUND = 64; + private static final int Y_UPPERBOUND = 768 - 64; + private static final int Y_LOWER_BOUND = 64; + + private final ArrayList<Actor> selectActor; HashMap<Actor, Integer> amount; ArrayList<SelectButton> buttons = new ArrayList<SelectButton>(); @@ -109,7 +114,7 @@ public class SelectPanel extends Pane { private static final int INFINITY = 99; - public SelectPanel(Level castToLevel, boolean chosen[]) { + public SelectPanel(Level castToLevel, boolean[] chosen) { super(); selectActor = new ArrayList<>(); planning = new ArrayList<>(); @@ -137,6 +142,7 @@ public class SelectPanel extends Pane { } } + //initialize chest and knight amount for (Actor actor : selectActor) { if (actor instanceof Knight || actor instanceof Chest) { amount.put(actor, 1); @@ -147,6 +153,7 @@ public class SelectPanel extends Pane { } } + //put sign to inventory for (Actor readActor : castToLevel.getActors()) { if (readActor instanceof Sign) { for (Actor reference : selectActor) { @@ -158,11 +165,10 @@ public class SelectPanel extends Pane { } } - Collection<Actor> list = castToLevel.getActors();; + Collection<Actor> list = castToLevel.getActors(); list.removeIf(actor -> actor instanceof Sign); - Collection<Actor> sList = selectActor; - sList.removeIf(actor -> actor.isRemove()); + ((Collection<Actor>) selectActor).removeIf(Actor::isRemove); this.castToLevel = castToLevel; @@ -170,12 +176,14 @@ public class SelectPanel extends Pane { } public void load() { + //back component leftPart = add(new Image("res/ActorExtensionPack/UI/inventoryLeft.png")); middlePart = add(new Image("res/ActorExtensionPack/UI/inventoryMiddle.png")); rightPart = add(new Image("res/ActorExtensionPack/UI/inventoryRight.png")); targetValid = add(new Image("res/ActorExtensionPack/UI/target_valid.png")); targetInvalid = add(new Image("res/ActorExtensionPack/UI/target_invalid.png")); + //item frame component for (int i = 0, j = OFFSET; i < MyMath.min(MAX_ITEM_DISPLAY, selectActor.size()); i++, j+=FRAME_WIDTH) { buttons.add(new SelectButton(new Image("res/ActorExtensionPack/UI/selectFrame.png"),new Vec2D(framePosition.x + j, framePosition.y), selectActor.get(i), amount.get(selectActor.get(i)))); buttons.get(i).setCastToPanel(this); @@ -191,7 +199,9 @@ public class SelectPanel extends Pane { //place event if (!remove) { + //place actor placeUpdate(); + //change trap status for (Actor actor : planning) { if (getGridPosition().equals(actor.getPosition()) && MouseListener.getInstance().leftClick()) { if (actor instanceof Trap) { @@ -201,20 +211,24 @@ public class SelectPanel extends Pane { } } } else { + //remove actor selected if (selected != null) { for (Actor reference : selectActor) { if (selected.getClass() == reference.getClass()) { - if (!(selected instanceof Sign) && selected.getClass() == reference.getClass()) { + if (!(selected instanceof Sign)) { amount.put(reference, amount.get(reference) + 1); - } else if ((selected instanceof Sign) && (reference instanceof Sign) && selected.getDirection() == reference.getDirection()) { + } else if (reference instanceof Sign && selected.getDirection() == reference.getDirection()) { amount.put(reference, amount.get(reference) + 1); } } } } + selected = null; storedPosition = new Vec2D(); place = false; + + //remove clicked actor deleteUpdate(); } @@ -305,6 +319,7 @@ public class SelectPanel extends Pane { } public void deleteUpdate() { + //delete single actor if (remove && MouseListener.getInstance().leftClick()) { Actor planningRemove = null; for (Actor actor : planning) { @@ -313,6 +328,7 @@ public class SelectPanel extends Pane { for (Actor reference : selectActor) { if (!(actor instanceof Sign) && actor.getClass() == reference.getClass()) { amount.put(reference, amount.get(reference) + 1); + //check different sign } else if ((actor instanceof Sign) && (reference instanceof Sign) && actor.getDirection() == reference.getDirection()) { amount.put(reference, amount.get(reference) + 1); } @@ -320,6 +336,7 @@ public class SelectPanel extends Pane { break; } } + //delete related actor if (planningRemove instanceof WallMaker) { int id = ((WallMaker) planningRemove).getRelateID(); Collection<Actor> list = planning; @@ -346,6 +363,7 @@ public class SelectPanel extends Pane { return; } if (MouseListener.getInstance().rightClick()) { + //cancel function amount.put(selected, amount.get(selected) + 1); selected = null; storedPosition = new Vec2D(); @@ -390,29 +408,27 @@ public class SelectPanel extends Pane { return; } } + //check for clicked actor for (Actor target : planning) { if (target.getPosition().equals(getGridPosition())) { invalidPlace(); return; } } - if (!MyMath.between(64, 1024 - 64, MouseListener.getInstance().position().x) - || !MyMath.between(64, 768 - 64, MouseListener.getInstance().position().y) ) { + //check for wall + if (!MyMath.between(X_LOWER_BOUND, X_UPPERBOUND, MouseListener.getInstance().position().x) + || !MyMath.between(Y_LOWER_BOUND, Y_UPPERBOUND, MouseListener.getInstance().position().y) ) { invalidPlace(); return; } validPlace(); } - private boolean place(Actor actor, Vec2D position) { + private boolean place(Actor actor) { planning.add(actor); return true; } - public void setCastToLevel(Level castToLevel) { - this.castToLevel = castToLevel; - } - private static Vec2D getGridPosition() { float xDiff = MouseListener.getInstance().position().x % Level.TILED_LENGTH; float yDiff = MouseListener.getInstance().position().y % Level.TILED_LENGTH; @@ -424,14 +440,14 @@ public class SelectPanel extends Pane { if (MouseListener.getInstance().leftClick() && target == targetValid) { Actor knight = new Knight(getGridPosition()); knight.animationUpdate(); - if (place(knight, getGridPosition())) { + if (place(knight)) { selected = null; } } } private void placeSkull() { - Actor skull = new Skull(storedPosition, UP, INVALID); + Skull skull = new Skull(storedPosition, UP, INVALID); //set spawn point if (!place) { @@ -453,7 +469,7 @@ public class SelectPanel extends Pane { } skull = new Skull(storedPosition, direction, (int) (wander / Level.TILED_LENGTH + 1)); - ((Skull) skull).setCastToLevel(castToLevel); + skull.setCastToLevel(castToLevel); planning.add(skull); @@ -469,7 +485,7 @@ public class SelectPanel extends Pane { } private void placeWitch() { - Actor witch = new Witch(storedPosition, UP, INVALID); + Witch witch = new Witch(storedPosition, UP, INVALID); //set spawn point if (!place) { @@ -491,7 +507,7 @@ public class SelectPanel extends Pane { } witch = new Witch(storedPosition, direction, (int) (wander / Level.TILED_LENGTH + 1)); - ((Witch) witch).setCastToLevel(castToLevel); + witch.setCastToLevel(castToLevel); planning.add(witch); selected = null; @@ -580,7 +596,7 @@ public class SelectPanel extends Pane { } private void placeWallMaker() { - Actor wallMaker1 = new WallMaker(storedPosition, INVALID); + WallMaker wallMaker1 = new WallMaker(storedPosition, INVALID); //set spawn point if (!place) { @@ -598,7 +614,7 @@ public class SelectPanel extends Pane { if (MouseListener.getInstance().leftClick() && target == targetValid) { wallMaker1 = new WallMaker(storedPosition); planning.add(wallMaker1); - Actor wallMaker2 = new WallMaker(getGridPosition(), ((WallMaker) wallMaker1).getRelateID()); + Actor wallMaker2 = new WallMaker(getGridPosition(), wallMaker1.getRelateID()); planning.add(wallMaker2); Vec2D spawnPoint1 = storedPosition; Vec2D spawnPoint2 = getGridPosition(); @@ -614,9 +630,9 @@ public class SelectPanel extends Pane { for (int i = (int) (start + Level.TILED_LENGTH); i < end; i += Level.TILED_LENGTH) { if (lightningType == Lightning.VERTICAL) { - planning.add(new Lightning(new Vec2D(spawnPoint1.x, i), lightningType, ((WallMaker) wallMaker1).getRelateID())); - } else if (lightningType == Lightning.HORIZON) { - planning.add(new Lightning(new Vec2D(i, spawnPoint1.y), lightningType, ((WallMaker) wallMaker1).getRelateID())); + planning.add(new Lightning(new Vec2D(spawnPoint1.x, i), lightningType, wallMaker1.getRelateID())); + } else { + planning.add(new Lightning(new Vec2D(i, spawnPoint1.y), lightningType, wallMaker1.getRelateID())); } } @@ -633,7 +649,7 @@ public class SelectPanel extends Pane { } private void placeSkeleton() { - Actor skeleton = new Skeleton(storedPosition, Skeleton.TYPE_HORIZON, INVALID); + Skeleton skeleton = new Skeleton(storedPosition, Skeleton.TYPE_HORIZON, INVALID); //set attribute int type = Skeleton.TYPE_HORIZON; float wander = MyMath.diff(getGridPosition().x, storedPosition.x); @@ -641,8 +657,8 @@ public class SelectPanel extends Pane { type = Skeleton.TYPE_VERTICAL; wander = MyMath.diff(getGridPosition().y, storedPosition.y); } - ((Skeleton) skeleton).setCurrentType(type); - ((Skeleton) skeleton).setWanderLoop((int) (wander / Level.TILED_LENGTH + 1)); + skeleton.setCurrentType(type); + skeleton.setWanderLoop((int) (wander / Level.TILED_LENGTH + 1)); //set spawn point if (!place) { @@ -685,7 +701,7 @@ public class SelectPanel extends Pane { if (MouseListener.getInstance().leftClick() && target == targetValid) { Actor trap = new Trap(getGridPosition(), OFF); trap.animationUpdate(); - if (place(trap, getGridPosition())) { + if (place(trap)) { selected = null; } } @@ -695,7 +711,7 @@ public class SelectPanel extends Pane { if (MouseListener.getInstance().leftClick() && target == targetValid) { Actor chest = new Chest(getGridPosition()); chest.animationUpdate(); - if (place(chest, getGridPosition())) { + if (place(chest)) { selected = null; } } @@ -705,7 +721,7 @@ public class SelectPanel extends Pane { if (MouseListener.getInstance().leftClick() && target == targetValid) { Actor sign = new Sign(getGridPosition(), direction); sign.animationUpdate(); - if (place(sign, getGridPosition())) { + if (place(sign)) { selected = null; } } diff --git a/src/Listener/MouseListener.java b/src/Listener/MouseListener.java index 933d643..3019c3c 100644 --- a/src/Listener/MouseListener.java +++ b/src/Listener/MouseListener.java @@ -33,21 +33,14 @@ public class MouseListener { } public void listen(Input input) { + //get mouse input and position if (mousePosition == null) { mousePosition = new Vec2D(input.getMouseX(), input.getMouseY()); } mousePosition.set(input.getMouseX(), input.getMouseY()); - if (input.wasPressed(MouseButtons.LEFT)) { - leftClick = true; - } else { - leftClick = false; - } + leftClick = input.wasPressed(MouseButtons.LEFT); - if (input.wasPressed(MouseButtons.RIGHT)) { - rightClick = true; - } else { - rightClick = false; - } + rightClick = input.wasPressed(MouseButtons.RIGHT); } diff --git a/src/Music/MusicPlayer.java b/src/Music/MusicPlayer.java index b5c3d3a..b90baf5 100644 --- a/src/Music/MusicPlayer.java +++ b/src/Music/MusicPlayer.java @@ -4,7 +4,6 @@ import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import java.io.File; -import java.io.FileInputStream; public class MusicPlayer { private final String musicPath; @@ -18,10 +17,6 @@ public class MusicPlayer { musicPath = path; } - public void play() { - play(0); - } - public void play(int option) { if (isPlaying) { player.close(); @@ -29,6 +24,7 @@ public class MusicPlayer { } File musicFile = new File(musicPath); try { + //play music AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicFile); player = AudioSystem.getClip(); player.open(audioInput); @@ -38,7 +34,7 @@ public class MusicPlayer { player.loop(Clip.LOOP_CONTINUOUSLY); } }catch (Exception e){ - System.out.println(e); + System.err.println(e.toString()); } } diff --git a/src/Page/GameStage.java b/src/Page/GameStage.java index d3ba89f..fe08485 100644 --- a/src/Page/GameStage.java +++ b/src/Page/GameStage.java @@ -3,11 +3,8 @@ package Page; import Music.MusicPlayer; import Music.MusicPlayerManager; -import java.util.ArrayList; - public class GameStage { private static GameStage gameStage = null; - private ArrayList<MusicPlayer> musicPlayers = new ArrayList<>();; //stage public static final int START_PAGE = 0; @@ -17,6 +14,13 @@ public class GameStage { public static final int MY_LEVEL_PAGE = 5; public static final int LOADING = 100; + //music index + public static int startMusic; + public static int playMusic; + public static int makeMusic; + public static int wining; + public static int lose; + private static int currentStage; private GameStage() { @@ -46,13 +50,8 @@ public class GameStage { currentStage = stage; } - public static int startMusic; - public static int playMusic; - public static int makeMusic; - public static int wining; - public static int lose; - private static void setMusic() { + //initial stage music startMusic = MusicPlayerManager.getInstance().addMusic(new MusicPlayer("res/ActorExtensionPack/Sound/wav/startScheme.wav")); playMusic = MusicPlayerManager.getInstance().addMusic(new MusicPlayer("res/ActorExtensionPack/Sound/wav/StartMenuBGM.wav")); makeMusic = MusicPlayerManager.getInstance().addMusic(new MusicPlayer("res/ActorExtensionPack/Sound/wav/makeScheme.wav")); diff --git a/src/Page/LevelButton.java b/src/Page/LevelButton.java index dbfd502..6f36039 100644 --- a/src/Page/LevelButton.java +++ b/src/Page/LevelButton.java @@ -1,6 +1,5 @@ package Page; -import Level.Level; import UI.Button; import UI.TextBox; import bagel.Image; @@ -9,7 +8,7 @@ import myUtil.Vec2D; public class LevelButton extends Button { private int num; - private LevelPage castToLevelPage; + private final LevelPage castToLevelPage; TextBox textBox; public LevelButton(Image button, Vec2D position, int num, LevelPage castToLevelPage) { diff --git a/src/Page/LevelPage.java b/src/Page/LevelPage.java index ac030d8..9b1fe85 100644 --- a/src/Page/LevelPage.java +++ b/src/Page/LevelPage.java @@ -19,9 +19,9 @@ public class LevelPage extends Pane { public int DEFAULT_LEVEL_COUNT = 15; - private int upPart; - private int middlePart; - private int bottomPart; + private final int upPart; + private final int middlePart; + private final int bottomPart; private static final Vec2D UP_POSITION = new Vec2D(220, 200); private static final Vec2D MIDDLE_POSITION1 = new Vec2D(220, 300); @@ -54,9 +54,6 @@ public class LevelPage extends Pane { private final static int TITLE_SIZE = 100; - private String path; - private int currentType; - private int start = 0; private int totalLevel; @@ -69,16 +66,14 @@ public class LevelPage extends Pane { public LevelPage(String path, int type) { super(); - this.path = path; - currentType = type; levelButtons = new ArrayList<>(); - if (currentType == DEFAULT_LEVEL) { + if (type == DEFAULT_LEVEL) { totalLevel = DEFAULT_LEVEL_COUNT; title = new TextBox("Challenge", 1000, UP_POSITION.clone().out_add(new Vec2D(-30,0)), Colour.WHITE); - } else if (currentType == MY_LEVEL) { + } else if (type == MY_LEVEL) { load(); title = new TextBox("My Map", 1000, UP_POSITION.clone().out_add(new Vec2D(-30,0)), Colour.WHITE); } diff --git a/src/ShadowLifeGame.java b/src/ShadowLifeGame.java index 02e94e0..2d8f71f 100644 --- a/src/ShadowLifeGame.java +++ b/src/ShadowLifeGame.java @@ -1,23 +1,16 @@ import Animation.Animation; -import Animation.AnimationStateManager; import Level.*; import Listener.MouseListener; -import Music.MusicPlayer; import Music.MusicPlayerManager; import Page.LevelPage; import UI.Button; -import UI.FontManager; import UI.TextBox; -import actor.*; import bagel.*; import bagel.util.Colour; -import bagel.util.Point; import myUtil.Vec2D; import Page.GameStage; import Page.StartPage; -import java.util.ArrayList; - import static Page.LevelPage.INVALID; public class ShadowLifeGame extends AbstractGame { @@ -25,31 +18,36 @@ public class ShadowLifeGame extends AbstractGame { public static final int HEIGHT = 768; //time control - private static float dt = 1; + private static final float dt = 1; private static float framePassed = 0; //int init private static final int RESET = 0; - Vec2D progressPosition = new Vec2D(192,384); - + //page and levels private Level level = LevelManager.getInstance().getLevel(3); - private StartPage startPage = new StartPage(); - private LevelPage levelPage = new LevelPage("res/ActorExtensionPack/Level", LevelPage.DEFAULT_LEVEL); + private final StartPage startPage = new StartPage(); + private final LevelPage levelPage = new LevelPage("res/ActorExtensionPack/Level", LevelPage.DEFAULT_LEVEL); private LevelPage myLevelPage = new LevelPage("res/ActorExtensionPack/Level/MyLevel", LevelPage.MY_LEVEL); - private MakeLevel sampleLevel = new MakeLevel(Level.DUNGEON, "res/ActorExtensionPack/Level/EmptyMap.csv", LevelManager.MAKING_INVENTORY); + private final MakeLevel sampleLevel = new MakeLevel(Level.DUNGEON, "res/ActorExtensionPack/Level/EmptyMap.csv", LevelManager.MAKING_INVENTORY); private MakeLevel makeLevel = sampleLevel.clone(); + //progress bar + Animation progress = new Animation(); private int loadCount = 0; + Vec2D progressPosition = new Vec2D(192,384); + private static final int LOADING_FONT_SIZE = 80; + private static final int LOADING_TIME = 3 * 64; + //make level identifier private boolean fromMyLevel = false; - Animation progress = new Animation(); - + //visualize component TextBox loading = new TextBox("FAKE LOADING...", 1000, progressPosition, Colour.WHITE); Image background = new Image("res/ActorExtensionPack/UI/startpage.png"); + //create generate button for make level Button generateButton = new Button(new Image("res/ActorExtensionPack/UI/yes.png"), new Vec2D(955, 500)) { @Override public void event() { @@ -62,6 +60,7 @@ public class ShadowLifeGame extends AbstractGame { } }; + //create level button Button levelButton = new Button(new Image("res/ActorExtensionPack/UI/LevelMenu.png"), new Vec2D(749, 0)) { @Override public void event() { @@ -99,7 +98,6 @@ public class ShadowLifeGame extends AbstractGame { MouseListener.getInstance().listen(input); background.drawFromTopLeft(0,0); - //level.update(framePassed % Level.TILED_LENGTH); if (GameStage.getInstance().getCurrentStage() == GameStage.START_PAGE) { if (makeLevel != null) { makeLevel = null; @@ -147,45 +145,24 @@ public class ShadowLifeGame extends AbstractGame { myLevelPage.setLevelChosen(INVALID); } } else if (GameStage.getInstance().getCurrentStage() >= GameStage.LOADING) { - loading.setFontSize(80); + loading.setFontSize(LOADING_FONT_SIZE); loading.display(); loadCount++; progress.playAt(progressPosition); - if (loadCount > 3 * 64) { + //check for the time pass + if (loadCount > LOADING_TIME) { progress.replay(); - loadCount = 0; + loadCount = RESET; GameStage.getInstance().goTo(GameStage.getInstance().getCurrentStage() - GameStage.LOADING); } } //update time framePassed += dt; - - //test only - //drawGridView(framePassed % Level.TILED_LENGTH); - - } - - private void drawGridView(float framePassedInUnit) { - Colour c = Colour.BLACK; - int thickness = 2; - if (framePassedInUnit == 31) { - c = Colour.GREEN; - thickness = 4; - } - if (framePassedInUnit == 63) { - c = Colour.RED; - thickness = 4; - } - for (int i = 0; i <= 1024; i += 64) { - Drawing.drawLine(new Point(i, 0), new Point(i, 768), 2, c); - } - for (int j = 0; j <= 768; j += 64) { - Drawing.drawLine(new Point(0, j), new Point(1024, j), 2, c); - } } public void createProgressBar() { + //add animation for progress bar Image f0 = new Image("res/ActorExtensionPack/UI/progressBar/0.png"); Image f1 = new Image("res/ActorExtensionPack/UI/progressBar/1.png"); Image f2 = new Image("res/ActorExtensionPack/UI/progressBar/2.png"); diff --git a/src/UI/Button.java b/src/UI/Button.java index 80d0863..e128090 100644 --- a/src/UI/Button.java +++ b/src/UI/Button.java @@ -6,29 +6,25 @@ import Music.MusicPlayerManager; import bagel.Drawing; import bagel.util.Colour; import bagel.util.Point; -import myUtil.MyMath; import myUtil.Vec2D; import bagel.Image; -import bagel.Input; -import bagel.MouseButtons; -import bagel.util.Vector2; public abstract class Button { - //private AABB volume; - private AABB boundingBox; - private Image button; + + private final AABB boundingBox; + private final Image button; private final int buttonPressedSound = MusicPlayerManager.getInstance().addMusic(new MusicPlayer("res/ActorExtensionPack/Sound/wav/ButtonPressed.wav")); public Button(Image button, Vec2D position) { this.button = button; Vec2D adding = new Vec2D((float)button.getWidth(), (float)button.getHeight()); this.boundingBox = new AABB(position, position.out_add(adding)); - //this.volume = new AABB(boundingBox.min.out_sub(frameSize), boundingBox.max.out_add(frameSize)); } public void onClick() { if (boundingBox.contains(MouseListener.getInstance().position())) { + //draw highlight Point a = boundingBox.min.toPoint(); Point b = new Point(boundingBox.max.x, boundingBox.min.y); Point c = boundingBox.max.toPoint(); @@ -37,6 +33,8 @@ public abstract class Button { Drawing.drawLine(c,b,2,Colour.WHITE); Drawing.drawLine(c,d,2,Colour.WHITE); Drawing.drawLine(a,d,2,Colour.WHITE); + + //check for mouse event if (MouseListener.getInstance().leftClick()) { MusicPlayerManager.getInstance().playMixed(buttonPressedSound, MusicPlayer.NONE); @@ -50,8 +48,4 @@ public abstract class Button { public void display() { button.drawFromTopLeft(boundingBox.min.x, boundingBox.min.y); } - - public void setButton(Image button) { - this.button = button; - } } \ No newline at end of file diff --git a/src/UI/Pane.java b/src/UI/Pane.java index 90b9c67..e701f6f 100644 --- a/src/UI/Pane.java +++ b/src/UI/Pane.java @@ -1,12 +1,11 @@ package UI; -import myUtil.Vec2D; import bagel.Image; import java.util.ArrayList; public abstract class Pane { - private ArrayList<Image> source; + private final ArrayList<Image> source; public Pane() { source = new ArrayList<>(); diff --git a/src/UI/TestButton.java b/src/UI/TestButton.java deleted file mode 100644 index 1ed8e4b..0000000 --- a/src/UI/TestButton.java +++ /dev/null @@ -1,15 +0,0 @@ -package UI; - -import myUtil.Vec2D; -import bagel.Image; - -public class TestButton extends Button { - public TestButton(Image button, Vec2D position) { - super(button, position); - } - - @Override - public void event() { - System.out.println("u clicked me!!!!"); - } -} diff --git a/src/UI/TestPane.java b/src/UI/TestPane.java deleted file mode 100644 index 5222d2e..0000000 --- a/src/UI/TestPane.java +++ /dev/null @@ -1,26 +0,0 @@ -package UI; - -import myUtil.Vec2D; -import bagel.Image; - -public class TestPane extends Pane { - - private int upPart; - private int middlePart; - private int bottomPart; - - public TestPane(Vec2D position) { - super(); - } - - public void load() { - upPart = add(new Image("res/ActorExtensionPack/UI/levelBackUp.png")); - middlePart = add(new Image("res/ActorExtensionPack/UI/levelBackMiddle.png")); - bottomPart = add(new Image("res/ActorExtensionPack/UI/levelBackBottom.png")); - } - - @Override - public void display() { - - } -} diff --git a/src/UI/TextBox.java b/src/UI/TextBox.java index d3a7378..add0662 100644 --- a/src/UI/TextBox.java +++ b/src/UI/TextBox.java @@ -1,19 +1,19 @@ package UI; -import myUtil.Vec2D; import bagel.DrawOptions; -import bagel.Drawing; import bagel.Font; import bagel.util.Colour; -import bagel.util.Point; +import myUtil.Vec2D; public class TextBox { private int fontSize = 20; private String str; - private float width; - private Vec2D position; - private Colour colour; + private final float width; + private final Vec2D position; + private final Colour colour; + + private static final float HEIGHT_RATIO = 2.0f / 3.0f; public TextBox(String str, float width, Vec2D position, Colour colour) { this.str = str; @@ -42,7 +42,7 @@ public class TextBox { if (i != 1) { DrawOptions drawOptions = new DrawOptions(); font.drawString(str.substring(0, i - 1), position.x, position.y, drawOptions.setBlendColour(colour)); - showLine(str.substring(i - 1), position.out_add(new Vec2D(0, 2 * fontSize / 3))); + showLine(str.substring(i - 1), position.out_add(new Vec2D(0, HEIGHT_RATIO * fontSize))); return; } else { throw new ArrayIndexOutOfBoundsException("Width is too short"); diff --git a/src/actor/Actor.java b/src/actor/Actor.java index fd27d6f..8da4d4f 100644 --- a/src/actor/Actor.java +++ b/src/actor/Actor.java @@ -2,7 +2,6 @@ package actor; import Animation.AnimationStateManager; import Level.Level; -import bagel.Image; import myUtil.Vec2D; public abstract class Actor implements Comparable<Actor> { @@ -38,13 +37,8 @@ public abstract class Actor implements Comparable<Actor> { //portal private boolean transfer; - //Animation - private int animationState; private AnimationStateManager getAnimationManager; - //type - private String type = "Actor"; - public Actor(Vec2D spawnPoint) { //Moving information position = new Vec2D(); @@ -139,14 +133,6 @@ public abstract class Actor implements Comparable<Actor> { transfer = false; } - public void setType(String type) { - this.type = type; - } - - public String type() { - return type; - } - public Vec2D getPosition() { return position; } @@ -163,14 +149,6 @@ public abstract class Actor implements Comparable<Actor> { return active; } - public int getAnimationState() { - return animationState; - } - - public void setAnimationState(int animationState) { - this.animationState = animationState; - } - public AnimationStateManager getAnimationManager() { return getAnimationManager; } @@ -214,16 +192,6 @@ public abstract class Actor implements Comparable<Actor> { } } - public void rotate90CounterClockwise() { - direction = Math.floorMod(--direction, NUM_DIRECTION); - if (direction == LEFT) { - face = LEFT; - } - if (direction == RIGHT) { - face = RIGHT; - } - } - public void destroy() { active = false; destroyed = true; diff --git a/src/actor/Lightning.java b/src/actor/Lightning.java index d32860a..0ce7021 100644 --- a/src/actor/Lightning.java +++ b/src/actor/Lightning.java @@ -12,10 +12,10 @@ public class Lightning extends Actor { private static final int LAYER = 3; - private int relateID; + private final int relateID; //type - private int currentType; + private final int currentType; public Lightning(Vec2D spawnPoint, int currentType, int relateID) { super(spawnPoint); diff --git a/src/actor/Portal.java b/src/actor/Portal.java index efe8735..b881ce4 100644 --- a/src/actor/Portal.java +++ b/src/actor/Portal.java @@ -15,7 +15,7 @@ public class Portal extends Actor{ private static final int LAYER = 1; - private int pairID; + private final int pairID; private static int pair = 0; private ArrayList<Actor> temp; diff --git a/src/actor/Projectile.java b/src/actor/Projectile.java index 64a1493..556db76 100644 --- a/src/actor/Projectile.java +++ b/src/actor/Projectile.java @@ -12,14 +12,6 @@ public class Projectile extends Actor { private static final int LAYER = 3; - //Direction constant - private static final Vec2D UP_VEC = new Vec2D(0, -1); - private static final Vec2D DOWN_VEC = new Vec2D(0, 1); - private static final Vec2D LEFT_VEC = new Vec2D(-1, 0); - private static final Vec2D RIGHT_VEC = new Vec2D(1, 0); - - private static final int PROJECTILE_SPEED = 2; - public Projectile(Vec2D spawnPoint, int type) { super(spawnPoint); if (getAnimationManager() == null) { diff --git a/src/actor/Sign.java b/src/actor/Sign.java index 57a1d9e..37b0cac 100644 --- a/src/actor/Sign.java +++ b/src/actor/Sign.java @@ -6,8 +6,6 @@ import bagel.Image; import myUtil.Vec2D; public class Sign extends Actor{ - //type - private static final String TYPE = "Sign"; private static final int LAYER = 0; diff --git a/src/actor/Skeleton.java b/src/actor/Skeleton.java index 54a31dc..4449e25 100644 --- a/src/actor/Skeleton.java +++ b/src/actor/Skeleton.java @@ -4,7 +4,6 @@ import Animation.Animation; import Animation.AnimationStateManager; import bagel.Image; import myUtil.Vec2D; -import Level.*; public class Skeleton extends Actor { //state @@ -129,10 +128,6 @@ public class Skeleton extends Actor { return wanderLoop; } - public int getCurrentType() { - return currentType; - } - public void setCurrentType(int currentType) { this.currentType = currentType; } diff --git a/src/actor/Skull.java b/src/actor/Skull.java index 3d9f1b3..0d9a519 100644 --- a/src/actor/Skull.java +++ b/src/actor/Skull.java @@ -15,7 +15,7 @@ public class Skull extends Actor { private static final int LAYER = 2; //wander loop - private int wanderLoop; + private final int wanderLoop; private int currentWander; Level castToLevel; @@ -105,8 +105,8 @@ public class Skull extends Actor { @Override public Actor clone() { - Actor skull = new Skull(getPosition(), getDirection(), wanderLoop); - ((Skull) skull).setCastToLevel(castToLevel); + Skull skull = new Skull(getPosition(), getDirection(), wanderLoop); + skull.setCastToLevel(castToLevel); return skull; } diff --git a/src/actor/Spider.java b/src/actor/Spider.java index 4cacec1..b3a5f6c 100644 --- a/src/actor/Spider.java +++ b/src/actor/Spider.java @@ -10,14 +10,10 @@ public class Spider extends Actor { private static final int IDLE = 0; private static final int WALK = 1; - //direction choice - private static final int FIRST = 0; - private static final int SECOND = 1; - private static final int LAYER = 2; //wander loop - private int wanderLoop; + private final int wanderLoop; private int currentWander; private int direction0; diff --git a/src/actor/Trap.java b/src/actor/Trap.java index e067226..2b03303 100644 --- a/src/actor/Trap.java +++ b/src/actor/Trap.java @@ -2,7 +2,6 @@ package actor; import Animation.Animation; import Animation.AnimationStateManager; -import Level.Level; import bagel.Image; import myUtil.Vec2D; @@ -41,11 +40,7 @@ public class Trap extends Actor { @Override public void behavior() { - if (!attack) { - attack = true; - } else { - attack = false; - } + attack = !attack; } @Override diff --git a/src/actor/WallMaker.java b/src/actor/WallMaker.java index c39dab5..1eaa9aa 100644 --- a/src/actor/WallMaker.java +++ b/src/actor/WallMaker.java @@ -9,7 +9,7 @@ public class WallMaker extends Actor{ //state private static final int STAND = 0; - private int relateID; + private final int relateID; private static int wID = 0; private static final int LAYER = 2; diff --git a/src/actor/Witch.java b/src/actor/Witch.java index 5eea34c..e36c467 100644 --- a/src/actor/Witch.java +++ b/src/actor/Witch.java @@ -15,7 +15,7 @@ public class Witch extends Actor { private static final int LAYER = 2; //wander loop - private int wanderLoop; + private final int wanderLoop; private int currentWander; Level castToLevel; @@ -95,8 +95,8 @@ public class Witch extends Actor { @Override public Actor clone() { - Actor witch = new Witch(getPosition(), getDirection(), wanderLoop); - ((Witch) witch).setCastToLevel(castToLevel); + Witch witch = new Witch(getPosition(), getDirection(), wanderLoop); + witch.setCastToLevel(castToLevel); return witch; } diff --git a/src/myUtil/MyMath.java b/src/myUtil/MyMath.java index 04b2ec1..89728c4 100644 --- a/src/myUtil/MyMath.java +++ b/src/myUtil/MyMath.java @@ -1,9 +1,6 @@ package myUtil; public class MyMath { - /** - * This class is created for float calculation. - */ /** * Commonly used value */ @@ -239,8 +236,7 @@ public class MyMath { float max = max(b1, b2); float min = min(b1, b2); if (var > max) return max; - if (var < min) return min; - return var; + return Math.max(var, min); } /** -- GitLab