function for fire hut event and more

Effect of hut fire is associated with new Outside method .destroyHuts().
This commit is contained in:
Andrea Rendine
2015-05-11 21:05:08 +02:00
committed by Blake Grotewold
parent 09c070adfa
commit cb717b41d0
2 changed files with 33 additions and 5 deletions
+1 -4
View File
@@ -79,10 +79,7 @@ Events.Outside = [
notification: _('a fire has started'), notification: _('a fire has started'),
blink: true, blink: true,
onLoad: function() { onLoad: function() {
var population = $SM.get('game.population', true); Outside.destroyHuts(1);
var huts = $SM.get('game.buildings["hut"]', true);
$SM.set('game.buildings["hut"]', (huts - 1));
Outside.killVillagers(4);
}, },
buttons: { buttons: {
'mourn': { 'mourn': {
+32 -1
View File
@@ -8,6 +8,7 @@ var Outside = {
_GATHER_DELAY: 60, _GATHER_DELAY: 60,
_TRAPS_DELAY: 90, _TRAPS_DELAY: 90,
_POP_DELAY: [0.5, 3], _POP_DELAY: [0.5, 3],
_HUT_ROOM: 4,
_INCOME: { _INCOME: {
'gatherer': { 'gatherer': {
@@ -173,7 +174,7 @@ var Outside = {
}, },
getMaxPopulation: function() { getMaxPopulation: function() {
return $SM.get('game.buildings["hut"]', true) * 4; return $SM.get('game.buildings["hut"]', true) * Outside._HUT_ROOM;
}, },
increasePopulation: function() { increasePopulation: function() {
@@ -219,6 +220,36 @@ var Outside = {
} }
}, },
destroyHuts: function(num, allowEmpty) {
var dead = 0;
for(var i = 0; i < num; i++){
var population = $SM.get('game.population', true);
var rate = population / Outside._HUT_ROOM;
var full = Math.floor(rate);
// by default this is used to destroy full or half-full huts
// pass allowEmpty to include empty huts in the armageddon
var huts = (allowEmpty) ? $SM.get('game.buildings["hut"]', true) : Math.ceil(rate);
if(!huts) {
break;
}
// random can be 0 but not 1; however, 0 as a target is useless
var target = Math.floor(Math.random() * huts) + 1;
var inhabitants = 0;
if(target <= full){
inhabitants = Outside._HUT_ROOM;
} else if(target == full + 1){
inhabitants = population % Outside._HUT_ROOM;
}
$SM.set('game.buildings["hut"]', ($SM.get('game.buildings["hut"]') - 1));
if(inhabitants){
Outside.killVillagers(inhabitants);
dead += inhabitants;
}
}
// this method returns the total number of victims, for further actions
return dead;
},
schedulePopIncrease: function() { schedulePopIncrease: function() {
var nextIncrease = Math.floor(Math.random()*(Outside._POP_DELAY[1] - Outside._POP_DELAY[0])) + Outside._POP_DELAY[0]; var nextIncrease = Math.floor(Math.random()*(Outside._POP_DELAY[1] - Outside._POP_DELAY[0])) + Outside._POP_DELAY[0];
Engine.log('next population increase scheduled in ' + nextIncrease + ' minutes'); Engine.log('next population increase scheduled in ' + nextIncrease + ' minutes');