Added untested Save and Load game functions for Trade Winds!

This commit is contained in:
Robbie Antenesse 2016-04-17 13:46:26 -06:00
parent 16e27ec04c
commit 8369415812
3 changed files with 104 additions and 4 deletions

View File

@ -41,9 +41,9 @@ function drawTitleScreen() {
// New Game
guiControl.drawPixelText("New Game", guiControl.title.leftBorder, guiControl.title.rowTop(0), 10, "white", 6);
// Load Game
guiControl.drawPixelText("Continue", guiControl.title.leftBorder, guiControl.title.rowTop(1), 10, (false) ? "white" : "black", 6);
guiControl.drawPixelText("Continue", guiControl.title.leftBorder, guiControl.title.rowTop(1), 10, (G.savedGameExists) ? "white" : "black", 6);
// Options
guiControl.drawPixelText("Options", guiControl.title.leftBorder, guiControl.title.rowTop(2) + pixel(), 8, "black", 6);
guiControl.drawPixelText("Options", guiControl.title.leftBorder, guiControl.title.rowTop(2) + pixel(), 8, (guiControl.optionsScreen) ? "white" : "black", 6);
// Draw cursor
OS.context.drawImage(guiControl.cursor, guiControl.title.leftBorder - (guiControl.iconScaled), guiControl.title.rowTop(guiControl.title.cursorPosition));
@ -58,9 +58,11 @@ function drawTitleScreen() {
mus_sail.Play();
guiControl.title.show = false;
G.gameStarted = true;
G.SaveGame();
break;
case 1:
if (false) { // once loading is in, allow this.
if (G.savedGameExists) { // once loading is in, allow this.
G.LoadGame();
snd_select.Play();
mus_title.Stop();
mus_sail.Play();

View File

@ -2,6 +2,7 @@ var Game = {};
G = Game;
G.gameStarted = false;
G.savedGameExists = (OS.Load("TradeWindsSave")) ? true : false;
G.player = {}; // Just a reference until G.player is created at rm_Ocean's load time.
G.oceanParticle = {}; // One ocean particle will exist at any time and move around the boat.
G.map = []; // List of island objects, generated/loaded and saved at game start, loaded on room start.
@ -114,6 +115,103 @@ G.economy = { // Aww yea, supply and demand.
}
};
G.SaveGame = function () {
var saveObject = {
playerX: G.player.x,
playerY: G.player.y,
money: G.inventory.money,
supplies: G.inventory.supplies,
cargo: G.inventory.cargo.slice(),
stats: {
inventory: G.stats.inventory,
hold: G.stats.hold,
speed: G.stats.speed,
hull: G.stats.hull,
maxHull: G.stats.maxHull,
popularity: G.stats.popularity,
haggling: G.stats.haggling,
crew: G.stats.crew,
energy: G.stats.energy,
maxEnergy: G.stats.maxEnergy,
illness: G.stats.illness
},
economy: {
innCost: G.economy.innCost,
innStays: G.economy.innStays,
itemWorth: G.economy.cargoItemWorth.slice(),
cargoSold: G.economy.cargoSold.slice(),
cargoBought: G.economy.cargoBought.slice()
},
map: []
};
for (var i = 0; i < G.map.length; i++) {
saveObject.map.push({
drawX: G.map[i].drawX,
drawY: G.map[i].drawY,
drawWidth: G.map[i].drawWidth,
drawHeight: G.map[i].drawHeight,
inventory: G.map[i].island.inventory.slice(),
innPriceDifference: G.map[i].island.innPriceDifference,
innStays: G.map[i].island.innStays,
priceDifferences: G.map[i].island.priceDifferences.slice(),
itemsSold: G.map[i].island.itemsSold.slice(),
itemsBought: G.map[i].island.itemsBought.slice()
});
}
if (OS.Save("TradeWindsSave", JSON.stringify(saveObject))) {
console.log("Game Saved!");
}
}
G.LoadGame = function () {
var loadObject = OS.Load("TradeWindsSave");
if (loadObject) {
loadObject = JSON.parse(loadObject);
G.player.x = loadObject.playerX;
G.player.y = loadObject.playerY;
G.inventory.money = loadObject.money;
G.inventory.supplies = loadObject.supplies;
G.inventory.cargo = loadObject.cargo.slice();
G.stats.inventory = loadObject.stats.inventory;
G.stats.hold = loadObject.stats.hold;
G.stats.speed = loadObject.stats.speed;
G.stats.hull = loadObject.stats.hull;
G.stats.maxHull = loadObject.stats.maxHull;
G.stats.popularity = loadObject.stats.popularity;
G.stats.haggling = loadObject.stats.haggling;
G.stats.crew = loadObject.stats.crew;
G.stats.energy = loadObject.stats.energy;
G.stats.maxEnergy = loadObject.stats.maxEnergy;
G.stats.illness = loadObject.stats.illness;
G.economy.innCost = loadObject.economy.innCost;
G.economy.innStays = loadObject.economy.innStays;
G.economy.itemWorth = loadObject.economy.itemWorth.slice();
G.economy.cargoSold = loadObject.economy.cargoSold.slice();
G.economy.cargoBought = loadObject.economy.cargoBought.slice();
for (var i = 0; i < loadObject.map.length; i++) {
G.map[i].drawX = loadObject.map[i].drawX;
G.map[i].drawY = loadObject.map[i].drawY;
G.map[i].drawWidth = loadObject.map[i].drawWidth;
G.map[i].drawHeight = loadObject.map[i].drawHeight;
G.map[i].island.x = rm_Ocean.squareSize * loadObject.map[i].drawX;
G.map[i].island.y = rm_Ocean.squareSize * loadObject.map[i].drawY;
G.map[i].island.inventory = loadObject.map[i].inventory.slice();
G.map[i].island.innPriceDifference = loadObject.map[i].innPriceDifference;
G.map[i].island.innStays = loadObject.map[i].innStays;
G.map[i].island.priceDifferences = loadObject.map[i].priceDifferences.slice();
G.map[i].island.itemsSold = loadObject.map[i].itemsSold.slice();
G.map[i].island.itemsBought = loadObject.map[i].itemsBought.slice();
}
loadObject = null;
console.log("Game Loaded!");
} else {
console.log("Could not load game!");
return false;
}
}
function loadGameManager () {
for (var i = 0; i < G.economy.cargoItemWorth.length; i++) {
G.economy.cargoItemWorth[i] += Math.round(Math.randomRange(-5, 5));

View File

@ -99,7 +99,7 @@ rm_Ocean.RunClock = function () {
if (rm_Ocean.clockTimerCount > rm_Ocean.clockTimerCutoff) {
rm_Ocean.clockTimerCount = 0;
// Play New_Day sound.
// Save the game.
G.SaveGame();
G.economy.UpdateEconomy();
for (var i = 0; i < G.map.length; i++) {