Pārlūkot izejas kodu

Add properties to Domain.
Add auto-init of Domains when the feature is gained.

Sam Jaffe 8 gadi atpakaļ
vecāks
revīzija
23a2315393

+ 15 - 0
resources/spells/domain/earth.json

@@ -0,0 +1,15 @@
+{
+  "name":"Domain::Earth",
+  "powers":[{"@c":".impl.Simple","name":"Turn air creatures"}],
+  "spells":[
+    "Magic Stone",
+    "Soften Earth and Stone",
+    "Stone Shape",
+    "Spike Stones",
+    "Wall of Stone",
+    "Stoneskin",
+    "Earthquake",
+    "Iron Body",
+    "Elemental Swarm"
+  ]
+}

+ 4 - 1
resources/spells/domain/plant.json

@@ -1,6 +1,9 @@
 {
   "name":"Domain::Plant",
-  "powers":[],
+  "powers":[
+    {"@c":".impl.Simple","name":"Rebuke plants"},
+    {"@c":".impl.Simple","name":"Knowledge (nature)"}
+  ],
   "spells":[
     "Entangle",
     "Barkskin",

+ 1 - 6
src/main/lombok/org/leumasjaffe/charsheet/model/DDCharacterClass.java

@@ -76,12 +76,7 @@ public class DDCharacterClass extends Observable.Instance implements Comparable<
 			return Stream.concat(Stream.of(main), 
 					secondary.map(Stream::of).orElse(Stream.empty())).toArray(DDSpellbook[]::new);
 		}
-		
-		public void setSecondary(DDSpellbook book) {
-			assert(!secondary.isPresent());
-			secondary = Optional.of(book);
-		}
-		
+				
 		DDSpellbook main;
 		Optional<DDSpellbook> secondary = Optional.empty();
 	}

+ 21 - 3
src/main/lombok/org/leumasjaffe/charsheet/model/features/DDPropertyChooser.java

@@ -8,13 +8,31 @@ import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import com.fasterxml.jackson.annotation.JsonTypeInfo;
 import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
 
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
+import lombok.experimental.FieldDefaults;
+import lombok.experimental.NonFinal;
+
 @JsonTypeInfo(use=Id.MINIMAL_CLASS)
 public interface DDPropertyChooser {
 	default int getTimes() { return 1; }
 	String getHeader();
 	default List<String> getChoices() { return Collections.emptyList(); }
-	public DDProperty get(String forSelection);
-	default void apply(DDCharacterClass to, String forSelection) {
-		to.addFeature(get(forSelection));
+	public DDProperty get(int selectedIndex);
+	default boolean applySideEffects(DDCharacterClass to, int selectedIndex) { return false; }
+	default void undoSideEffects(DDCharacterClass to, int selectedIndex) {}
+	
+	@RequiredArgsConstructor
+	@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+	public static class State {
+		DDCharacterClass ddCharacter;
+		DDPropertyChooser chooser;
+		@NonFinal boolean hasApplied = false;
+		@NonFinal int previousIndex = -1;
+		
+		public void apply(int selectedIndex) {
+			if (hasApplied) { chooser.undoSideEffects(ddCharacter, previousIndex); }
+			hasApplied = chooser.applySideEffects(ddCharacter, previousIndex = selectedIndex);
+		}		
 	}
 }

+ 16 - 7
src/main/lombok/org/leumasjaffe/charsheet/model/features/impl/DomainFeature.java

@@ -1,6 +1,5 @@
 package org.leumasjaffe.charsheet.model.features.impl;
 
-import java.util.Collections;
 import java.util.List;
 
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
@@ -24,16 +23,26 @@ public class DomainFeature implements DDProperty {
 	@Getter @Setter
 	public static class Chooser implements DDPropertyChooser {
 		@Delegate(types=IFaceHelper.class) DomainFeature impl = new DomainFeature("", "");
-		List<String> choices = Collections.emptyList();
+		List<String> choices;
 		int times;
 		
 		@Override public String getHeader() { return impl.getName(); }
-		@Override public DDProperty get(String forSelection) {
-			return new DomainFeature(impl.getName() + " " + forSelection, impl.getDescription());
+		@Override public DDProperty get(int idx) {
+			return new DomainFeature(impl.getName() + " " + getChoice(idx), impl.getDescription());
 		}
-		@Override public void apply(DDCharacterClass to, String forSelection) { 
-			to.addFeature(get(forSelection));
-			to.getSpellBook().get().setSecondary(new Domain());
+		@Override public boolean applySideEffects(DDCharacterClass to, int idx) {
+			return getDomain(to).addDomain(getChoice(idx));
+		}
+		@Override public void undoSideEffects(DDCharacterClass to, int idx) {
+			getDomain(to).removeDomain(getChoice(idx));
+		}
+		
+		private Domain getDomain(DDCharacterClass to) {
+			return Domain.class.cast(to.getSpellBook().get().getSecondary().get());
+		}
+		
+		private String getChoice(int idx) {
+			return choices.get(idx);
 		}
 	}
 	String name, description;

+ 2 - 2
src/main/lombok/org/leumasjaffe/charsheet/model/features/impl/Simple.java

@@ -21,8 +21,8 @@ public class Simple implements DDProperty {
 		@Delegate(types=IFaceHelper.class) Simple impl = new Simple("", "");
 		
 		@Override public String getHeader() { return impl.getName(); }
-		@Override public DDProperty get(String selected) { 
-			return new Simple(impl.getName() + selected, impl.getDescription());
+		@Override public DDProperty get(int selectedIndex) { 
+			return new Simple(impl.getName(), impl.getDescription());
 		}
 	}
 	String name, description;

+ 11 - 2
src/main/lombok/org/leumasjaffe/charsheet/model/magic/impl/Domain.java

@@ -10,6 +10,7 @@ import java.util.Set;
 import java.util.stream.Collectors;
 
 import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
+import org.leumasjaffe.charsheet.model.features.DDProperty;
 import org.leumasjaffe.charsheet.model.magic.DDSpell;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -34,9 +35,9 @@ public class Domain extends Prepared implements DDSpellbook.Secondary {
 			return mapper.readValue(new File("resources/spells/domain/" + name.toLowerCase() + ".json"), 
 					new TypeReference<SpellBookImpl>() {});
 		}
-		@JsonValue private String getImplName() { return name.replaceAll(".*::", ""); }
+		@JsonValue public String getImplName() { return name.replaceAll(".*::", ""); }
 		String name;
-		List<Object> powers;
+		List<DDProperty> powers;
 		List<DDSpell> spells;
 	}
 	
@@ -95,4 +96,12 @@ public class Domain extends Prepared implements DDSpellbook.Secondary {
 		if (spellsPreparedPreviously.size() < level || level == 0) return Collections.emptyList();
 		return Collections.singletonList(spellsPreparedPreviously.get(level-1));
 	}
+
+	public boolean addDomain(String forSelection) {
+		return this.domains.add(SpellBookImpl.create(forSelection));
+	}
+
+	public void removeDomain(String forSelection) {
+		this.domains.remove(SpellBookImpl.create(forSelection));
+	}
 }

+ 1 - 1
src/main/lombok/org/leumasjaffe/charsheet/view/level/LU_FeaturesPanel.java

@@ -81,7 +81,7 @@ class LU_FeaturesPanel extends JPanel {
 		panel_1.add(features, gbc_features);
 		info.ddClass.getProto().getFeatures(info.toLevel).forEach(prop -> {
 			for (int i = 0; i < prop.getTimes(); ++i) {
-				features.add(new PropertyChoicePanel(prop));
+				features.add(new PropertyChoicePanel(info.ddClass, prop, i));
 			}
 		});
 		

+ 14 - 1
src/main/lombok/org/leumasjaffe/charsheet/view/level/PropertyChoicePanel.java

@@ -2,17 +2,19 @@ package org.leumasjaffe.charsheet.view.level;
 
 import javax.swing.JPanel;
 
+import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.features.DDPropertyChooser;
 import java.awt.GridBagLayout;
 import javax.swing.JLabel;
 import java.awt.GridBagConstraints;
 import javax.swing.JComboBox;
 import java.awt.Insets;
+import java.awt.event.ItemEvent;
 
 @SuppressWarnings("serial")
 public class PropertyChoicePanel extends JPanel {
 
-	public PropertyChoicePanel(DDPropertyChooser prop) {
+	public PropertyChoicePanel(DDCharacterClass dchara, DDPropertyChooser prop, int idx) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -29,12 +31,23 @@ public class PropertyChoicePanel extends JPanel {
 		add(lblName, gbc_lblName);
 		
 		if (!prop.getChoices().isEmpty()) {
+			DDPropertyChooser.State state = new DDPropertyChooser.State(dchara, prop);
+
 			JComboBox<String> comboBox = new JComboBox<>(prop.getChoices().toArray(new String[0]));
 			GridBagConstraints gbc_comboBox = new GridBagConstraints();
 			gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
 			gbc_comboBox.gridx = 1;
 			gbc_comboBox.gridy = 0;
 			add(comboBox, gbc_comboBox);
+			
+			comboBox.setSelectedIndex(idx);
+			state.apply(idx);
+			comboBox.addItemListener(e -> {
+				if (e.getStateChange() == ItemEvent.SELECTED) {
+					state.apply(comboBox.getSelectedIndex());
+				}
+			});
+			
 		}
 	}