Skip to content

Commit ee6f6b7

Browse files
committed
Cannot buy the same ship you are in
1 parent 2e935bc commit ee6f6b7

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ You can destroy asteroids for easy money, even with the starting ship's ammo-les
3232

3333
Warnings get posted if you get close to dangerous ships or may soon collide with something on your current course. Blue dots along the edge of the screen indicate a planet is nearby.
3434

35-
Watch out about buying a new ship if you can only barely afford it - you might need to buy new compatible weaponry too! Also be careful not to actually re-buy the ship you're already in.
35+
Watch out about buying a new ship if you can only barely afford it - you might need to buy new compatible weaponry too!
3636

3737
Mercenaries will follow you around and should start with a compatible weapon again in v1.4.1. They'll pick up items as well and keep them, greedy little buggers! But then they drop everything again on death, so ...
3838

main/src/org/destinationsol/game/screens/ChangeShip.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,46 @@ public void updateCustom(SolApplication cmp, SolInputManager.Ptr[] ptrs, boolean
8585
return;
8686
}
8787
SolItem selItem = is.getSelectedItem();
88-
boolean enabled = selItem != null && hero.getMoney() >= selItem.getPrice();
89-
myBuyCtrl.setDisplayName(enabled ? "Change" : "---");
90-
myBuyCtrl.setEnabled(enabled);
91-
if (!enabled) return;
88+
if(selItem == null) {
89+
myBuyCtrl.setDisplayName("---");
90+
myBuyCtrl.setEnabled(false);
91+
return;
92+
}
93+
boolean enabled = hasMoneyToBuyShip(hero, selItem);
94+
boolean sameShip = isSameShip(hero, selItem);
95+
if(enabled && !sameShip) {
96+
myBuyCtrl.setDisplayName("Change");
97+
myBuyCtrl.setEnabled(true);
98+
} else if(enabled && sameShip) {
99+
myBuyCtrl.setDisplayName("Have it");
100+
myBuyCtrl.setEnabled(false);
101+
return;
102+
} else {
103+
myBuyCtrl.setDisplayName("---");
104+
myBuyCtrl.setEnabled(false);
105+
return;
106+
}
92107
if (myBuyCtrl.isJustOff()) {
93108
hero.setMoney(hero.getMoney() - selItem.getPrice());
94109
changeShip(game, hero, (ShipItem) selItem);
95110
}
96111
}
97112

113+
private boolean hasMoneyToBuyShip(SolShip hero, SolItem shipToBuy) {
114+
return hero.getMoney() >= shipToBuy.getPrice();
115+
}
116+
117+
private boolean isSameShip(SolShip hero, SolItem shipToBuy) {
118+
if(shipToBuy instanceof ShipItem) {
119+
ShipItem ship = (ShipItem) shipToBuy;
120+
HullConfig config1 = ship.getConfig();
121+
HullConfig config2 = hero.getHull().getHullConfig();
122+
return config1.equals(config2);
123+
} else {
124+
throw new IllegalArgumentException("ChangeShip:isSameShip received " + shipToBuy.getClass() + " argument instead of ShipItem!");
125+
}
126+
}
127+
98128
private void changeShip(SolGame game, SolShip hero, ShipItem selected) {
99129
HullConfig newConfig = selected.getConfig();
100130
Hull hull = hero.getHull();

main/src/org/destinationsol/game/ship/hulls/Hull.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,8 @@ public Fixture getShieldFixture() {
205205
public float getMass() {
206206
return myMass;
207207
}
208+
209+
public HullConfig getHullConfig() {
210+
return config;
211+
}
208212
}

0 commit comments

Comments
 (0)