Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into Sound-FX
Browse files Browse the repository at this point in the history
  • Loading branch information
bjasspa committed Jan 9, 2016
2 parents 91575a2 + 41a08f3 commit 4883b07
Show file tree
Hide file tree
Showing 25 changed files with 439 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.phbouillon.android.framework;

public interface TimeFactorChangeListener {
void timeFactorChanged(float oldTimeFactor, float newTimeFactor);
}
13 changes: 11 additions & 2 deletions src/de/phbouillon/android/framework/impl/AndroidGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import de.phbouillon.android.framework.Graphics;
import de.phbouillon.android.framework.Input;
import de.phbouillon.android.framework.Screen;
import de.phbouillon.android.framework.TimeFactorChangeListener;
import de.phbouillon.android.framework.impl.gl.GlUtils;
import de.phbouillon.android.games.alite.AliteLog;
import de.phbouillon.android.games.alite.AliteStartManager;
Expand Down Expand Up @@ -79,13 +80,18 @@ enum GLGameState {
protected final TextureManager textureManager;
private int [] sizeArray = null;
private FatalExceptionScreen fatalException = null;
private TimeFactorChangeListener timeFactorChangeListener = null;

public AndroidGame(int targetWidth, int targetHeight) {
this.targetHeight = targetHeight;
this.targetWidth = targetWidth;
textureManager = new TextureManager(this);
}


public void setTimeFactorChangeListener(TimeFactorChangeListener tfl) {
timeFactorChangeListener = tfl;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -359,6 +365,9 @@ public float getTimeFactor() {
return timeFactor;
}
public void setTimeFactor(float tf) {
timeFactor = tf;
if (timeFactorChangeListener != null && Math.abs(timeFactor - tf) > 0.0001f) {
timeFactorChangeListener.timeFactorChanged(timeFactor, tf);
}
timeFactor = tf;
}
}
16 changes: 8 additions & 8 deletions src/de/phbouillon/android/games/alite/Alite.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,6 @@ protected void saveState() {
@Override
public void onPause() {
AliteLog.d("Alite.onPause", "onPause begin");
if (getCurrentScreen() != null && !(getCurrentScreen() instanceof FlightScreen)) {
try {
AliteLog.d("[ALITE]", "Performing autosave.");
getFileUtils().autoSave(this);
} catch (Exception e) {
AliteLog.e("[ALITE]", "Autosaving commander failed.", e);
}
}
try {
setSaving(true);
super.onPause();
Expand All @@ -368,6 +360,14 @@ public void onPause() {

@Override
public void onDestroy() {
if (getCurrentScreen() != null && !(getCurrentScreen() instanceof FlightScreen)) {
try {
AliteLog.d("[ALITE]", "Performing autosave.");
getFileUtils().autoSave(this);
} catch (Exception e) {
AliteLog.e("[ALITE]", "Autosaving commander failed.", e);
}
}
AliteLog.e("Alite.OnDestroy", "Destroying Alite...");
while (saving) {
AliteLog.e("OnDestroy", "Still saving...");
Expand Down
12 changes: 12 additions & 0 deletions src/de/phbouillon/android/games/alite/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ public final void loadCommander(Alite alite, DataInputStream dis) throws IOExcep
} catch (IOException e) {
AliteLog.e("[ALITE] loadCommander", "Old version. Cmdr data lacks price data for inventory", e);
}
try {
for (int i = 0; i < 18; i++) {
long weightInGrams = dis.readLong();
cobra.setUnpunishedTradeGood(TradeGoodStore.get().fromNumber(i), Weight.grams(weightInGrams));
}
} catch (IOException e) {
AliteLog.e("[ALITE] loadCommander", "Old version. Cmdr data lacks unpunished data for inventory", e);
}

AliteLog.d("[ALITE] loadCommander", String.format("Loaded Commander '%s', galaxyNumber: %d, seed: %04x %04x %04x", player.getName(), generator.getCurrentGalaxy(), (int) generator.getCurrentSeed()[0], (int) generator.getCurrentSeed()[1], (int) generator.getCurrentSeed()[2]));
}

Expand Down Expand Up @@ -617,6 +626,9 @@ public final void saveCommander(final Alite alite, DataOutputStream dos) throws
for (InventoryItem item: cobra.getInventory()) {
dos.writeLong(item.getPrice());
}
for (InventoryItem item: cobra.getInventory()) {
dos.writeLong(item.getUnpunished().getWeightInGrams());
}
}

private String determineOldestAutosaveSlot(Alite alite) throws IOException {
Expand Down
21 changes: 21 additions & 0 deletions src/de/phbouillon/android/games/alite/model/InventoryItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class InventoryItem {
private Weight weight;
private Weight unpunished;
private long totalBuyPrice;

public InventoryItem() {
Expand All @@ -18,9 +19,29 @@ public void set(Weight w, long price) {
weight = weight.set(w);
}

public void addUnpunished(Weight w) {
unpunished = unpunished.add(w);
}

public void subUnpunished(Weight weight) {
unpunished = unpunished.sub(weight);
if (unpunished.getWeightInGrams() < 0) {
unpunished = Weight.grams(0);
}
}

public void resetUnpunished() {
unpunished = Weight.grams(0);
}

public Weight getUnpunished() {
return unpunished;
}

public void clear() {
this.weight = Weight.grams(0);
this.totalBuyPrice = 0;
this.unpunished = Weight.grams(0);
}

public long getPrice() {
Expand Down
21 changes: 21 additions & 0 deletions src/de/phbouillon/android/games/alite/model/PlayerCobra.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,27 @@ public void addTradeGood(TradeGood good, Weight weight, long price) {
inventory[ordinal].add(weight, price);
}

public void addUnpunishedTradeGood(TradeGood good, Weight weight) {
int ordinal = TradeGoodStore.get().ordinal(good);
inventory[ordinal].addUnpunished(weight);
}

public void subUnpunishedTradeGood(TradeGood good, Weight weight) {
int ordinal = TradeGoodStore.get().ordinal(good);
inventory[ordinal].subUnpunished(weight);
}

public void setTradeGood(TradeGood good, Weight weight, long price) {
int ordinal = TradeGoodStore.get().ordinal(good);
inventory[ordinal].set(weight, price);
}

public void setUnpunishedTradeGood(TradeGood good, Weight unpunished) {
int ordinal = TradeGoodStore.get().ordinal(good);
inventory[ordinal].resetUnpunished();
inventory[ordinal].addUnpunished(unpunished);
}

public Weight removeTradeGood(TradeGood good) {
int ordinal = TradeGoodStore.get().ordinal(good);
Weight currentWeight = inventory[ordinal].getWeight();
Expand Down Expand Up @@ -246,6 +262,10 @@ public boolean hasCargo() {
return false;
}

public boolean hasCargo(TradeGood good) {
return inventory[TradeGoodStore.get().ordinal(good)].getWeight().getWeightInGrams() > 0;
}

public void clearInventory() {
fillTradeGoods();
}
Expand All @@ -254,6 +274,7 @@ public void setInventory(InventoryItem [] data) {
clearInventory();
for (int i = 0; i < data.length; i++) {
setTradeGood(TradeGoodStore.get().goods()[i], data[i].getWeight(), data[i].getPrice());
setUnpunishedTradeGood(TradeGoodStore.get().goods()[i], data[i].getUnpunished());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public TradeGood fromNumber(int number) {
return goods[number];
}

public TradeGood getRandomTradeGoodForContainer() {
int num;

do {
num = (int) (Math.random() * NUMBER_OF_GOODS);
} while (num == ALIEN_ITEMS);

return goods[num];
}

public TradeGood food() { return goods[FOOD]; }
public TradeGood textiles() { return goods[TEXTILES]; }
public TradeGood radioactives() { return goods[RADIOACTIVES]; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ public Screen touched(Alite game, int x, int y) {
else {
if (entry.title.equals("Launch")) {
SoundManager.play(Assets.click);
try {
AliteLog.d("[ALITE]", "Performing autosave. [Launch]");
((Alite) game).getFileUtils().autoSave((Alite) game);
} catch (Exception e) {
AliteLog.e("[ALITE]", "Autosaving commander failed.", e);
}
// newScreen = new AutomaticLaunchScreen(game);
newScreen = new FlightScreen(game, true);
} else if (entry.title.equals("Gal. Jump")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ protected void presentSelection(int row, int column) {
public void setBoughtAmountString(String s) {
this.boughtAmount = s;
}



@Override
public void performTrade(int row, int column) {
TradeGood tradeGood = TradeGoodStore.get().goods()[row * COLUMNS + column];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import de.phbouillon.android.games.alite.Settings;
import de.phbouillon.android.games.alite.SoundManager;
import de.phbouillon.android.games.alite.colors.AliteColors;
import de.phbouillon.android.games.alite.model.EquipmentStore;
import de.phbouillon.android.games.alite.model.InventoryItem;
import de.phbouillon.android.games.alite.model.Player;
import de.phbouillon.android.games.alite.model.PlayerCobra;
Expand All @@ -44,6 +45,8 @@
import de.phbouillon.android.games.alite.model.trading.TradeGood;
import de.phbouillon.android.games.alite.model.trading.TradeGoodStore;
import de.phbouillon.android.games.alite.model.trading.Unit;
import de.phbouillon.android.games.alite.screens.opengl.ingame.FlightScreen;
import de.phbouillon.android.games.alite.screens.opengl.ingame.InGameManager;

//This screen never needs to be serialized, as it is not part of the InGame state.
@SuppressWarnings("serial")
Expand All @@ -59,6 +62,7 @@ static class InventoryPair {
}

private static final String INVENTORY_HINT = "(Tap again to sell)";
private static final String EJECT_HINT = "(Tap again to eject)";
private final ArrayList<InventoryPair> inventoryList = new ArrayList<InventoryPair>();
private Pixmap [] tradegoods;
private Pixmap [] beam;
Expand Down Expand Up @@ -266,9 +270,43 @@ protected void presentSelection(int row, int column) {
String gainText = computeGainLossString(pair) + ". ";
int widthComplete = width + game.getGraphics().getTextWidth(gainText, Assets.regularFont);
game.getGraphics().drawText(gainText, X_OFFSET + width, 1050, gainText.startsWith("Loss") ? AliteColors.get().conditionRed() : AliteColors.get().conditionGreen(), Assets.regularFont);
game.getGraphics().drawText(INVENTORY_HINT, X_OFFSET + widthComplete, 1050, AliteColors.get().message(), Assets.regularFont);
Alite alite = (Alite) game;
if (alite.getCurrentScreen() instanceof FlightScreen && alite.getCobra().isEquipmentInstalled(EquipmentStore.fuelScoop)) {
game.getGraphics().drawText(EJECT_HINT, X_OFFSET + widthComplete, 1050, AliteColors.get().message(), Assets.regularFont);
} else {
game.getGraphics().drawText(INVENTORY_HINT, X_OFFSET + widthComplete, 1050, AliteColors.get().message(), Assets.regularFont);
}
}


protected void performTradeWhileInFlight(int row, int column) {
if (!((Alite) game).getCobra().isEquipmentInstalled(EquipmentStore.fuelScoop)) {
super.performTradeWhileInFlight(row, column);
return;
}
SoundManager.play(Assets.retroRocketsOrEscapeCapsuleFired);
int index = row * COLUMNS + column;
if (index >= inventoryList.size()) {
return;
}
InventoryPair pair = inventoryList.get(index);
TradeGood tradeGood = pair.good;
Player player = ((Alite) game).getPlayer();
Weight ejectedWeight;
long ejectedPrice = pair.item.getPrice();
if (pair.item.getWeight().compareTo(Weight.tonnes(4)) < 0) {
ejectedWeight = pair.item.getWeight();
player.getCobra().removeTradeGood(tradeGood);
} else {
ejectedWeight = Weight.tonnes(4);
player.getCobra().setTradeGood(tradeGood, pair.item.getWeight().sub(ejectedWeight), pair.item.getPrice());
player.getCobra().subUnpunishedTradeGood(tradeGood, ejectedWeight);
}
InGameManager manager = ((FlightScreen) ((Alite) game).getCurrentScreen()).getInGameManager();
manager.getLaserManager().ejectPlayerCargoCanister(manager.getShip(), tradeGood, ejectedWeight, ejectedPrice);

createButtons();
}

@Override
public void performTrade(int row, int column) {
int index = row * COLUMNS + column;
Expand All @@ -286,6 +324,8 @@ public void performTrade(int row, int column) {
Market market = player.getMarket();
int factor = pair.good.getUnit() == Unit.TON ? 1000000 : pair.good.getUnit() == Unit.KILOGRAM ? 1000 : 1;
long price = computePrice(market, factor, pair.item.getWeight().getWeightInGrams(), pair.good);
((Alite) game).getPlayer().setLegalValue(
((Alite) game).getPlayer().getLegalValue() + (int) (tradeGood.getLegalityType() * pair.item.getUnpunished().getQuantityInAppropriateUnit()));
player.getCobra().removeTradeGood(tradeGood);
player.setCash(player.getCash() + price);
cashLeft = String.format("Cash: %d.%d Cr", player.getCash() / 10, player.getCash() % 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import de.phbouillon.android.framework.Screen;
import de.phbouillon.android.framework.impl.AndroidGraphics;
import de.phbouillon.android.games.alite.Alite;
import de.phbouillon.android.games.alite.AliteLog;
import de.phbouillon.android.games.alite.AliteStartManager;
import de.phbouillon.android.games.alite.Assets;
import de.phbouillon.android.games.alite.screens.opengl.ingame.FlightScreen;
Expand Down Expand Up @@ -67,6 +68,12 @@ public void processTouch(TouchEvent touch) {
super.processTouch(touch);
if (messageResult != 0) {
if (messageResult == 1) {
try {
AliteLog.d("[ALITE]", "Performing autosave. [Quit]");
((Alite) game).getFileUtils().autoSave(((Alite) game));
} catch (Exception e) {
AliteLog.e("[ALITE]", "Autosaving commander failed.", e);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((Alite) game).finishAffinity();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ protected void presentTradeStatus() {
g.drawText(spareText, currentX, 1000, AliteColors.get().informationText(), Assets.regularFont);
}

protected void performTradeWhileInFlight(int row, int column) {
SoundManager.play(Assets.error);
errorText = "Not Docked.";
}

public void presentTradeGoods(float deltaTime) {
Graphics g = game.getGraphics();

Expand Down Expand Up @@ -174,8 +179,7 @@ public void processTouch(TouchEvent touch) {
handled = true;
if (selection == tradeButton[x][y]) {
if (((Alite) game).getCurrentScreen() instanceof FlightScreen) {
SoundManager.play(Assets.error);
errorText = "Not Docked.";
performTradeWhileInFlight(y, x);
} else {
SoundManager.play(Assets.click);
performTrade(y, x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ private TutAdvancedFlying(final Alite alite, int lineIndex, FlightScreen flight)
savedGalaxySeed = alite.getGenerator().getCurrentSeed();
savedPresentSystem = alite.getPlayer().getCurrentSystem();
savedHyperspaceSystem = alite.getPlayer().getHyperspaceSystem();
savedInstalledEquipment = alite.getCobra().getInstalledEquipment();
savedInstalledEquipment = new ArrayList<Equipment>();
for (Equipment e: alite.getCobra().getInstalledEquipment()) {
savedInstalledEquipment.add(e);
}
AliteLog.w("Setting installed Equipment [constructor]", "Number of Equipment items: " + savedInstalledEquipment.size());
savedLasers[0] = alite.getCobra().getLaser(PlayerCobra.DIR_FRONT);
savedLasers[1] = alite.getCobra().getLaser(PlayerCobra.DIR_RIGHT);
Expand All @@ -112,9 +115,9 @@ private TutAdvancedFlying(final Alite alite, int lineIndex, FlightScreen flight)
for (int i = 0; i < TradeGoodStore.get().goods().length; i++) {
savedInventory[i] = new InventoryItem();
savedInventory[i].set(currentItems[i].getWeight(), currentItems[i].getPrice());
savedInventory[i].addUnpunished(currentItems[i].getUnpunished());
}



for (Equipment e: savedInstalledEquipment) {
alite.getCobra().removeEquipment(e);
}
Expand Down Expand Up @@ -245,7 +248,7 @@ public void execute(float deltaTime) {
switchScreen.savedPresentSystem = savedPresentSystem;
switchScreen.savedHyperspaceSystem = savedHyperspaceSystem;
switchScreen.savedInstalledEquipment = new ArrayList<Equipment>();
for (int i = 0; i < savedInstalledEquipment.size(); i++) {
for (int i = 0; i < savedInstalledEquipment.size(); i++) {
switchScreen.savedInstalledEquipment.add(savedInstalledEquipment.get(i));
}
switchScreen.savedFuel = savedFuel;
Expand Down Expand Up @@ -751,6 +754,7 @@ public static boolean initialize(Alite alite, DataInputStream dis) {
for (int i = 0; i < ta.savedInventory.length; i++) {
ta.savedInventory[i] = new InventoryItem();
ta.savedInventory[i].set(Weight.grams(dis.readLong()), dis.readLong());
ta.savedInventory[i].addUnpunished(Weight.grams(dis.readLong()));
}
ta.time = dis.readLong();
ta.savedMarketFluct = dis.readInt();
Expand Down Expand Up @@ -810,6 +814,7 @@ public void saveScreenState(DataOutputStream dos) throws IOException {
for (InventoryItem w: savedInventory) {
dos.writeLong(w.getWeight().getWeightInGrams());
dos.writeLong(w.getPrice());
dos.writeLong(w.getUnpunished().getWeightInGrams());
}
dos.writeLong(time);
dos.writeInt(savedMarketFluct);
Expand Down
Loading

0 comments on commit 4883b07

Please sign in to comment.