Skip to content

Commit

Permalink
create StateManager, change all State calls to managed calls
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LucidCrux committed Jul 23, 2013
1 parent 1b1088d commit db4a346
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 221 deletions.
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
<meta itemprop="name" property="og:title" content="A Dark Room" />
<link rel="shortcut icon" href="favicon.ico" />
<link rel="image_src" href="img/adr.png" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
use local file for testing to avoid loads -->
<script src="lib/jquery.min.js"></script>
<script src="lib/jquery.color-2.1.2.min.js"></script>
<script src="lib/jquery.event.move.js"></script>
<script src="lib/jquery.event.swipe.js"></script>
<script src="script/Button.js"></script>
<script src="script/engine.js"></script>
<script src="script/state_manager.js"></script>
<script src="script/header.js"></script>
<script src="script/notifications.js"></script>
<script src="script/events.js"></script>
Expand Down
85 changes: 31 additions & 54 deletions script/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ var Engine = {

options: {
state: null,
debug: false,
log: false
debug: true,
log: true
},

init: function(options) {
Expand Down Expand Up @@ -112,6 +112,7 @@ var Engine = {
swipeElement.on('swipeup', Engine.swipeUp);
swipeElement.on('swipedown', Engine.swipeDown);

$SM.init();
Notifications.init();
Events.init();
Room.init();
Expand All @@ -122,7 +123,7 @@ var Engine = {
if(Engine.getStore('compass') > 0) {
Path.init();
}
if(State.ship) {
if($SM.get('features.location.spaceShip')) {
Ship.init();
}

Expand Down Expand Up @@ -165,8 +166,6 @@ var Engine = {
} catch(e) {
State = {
version: 1.2,
stores: {},
perks: {}
};
Engine.event('progress', 'new game');
}
Expand Down Expand Up @@ -324,58 +323,48 @@ var Engine = {
},

addPerk: function(name) {
if(!State.perks) {
State.perks = {};
if(!$SM.get('character.perks')) {
$SM.set('character.perks', {});
}
State.perks[name] = true;
$SM.set('character.perks[\''+name+'\']', true);
Notifications.notify(null, Engine.Perks[name].notify);
if(Engine.activeModule == Path) {
Path.updatePerks();
}
},

hasPerk: function(name) {
return typeof State.perks == 'object' && State.perks[name] == true;
return typeof $SM.get('character.perks') == 'object' && $SM.get('character.perks[\''+name+'\']') == true;
},

setStore: function(name, number) {
if(typeof State.stores == 'undefined') {
State.stores = {};
}
if(number > Engine.MAX_STORE) number = Engine.MAX_STORE;
State.stores[name] = number;
$SM.set('stores[\''+name+'\']', number);
Room.updateStoresView();
Room.updateBuildButtons();
if(State.outside) {
if($SM.get('features.location.outside')) {
Outside.updateVillage();
}
Engine.saveGame();
},

setStores: function(list) {
if(typeof State.stores == 'undefined') {
State.stores = {};
}
for(k in list) {
State.stores[k] = list[k] > Engine.MAX_STORE ? Engine.MAX_STORE : list[k];
$SM.set('stores[\''+k+'\']', list[k]);
}
Room.updateStoresView();
Room.updateBuildButtons();
if(State.outside) {
if($SM.get('features.location.outside')) {
Outside.updateVillage();
}
Engine.saveGame();
},

addStore: function(name, number) {
if(typeof State.stores == 'undefined') {
State.stores = {};
}
var num = State.stores[name];
var num = $SM.get('stores[\''+name+'\']');
if(typeof num != 'number' || isNaN(num) || num < 0) num = 0;
num += number;
if(num > Engine.MAX_STORE) num = Engine.MAX_STORE;
State.stores[name] = num;
$SM.set('stores[\''+name+'\']', num);
Room.updateStoresView();
Room.updateBuildButtons();
Outside.updateVillage();
Expand All @@ -386,14 +375,10 @@ var Engine = {
},

addStores: function(list, ignoreCosts) {
if(typeof State.stores == 'undefined') {
State.stores = {};
}

// Make sure any income costs can be paid
if(!ignoreCosts) {
for(k in list) {
var num = State.stores[k];
var num = $SM.get('stores[\''+k+'\']');
if(typeof num != 'number' || isNaN(num) || num < 0) num = 0;
if(num + list[k] < 0) {
return false;
Expand All @@ -403,12 +388,12 @@ var Engine = {

// Actually do the update
for(k in list) {
var num = State.stores[k];
var num = $SM.get('stores[\''+k+'\']');
if(typeof num != 'number') num = 0;
num += list[k];
num = num < 0 ? 0 : num;
num = num > Engine.MAX_STORE ? Engine.MAX_STORE : num;
State.stores[k] = num;
$SM.set('stores[\''+k+'\']', num);
}
Room.updateStoresView();
Room.updateBuildButtons();
Expand All @@ -421,50 +406,42 @@ var Engine = {
},

storeAvailable: function(name) {
return typeof State.stores[name] == 'number';
return typeof $SM.get('stores[\''+name+'\']') == 'number';
},

getStore: function(name) {
if(typeof State.stores == 'undefined' || typeof State.stores[name] == 'undefined' ) {
if(typeof $SM.get('stores[\''+name+'\']') == 'undefined') {
return 0;
}
return State.stores[name];
return $SM.get('stores[\''+name+'\']');
},

setIncome: function(source, options) {
if(typeof State.income == 'undefined') {
State.income = {};
}
var existing = State.income[source];
var existing = $SM.get('income[\''+source+'\']');
if(typeof existing != 'undefined') {
options.timeLeft = existing.timeLeft;
}
State.income[source] = options;
$SM.set('income[\''+source+'\']', options);
},

getIncome: function(source) {
if(typeof State.income == 'undefined') {
State.income = {};
}
var existing = State.income[source];
var existing = $SM.get('income[\''+source+'\']');
if(typeof existing != 'undefined') {
return existing;
}
return {};
},

removeIncome: function(source) {
if(State.income) {
delete State.income[source];
}
$SM.remove('income[\''+source+'\']');
Room.updateIncomeView();
},

collectIncome: function() {
if(typeof State.income != 'undefined' && Engine.activeModule != Space) {
if(typeof $SM.get('income') != 'undefined' && Engine.activeModule != Space) {
var changed = false;
for(var source in State.income) {
var income = State.income[source];
for(var source in $SM.get('income')) {
var income = $SM.get('income[\''+source+'\']');
if(typeof income.timeLeft != 'number')
{
income.timeLeft = 0;
Expand Down Expand Up @@ -501,15 +478,15 @@ var Engine = {
},

addStolen: function(stores) {
if(!State.stolen) State.stolen = {};
if(!$SM.get('game.stolen')) $SM.set('game.stolen', {});
for(var k in stores) {
if(!State.stolen[k]) State.stolen[k] = 0;
State.stolen[k] -= stores[k];
if(!$SM.get('game.stolen[\''+k+'\']')) $SM.set('game.stolen[\''+k+'\']', 0);
$SM.add('game.stolen[\''+k+'\']', stores[k] * -1);
}
},

startThieves: function() {
State.thieves = 1;
$SM.set('game.thieves', 1);
Engine.setIncome('thieves', {
delay: 10,
stores: {
Expand Down
10 changes: 5 additions & 5 deletions script/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ var Events = {
var weaponName = btn.attr('id').substring(7).replace('-', ' ');
var weapon = World.Weapons[weaponName];
if(weapon.type == 'unarmed') {
if(!State.punches) State.punches = 0;
State.punches++;
if(State.punches == 50 && !Engine.hasPerk('boxer')) {
if(!$SM.get('character.punches')) $SM.set('character.punches', 0);
$SM.add('character.punches', 1);
if($SM.get('character.punches') == 50 && !Engine.hasPerk('boxer')) {
Engine.addPerk('boxer');
} else if(State.punches == 150 && !Engine.hasPerk('martial artist')) {
} else if($SM.get('character.punches') == 150 && !Engine.hasPerk('martial artist')) {
Engine.addPerk('martial artist');
} else if(State.punches == 300 && !Engine.hasPerk('unarmed master')) {
} else if($SM.get('character.punches') == 300 && !Engine.hasPerk('unarmed master')) {
Engine.addPerk('unarmed master');
}

Expand Down
8 changes: 4 additions & 4 deletions script/events/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Events.Global = [
{ /* The Thief */
title: 'The Thief',
isAvailable: function() {
return (Engine.activeModule == Room || Engine.activeModule == Outside) && State.thieves == 1;
return (Engine.activeModule == Room || Engine.activeModule == Outside) && $SM.get('game.thieves') == 1;
},
scenes: {
'start': {
Expand All @@ -32,9 +32,9 @@ Events.Global = [
'the point is made. in the next few days, the missing supplies are returned.'
],
onLoad: function() {
State.thieves = 2;
$SM.set('game.thieves', 2);
Engine.removeIncome('thieves');
Engine.addStores(State.stolen);
Engine.addStores($SM.get('game.stolen'));
},
buttons: {
'leave': {
Expand All @@ -49,7 +49,7 @@ Events.Global = [
"shares what he knows about sneaking before he goes."
],
onLoad: function() {
State.thieves = 2;
$SM.set('game.thieves', 2);
Engine.removeIncome('thieves');
Engine.addPerk('stealthy');
},
Expand Down
2 changes: 1 addition & 1 deletion script/events/outside.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Events.Outside = [
{ /* Soldier attack */
title: 'A Military Raid',
isAvailable: function() {
return Engine.activeModule == Outside && Outside.getPopulation() > 0 && State.cityCleared;
return Engine.activeModule == Outside && Outside.getPopulation() > 0 && $SM.get('game.cityCleared');;
},
scenes: {
'start': {
Expand Down
6 changes: 3 additions & 3 deletions script/events/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Events.Room = [
{ /* The Scout -- Map Merchant */
title: 'The Scout',
isAvailable: function() {
return Engine.activeModule == Room && typeof State.world == 'object';
return Engine.activeModule == Room && $SM.get('features.location.world');
},
scenes: {
'start': {
Expand Down Expand Up @@ -435,7 +435,7 @@ Events.Room = [
{ /* The Wandering Master */
title: 'The Master',
isAvailable: function() {
return Engine.activeModule == Room && typeof State.world == 'object';
return Engine.activeModule == Room && $SM.get('features.location.world');
},
scenes: {
'start': {
Expand Down Expand Up @@ -507,7 +507,7 @@ Events.Room = [
{ /* The Sick Man */
title: 'The Sick Man',
isAvailable: function() {
return Engine.activeModule == Room && typeof State.world == 'object';
return Engine.activeModule == Room && $SM.get('features.location.world');
},
scenes: {
'start': {
Expand Down
Loading

0 comments on commit db4a346

Please sign in to comment.