Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Player equipment can now be loaded from save
Browse files Browse the repository at this point in the history
Fix equipping armor
  • Loading branch information
erodozer committed Jun 15, 2012
1 parent 5abad75 commit 5df01eb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
1 change: 1 addition & 0 deletions data/actors/jobs/Red Mage/job.ini
Expand Up @@ -6,4 +6,5 @@ itl = 10
acc = 7
spd = 10
mdef = 20
weight = 1
name = RedMAGE
48 changes: 46 additions & 2 deletions src/actors/Player.java
Expand Up @@ -136,7 +136,7 @@ public Player(Preferences p)
{
this(p.get("name", "aaaa"), p.get("job", "Fighter"));

/**
/*
* loads all the stats for the player,
* if the stats don't exist then just use
* the job's initial stats
Expand All @@ -157,6 +157,33 @@ public Player(Preferences p)
mdef = p.getInt("mdef", mdef);
luk = p.getInt("luck", luk);
exp = p.getInt("exp", 0);

/*
* Loads all the armor and weapons
*/
weight = p.getInt("weight", 0);

for (int i = 0; i < weapons.length; i++)
{
weapons[i] = Item.loadItem(p.get(String.format("weapon%02d", i), null));
}

String e = p.get("eWeapon", "-1");
if (Integer.parseInt(e) != -1)
weapon = weapons[Integer.parseInt(e)];

e = p.get("eArmor", "");
for (int i = 0; i < armor.length; i++)
{
armor[i] = Item.loadItem(p.get(String.format("armor%02d", i), null));
if (e.contains(""+i+" "))
wearArmor(armor[i]);
}
Scanner s = new Scanner(e);
while (s.hasNextInt())
wearArmor(armor[s.nextInt()]);
s.close();
e = null;
}

/**
Expand Down Expand Up @@ -190,6 +217,23 @@ public void savePlayer(Ini ini, String section)
ini.put(section, "luck", luk);
ini.put(section, "exp", exp);

String e = "";
for (int i = 0; i < weapons.length; i++)
{
ini.put(section, String.format("weapon%02d", i), weapons[i]);
if (getWeapon() == weapons[i])
e = i + "";
}
ini.put(section, "eWeapon", e); //equipped item index

e = "";
for (int i = 0; i < armor.length; i++)
{
ini.put(section, String.format("armor%02d", i), armor[i]);
if (this.isWearing(armor[i]))
e += i + " ";
}
ini.put(section, "eArmor", e);
}

/**
Expand Down Expand Up @@ -789,7 +833,7 @@ public boolean isWearing(Item item) {
*/
public boolean wearArmor(Item armor)
{
if (armor.isEquipment() && armor.getWeight() <= weight)
if (armor != null && armor.isEquipment() && armor.getWeight() <= weight)
{
//only equip the armor if there is a piece of armor of that type that is not already equipped
for (int i = 0; i < equippedArmor.size(); i++)
Expand Down
22 changes: 13 additions & 9 deletions src/item/Item.java
Expand Up @@ -59,6 +59,10 @@ public boolean accept(File f, String s) {
public static Item loadItem(String s)
{
Item i = null;
//ignore loading if the value is null
if (s == null)
return null;

//compare first against the dictionary since it knows which item names are valid
if (Dictionary.contains(s))
if (cache.containsKey(s))
Expand Down Expand Up @@ -181,21 +185,21 @@ public Item(String s) throws Exception
{
Preferences equip = inifile.node(eqSec);
// type is armor or weapon
if (type != 2) {
if (type != ACCESSORY_TYPE) {
// get armor weight and restricted jobs
weight = equip.getInt("weight", 0);
restrict = equip.get("restrict", "").split(",");
if (type == 1)
slot = equip.getInt("slot", 0);
}
hp = equip.getInt("hp", 1);
str = equip.getInt("str", 1);
itl = equip.getInt("int", 1);
spd = equip.getInt("spd", 1);
evd = equip.getInt("evd", 1);
acc = equip.getInt("acc", 1);
vit = equip.getInt("vit", 1);
mdef = equip.getInt("mdef", 1);
hp = equip.getInt("hp", 0);
str = equip.getInt("str", 0);
itl = equip.getInt("int", 0);
spd = equip.getInt("spd", 0);
evd = equip.getInt("evd", 0);
acc = equip.getInt("acc", 0);
vit = equip.getInt("vit", 0);
mdef = equip.getInt("mdef", 0);
}

String command = main.get("command", null);
Expand Down
4 changes: 2 additions & 2 deletions src/scenes/MenuScene/GUI/EquipmentGUI.java
Expand Up @@ -84,7 +84,7 @@ public int[] updateArrowPosition(int index)
int x = index % 2;
int y = index % 4 / 2;
arrowPosition[0] = eWindows[i].getX() + 58 + 80*x;
arrowPosition[1] = eWindows[i].getY() + 16 + 16*y;
arrowPosition[1] = eWindows[i].getY() + 16 + 12*y;
}
return arrowPosition;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public void paint(Graphics g) {
t = ((p.getWeapon() == p.getWeapons()[i] && p.getWeapon() != null)?"E-":"") + ((p.getWeapons()[i] != null)?p.getWeapons()[i].getName():"");
else
t = ((p.isWearing(p.getArmor()[i])?"E-":"") + ((p.getArmor()[i] != null)?p.getArmor()[i].getName():""));
f.drawString(g, t, 20+(items.getWidth()/2*(i%2)), 14+(items.getHeight()/2*(i/2)), items);
f.drawString(g, t, 20+(items.getWidth()/2*(i%2)), 14+(12*(i/2)), items);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/MenuScene/System/ArmorState.java
Expand Up @@ -25,7 +25,7 @@ public void handleKeyInput(int key)
Item armor = player.getArmor()[row+col];
if (player.isWearing(armor))
player.takeOffArmor(armor);
else if (player.getWeapons()[row+col] != null)
else if (armor != null)
player.wearArmor(armor);
}
}
Expand Down

0 comments on commit 5df01eb

Please sign in to comment.