From 0980495a28deddba2932615b69016c7be1dbed80 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 16 Jul 2021 15:39:03 -0400 Subject: [PATCH] (commented) support for Call server-to-client sounds --- .../annotations/impl/AssetsProcess.java | 42 ++++++++++++++++--- core/src/mindustry/core/NetClient.java | 16 +++++++ core/src/mindustry/io/TypeIO.java | 10 +++++ core/src/mindustry/type/StatusEffect.java | 2 +- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java b/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java index 0a0538483b..52706b508e 100644 --- a/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java +++ b/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java @@ -1,5 +1,7 @@ package mindustry.annotations.impl; +import arc.*; +import arc.audio.*; import arc.files.*; import arc.scene.style.*; import arc.struct.*; @@ -118,9 +120,31 @@ public class AssetsProcess extends BaseProcessor{ void processSounds(String classname, String path, String rtype) throws Exception{ TypeSpec.Builder type = TypeSpec.classBuilder(classname).addModifiers(Modifier.PUBLIC); MethodSpec.Builder loadBegin = MethodSpec.methodBuilder("load").addModifiers(Modifier.PUBLIC, Modifier.STATIC); + CodeBlock.Builder staticb = CodeBlock.builder(); + + type.addField(FieldSpec.builder(IntMap.class, "idToSound", Modifier.STATIC, Modifier.PRIVATE).initializer("new IntMap()").build()); + type.addField(FieldSpec.builder(ObjectIntMap.class, "soundToId", Modifier.STATIC, Modifier.PRIVATE).initializer("new ObjectIntMap()").build()); + + type.addMethod(MethodSpec.methodBuilder("getSoundId") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addParameter(Sound.class, "sound") + .returns(int.class) + .addStatement("return soundToId.get(sound, -1)").build()); + + type.addMethod(MethodSpec.methodBuilder("getSound") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addParameter(int.class, "id") + .returns(Sound.class) + .addStatement("return (Sound)idToSound.get(id, () -> Sounds.none)").build()); HashSet names = new HashSet<>(); - Fi.get(path).walk(p -> { + Seq files = new Seq<>(); + Fi.get(path).walk(files::add); + + files.sortComparing(Fi::name); + int id = 0; + + for(Fi p : files){ String name = p.nameWithoutExtension(); if(names.contains(name)){ @@ -133,14 +157,20 @@ public class AssetsProcess extends BaseProcessor{ String filepath = path.substring(path.lastIndexOf("/") + 1) + p.path().substring(p.path().lastIndexOf(path) + path.length()); - String filename = "\"" + filepath + "\""; - loadBegin.addStatement("arc.Core.assets.load(" + filename + ", " + rtype + ".class).loaded = a -> " + name + " = (" + rtype + ")a", filepath, filepath.replace(".ogg", ".mp3")); + staticb.addStatement("soundToId.put($L, $L)", name, id); - type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), name, Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio." + rtype.substring(rtype.lastIndexOf(".") + 1) + "()").build()); - }); + loadBegin.addStatement("$T.assets.load($S, $L.class).loaded = a -> { $L = ($L)a; soundToId.put(a, $L); idToSound.put($L, a); }", + Core.class, filepath, rtype, name, rtype, id, id); + + type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), name, Modifier.STATIC, Modifier.PUBLIC).initializer("new " + rtype + "()").build()); + + id ++; + } + + type.addStaticBlock(staticb.build()); if(classname.equals("Sounds")){ - type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), "none", Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio." + rtype.substring(rtype.lastIndexOf(".") + 1) + "()").build()); + type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), "none", Modifier.STATIC, Modifier.PUBLIC).initializer("new " + rtype + "()").build()); } type.addMethod(loadBegin.build()); diff --git a/core/src/mindustry/core/NetClient.java b/core/src/mindustry/core/NetClient.java index 04117e67a5..f0e63dbb7d 100644 --- a/core/src/mindustry/core/NetClient.java +++ b/core/src/mindustry/core/NetClient.java @@ -159,6 +159,22 @@ public class NetClient implements ApplicationListener{ clientPacketReliable(type, contents); } + //TODO enable in build 129 + /* + @Remote(variants = Variant.both, unreliable = true) + public static void sound(Sound sound, float volume, float pitch, float pan){ + if(sound == null) return; + + sound.play(volume * Core.settings.getInt("sfxvol") / 100f, pitch, pan); + } + + @Remote(variants = Variant.both, unreliable = true) + public static void soundAt(Sound sound, float x, float y, float volume, float pitch){ + if(sound == null) return; + + sound.at(x, y, pitch, volume); + }*/ + @Remote(variants = Variant.both, unreliable = true) public static void effect(Effect effect, float x, float y, float rotation, Color color){ if(effect == null) return; diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index 107b3bc3f9..b42be76a48 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -1,5 +1,6 @@ package mindustry.io; +import arc.audio.*; import arc.graphics.*; import arc.math.geom.*; import arc.struct.*; @@ -501,6 +502,15 @@ public class TypeIO{ return id == -1 ? null : content.item(id); } + //note that only the standard sound constants in Sounds are supported; modded sounds are not. + public static void writeSound(Writes write, Sound sound){ + write.s(Sounds.getSoundId(sound)); + } + + public static Sound readSound(Reads read){ + return Sounds.getSound(read.s()); + } + public static void writeWeather(Writes write, Weather item){ write.s(item == null ? -1 : item.id); } diff --git a/core/src/mindustry/type/StatusEffect.java b/core/src/mindustry/type/StatusEffect.java index 6d4c332a37..fe9a7f487f 100644 --- a/core/src/mindustry/type/StatusEffect.java +++ b/core/src/mindustry/type/StatusEffect.java @@ -76,7 +76,7 @@ public class StatusEffect extends UnlockableContent{ if(reloadMultiplier != 1) stats.addPercent(Stat.reloadMultiplier, reloadMultiplier); if(buildSpeedMultiplier != 1) stats.addPercent(Stat.buildSpeedMultiplier, buildSpeedMultiplier); if(damage > 0) stats.add(Stat.damage, damage * 60f, StatUnit.perSecond); - if(damage < 0) stats.add(Stat.healing, -(damage * 60f), StatUnit.perSecond); + if(damage < 0) stats.add(Stat.healing, -damage * 60f, StatUnit.perSecond); boolean reacts = false;