Pārlūkot izejas kodu

Creating Effect class to track information about spell effects, and format them appropriately.
An Effect is serialized as JSON (if present), with one required field (format) and six optional fields.

format: A string on how to write the effect description. Permits the following three special tokens: "{count}", "{perlevel}", "{step}", "{beyond}", and "{upto}", which will substitute the values for those named things.
resolved: A string on how to write the effect description. Permits the following special token: "{atlevel}", which performs the math for you.
count: The fixed range as an integer. This is a value that is not increased by your level.
perlevel: The amount you receive for each {step} levels, as an integer.
step: An integer for how many levels you need to gain for the perlevel bonus to happen.
beyond: An integer representing the level we start counting from
upto: An integer representing the maximum level bonus

For example, you might describe Scorching Ray's effect as:
{
"format":"{count} rays + {perlevel} more rays per {step} levels",
"resolved":"{atlevel} rays",
"count":1,
"perlevel":1,
"step":4,
"beyond":3,
"upto":11
}

Sam Jaffe 8 gadi atpakaļ
vecāks
revīzija
57566b8f10

+ 5 - 1
resources/spells/default.json

@@ -12,7 +12,11 @@
     "components":["V","S"],
     "castingTime":"Standard",
     "range":"Medium",
-    "effect":"Up to 2 gallons of water/level",
+    "effect":{
+      "format":"Up to {perlevel} gallons of water/level",
+      "resolved":"Up to {atlevel} gallons of water",
+      "perlevel":2
+    },
     "duration":"Instantaneous",
     "savingThrow":"None",
     "spellResistence":false,

+ 2 - 1
src/org/leumasjaffe/charsheet/model/magic/DDSpell.java

@@ -2,6 +2,7 @@ package org.leumasjaffe.charsheet.model.magic;
 
 import java.util.EnumSet;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -64,7 +65,7 @@ public class DDSpell {
 	@NonNull EnumSet<Component> components = EnumSet.noneOf(Component.class);
 	@NonNull DDActionType castingTime;
 	@NonNull Range range;
-	String effect; // TODO
+	Optional<Effect> effect; // TODO
 	Area area;
 	String target;
 	@NonNull String duration; // TODO

+ 31 - 0
src/org/leumasjaffe/charsheet/model/magic/Effect.java

@@ -0,0 +1,31 @@
+package org.leumasjaffe.charsheet.model.magic;
+
+import org.leumasjaffe.charsheet.util.StringHelper;
+import org.leumasjaffe.format.Named;
+
+import lombok.AccessLevel;
+import lombok.NonNull;
+import lombok.RequiredArgsConstructor;
+import lombok.experimental.FieldDefaults;
+import lombok.experimental.NonFinal;
+
+@RequiredArgsConstructor
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+public class Effect {
+	@NonNull String format;
+	String resolved;
+	int count, perlevel, beyond;
+	@NonFinal int step = 1, upto = Integer.MAX_VALUE;
+	
+	public String getResolved(int level) {
+		final int result = count + perlevel * ((Math.min(level, upto)-beyond) / step);
+		return resolved == null ? toString() :
+			StringHelper.format(resolved, new Named("atlevel", result));
+	}
+	
+	public String toString() {
+		return StringHelper.format(format, new Named("count", count), 
+				new Named("perlevel", perlevel), new Named("step", step),
+				new Named("beyond", beyond), new Named("upto", upto));
+	}
+}

+ 12 - 1
src/org/leumasjaffe/charsheet/view/magic/SpellInfoPanel.java

@@ -4,7 +4,9 @@ import javax.swing.JPanel;
 import java.awt.GridBagLayout;
 import java.awt.GridBagConstraints;
 import java.awt.Insets;
+import java.util.Optional;
 import java.util.Set;
+import java.util.function.Function;
 
 import javax.swing.JTextArea;
 
@@ -228,7 +230,8 @@ class SpellInfoPanel extends JPanel {
 		gbc_lblEffect.gridy = 4;
 		panel.add(lblEffect, gbc_lblEffect);
 		
-		JTextField effect = new JTextField(asString(spell.getEffect()));
+		JTextField effect = new JTextField(asString(spell.getEffect(), e -> e.getResolved(dclass.getLevel())));
+		effect.setToolTipText(asString(spell.getEffect()));
 		effect.setEditable(false);
 		effect.setColumns(10);
 		GridBagConstraints gbc_effect = new GridBagConstraints();
@@ -330,6 +333,14 @@ class SpellInfoPanel extends JPanel {
 		description.setLineWrap(true);
 	}
 
+	private <T> String asString(Optional<T> obj, Function<? super T, String> ts) {
+		return (!obj.isPresent()) ? "" : ts.apply(obj.get());
+	}
+	
+	private <T> String asString(Optional<T> obj) {
+		return (!obj.isPresent()) ? "" : obj.get().toString();
+	}
+
 	private String asString(Object obj) {
 		return obj == null ? "" : obj.toString();
 	}