Files
adarkroom/script/path.js
T
LucidCrux db4a346d21 create StateManager, change all State calls to managed calls
Introduced state_manager.js almost all State gets/sets are now run
through the manager (alias $SM). For now it was a simple, mostly
straightforward replacement of calls. This means that there are
redundancies and a lot of now unneeded code for things the SM will
handle. However, since I had trouble with making those changes as well
as introducing the manager all at once my first attempt, I am taking the
wiser approach and making "one change" at a time like I should have
instead of being too sure of myself.

At this point, it seems to work, but there may be bugs I didn't catch.
There was also no attempt made to update old saves to work with this. In
theory, it shouldn't be too hard. (included is a list of all state
changes)

TODO:
Save Update.
Refactor: a lot, many many redundancies now.
Refactor: "location centric" to "global centric".
Relocate all calls to different update functions to event listeners
where possible.

======================================================

The changes to State are as follows:

.room (exists) > features.location.room
.room > game.room
.room.builder > game.room.builder
.room.temperature > game.room.temperature
.room.fire > game.room.fire
.room.buttons > game.room.buttons

.outside (exists) > features.location.outside
.outside > game.outside
.outside.population > game.outside.population
.outside.buildings > game.outside.buildings
.outside.workers > game.outside.workers
.outside.seenForest > game.outside.seenForest

.world (exists) > features.location.world
.world > game.world
.world.map > game.world.map
.world.mask > game.world.mask
.starved > character.starved
.dehydrated > character.dehydrated

.ship (exists) > featuers.location.spaceShip
.ship > game.spaceShip
.ship.hull > game.spaceShip.hull
.ship.thrusters > game.spaceShip.thrusters
.ship.seenWarning > game.spaceShip.seenWarning
.ship.seenShip > game.spaceShip.seenShip

.punches > character.punches
.perks > character.perks

.thieves > game.thieves
.stolen > game.stolen
.cityCleared > game.cityCleared

.stores > stores
.income > income
2013-07-23 01:24:47 -06:00

296 lines
9.0 KiB
JavaScript

var Path = {
DEFAULT_BAG_SPACE: 10,
// Everything not in this list weighs 1
Weight: {
'bone spear': 2,
'iron sword': 3,
'steel sword': 5,
'rifle': 5,
'bullets': 0.1,
'energy cell': 0.2,
'laser rifle': 5,
'bolas': 0.5
},
name: 'Path',
options: {}, // Nuthin'
init: function(options) {
this.options = $.extend(
this.options,
options
);
// Init the World
World.init();
// Create the path tab
this.tab = Header.addLocation("A Dusty Path", "path", Path);
// Create the Path panel
this.panel = $('<div>').attr('id', "pathPanel")
.addClass('location')
.appendTo('div#locationSlider');
// Add the outfitting area
var outfitting = $('<div>').attr('id', 'outfitting').appendTo(this.panel);
var bagspace = $('<div>').attr('id', 'bagspace').appendTo(outfitting);
// Add the embark button
new Button.Button({
id: 'embarkButton',
text: "embark",
click: Path.embark,
width: '80px',
cooldown: World.DEATH_COOLDOWN
}).appendTo(this.panel);
Path.outfit = {};
Engine.updateSlider();
},
getWeight: function(thing) {
var w = Path.Weight[thing];
if(typeof w != 'number') w = 1;
return w;
},
getCapacity: function() {
if(Engine.getStore('convoy') > 0) {
return Path.DEFAULT_BAG_SPACE + 60;
} else if(Engine.getStore('wagon') > 0) {
return Path.DEFAULT_BAG_SPACE + 30;
} else if(Engine.getStore('rucksack') > 0) {
return Path.DEFAULT_BAG_SPACE + 10;
}
return Path.DEFAULT_BAG_SPACE;
},
getFreeSpace: function() {
var num = 0;
if(Path.outfit) {
for(var k in Path.outfit) {
var n = Path.outfit[k];
if(isNaN(n)) {
// No idea how this happens, but I will fix it here!
Path.outfit[k] = n = 0;
}
num += n * Path.getWeight(k);
}
}
return Path.getCapacity() - num;
},
updatePerks: function() {
if($SM.get('character.perks')) {
var perks = $('#perks');
var needsAppend = false;
if(perks.length == 0) {
needsAppend = true;
perks = $('<div>').attr('id', 'perks');
}
for(var k in $SM.get('character.perks')) {
var id = 'perk_' + k.replace(' ', '-');
var r = $('#' + id);
if($SM.get('character.perks[\''+k+'\']') && r.length == 0) {
r = $('<div>').attr('id', id).addClass('perkRow').appendTo(perks);
$('<div>').addClass('row_key').text(k).appendTo(r);
$('<div>').addClass('tooltip bottom right').text(Engine.Perks[k].desc).appendTo(r);
}
}
if(needsAppend && perks.children().length > 0) {
perks.appendTo(Path.panel);
}
}
if(Engine.activeModule === Path) {
$('#storesContainer').css({top: perks.height() + 26 + 'px'});
}
},
updateOutfitting: function() {
var outfit = $('div#outfitting');
if(!Path.outfit) {
Path.outfit = {};
}
// Add the armour row
var armour = "none";
if(Engine.getStore('s armour') > 0)
armour = "steel";
else if(Engine.getStore('i armour') > 0)
armour = "iron";
else if(Engine.getStore('l armour') > 0)
armour = "leather";
var aRow = $('#armourRow');
if(aRow.length == 0) {
aRow = $('<div>').attr('id', 'armourRow').addClass('outfitRow').prependTo(outfit);
$('<div>').addClass('row_key').text('armour').appendTo(aRow);
$('<div>').addClass('row_val').text(armour).appendTo(aRow);
$('<div>').addClass('clear').appendTo(aRow);
} else {
$('.row_val', aRow).text(armour);
}
// Add the water row
var wRow = $('#waterRow');
if(wRow.length == 0) {
wRow = $('<div>').attr('id', 'waterRow').addClass('outfitRow').insertAfter(aRow);
$('<div>').addClass('row_key').text('water').appendTo(wRow);
$('<div>').addClass('row_val').text(World.getMaxWater()).appendTo(wRow);
$('<div>').addClass('clear').appendTo(wRow);
} else {
$('.row_val', wRow).text(World.getMaxWater());
}
var space = Path.getFreeSpace();
var total = 0;
// Add the non-craftables to the craftables
var carryable = $.extend({
'cured meat': { type: 'tool' },
'bullets': { type: 'tool' },
'grenade': {type: 'weapon' },
'bolas': {type: 'weapon' },
'laser rifle': {type: 'weapon' },
'energy cell': {type: 'tool' },
'bayonet': {type: 'weapon' },
'charm': {type: 'tool'},
'medicine': {type: 'tool'}
}, Room.Craftables);
for(var k in carryable) {
var store = carryable[k];
var have = $SM.get('stores[\''+k+'\']');
var num = Path.outfit[k];
num = typeof num == 'number' ? num : 0;
var numAvailable = Engine.getStore(k);
var row = $('div#outfit_row_' + k.replace(' ', '-'), outfit);
if((store.type == 'tool' || store.type == 'weapon') && have > 0) {
total += num * Path.getWeight(k);
if(row.length == 0) {
row = Path.createOutfittingRow(k, num);
var curPrev = null;
outfit.children().each(function(i) {
var child = $(this);
if(child.attr('id').indexOf('outfit_row_') == 0) {
var cName = child.attr('id').substring(11).replace('-', ' ');
if(cName < k && (curPrev == null || cName > curPrev)) {
curPrev = cName;
}
}
});
if(curPrev == null) {
row.insertAfter(wRow);
}
else
{
row.insertAfter(outfit.find('#outfit_row_' + curPrev.replace(' ', '-')));
}
} else {
$('div#' + row.attr('id') + ' > div.row_val > span', outfit).text(num);
$('div#' + row.attr('id') + ' .tooltip .numAvailable', outfit).text(numAvailable - num);
}
if(num == 0) {
$('.dnBtn', row).addClass('disabled');
$('.dnManyBtn', row).addClass('disabled');
} else {
$('.dnBtn', row).removeClass('disabled');
$('.dnManyBtn', row).removeClass('disabled');
}
if(num >= numAvailable || space < Path.getWeight(k)) {
$('.upBtn', row).addClass('disabled');
$('.upManyBtn', row).addClass('disabled');
} else if(space >= Path.getWeight(k)) {
$('.upBtn', row).removeClass('disabled');
$('.upManyBtn', row).removeClass('disabled');
}
} else if(have == 0 && row.length > 0) {
row.remove();
}
}
// Update bagspace
$('#bagspace').text('free ' + Math.floor(Path.getCapacity() - total) + '/' + Path.getCapacity());
if(Path.outfit['cured meat'] > 0) {
Button.setDisabled($('#embarkButton'), false);
} else {
Button.setDisabled($('#embarkButton'), true);
}
},
createOutfittingRow: function(name, num) {
var row = $('<div>').attr('id', 'outfit_row_' + name.replace(' ', '-')).addClass('outfitRow');
$('<div>').addClass('row_key').text(name).appendTo(row);
var val = $('<div>').addClass('row_val').appendTo(row);
$('<span>').text(num).appendTo(val);
$('<div>').addClass('upBtn').appendTo(val).click([1], Path.increaseSupply);
$('<div>').addClass('dnBtn').appendTo(val).click([1], Path.decreaseSupply);
$('<div>').addClass('upManyBtn').appendTo(val).click([10], Path.increaseSupply);
$('<div>').addClass('dnManyBtn').appendTo(val).click([10], Path.decreaseSupply);
$('<div>').addClass('clear').appendTo(row);
var numAvailable = Engine.getStore(name);
var tt = $('<div>').addClass('tooltip bottom right').appendTo(row);
$('<div>').addClass('row_key').text('weight').appendTo(tt);
$('<div>').addClass('row_val').text(Path.getWeight(name)).appendTo(tt);
$('<div>').addClass('row_key').text('available').appendTo(tt);
$('<div>').addClass('row_val').addClass('numAvailable').text(numAvailable).appendTo(tt);
return row;
},
increaseSupply: function(btn) {
var supply = $(this).closest('.outfitRow').children('.row_key').text().replace('-', ' ');
Engine.log('increasing ' + supply + ' by up to ' + btn.data);
var cur = Path.outfit[supply];
cur = typeof cur == 'number' ? cur : 0;
if(Path.getFreeSpace() >= Path.getWeight(supply) && cur < Engine.getStore(supply)) {
var maxExtraByWeight = Math.floor(Path.getFreeSpace() / Path.getWeight(supply));
var maxExtraByStore = Engine.getStore(supply) - cur;
var maxExtraByBtn = btn.data;
Path.outfit[supply] = cur + Math.min(maxExtraByBtn, Math.min(maxExtraByWeight, maxExtraByStore));
Path.updateOutfitting();
}
},
decreaseSupply: function(btn) {
var supply = $(this).closest('.outfitRow').children('.row_key').text().replace('-', ' ');
Engine.log('decreasing ' + supply + ' by up to ' + btn.data);
var cur = Path.outfit[supply];
cur = typeof cur == 'number' ? cur : 0;
if(cur > 0) {
Path.outfit[supply] = Math.max(0, cur - btn.data);
Path.updateOutfitting();
}
},
onArrival: function(transition_diff) {
Path.setTitle();
Path.updateOutfitting();
Path.updatePerks();
Engine.moveStoresView($('#perks'), transition_diff);
},
setTitle: function() {
document.title = 'A Dusty Path';
},
embark: function() {
for(var k in Path.outfit) {
Engine.addStore(k, -Path.outfit[k]);
}
World.onArrival();
$('#outerSlider').animate({left: '-700px'}, 300);
Engine.activeModule = World;
}
}