From 4c9761eae5f2aba21e3dad6d4b221bf6d36c0a7d Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 3 Feb 2022 21:35:44 -0500 Subject: [PATCH] PartProgress ContentParser support --- .../mindustry/entities/part/WeaponPart.java | 8 ++--- core/src/mindustry/mod/ContentParser.java | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/core/src/mindustry/entities/part/WeaponPart.java b/core/src/mindustry/entities/part/WeaponPart.java index 98d4ab94e3..b529aa1d88 100644 --- a/core/src/mindustry/entities/part/WeaponPart.java +++ b/core/src/mindustry/entities/part/WeaponPart.java @@ -54,6 +54,10 @@ public abstract class WeaponPart{ return p -> 1f - get(p); } + default PartProgress add(float amount){ + return p -> Mathf.clamp(get(p) + amount); + } + default PartProgress delay(float amount){ return p -> Mathf.clamp((get(p) - amount) / (1f - amount)); } @@ -86,10 +90,6 @@ public abstract class WeaponPart{ return p -> func.get(get(p), other.get(p)); } - default PartProgress add(float amount){ - return p -> Mathf.clamp(get(p) + amount); - } - default PartProgress curve(Interp interp){ return p -> interp.apply(get(p)); } diff --git a/core/src/mindustry/mod/ContentParser.java b/core/src/mindustry/mod/ContentParser.java index a50d8a3f1c..6a077d6bc8 100644 --- a/core/src/mindustry/mod/ContentParser.java +++ b/core/src/mindustry/mod/ContentParser.java @@ -25,6 +25,7 @@ import mindustry.entities.abilities.*; import mindustry.entities.bullet.*; import mindustry.entities.effect.*; import mindustry.entities.part.*; +import mindustry.entities.part.WeaponPart.*; import mindustry.game.*; import mindustry.game.Objectives.*; import mindustry.gen.*; @@ -136,6 +137,40 @@ public class ContentParser{ readFields(result, data); return result; }); + put(PartProgress.class, (type, data) -> { + //simple case: it's a string or number constant + if(data.isString()) return field(PartProgress.class, data.asString()); + if(data.isNumber()) return PartProgress.constant(data.asFloat()); + + if(!data.has("type")){ + throw new RuntimeException("PartProgress object need a 'type' string field. Check the PartProgress class for a list of constants."); + } + + PartProgress base = (PartProgress)field(PartProgress.class, data.getString("type")); + + //no operations I guess (why would you do this?) + if(!data.has("operation")){ + return base; + } + + //this is the name of the method to call + String op = data.getString("operation"); + + //I have to hard-code this, no easy way of getting parameter names, unfortunately + return switch(op){ + case "inv" -> base.inv(); + case "delay" -> base.delay(data.getFloat("amount")); + case "shorten" -> base.shorten(data.getFloat("amount")); + case "add" -> base.add(data.getFloat("amount")); + case "blend" -> base.blend(parser.readValue(PartProgress.class, data.get("other")), data.getFloat("amount")); + case "mul" -> base.mul(parser.readValue(PartProgress.class, data.get("other"))); + case "min" -> base.min(parser.readValue(PartProgress.class, data.get("other"))); + case "sin" -> base.sin(data.getFloat("scl"), data.getFloat("mag")); + case "absin" -> base.sin(data.getFloat("scl"), data.getFloat("mag")); + case "curve" -> base.curve(parser.readValue(Interp.class, data.get("interp"))); + default -> throw new RuntimeException("Unknown operation '" + op + "', check PartProgress class for a list of methods."); + }; + }); put(Sound.class, (type, data) -> { if(fieldOpt(Sounds.class, data) != null) return fieldOpt(Sounds.class, data); if(Vars.headless) return new Sound();