diff --git a/src/main/java/cz/jzitnik/game/Game.java b/src/main/java/cz/jzitnik/game/Game.java index 2def062..1348349 100644 --- a/src/main/java/cz/jzitnik/game/Game.java +++ b/src/main/java/cz/jzitnik/game/Game.java @@ -12,8 +12,10 @@ import cz.jzitnik.game.mobs.EntitySpawnProvider; import cz.jzitnik.game.sprites.Breaking; import cz.jzitnik.game.sprites.Steve.SteveState; import cz.jzitnik.game.annotations.AutoTransient; +import cz.jzitnik.game.annotations.WalkSound; import cz.jzitnik.game.annotations.BreaksByPlace; import cz.jzitnik.game.annotations.MineSound; +import cz.jzitnik.game.annotations.PlaceSound; import cz.jzitnik.game.blocks.Chest; import cz.jzitnik.game.blocks.Furnace; import cz.jzitnik.game.config.Configuration; @@ -27,6 +29,7 @@ import cz.jzitnik.tui.ScreenMovingCalculationProvider; import cz.jzitnik.tui.ScreenRenderer; import lombok.Getter; import lombok.Setter; + import org.jline.terminal.Terminal; import java.util.ArrayList; @@ -93,6 +96,8 @@ public class Game extends AutoTransientSupport { entitySpawnProvider.update(this, terminal); + playMovePlayerSound(cords[0] + 1, cords[1]); + update(screenRenderer); } @@ -114,6 +119,8 @@ public class Game extends AutoTransientSupport { entitySpawnProvider.update(this, terminal); + playMovePlayerSound(cords[0] - 1, cords[1]); + update(screenRenderer); } @@ -275,6 +282,8 @@ public class Game extends AutoTransientSupport { gameStates.dependencies.eventHandlerProvider.handleMine(screenRenderer, this, x, y); + screenRenderer.render(this); + for (Block block : blocksCopy) { if (block.getClass().isAnnotationPresent(MineSound.class)) { var key = block.getClass().getAnnotation(MineSound.class).value(); @@ -285,8 +294,6 @@ public class Game extends AutoTransientSupport { } } - screenRenderer.render(this); - update(screenRenderer); } @@ -359,6 +366,9 @@ public class Game extends AutoTransientSupport { ArrayList combinedList = new ArrayList<>(); combinedList.addAll(world[cords2[1]][cords2[0]]); combinedList.addAll(world[cords2[1] + 1][cords2[0]]); + if (player.getFallDistance() != 0) { + playMovePlayerSound(cords2[0], cords2[1]); + } player.fell(combinedList, this, screenRenderer); screenRenderer.render(this); break; @@ -435,6 +445,11 @@ public class Game extends AutoTransientSupport { .toList(); if (placeHandler.place(this, x, y)) { + if (item.getClass().isAnnotationPresent(PlaceSound.class)) { + var key = item.getClass().getAnnotation(PlaceSound.class).value(); + + gameStates.dependencies.sound.playSound(configuration, key); + } blocks.removeAll(blocksRemove); gameStates.dependencies.eventHandlerProvider.handlePlace(screenRenderer, this, x, y); screenRenderer.render(this); @@ -470,4 +485,16 @@ public class Game extends AutoTransientSupport { screenRenderer.render(this); }).start(); } + + private void playMovePlayerSound(int x, int y) { + var blocks = world[y+1][x]; + + for (Block block : blocks) { + if (block.getClass().isAnnotationPresent(WalkSound.class)) { + var key = block.getClass().getAnnotation(WalkSound.class).value(); + + gameStates.dependencies.sound.playSound(configuration, key); + } + } + } } diff --git a/src/main/java/cz/jzitnik/game/annotations/PlaceSound.java b/src/main/java/cz/jzitnik/game/annotations/PlaceSound.java new file mode 100644 index 0000000..b5e6916 --- /dev/null +++ b/src/main/java/cz/jzitnik/game/annotations/PlaceSound.java @@ -0,0 +1,15 @@ +package cz.jzitnik.game.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import cz.jzitnik.game.core.sound.SoundKey; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@RequireAnnotation(ItemRegistry.class) +public @interface PlaceSound { + SoundKey value(); +} diff --git a/src/main/java/cz/jzitnik/game/annotations/SoundRegistry.java b/src/main/java/cz/jzitnik/game/annotations/SoundRegistry.java index 9ff46af..7e9a20c 100644 --- a/src/main/java/cz/jzitnik/game/annotations/SoundRegistry.java +++ b/src/main/java/cz/jzitnik/game/annotations/SoundRegistry.java @@ -14,5 +14,5 @@ import java.lang.annotation.ElementType; public @interface SoundRegistry { SoundKey key(); SoundType type(); - String resourceLocation(); + String[] resourceLocation(); } diff --git a/src/main/java/cz/jzitnik/game/annotations/WalkSound.java b/src/main/java/cz/jzitnik/game/annotations/WalkSound.java new file mode 100644 index 0000000..b6da529 --- /dev/null +++ b/src/main/java/cz/jzitnik/game/annotations/WalkSound.java @@ -0,0 +1,15 @@ +package cz.jzitnik.game.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import cz.jzitnik.game.core.sound.SoundKey; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@RequireAnnotation(BlockRegistry.class) +public @interface WalkSound { + SoundKey value(); +} diff --git a/src/main/java/cz/jzitnik/game/core/sound/Sound.java b/src/main/java/cz/jzitnik/game/core/sound/Sound.java index dc159be..8166f9b 100644 --- a/src/main/java/cz/jzitnik/game/core/sound/Sound.java +++ b/src/main/java/cz/jzitnik/game/core/sound/Sound.java @@ -2,9 +2,9 @@ package cz.jzitnik.game.core.sound; import java.io.IOException; import java.util.HashMap; +import java.util.Random; import java.util.Set; -import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; @@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class Sound { + private Random random = new Random(); private HashMap map = new HashMap<>(); public Sound() { @@ -41,7 +42,10 @@ public class Sound { var annotation = map.get(soundKey); try { - Clip clip = SoundPlayer.playSound(annotation.resourceLocation(), volume); + var resources = annotation.resourceLocation(); + + var resource = resources[random.nextInt(resources.length)]; + SoundPlayer.playSound(resource, volume); } catch (LineUnavailableException | IOException | UnsupportedAudioFileException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/main/java/cz/jzitnik/game/core/sound/SoundKey.java b/src/main/java/cz/jzitnik/game/core/sound/SoundKey.java index 0ee3e6a..f705d9e 100644 --- a/src/main/java/cz/jzitnik/game/core/sound/SoundKey.java +++ b/src/main/java/cz/jzitnik/game/core/sound/SoundKey.java @@ -1,5 +1,6 @@ package cz.jzitnik.game.core.sound; public enum SoundKey { - DIRT_MINE + GRASS, + GRASS_WALKING } diff --git a/src/main/java/cz/jzitnik/game/core/sound/registry/DirtMineSound.java b/src/main/java/cz/jzitnik/game/core/sound/registry/DirtMineSound.java deleted file mode 100644 index fd22b45..0000000 --- a/src/main/java/cz/jzitnik/game/core/sound/registry/DirtMineSound.java +++ /dev/null @@ -1,9 +0,0 @@ -package cz.jzitnik.game.core.sound.registry; - -import cz.jzitnik.game.annotations.SoundRegistry; -import cz.jzitnik.game.core.sound.SoundKey; -import cz.jzitnik.game.core.sound.SoundType; - -@SoundRegistry(key = SoundKey.DIRT_MINE, resourceLocation = "dirt_mine.wav", type = SoundType.BLOCK) -public class DirtMineSound { -} diff --git a/src/main/java/cz/jzitnik/game/core/sound/registry/GrassSound.java b/src/main/java/cz/jzitnik/game/core/sound/registry/GrassSound.java new file mode 100644 index 0000000..ddb25c3 --- /dev/null +++ b/src/main/java/cz/jzitnik/game/core/sound/registry/GrassSound.java @@ -0,0 +1,14 @@ +package cz.jzitnik.game.core.sound.registry; + +import cz.jzitnik.game.annotations.SoundRegistry; +import cz.jzitnik.game.core.sound.SoundKey; +import cz.jzitnik.game.core.sound.SoundType; + +@SoundRegistry(key = SoundKey.GRASS, resourceLocation = { + "dirt/grass1.wav", + "dirt/grass2.wav", + "dirt/grass3.wav", + "dirt/grass4.wav" +}, type = SoundType.BLOCK) +public class GrassSound { +} diff --git a/src/main/java/cz/jzitnik/game/core/sound/registry/GrassWalkingSound.java b/src/main/java/cz/jzitnik/game/core/sound/registry/GrassWalkingSound.java new file mode 100644 index 0000000..8f7f442 --- /dev/null +++ b/src/main/java/cz/jzitnik/game/core/sound/registry/GrassWalkingSound.java @@ -0,0 +1,16 @@ +package cz.jzitnik.game.core.sound.registry; + +import cz.jzitnik.game.annotations.SoundRegistry; +import cz.jzitnik.game.core.sound.SoundKey; +import cz.jzitnik.game.core.sound.SoundType; + +@SoundRegistry(key = SoundKey.GRASS_WALKING, resourceLocation = { + "dirt/walk1.wav", + "dirt/walk2.wav", + "dirt/walk3.wav", + "dirt/walk4.wav", + "dirt/walk5.wav", + "dirt/walk6.wav", +}, type = SoundType.BLOCK) +public class GrassWalkingSound { +} diff --git a/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/DirtBlock.java b/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/DirtBlock.java index e137f2f..9b04789 100644 --- a/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/DirtBlock.java +++ b/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/DirtBlock.java @@ -2,16 +2,13 @@ package cz.jzitnik.game.entities.items.registry.blocks.blocks; import cz.jzitnik.game.SpriteLoader; import cz.jzitnik.game.annotations.BlockRegistry; -import cz.jzitnik.game.annotations.MineSound; import cz.jzitnik.game.annotations.ResetDataOnMine; -import cz.jzitnik.game.core.sound.SoundKey; import cz.jzitnik.game.entities.Block; import cz.jzitnik.game.entities.items.ItemType; import cz.jzitnik.game.logic.services.grass.GrassDirtData; import java.util.ArrayList; -@MineSound(SoundKey.DIRT_MINE) @ResetDataOnMine @BlockRegistry("dirt") public class DirtBlock extends Block { diff --git a/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/GrassBlock.java b/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/GrassBlock.java index 4625701..5fda4cb 100644 --- a/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/GrassBlock.java +++ b/src/main/java/cz/jzitnik/game/entities/items/registry/blocks/blocks/GrassBlock.java @@ -2,13 +2,20 @@ package cz.jzitnik.game.entities.items.registry.blocks.blocks; import cz.jzitnik.game.SpriteLoader; import cz.jzitnik.game.annotations.BlockRegistry; +import cz.jzitnik.game.annotations.MineSound; +import cz.jzitnik.game.annotations.PlaceSound; import cz.jzitnik.game.annotations.ResetDataOnMine; +import cz.jzitnik.game.annotations.WalkSound; +import cz.jzitnik.game.core.sound.SoundKey; import cz.jzitnik.game.entities.Block; import cz.jzitnik.game.entities.items.ItemType; import cz.jzitnik.game.logic.services.grass.GrassDirtData; import java.util.ArrayList; +@MineSound(SoundKey.GRASS) +@PlaceSound(SoundKey.GRASS) +@WalkSound(SoundKey.GRASS_WALKING) @ResetDataOnMine @BlockRegistry(value = "grass", drops = "dirt") public class GrassBlock extends Block { diff --git a/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/DirtItem.java b/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/DirtItem.java index d8b2868..5bbfa9e 100644 --- a/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/DirtItem.java +++ b/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/DirtItem.java @@ -2,9 +2,12 @@ package cz.jzitnik.game.entities.items.registry.items.blocks; import cz.jzitnik.game.SpriteLoader; import cz.jzitnik.game.annotations.ItemRegistry; +import cz.jzitnik.game.annotations.PlaceSound; +import cz.jzitnik.game.core.sound.SoundKey; import cz.jzitnik.game.entities.items.Item; import cz.jzitnik.game.entities.items.ItemType; +@PlaceSound(SoundKey.DIRT) @ItemRegistry("dirt") public class DirtItem extends Item { public DirtItem() { diff --git a/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/GrassItem.java b/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/GrassItem.java index 4d88825..8c45329 100644 --- a/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/GrassItem.java +++ b/src/main/java/cz/jzitnik/game/entities/items/registry/items/blocks/GrassItem.java @@ -2,9 +2,12 @@ package cz.jzitnik.game.entities.items.registry.items.blocks; import cz.jzitnik.game.SpriteLoader; import cz.jzitnik.game.annotations.ItemRegistry; +import cz.jzitnik.game.annotations.PlaceSound; +import cz.jzitnik.game.core.sound.SoundKey; import cz.jzitnik.game.entities.items.Item; import cz.jzitnik.game.entities.items.ItemType; +@PlaceSound(SoundKey.DIRT) @ItemRegistry("grass") public class GrassItem extends Item { public GrassItem() { diff --git a/src/main/resources/sounds/dirt/grass1.wav b/src/main/resources/sounds/dirt/grass1.wav new file mode 100644 index 0000000..79a61df Binary files /dev/null and b/src/main/resources/sounds/dirt/grass1.wav differ diff --git a/src/main/resources/sounds/dirt/grass2.wav b/src/main/resources/sounds/dirt/grass2.wav new file mode 100644 index 0000000..3b60874 Binary files /dev/null and b/src/main/resources/sounds/dirt/grass2.wav differ diff --git a/src/main/resources/sounds/dirt/grass3.wav b/src/main/resources/sounds/dirt/grass3.wav new file mode 100644 index 0000000..9893223 Binary files /dev/null and b/src/main/resources/sounds/dirt/grass3.wav differ diff --git a/src/main/resources/sounds/dirt/grass4.wav b/src/main/resources/sounds/dirt/grass4.wav new file mode 100644 index 0000000..5fb8414 Binary files /dev/null and b/src/main/resources/sounds/dirt/grass4.wav differ diff --git a/src/main/resources/sounds/dirt/walk1.wav b/src/main/resources/sounds/dirt/walk1.wav new file mode 100644 index 0000000..f155ea9 Binary files /dev/null and b/src/main/resources/sounds/dirt/walk1.wav differ diff --git a/src/main/resources/sounds/dirt/walk2.wav b/src/main/resources/sounds/dirt/walk2.wav new file mode 100644 index 0000000..be550df Binary files /dev/null and b/src/main/resources/sounds/dirt/walk2.wav differ diff --git a/src/main/resources/sounds/dirt/walk3.wav b/src/main/resources/sounds/dirt/walk3.wav new file mode 100644 index 0000000..c5a0d4c Binary files /dev/null and b/src/main/resources/sounds/dirt/walk3.wav differ diff --git a/src/main/resources/sounds/dirt/walk4.wav b/src/main/resources/sounds/dirt/walk4.wav new file mode 100644 index 0000000..ab03a43 Binary files /dev/null and b/src/main/resources/sounds/dirt/walk4.wav differ diff --git a/src/main/resources/sounds/dirt/walk5.wav b/src/main/resources/sounds/dirt/walk5.wav new file mode 100644 index 0000000..b22db89 Binary files /dev/null and b/src/main/resources/sounds/dirt/walk5.wav differ diff --git a/src/main/resources/sounds/dirt/walk6.wav b/src/main/resources/sounds/dirt/walk6.wav new file mode 100644 index 0000000..424011a Binary files /dev/null and b/src/main/resources/sounds/dirt/walk6.wav differ