アイテムを使えるようにする
フィールドで移動したときにアイテムを拾えるようになりましたが、アイテムを使う方法がありません。
戦闘画面でアイテムを使えるようにします。
まず「アイテム」ボタンを画面上に追加します。
public BattleFrame() {
super("戦闘");
Container pane = getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
expLabel = new JLabel("経験値:");
pane.add(expLabel);
iconsPanel = new JPanel();
iconsPanel.setLayout(new BoxLayout(iconsPanel, BoxLayout.X_AXIS));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
pane.add(iconsPanel);
pane.add(buttonsPanel);
braveLabel = new BraveLabel();
iconsPanel.add(braveLabel);
JButton battleButton = new JButton("こうげき");
battleButton.addActionListener(new BattleAction(this));
buttonsPanel.add(battleButton);
JButton escapeButton = new JButton("にげる");
escapeButton.addActionListener(new EscapeAction(this));
buttonsPanel.add(escapeButton);
JButton healButton = new JButton("かいふく");
healButton.addActionListener(new HealAction(this));
buttonsPanel.add(healButton);
JButton itemButton = new JButton("アイテム");
buttonsPanel.add(itemButton);
}
勇者が持っているアイテムの一覧を取得できるようにします。
Brave.java
public void addGold(int gold) {
this.gold += gold;
System.out.println(getName() + "は、" + this.gold + "ゴールドを持っています。");
}
public List<Item> getItems() {
return items;
}
}
ItemAction を作成して、「アイテム」ボタンが押された時の実装を作成します。
createItemArray() では、同一のアイテムを複数表示しないようにして今s。
package rpg.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import rpg.model.Brave;
import rpg.model.item.Item;
import rpg.view.BattleFrame;
public class ItemAction implements ActionListener {
private BattleFrame battleFrame;
public ItemAction(BattleFrame battleFrame) {
this.battleFrame = battleFrame;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = createItemArray();
JOptionPane.showInputDialog(
battleFrame,
"アイテムを選択してください",
"アイテム",
JOptionPane.PLAIN_MESSAGE,
null,
items,
items[0]);
}
private Object[] createItemArray() {
Brave brave = battleFrame.getBrave();
List<Item> items = brave.getItems();
List<String> list = new ArrayList<>();
for (Item i : items) {
if (!list.contains(i.getName())) {
list.add(i.getName());
}
}
return list.toArray();
}
}
アイテムボタンが押されたら ItemAction が呼ばれるようにします。
BattleFrame に以下の1行を追加します。
JButton itemButton = new JButton("アイテム");
itemButton.addActionListener(new ItemAction(this));
buttonsPanel.add(itemButton);
選択されたアイテムを取得して、そのアイテムを使う処理を追加します。
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = createItemArray();
Object itemName = JOptionPane.showInputDialog(
battleFrame,
"アイテムを選択してください",
"アイテム",
JOptionPane.PLAIN_MESSAGE,
null,
items,
items[0]);
System.out.println(itemName);
Brave brave = battleFrame.getBrave();
Item item = brave.getItem((String)itemName);
brave.useItem(item);
}
アイテムをひとつも持っていないときに「アイテム」ボタンを押すと例外が発生するのを回避します。
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = createItemArray();
if (items.length == 0) {
JOptionPane.showMessageDialog(battleFrame,
"アイテムがありません。",
"アイテム",
JOptionPane.INFORMATION_MESSAGE);
return;
}
Object itemName = JOptionPane.showInputDialog(
battleFrame,
"アイテムを選択してください",
"アイテム",
JOptionPane.PLAIN_MESSAGE,
null,
items,
items[0]);
System.out.println(itemName);
Brave brave = battleFrame.getBrave();
Item item = brave.getItem((String)itemName);
brave.useItem(item);
}
アイテムを選択するダイアログで「取消」すると例外が発生しますので、これを回避するようにします。
「取消」すると戻り値の itemName が null になるので、以後の処理を行わずに return すようにします。
ItemActionを修正します。
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = createItemArray();
if (items.length == 0) {
JOptionPane.showMessageDialog(battleFrame,
"アイテムがありません。",
"アイテム",
JOptionPane.INFORMATION_MESSAGE);
return;
}
Object itemName = JOptionPane.showInputDialog(
battleFrame,
"アイテムを選択してください",
"アイテム",
JOptionPane.PLAIN_MESSAGE,
null,
items,
items[0]);
if (itemName == null) return;
System.out.println(itemName);
Brave brave = battleFrame.getBrave();
Item item = brave.getItem((String)itemName);
brave.useItem(item);
}