Skip to content

Commit

Permalink
Merge pull request #86 from yunsseong/main
Browse files Browse the repository at this point in the history
Add sound features and fix bugs
  • Loading branch information
math-odd authored Oct 20, 2023
2 parents 7a156ff + a8268f1 commit 6482a12
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 18 deletions.
12 changes: 10 additions & 2 deletions res/scores_1p
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
AAA
2350
AAA
2110
AAA
2030
AAA
1930
BBB
-30
AAA
1320
AAA
1030
AAA
1010
Binary file modified res/sound/SFX/S_Achievement.wav
Binary file not shown.
Binary file added res/sound/SFX/S_Item_Bomb_Equipped.wav
Binary file not shown.
Binary file added res/sound/SFX/S_Item_Create.wav
Binary file not shown.
3 changes: 2 additions & 1 deletion src/engine/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public static void main(final String[] args) {
break;
case 1:
// Main menu.
SoundManager.resetBGM();
SoundManager.playSound("BGM/B_Main_a", "menu", true, true, 2f);
currentScreen = new TitleScreen(width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
Expand Down Expand Up @@ -254,11 +255,11 @@ public static void main(final String[] args) {
}
currentScreen = new ScoreScreen(width, height, FPS, gameState);
SoundManager.resetBGM();
SoundManager.stopSound("ship_moving");
SoundManager.playSound("BGM/B_Gameover", "B_gameover", true, true, 2f);
SoundManager.playSound("SFX/S_Gameover","S_gameover",false,false);
returnCode = frame.setScreen(currentScreen);
SoundManager.stopSound("B_gameover",2f);
SoundManager.stopSound("S_gameover",2f);
LOGGER.info("Closing score screen.");
break;
case 3:
Expand Down
46 changes: 35 additions & 11 deletions src/engine/SoundManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.FloatControl.Type;

Expand Down Expand Up @@ -42,13 +43,21 @@ public void run() {
clip.open(audioIn);
FloatControl floatControl = (FloatControl)clip.getControl(Type.MASTER_GAIN);
floatControl.setValue(master);
clips.put(clipName, clip);
if(isBgm) {
bgms.add(clip);
} else {
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
});
}
if (isLoop) {
clip.loop(-1);
} else {
clip.start();
}
clips.put(clipName, clip);
if(isBgm) bgms.add(clip);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -70,14 +79,22 @@ public void run() {
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
FloatControl floatControl = (FloatControl) clip.getControl(Type.MASTER_GAIN);
floatControl.setValue((float)minimum);
floatControl.setValue(minimum);
clips.put(clipName, clip);
if(isBgm) {
bgms.add(clip);
} else {
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
});
}
if (isLoop) {
clip.loop(-1);
} else {
clip.start();
}
clips.put(clipName, clip);
if(isBgm) bgms.add(clip);
fadeIn(clip, fadeInSpeed);
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -89,8 +106,9 @@ public void run() {
public static void stopSound(String clipName) {
Clip clip = clips.get(clipName);
if (clip != null && clip.isActive()) {
clip.stop();
bgms.remove(clip);
clips.remove(clipName);
clip.close();
}
}

Expand Down Expand Up @@ -118,8 +136,9 @@ public void run() {
throw new RuntimeException(e);
}
}
clip.stop();
bgms.remove(clip);
clips.remove(clipName);
clip.close();
}
}).start();
}
Expand Down Expand Up @@ -153,7 +172,6 @@ public void run() {
public static void setMasterVolume(float volume) {
masterVolume = volume;
master = getValue(masterVolume);
System.out.println("master = "+master + "masterVolume = "+masterVolume);
if(master > maximum) master = maximum;
else if(master < minimum) master = minimum;
for (Clip clip : clips.values()) {
Expand All @@ -168,7 +186,7 @@ public static void bgmSetting(boolean bgm){
if(bgm){
for(Clip clip : bgms){
FloatControl floatControl = (FloatControl)clip.getControl(Type.MASTER_GAIN);
floatControl.setValue(getValue(master));
floatControl.setValue(getValue(masterVolume));
}
}
else{
Expand All @@ -179,6 +197,11 @@ public static void bgmSetting(boolean bgm){
}
}

private static float getVolume(float res) {
double temp = (res - minimum) / one;
return (float) Math.pow(10, temp / 50);
}

private static float getValue(float volume){
float res = (float)(minimum + one*(50*Math.log10(volume)));
if(res<minimum) return minimum;
Expand All @@ -192,10 +215,11 @@ public static boolean isPlaying(String clipName){
else return false;
}

public static void setVolume(String clipName, float volume){
public static void setVolume(String clipName, float percent){
Clip clip = clips.get(clipName);
FloatControl floatcontrol = (FloatControl)clip.getControl(Type.MASTER_GAIN);
floatcontrol.setValue(getValue(volume));
float volume = getVolume(floatcontrol.getValue());
floatcontrol.setValue(getValue((percent/100)*volume));
}

public static void playBGM(int levelNum) {
Expand Down
4 changes: 4 additions & 0 deletions src/entity/EnemyShipFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ public final void destroy(final EnemyShip destroyedShip) {
this.shooters.remove(destroyedShipIndex);
this.logger.info("Shooters list reduced to "
+ this.shooters.size() + " members.");
if (this.shooters.isEmpty())
SoundManager.playSound("SFX/S_LevelClear", "level_start_count", false, false);
}
}
if (destroyedShip.isDestroyed()) this.shipCount--;
Expand Down Expand Up @@ -572,6 +574,8 @@ public final List<EnemyShip> destroyByBomb(final EnemyShip destroyedShip) {
this.shooters.remove(destroyedShipIndex);
this.logger.info("Shooters list reduced to "
+ this.shooters.size() + " members.");
if (this.shooters.isEmpty())
SoundManager.playSound("SFX/S_LevelClear", "level_start_count", false, false);
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,14 @@ private void draw() {

// Countdown to game start.
if (!this.inputDelay.checkFinished()) {

int countdown = (int) ((INPUT_DELAY
- (System.currentTimeMillis()
- this.gameStartTime)) / 1000);
long beep = ((INPUT_DELAY - (System.currentTimeMillis() - this.gameStartTime)));
if ((beep<4000 && beep>3984) || (beep<3000 && beep>2984) || (beep<2000 && beep>1984))

if ((beep<3995 && beep>3975) || (beep<2995 && beep>2975) || (beep<1995 && beep>1975))
SoundManager.playSound("SFX/S_LevelStart_b", "level_start_beep", false, false);
if ((beep<1000 && beep>984))
if ((beep<995 && beep>975))
SoundManager.playSound("SFX/S_LevelStart_a", "level_start_count", false, false);
drawManager.drawCountDown(this, this.level, countdown,
this.bonusLife);
Expand Down Expand Up @@ -853,10 +853,12 @@ private void manageCollisions() {

if (enemyShip.hasItem()) {
items.add(new Item(enemyShip.getPositionX(), enemyShip.getPositionY(), enemyShip.getItemRange(), level));
SoundManager.playSound("SFX/S_Item_Create", "itemCreate", false, false);
}

if (this.isBomb) {
List<EnemyShip> enemyShips = this.enemyShipFormation.destroyByBomb(enemyShip);
SoundManager.playSound("SFX/S_Item_Bomb", "Bomb", false, false);
for (EnemyShip enemy : enemyShips) {
this.score += enemy.getPointValue();
this.shipsDestroyed++;
Expand Down Expand Up @@ -925,6 +927,7 @@ && checkCollision(bullet, this.enemyShipSpecial)) {

if (this.isBomb){
List<EnemyShip> enemyShips = this.enemyShipFormation.destroyByBomb(enemyShip);
SoundManager.playSound("SFX/S_Item_Bomb", "Bomb", false, false);
for(EnemyShip enemy : enemyShips) {
this.score += enemy.getPointValue();
this.shipsDestroyed++;
Expand All @@ -944,6 +947,7 @@ && checkCollision(bullet, this.enemyShipSpecial)) {

if (this.isBomb){
List<EnemyShip> enemyShips = this.enemyShipFormation.destroyByBomb(enemyShip);
SoundManager.playSound("SFX/S_Item_Bomb", "Bomb", false, false);
for(EnemyShip enemy : enemyShips) {
this.score += enemy.getPointValue();
this.shipsDestroyed++;
Expand Down Expand Up @@ -986,13 +990,15 @@ && checkCollision(bullet, this.enemyShipSpecial)) {
for (Item item : this.items) {
if (checkCollision(item, this.ship) && !this.levelFinished) {
recyclableItem.add(item);
SoundManager.playSound("SFX/S_Item_Get", "ItemGet", false, false);
this.ship.getItemQueue().enque(item);
}
}
if (gameState.getMode() == 2) {
for (Item item : this.items2) {
if (checkCollision(item, this.ship2) && !this.levelFinished) {
recyclableItem.add(item);
SoundManager.playSound("SFX/S_Item_Get", "ItemGet", false, false);
this.ship2.getItemQueue().enque(item);
}
}
Expand Down Expand Up @@ -1122,21 +1128,30 @@ private void useItem(Item item, Ship ship) {
item.getItemType() == Item.ItemType.SubPlaneItem) {
ship.setAuxiliaryShipsMode();
this.logger.info("SubPlane Item 사용");
SoundManager.playSound("SFX/S_Item_SubShip", "SubPlaneItem", false, true); // 보조비행기 아이템 bgm

}
else if (!item.getIsGet() &&
item.getItemType() == Item.ItemType.SpeedUpItem) {
ship.setItemSpeed();
this.logger.info("SpeedUp Item 사용");
SoundManager.playSound("SFX/S_Item_SpeedUp", "SpeedUpItem", false, true); // 속도 증가 아이템 bgm


}
else if (!item.getIsGet() &&
item.getItemType() == Item.ItemType.InvincibleItem) {
ship.runInvincible();
this.logger.info("Invincible Item 사용");
SoundManager.playSound("SFX/S_Item_Invicible", "InvincibleItem", false, true); // 무적 상태 아이템 bgm

}
else if (!item.getIsGet() &&
item.getItemType() == Item.ItemType.BombItem) {
setBomb(true);
this.logger.info("Bomb Item 사용");
SoundManager.playSound("SFX/S_Item_Bomb_Equipped", "InvincibleItem", false, true); // 무적 상태 아이템 bgm


}
item.setIsGet();
this.logger.info("You have " + this.ship.getItemQueue().getSize() + " items");
Expand Down
1 change: 1 addition & 0 deletions src/screen/SettingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected final void update() {
break;
}
}

/** Move up and down when selecting key */
if((inputManager.isKeyDown(KeyEvent.VK_UP)
|| inputManager.isKeyDown(KeyEvent.VK_W)) && itemCode>1 && selected && !keyChangeMode){
Expand Down

0 comments on commit 6482a12

Please sign in to comment.