From c2dab23ce48f1131b92b6dfa29c1609e2b949ce1 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 14 Dec 2021 02:18:59 +0900 Subject: [PATCH 1/9] Display settings and options for each setting --- Invaders.iml | 39 ++++++++++ src/engine/DrawManager.java | 125 ++++++++++++++++++++++++++++++--- src/screen/SettingsScreen.java | 118 ++++++++++++++++++++++++++++--- 3 files changed, 264 insertions(+), 18 deletions(-) create mode 100644 Invaders.iml diff --git a/Invaders.iml b/Invaders.iml new file mode 100644 index 000000000..42bcaa3f7 --- /dev/null +++ b/Invaders.iml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/engine/DrawManager.java b/src/engine/DrawManager.java index b01e6ad65..04d851ec7 100644 --- a/src/engine/DrawManager.java +++ b/src/engine/DrawManager.java @@ -549,14 +549,14 @@ public void drawHelp(final Screen screen) { */ public void drawSettings(final Screen screen) { String settingsString = "Settings"; - String[] instructionsStrings = {"Press Space to return", "Use the arrow keys to", "enable/disable settings"}; + String[] instructionsString1 = {"Use A+D / Arrows", "to change settings", "", "Press space to return"}; int i = 0; backBufferGraphics.setColor(Color.GREEN); drawCenteredBigString(screen, settingsString, screen.getHeight() / 8); backBufferGraphics.setColor(Color.GRAY); - for (String instructionsString : instructionsStrings) { + for (String instructionsString : instructionsString1) { drawCenteredRegularString(screen, instructionsString, (int) (screen.getHeight() / 5 + Math.round(fontRegularMetrics.getHeight() * 1.5 * i))); i++; @@ -566,15 +566,124 @@ public void drawSettings(final Screen screen) { } /** - * Draws a centered string on regular font. - * + * Draws settings options + * * @param screen * Screen to draw on. - * @param string - * String to draw. - * @param height - * Height of the drawing. */ + public void drawSettingsOptions(final Screen screen, final int option) { + String screenSizeString = "Screen size"; + String speedString = "Speed"; + String difficultyString = "Difficulty"; + String volumeString = "Volume"; + + if (option == 1) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, screenSizeString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 2); + if (option == 2) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, speedString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 4); + if (option == 3) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, difficultyString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 6); + if (option == 4) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, volumeString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 8); + } + + /** + * Draws settings options to change + * + * @param screen + * Screen to draw on. + */ + public void drawSettingsChange(final Screen screen, final int option, final int change) { + String screenSizeOption1 = "Standard"; + String screenSizeOption2 = "Extended"; + String screenSizeOption3 = "Full screen"; + String speedOption1 = "Slow"; + String speedOption2 = "Regular"; + String speedOption3 = "Fast"; + String difficultyOption1 = "Easy"; + String difficultyOption2 = "Medium"; + String difficultyOption3 = "Hard"; + String volumeOption1 = "25%"; + String volumeOption2 = "50%"; + String volumeOption3 = "75%"; + String volumeOption4 = "100%"; + + String displayOption = "Nothing to display"; + + + // Screen size + if (option == 1) { + if (change == 1) + displayOption = screenSizeOption1; + if (change == 2) + displayOption = screenSizeOption2; + if (change == 3) + displayOption = screenSizeOption3; + } + // Speed + else if (option == 2) { + if (change == 1) + displayOption = speedOption1; + if (change == 2) + displayOption = speedOption2; + if (change == 3) + displayOption = speedOption3; + } + // Difficulty + else if (option == 3) { + if (change == 1) + displayOption = difficultyOption1; + if (change == 2) + displayOption = difficultyOption2; + if (change == 3) + displayOption = difficultyOption3; + } + // Volume + else if (option == 4) { + if (change == 1) + displayOption = volumeOption1; + if (change == 2) + displayOption = volumeOption2; + if (change == 3) + displayOption = volumeOption3; + if (change == 4) + displayOption = volumeOption4; + } + // Default + else { + displayOption = "Nothing to display"; + } + + backBufferGraphics.setColor(Color.darkGray); + drawCenteredBigString(screen, displayOption, screen.getHeight() / 2); + } + + /** + * Draws a centered string on regular font. + * + * @param screen + * Screen to draw on. + * @param string + * String to draw. + * @param height + * Height of the drawing. + */ public void drawCenteredRegularString(final Screen screen, final String string, final int height) { backBufferGraphics.setFont(fontRegular); diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index a257afe4f..8ba119f68 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -1,17 +1,25 @@ package screen; import java.awt.event.KeyEvent; -import java.io.IOException; -import java.util.List; - +import engine.Cooldown; import engine.Core; -import engine.Score; /** - * Implements the help screen, it shows the player instructions on how to play the game. + * Implements the Settings screen, it shows the options to change the settings of the game. */ public class SettingsScreen extends Screen { + /** Number of options in the settings menu. */ + private static final int NO_OF_OPTIONS = 4; + /** Current settings option. */ + private static int settingsOption; + /** Change settings. */ + private static int change; + /** Milliseconds between changes in user selection. */ + private static final int SELECTION_TIME = 200; + /** Time between changes in user selection. */ + private Cooldown selectionCooldown; + /** * Constructor, establishes the properties of the screen. * @@ -26,6 +34,11 @@ public SettingsScreen(final int width, final int height, final int fps) { super(width, height, fps); this.returnCode = 1; + this.settingsOption = 1; + this.change = 1; + + this.selectionCooldown = Core.getCooldown(SELECTION_TIME); + this.selectionCooldown.reset(); } /** @@ -46,9 +59,94 @@ protected final void update() { super.update(); draw(); - if (inputManager.isKeyDown(KeyEvent.VK_SPACE) - && this.inputDelay.checkFinished()) - this.isRunning = false; + + if (this.selectionCooldown.checkFinished() + && this.inputDelay.checkFinished()) { + if (inputManager.isKeyDown(KeyEvent.VK_UP) + || inputManager.isKeyDown(KeyEvent.VK_W)) { + previousMenuItem(); + this.change = 1; + this.selectionCooldown.reset(); + } + if (inputManager.isKeyDown(KeyEvent.VK_DOWN) + || inputManager.isKeyDown(KeyEvent.VK_S)) { + nextMenuItem(); + this.change = 1; + this.selectionCooldown.reset(); + } + if (inputManager.isKeyDown(KeyEvent.VK_LEFT) + || inputManager.isKeyDown(KeyEvent.VK_A)) { + previousMenuChange(); + this.selectionCooldown.reset(); + } + if (inputManager.isKeyDown(KeyEvent.VK_RIGHT) + || inputManager.isKeyDown(KeyEvent.VK_D)) { + nextMenuChange(); + this.selectionCooldown.reset(); + } + if (inputManager.isKeyDown(KeyEvent.VK_SPACE)) + this.isRunning = false; + } + } + + /** + * Shifts the focus to the next menu item. + */ + private void nextMenuItem() { + if (this.settingsOption == NO_OF_OPTIONS) + this.settingsOption = 1; + else if (this.settingsOption == 1) + this.settingsOption = 2; + else + this.settingsOption++; + } + + /** + * Shifts the focus to the previous menu item. + */ + private void previousMenuItem() { + if (this.settingsOption == 1) + this.settingsOption = NO_OF_OPTIONS; + else if (this.settingsOption == 2) + this.settingsOption = 1; + else + this.settingsOption--; + } + + /** + * Shifts the focus to the next change in the settings option. + */ + private void nextMenuChange() { + int no_of_changes = 3; + + if(this.settingsOption == 4) + no_of_changes = 4; + + if (this.change == no_of_changes) + this.change = 1; + else if (this.change == 1) + this.change = 2; + else + this.change++; + } + + /** + * Shifts the focus to the previous change in the settings option. + */ + private void previousMenuChange() { + int no_of_changes; + + if(this.settingsOption == 4) + no_of_changes = 4; + else + no_of_changes = 3; + + if (this.change == 1) + this.change = no_of_changes; + else if (this.change == 2) + this.change = 1; + else + this.change--; } /** @@ -56,9 +154,9 @@ protected final void update() { */ private void draw() { drawManager.initDrawing(this); - drawManager.drawSettings(this); - + drawManager.drawSettingsChange(this, settingsOption, change); + drawManager.drawSettingsOptions(this, settingsOption); drawManager.completeDrawing(this); } } \ No newline at end of file From 608afd26c613be4c342a58c2780570cb8834d5f6 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 14 Dec 2021 03:18:10 +0900 Subject: [PATCH 2/9] Change screen size --- src/engine/Core.java | 17 +++++++++++++++-- src/screen/SettingsScreen.java | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/engine/Core.java b/src/engine/Core.java index 4bb8469fc..2d8d384bf 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -19,9 +19,9 @@ public final class Core { /** Width of current screen. */ - private static final int WIDTH = 448; + private static int WIDTH = 448; /** Height of current screen. */ - private static final int HEIGHT = 520; + private static int HEIGHT = 520; /** Max fps of current screen. */ private static final int FPS = 60; @@ -117,6 +117,14 @@ public static void main(final String[] args) { switch (returnCode) { case 1: // Main menu. + /* This makes the old window disappear */ + frame.setVisible(false); + /* This creates a new window with new width & height values */ + frame = new Frame(WIDTH, HEIGHT); + DrawManager.getInstance().setFrame(frame); + width = frame.getWidth(); + height = frame.getHeight(); + currentScreen = new TitleScreen(width, height, FPS); LOGGER.info("Starting " + WIDTH + "x" + HEIGHT + " title screen at " + FPS + " fps."); @@ -262,4 +270,9 @@ public static Cooldown getVariableCooldown(final int milliseconds, final int variance) { return new Cooldown(milliseconds, variance); } + + public static void setSize(int width, int height) { + WIDTH = width; + HEIGHT = height; + } } \ No newline at end of file diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index 8ba119f68..5171018bc 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -1,5 +1,6 @@ package screen; +import java.awt.*; import java.awt.event.KeyEvent; import engine.Cooldown; import engine.Core; @@ -19,6 +20,8 @@ public class SettingsScreen extends Screen { private static final int SELECTION_TIME = 200; /** Time between changes in user selection. */ private Cooldown selectionCooldown; + /** Get screen size using the Toolkit class */ + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); /** * Constructor, establishes the properties of the screen. @@ -86,9 +89,26 @@ protected final void update() { } if (inputManager.isKeyDown(KeyEvent.VK_SPACE)) this.isRunning = false; + + // Change screen size + if (settingsOption == 1) { + changeScreenSize(); + } } } + /** + * Changes the screen size + */ + private void changeScreenSize() { + if (this.change == 1) + Core.setSize(448, 520); + else if (this.change == 2) + Core.setSize(1020,520); + else if (this.change == 3) + Core.setSize(screenSize.width, screenSize.height); + } + /** * Shifts the focus to the next menu item. */ From c939f995f48d09ad00865a99a6a3e067e86432d1 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 14:54:53 +0900 Subject: [PATCH 3/9] Testing volume control --- src/engine/Core.java | 11 +++---- src/engine/DrawManager.java | 36 +++++++--------------- src/engine/Sound.java | 17 +++++++++++ src/screen/SettingsScreen.java | 55 ++++++++++++++++++++++++++++++---- 4 files changed, 82 insertions(+), 37 deletions(-) diff --git a/src/engine/Core.java b/src/engine/Core.java index 2d8d384bf..d3bcbbedd 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -61,7 +61,7 @@ public final class Core { /** Difficulty settings list. */ private static List gameSettings; /** Application logger. */ - private static final Logger LOGGER = Logger.getLogger(Core.class + public static final Logger LOGGER = Logger.getLogger(Core.class .getSimpleName()); /** Logger handler for printing to disk. */ private static Handler fileHandler; @@ -110,6 +110,9 @@ public static void main(final String[] args) { GameState gameState; + // Play music + Sound.playMusic(); + int returnCode = 1; do { gameState = new GameState(1, 0, MAX_LIVES, 0, 0); @@ -117,10 +120,8 @@ public static void main(final String[] args) { switch (returnCode) { case 1: // Main menu. - /* This makes the old window disappear */ - frame.setVisible(false); - /* This creates a new window with new width & height values */ - frame = new Frame(WIDTH, HEIGHT); + frame.setVisible(false); // This makes the old window disappear + frame = new Frame(WIDTH, HEIGHT); // This creates a new window with new width & height values DrawManager.getInstance().setFrame(frame); width = frame.getWidth(); height = frame.getHeight(); diff --git a/src/engine/DrawManager.java b/src/engine/DrawManager.java index 04d851ec7..15f7c27cc 100644 --- a/src/engine/DrawManager.java +++ b/src/engine/DrawManager.java @@ -549,7 +549,8 @@ public void drawHelp(final Screen screen) { */ public void drawSettings(final Screen screen) { String settingsString = "Settings"; - String[] instructionsString1 = {"Use A+D / Arrows", "to change settings", "", "Press space to return"}; + String[] instructionsString1 = {"Use A+D / Arrows", "to change settings", "", "Press space to return", + "and show changes"}; int i = 0; backBufferGraphics.setColor(Color.GREEN); @@ -573,7 +574,6 @@ public void drawSettings(final Screen screen) { */ public void drawSettingsOptions(final Screen screen, final int option) { String screenSizeString = "Screen size"; - String speedString = "Speed"; String difficultyString = "Difficulty"; String volumeString = "Volume"; @@ -582,20 +582,14 @@ public void drawSettingsOptions(final Screen screen, final int option) { else backBufferGraphics.setColor(Color.WHITE); drawCenteredRegularString(screen, screenSizeString, - screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 2); - if (option == 2) - backBufferGraphics.setColor(Color.GREEN); - else - backBufferGraphics.setColor(Color.WHITE); - drawCenteredRegularString(screen, speedString, screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 4); - if (option == 3) + if (option == 2) backBufferGraphics.setColor(Color.GREEN); else backBufferGraphics.setColor(Color.WHITE); drawCenteredRegularString(screen, difficultyString, screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 6); - if (option == 4) + if (option == 3) backBufferGraphics.setColor(Color.GREEN); else backBufferGraphics.setColor(Color.WHITE); @@ -613,9 +607,6 @@ public void drawSettingsChange(final Screen screen, final int option, final int String screenSizeOption1 = "Standard"; String screenSizeOption2 = "Extended"; String screenSizeOption3 = "Full screen"; - String speedOption1 = "Slow"; - String speedOption2 = "Regular"; - String speedOption3 = "Fast"; String difficultyOption1 = "Easy"; String difficultyOption2 = "Medium"; String difficultyOption3 = "Hard"; @@ -623,7 +614,7 @@ public void drawSettingsChange(final Screen screen, final int option, final int String volumeOption2 = "50%"; String volumeOption3 = "75%"; String volumeOption4 = "100%"; - + String volumeOption5 = "Mute"; String displayOption = "Nothing to display"; @@ -636,17 +627,8 @@ public void drawSettingsChange(final Screen screen, final int option, final int if (change == 3) displayOption = screenSizeOption3; } - // Speed - else if (option == 2) { - if (change == 1) - displayOption = speedOption1; - if (change == 2) - displayOption = speedOption2; - if (change == 3) - displayOption = speedOption3; - } // Difficulty - else if (option == 3) { + else if (option == 2) { if (change == 1) displayOption = difficultyOption1; if (change == 2) @@ -655,7 +637,7 @@ else if (option == 3) { displayOption = difficultyOption3; } // Volume - else if (option == 4) { + else if (option == 3) { if (change == 1) displayOption = volumeOption1; if (change == 2) @@ -664,6 +646,8 @@ else if (option == 4) { displayOption = volumeOption3; if (change == 4) displayOption = volumeOption4; + if (change == 5) + displayOption = volumeOption5; } // Default else { @@ -671,7 +655,7 @@ else if (option == 4) { } backBufferGraphics.setColor(Color.darkGray); - drawCenteredBigString(screen, displayOption, screen.getHeight() / 2); + drawCenteredBigString(screen, displayOption, screen.getHeight() / 2 + fontRegularMetrics.getHeight() * 2); } /** diff --git a/src/engine/Sound.java b/src/engine/Sound.java index 8b0aa1b1c..b6834abc0 100644 --- a/src/engine/Sound.java +++ b/src/engine/Sound.java @@ -4,10 +4,13 @@ import javax.sound.sampled.*; import java.io.File; +import screen.SettingsScreen; public class Sound { + private static float decibels = 0; + public static void playMusic() //background music class { File musicfile = new File("sounds/music.wav"); @@ -16,7 +19,9 @@ public static void playMusic() //background music class AudioInputStream Audio = AudioSystem.getAudioInputStream(musicfile); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); + SettingsScreen.adjustMusicVolume(clip); clip.loop(Clip.LOOP_CONTINUOUSLY); } catch(Exception e) @@ -33,6 +38,7 @@ public static void shoot() //shooting sound effect AudioInputStream Audio = AudioSystem.getAudioInputStream(shot); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); } catch(Exception e) @@ -49,6 +55,7 @@ public static void death() //shooting sound effect AudioInputStream Audio = AudioSystem.getAudioInputStream(dead); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); } catch(Exception e) @@ -57,4 +64,14 @@ public static void death() //shooting sound effect } } + public static void setGain(Clip clip) { + FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + gainControl.setValue(gainControl.getValue()-decibels); // Reduce volume by decibels + Core.LOGGER.info("Decibel value is " + decibels); + } + + public static void setDecibels(float newDecibels) { + decibels = newDecibels; + } + } diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index 5171018bc..f03479f68 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -4,6 +4,9 @@ import java.awt.event.KeyEvent; import engine.Cooldown; import engine.Core; +import engine.Sound; + +import javax.sound.sampled.Clip; /** * Implements the Settings screen, it shows the options to change the settings of the game. @@ -11,7 +14,7 @@ public class SettingsScreen extends Screen { /** Number of options in the settings menu. */ - private static final int NO_OF_OPTIONS = 4; + private static final int NO_OF_OPTIONS = 3; /** Current settings option. */ private static int settingsOption; /** Change settings. */ @@ -91,12 +94,48 @@ protected final void update() { this.isRunning = false; // Change screen size - if (settingsOption == 1) { + if (this.settingsOption == 1) { changeScreenSize(); } + + // Adjust volume + if (this.settingsOption == 3) { + adjustVolume(); + } } } + /** + * Adjust volume + */ + private void adjustVolume() { + // 25% volume + if (this.change == 1) + Sound.setDecibels(40); + // 50% volume + if (this.change == 2) + Sound.setDecibels(50); + // 75% volume + if (this.change == 3) + Sound.setDecibels(60); + // 100% volume + if (this.change == 4) + Sound.setDecibels(0); + // 0% volume + if (this.change == 5) + Sound.setDecibels(100); + } + + /** + * Adjust BGM volume + */ + public static void adjustMusicVolume(Clip clip) { + if (settingsOption == 3) + Sound.setGain(clip); + clip.stop(); + clip.start(); + } + /** * Changes the screen size */ @@ -139,8 +178,8 @@ else if (this.settingsOption == 2) private void nextMenuChange() { int no_of_changes = 3; - if(this.settingsOption == 4) - no_of_changes = 4; + if(this.settingsOption == 3) + no_of_changes = 5; if (this.change == no_of_changes) this.change = 1; @@ -156,8 +195,8 @@ else if (this.change == 1) private void previousMenuChange() { int no_of_changes; - if(this.settingsOption == 4) - no_of_changes = 4; + if(this.settingsOption == 3) + no_of_changes = 5; else no_of_changes = 3; @@ -179,4 +218,8 @@ private void draw() { drawManager.drawSettingsOptions(this, settingsOption); drawManager.completeDrawing(this); } + + public static int getChange() { + return change; + } } \ No newline at end of file From 19c33931b950a04550f0957309705babe08332e4 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 15:01:01 +0900 Subject: [PATCH 4/9] Add volume feature --- src/engine/Core.java | 2 +- src/engine/Sound.java | 16 +++++++++------- src/screen/SettingsScreen.java | 10 ---------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/engine/Core.java b/src/engine/Core.java index d3bcbbedd..dc86ff69d 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -111,7 +111,7 @@ public static void main(final String[] args) { GameState gameState; // Play music - Sound.playMusic(); + //Sound.playMusic(); int returnCode = 1; do { diff --git a/src/engine/Sound.java b/src/engine/Sound.java index 468880509..56e02a8fd 100644 --- a/src/engine/Sound.java +++ b/src/engine/Sound.java @@ -21,7 +21,6 @@ public static void playMusic() //background music class clip.open(Audio); setGain(clip); clip.start(); - SettingsScreen.adjustMusicVolume(clip); clip.loop(Clip.LOOP_CONTINUOUSLY); } catch(Exception e) @@ -64,12 +63,6 @@ public static void death() //shooting sound effect } } - public static void setGain(Clip clip) { - FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); - gainControl.setValue(gainControl.getValue()-decibels); // Reduce volume by decibels - Core.LOGGER.info("Decibel value is " + decibels); - } - public static void setDecibels(float newDecibels) { decibels = newDecibels; } @@ -82,6 +75,7 @@ public static void enemydeath() //shooting sound effect AudioInputStream Audio = AudioSystem.getAudioInputStream(bang); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); } catch(Exception e) @@ -98,6 +92,7 @@ public static void enemyshot() //shooting sound effect AudioInputStream Audio = AudioSystem.getAudioInputStream(zap); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); } catch(Exception e) @@ -114,6 +109,7 @@ public static void gameover() //shooting sound effect AudioInputStream Audio = AudioSystem.getAudioInputStream(over); Clip clip = AudioSystem.getClip(); clip.open(Audio); + setGain(clip); clip.start(); } catch(Exception e) @@ -122,4 +118,10 @@ public static void gameover() //shooting sound effect } } + public static void setGain(Clip clip) { + FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + gainControl.setValue(-decibels); // Reduce volume by decibels + } + + } diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index f03479f68..a12d9d894 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -126,16 +126,6 @@ private void adjustVolume() { Sound.setDecibels(100); } - /** - * Adjust BGM volume - */ - public static void adjustMusicVolume(Clip clip) { - if (settingsOption == 3) - Sound.setGain(clip); - clip.stop(); - clip.start(); - } - /** * Changes the screen size */ From 90ab2510ed6afd1dda4e9ab545d07cad0b866b2a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 15:11:04 +0900 Subject: [PATCH 5/9] Update from upstream --- src/engine/Core.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/Core.java b/src/engine/Core.java index 2d8d384bf..6cbc609f6 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -109,7 +109,7 @@ public static void main(final String[] args) { gameSettings.add(SETTINGS_LEVEL_7); GameState gameState; - + Sound.playMusic(); int returnCode = 1; do { gameState = new GameState(1, 0, MAX_LIVES, 0, 0); From 7f56310d7a35c55d1ece263a8fcf8496fc6c2fa8 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 16:52:23 +0900 Subject: [PATCH 6/9] Fix bgm music volume adjustment --- src/engine/Core.java | 2 ++ src/engine/DrawManager.java | 2 +- src/engine/Sound.java | 20 ++++++++++++++++--- src/screen/SettingsScreen.java | 36 +++++++++++++++++++--------------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/engine/Core.java b/src/engine/Core.java index 1f7b1dc4c..08abb5779 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -109,7 +109,9 @@ public static void main(final String[] args) { gameSettings.add(SETTINGS_LEVEL_7); GameState gameState; + Sound.playMusic(); + int returnCode = 1; do { gameState = new GameState(1, 0, MAX_LIVES, 0, 0); diff --git a/src/engine/DrawManager.java b/src/engine/DrawManager.java index 15f7c27cc..ed9dee4af 100644 --- a/src/engine/DrawManager.java +++ b/src/engine/DrawManager.java @@ -550,7 +550,7 @@ public void drawHelp(final Screen screen) { public void drawSettings(final Screen screen) { String settingsString = "Settings"; String[] instructionsString1 = {"Use A+D / Arrows", "to change settings", "", "Press space to return", - "and show changes"}; + "and apply changes"}; int i = 0; backBufferGraphics.setColor(Color.GREEN); diff --git a/src/engine/Sound.java b/src/engine/Sound.java index 56e02a8fd..b86be23eb 100644 --- a/src/engine/Sound.java +++ b/src/engine/Sound.java @@ -11,6 +11,8 @@ public class Sound private static float decibels = 0; + private static Clip musicClip; + public static void playMusic() //background music class { File musicfile = new File("sounds/music.wav"); @@ -22,6 +24,8 @@ public static void playMusic() //background music class setGain(clip); clip.start(); clip.loop(Clip.LOOP_CONTINUOUSLY); + + musicClip = clip; } catch(Exception e) { @@ -56,6 +60,11 @@ public static void death() //shooting sound effect clip.open(Audio); setGain(clip); clip.start(); + + if (SettingsScreen.getMusicRestart()) { + clip.stop(); + } + } catch(Exception e) { @@ -63,9 +72,7 @@ public static void death() //shooting sound effect } } - public static void setDecibels(float newDecibels) { - decibels = newDecibels; - } + public static void enemydeath() //shooting sound effect { @@ -118,10 +125,17 @@ public static void gameover() //shooting sound effect } } + public static void stop() { + musicClip.stop(); + } + public static void setGain(Clip clip) { FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-decibels); // Reduce volume by decibels } + public static void setDecibels(float newDecibels) { + decibels = newDecibels; + } } diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index a12d9d894..a57f61876 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -19,6 +19,8 @@ public class SettingsScreen extends Screen { private static int settingsOption; /** Change settings. */ private static int change; + /** Restart music */ + private static boolean musicRestart; /** Milliseconds between changes in user selection. */ private static final int SELECTION_TIME = 200; /** Time between changes in user selection. */ @@ -90,8 +92,11 @@ protected final void update() { nextMenuChange(); this.selectionCooldown.reset(); } - if (inputManager.isKeyDown(KeyEvent.VK_SPACE)) + if (inputManager.isKeyDown(KeyEvent.VK_SPACE)) { + Sound.stop(); + Sound.playMusic(); this.isRunning = false; + } // Change screen size if (this.settingsOption == 1) { @@ -109,20 +114,15 @@ protected final void update() { * Adjust volume */ private void adjustVolume() { - // 25% volume - if (this.change == 1) + if (this.change == 1) // 25% volume Sound.setDecibels(40); - // 50% volume - if (this.change == 2) - Sound.setDecibels(50); - // 75% volume - if (this.change == 3) - Sound.setDecibels(60); - // 100% volume - if (this.change == 4) + if (this.change == 2) // 50% volume + Sound.setDecibels(35); + if (this.change == 3) // 75% volume + Sound.setDecibels(30); + if (this.change == 4) // 100% volume Sound.setDecibels(0); - // 0% volume - if (this.change == 5) + if (this.change == 5) // Mute Sound.setDecibels(100); } @@ -130,11 +130,11 @@ private void adjustVolume() { * Changes the screen size */ private void changeScreenSize() { - if (this.change == 1) + if (this.change == 1) // Standard Core.setSize(448, 520); - else if (this.change == 2) + else if (this.change == 2) // Extended Core.setSize(1020,520); - else if (this.change == 3) + else if (this.change == 3) // Full Screen Core.setSize(screenSize.width, screenSize.height); } @@ -212,4 +212,8 @@ private void draw() { public static int getChange() { return change; } + + public static boolean getMusicRestart() { + return musicRestart; + } } \ No newline at end of file From 13ce1f9f4b281ad8686d834369dbf33006b34b28 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 18:17:47 +0900 Subject: [PATCH 7/9] Preparing for difficulty settings option --- src/screen/SettingsScreen.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index a57f61876..352f4aef0 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -107,9 +107,26 @@ protected final void update() { if (this.settingsOption == 3) { adjustVolume(); } + + // Change difficulty + if (this.settingsOption == 3) { + changeDifficulty(); + } } } + /** + * Change difficulty level + */ + private void changeDifficulty() { + if (this.change == 1) + + if (this.change == 2) + + if (this.change == 3) + + } + /** * Adjust volume */ From bed4c216c4f4737050c9d9fa992d608faf825a3e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 19:04:28 +0900 Subject: [PATCH 8/9] Add difficulty setting --- Invaders.iml | 16 +++++++++++ src/engine/Sound.java | 5 ---- src/entity/EnemyShip.java | 2 +- src/entity/EnemyShipFormation.java | 43 +++++++++++++++++++++++++++--- src/screen/SettingsScreen.java | 37 ++++++++++++++----------- 5 files changed, 78 insertions(+), 25 deletions(-) diff --git a/Invaders.iml b/Invaders.iml index c0a3a9681..7d1b59426 100644 --- a/Invaders.iml +++ b/Invaders.iml @@ -25,5 +25,21 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/engine/Sound.java b/src/engine/Sound.java index b86be23eb..d035b1f40 100644 --- a/src/engine/Sound.java +++ b/src/engine/Sound.java @@ -60,11 +60,6 @@ public static void death() //shooting sound effect clip.open(Audio); setGain(clip); clip.start(); - - if (SettingsScreen.getMusicRestart()) { - clip.stop(); - } - } catch(Exception e) { diff --git a/src/entity/EnemyShip.java b/src/entity/EnemyShip.java index 73a0897a4..862d37338 100644 --- a/src/entity/EnemyShip.java +++ b/src/entity/EnemyShip.java @@ -28,7 +28,7 @@ public class EnemyShip extends Entity { private static final HashMap HEALTH_COLOR_CODES = new HashMap(){{ put(1, Color.WHITE); - put(3, Color.GREEN); + put(3, Color.YELLOW); put(5, Color.BLUE); }}; diff --git a/src/entity/EnemyShipFormation.java b/src/entity/EnemyShipFormation.java index 8d6b904b3..a8954e386 100644 --- a/src/entity/EnemyShipFormation.java +++ b/src/entity/EnemyShipFormation.java @@ -91,6 +91,14 @@ public class EnemyShipFormation implements Iterable { private List shooters; /** Number of not destroyed ships. */ private int shipCount; + /** Set health of enemy ships. */ + private static int health; + /** Health of enemy ships type A. */ + private static int A_health = 1; + /** Health of enemy ships type B. */ + private static int B_health = 3; + /** Health of enemy ships type C. */ + private static int C_health = 5; /** Directions the formation can move. */ private enum Direction { @@ -135,18 +143,24 @@ public EnemyShipFormation(final GameSettings gameSettings) { for (List column : this.enemyShips) { for (int i = 0; i < this.nShipsHigh; i++) { - if (i / (float) this.nShipsHigh < PROPORTION_C) + if (i / (float) this.nShipsHigh < PROPORTION_C) { spriteType = SpriteType.EnemyShipC1; + health = C_health; + } else if (i / (float) this.nShipsHigh < PROPORTION_B - + PROPORTION_C) + + PROPORTION_C) { spriteType = SpriteType.EnemyShipB1; - else + health = B_health; + } + else { spriteType = SpriteType.EnemyShipA1; + health = A_health; + } column.add(new EnemyShip((SEPARATION_DISTANCE * this.enemyShips.indexOf(column)) + positionX, (SEPARATION_DISTANCE * i) - + positionY, spriteType, 1)); + + positionY, spriteType, health)); this.shipCount++; } } @@ -424,4 +438,25 @@ public final Iterator iterator() { public final boolean isEmpty() { return this.shipCount <= 0; } + + /** + * Set health for enemy ships type A. + */ + public static void setA_Health(int newHealth) { + A_health = newHealth; + } + + /** + * Set health for enemy ships type A. + */ + public static void setB_Health(int newHealth) { + B_health = newHealth; + } + + /** + * Set health for enemy ships type A. + */ + public static void setC_Health(int newHealth) { + C_health = newHealth; + } } diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java index 352f4aef0..1d3015499 100644 --- a/src/screen/SettingsScreen.java +++ b/src/screen/SettingsScreen.java @@ -5,6 +5,7 @@ import engine.Cooldown; import engine.Core; import engine.Sound; +import entity.EnemyShipFormation; import javax.sound.sampled.Clip; @@ -19,8 +20,6 @@ public class SettingsScreen extends Screen { private static int settingsOption; /** Change settings. */ private static int change; - /** Restart music */ - private static boolean musicRestart; /** Milliseconds between changes in user selection. */ private static final int SELECTION_TIME = 200; /** Time between changes in user selection. */ @@ -103,14 +102,14 @@ protected final void update() { changeScreenSize(); } - // Adjust volume - if (this.settingsOption == 3) { - adjustVolume(); + // Change difficulty + if (this.settingsOption == 2) { + changeDifficulty(); } - // Change difficulty + // Adjust volume if (this.settingsOption == 3) { - changeDifficulty(); + adjustVolume(); } } } @@ -119,12 +118,24 @@ protected final void update() { * Change difficulty level */ private void changeDifficulty() { - if (this.change == 1) + if (this.change == 1) { // Easy + EnemyShipFormation.setA_Health(1); + EnemyShipFormation.setB_Health(1); + EnemyShipFormation.setC_Health(1); + } + + if (this.change == 2) { // Medium + EnemyShipFormation.setA_Health(1); + EnemyShipFormation.setB_Health(3); + EnemyShipFormation.setC_Health(5); + } - if (this.change == 2) + if (this.change == 3) { // Hard + EnemyShipFormation.setA_Health(5); + EnemyShipFormation.setB_Health(5); + EnemyShipFormation.setC_Health(5); + } - if (this.change == 3) - } /** @@ -229,8 +240,4 @@ private void draw() { public static int getChange() { return change; } - - public static boolean getMusicRestart() { - return musicRestart; - } } \ No newline at end of file From b6882db4454f67bac78ce02dce6fab18088867a0 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Dec 2021 19:07:49 +0900 Subject: [PATCH 9/9] Change EnemyShipTest.java file --- test/entity/EnemyShipTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/entity/EnemyShipTest.java b/test/entity/EnemyShipTest.java index a8083e740..6ef8cf05c 100644 --- a/test/entity/EnemyShipTest.java +++ b/test/entity/EnemyShipTest.java @@ -35,6 +35,6 @@ void reduceHealthWithColorChange() { testShip.reduceHealth(); testShip.reduceHealth(); assertEquals(3, testShip.getHealth()); - assertEquals(Color.GREEN, testShip.getColor()); + assertEquals(Color.YELLOW, testShip.getColor()); } } \ No newline at end of file