Jelajahi Sumber

Adding ability for spells to disappear when cast

Sam Jaffe 8 tahun lalu
induk
melakukan
7924b255b0

+ 4 - 6
resources/Potato.json

@@ -11,15 +11,13 @@
         "spellInfo":{
           "0":{
             "spellsPerDay":4,
+            "spellsPrepared":[]
+          },
+          "1":{
+            "spellsPerDay":2,
             "spellsPrepared":[
-              "Create Water",
-              "Create Water",
               "Create Water"
             ]
-          },
-          "1":{
-            "spellsPerDay":0,
-            "spellsPrepared":[]
           }
         }
       }

+ 21 - 5
src/org/leumasjaffe/charsheet/view/magic/SpellLevelPanel.java

@@ -6,19 +6,27 @@ import java.awt.Component;
 import javax.swing.Box;
 import java.awt.GridBagConstraints;
 import java.awt.Insets;
-import java.util.Collection;
 
 import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
-import org.leumasjaffe.charsheet.model.magic.DDSpell;
 
+import lombok.AccessLevel;
+import lombok.experimental.FieldDefaults;
+
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class SpellLevelPanel extends JPanel {
 	/**
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
+	JPanel panel;
+	DDCharacterClass dclass;
+	int level;
 
-	public SpellLevelPanel(JPanel header, DDCharacterClass dclass, Collection<DDSpell> spells) {
+	public SpellLevelPanel(JPanel header, DDCharacterClass dclass, int level) {
+		this.dclass = dclass;
+		this.level = level;
+		
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
@@ -34,14 +42,14 @@ public class SpellLevelPanel extends JPanel {
 		gbc_panel_1.gridy = 0;
 		add(header, gbc_panel_1);
 		
-		JPanel panel = new JPanel(new VerticalLayout());
+		panel = new JPanel(new VerticalLayout());
 		GridBagConstraints gbc_panel = new GridBagConstraints();
 		gbc_panel.insets = new Insets(0, 0, 5, 0);
 		gbc_panel.fill = GridBagConstraints.BOTH;
 		gbc_panel.gridx = 1;
 		gbc_panel.gridy = 1;
 		add(panel, gbc_panel);
-		spells.forEach(spell -> panel.add(new SpellLine(dclass, spell)));
+		reload();
 		
 		Component horizontalStrut = Box.createHorizontalStrut(20);
 		GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
@@ -50,5 +58,13 @@ public class SpellLevelPanel extends JPanel {
 		gbc_horizontalStrut.gridy = 2;
 		add(horizontalStrut, gbc_horizontalStrut);
 	}
+	
+	public boolean isCastableFromHere() { return false; }
+
+	protected void reload() {
+		panel.removeAll();
+		dclass.getSpellBook().get().spellsPreparedAtLevel(level).forEach(spell -> panel.add(new SpellLine(this.dclass, spell, isCastableFromHere())));
+		panel.repaint();
+	}
 
 }

+ 29 - 0
src/org/leumasjaffe/charsheet/view/magic/SpellLevelPerDayPanel.java

@@ -0,0 +1,29 @@
+package org.leumasjaffe.charsheet.view.magic;
+
+import org.leumasjaffe.charsheet.model.DDCharacterClass;
+
+import lombok.AccessLevel;
+import lombok.experimental.FieldDefaults;
+
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+public class SpellLevelPerDayPanel extends SpellLevelPanel {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	SpellsPerDayHeader header;
+	
+	public SpellLevelPerDayPanel(SpellsPerDayHeader header, DDCharacterClass dclass, int level) {
+		super(header, dclass, level);
+		this.header = header;
+	}
+	
+	public boolean isCastableFromHere() { return true; }
+
+	public void reload() {
+		if (header != null) { header.reload(); }
+		super.reload();
+	}
+}

+ 2 - 2
src/org/leumasjaffe/charsheet/view/magic/SpellLine.java

@@ -20,7 +20,7 @@ public class SpellLine extends JPanel {
 	 */
 	private static final long serialVersionUID = 1L;
 
-	public SpellLine(final DDCharacterClass dclass, final DDSpell spell) {
+	public SpellLine(final DDCharacterClass dclass, final DDSpell spell, boolean isPrepared) {
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
@@ -101,7 +101,7 @@ public class SpellLine extends JPanel {
 		gbc_lblAction_1.gridy = 0;
 		add(lblRange, gbc_lblAction_1);
 		
-		addMouseListener(new PopClickListener(new SpellMenu(dclass, spell)));
+		addMouseListener(new PopClickListener(new SpellMenu(dclass, spell, isPrepared)));
 	}
 
 }

+ 13 - 4
src/org/leumasjaffe/charsheet/view/magic/SpellMenu.java

@@ -4,6 +4,7 @@ import javax.swing.JPopupMenu;
 
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.magic.DDSpell;
+import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
 
 import javax.swing.JFrame;
 import javax.swing.JMenuItem;
@@ -15,19 +16,27 @@ class SpellMenu extends JPopupMenu {
 	 */
 	private static final long serialVersionUID = 1L;
 
-	public SpellMenu(final DDCharacterClass dclass, final DDSpell spell) {
+	public SpellMenu(final DDCharacterClass dclass, final DDSpell spell, boolean isPrepared) {
+		final int spellLevel = spell.getClassLevel(dclass.getName());
 		
 		JMenuItem mntmInfo = new JMenuItem("Info");
 		mntmInfo.addActionListener( e -> {
-			JFrame frame = new JFrame(spell.getName() +  " (" + dclass.getName() + " " + spell.getClassLevel(dclass.getName()) + ")");
+			JFrame frame = new JFrame(spell.getName() +  " (" + dclass.getName() + " " + spellLevel + ")");
 			frame.add(new SpellInfoPanel(dclass, spell));
 			frame.pack();
 			frame.setVisible(true);
 		});
 		add(mntmInfo);
 		
-		JMenuItem mntmCast = new JMenuItem("Cast");
-		add(mntmCast);
+		if (isPrepared) {
+			JMenuItem mntmCast = new JMenuItem("Cast");
+			mntmCast.addActionListener(e -> {
+				final DDSpellbook book = dclass.getSpellBook().get();
+				book.castSpell(spellLevel, spell);
+				((SpellLevelPerDayPanel) this.getInvoker().getParent().getParent()).reload();
+			});
+			add(mntmCast);
+		}
 		
 	}
 

+ 1 - 4
src/org/leumasjaffe/charsheet/view/magic/SpellPanel.java

@@ -4,7 +4,6 @@ import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 
 import java.awt.GridBagLayout;
-import java.util.Collection;
 import java.util.List;
 import java.util.function.Function;
 
@@ -15,7 +14,6 @@ import org.leumasjaffe.charsheet.model.Ability;
 import org.leumasjaffe.charsheet.model.DDCharacter;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.DDClass;
-import org.leumasjaffe.charsheet.model.magic.DDSpell;
 import org.leumasjaffe.charsheet.model.magic.DDSpellList;
 import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
 import org.leumasjaffe.charsheet.model.observable.IntValue;
@@ -88,8 +86,7 @@ public class SpellPanel extends JPanel {
 		root.setLayout(new VerticalLayout());
 		
 		for (int i = 0; i < highestSpellLevel; ++i) {
-			Collection<DDSpell> spells = getSpells.apply(i);
-			root.add(new SpellLevelPanel(getPanel.apply(i), dclass, spells));
+			root.add(new SpellLevelPanel(getPanel.apply(i), dclass, i));
 		}
 		
 		preparedPane.setViewportView(root);

+ 8 - 1
src/org/leumasjaffe/charsheet/view/magic/SpellsPerDayHeader.java

@@ -9,6 +9,7 @@ import javax.swing.JPanel;
 import javax.swing.JTextField;
 
 import org.leumasjaffe.charsheet.model.Ability;
+import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
 import org.leumasjaffe.charsheet.model.observable.IntValue;
 import java.awt.Dimension;
 
@@ -17,6 +18,7 @@ public class SpellsPerDayHeader extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
+	private JTextField textFieldRemaining;
 
 	public SpellsPerDayHeader(int level, int elems, int perDay, IntValue ability) {
 		setPreferredSize(new Dimension(350, 20));
@@ -70,7 +72,7 @@ public class SpellsPerDayHeader extends JPanel {
 		gbc_lblSpellsPerDay.gridy = 0;
 		add(lblSpellsPerDay, gbc_lblSpellsPerDay);
 		
-		JTextField textFieldRemaining = new JTextField(Integer.toString(elems));
+		textFieldRemaining = new JTextField(Integer.toString(perDay));
 		GridBagConstraints gbc_textFieldRemaining = new GridBagConstraints();
 		gbc_textFieldRemaining.fill = GridBagConstraints.HORIZONTAL;
 		gbc_textFieldRemaining.insets = new Insets(0, 0, 0, 5);
@@ -97,4 +99,9 @@ public class SpellsPerDayHeader extends JPanel {
 		textFieldOutOf.setEditable(false);
 		textFieldOutOf.setColumns(10);
 	}
+
+	// Technically unsafe, but since this should only get called when a spell is cast, the number will not drop below 0
+	public void reload() {
+		textFieldRemaining.setText(Integer.toString(Integer.parseInt(textFieldRemaining.getText())-1));
+	}
 }