feat(sounds): Added some new sounds

This commit is contained in:
2025-03-26 20:46:52 +01:00
parent 95e1e52205
commit c674663cc0
23 changed files with 111 additions and 18 deletions

View File

@ -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<Block> 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);
}
}
}
}

View File

@ -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();
}

View File

@ -14,5 +14,5 @@ import java.lang.annotation.ElementType;
public @interface SoundRegistry {
SoundKey key();
SoundType type();
String resourceLocation();
String[] resourceLocation();
}

View File

@ -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();
}

View File

@ -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<SoundKey, SoundRegistry> 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();

View File

@ -1,5 +1,6 @@
package cz.jzitnik.game.core.sound;
public enum SoundKey {
DIRT_MINE
GRASS,
GRASS_WALKING
}

View File

@ -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 {
}

View File

@ -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 {
}

View File

@ -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 {
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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() {

View File

@ -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() {