/
gameLogic.js
80 lines (69 loc) · 2.17 KB
/
gameLogic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var GameLogic = {
createImmigrant: function(target){
var immigrant = new Immigrant([0, 0], target);
Game.addPerson(immigrant);
target.building.addPerson(immigrant);
},
addPerson: function(person){
// people added this way don't count toward population
Game.addPerson(person);
},
immigrantArrived: function(person){
GameLogic.population++;
},
update: function(dt){
GameLogic.updateFuncs[GameLogic.updateNo++](dt);
if (GameLogic.updateNo == GameLogic.updateFuncs.length){
GameLogic.updateNo = 0;
}
},
addJob: function(num){
if (num === undefined){
num = 1;
}
GameLogic.employed += num;
},
removeJob: function(num){
if (num === undefined){
num = 1;
}
GameLogic.employed -= num;
},
unemployment: function(){
return GameLogic.population - GameLogic.employed;
},
// split up updateFuncs so that certain things aren't being called every frame
updateFuncs: [
function(dt){
// count available housing
var available = false;
for (var i = 0; i < Game.buildings.length; i++){
// TODO: separate housing from other buildings
// if the building has capacity and a certain probability is hit, then create an immigrant
// for this building
if (Game.buildings[i].getCapacity() > 0 && Math.random() < GameLogic.immigrantProbability){
available = Game.buildings[i].location;
available = Game.tiles[available[1]][available[0]];
}
}
if (available !== false){
GameLogic.createImmigrant(available);
}
},
function(dt) {
// compute info variables
$("#info-population").html(Math.round(GameLogic.population * GameLogic.employablePct));
if (GameLogic.population > 0){
$("#info-unemployment").html(Math.round(100 - GameLogic.employed / GameLogic.population * 100) + "%");
}else{
$("#info-unemployment").html("N/A");
}
},
function(dt) {}
]
};
GameLogic.population = 0;
GameLogic.employed = 0;
GameLogic.updateNo = 0;
GameLogic.employablePct = 1; // TODO: change job-seeker behaviour to take this into account
GameLogic.immigrantProbability = 0.01;