-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
223 lines (180 loc) · 4.85 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var days = 0;
var seconds = 0.001; // Real seconds per game day
var interval = 1000 * seconds;
var dateInGame = new Date();
var beginDate = new Date();
var age = 0;
var displayAge;
var displayDateInGame;
var displayBeginDate = convertDate(beginDate);
var displayDateOfBirth;
var human = new Human(beginDate.getTime());
var deadHuman = new Human(beginDate.getTime());
var genericHumanStatus = new Task("Natural Status", strperday=0.001, intperday=0.001);
displayDateOfBirth = convertDate(human.dateOfBirth);
var lifeStages = {
NEW_BORN : "New Born",
BABY : "Baby",
CHILD : "Child",
TEENAGER : "Teenager",
ADULT : "Adult",
ELDERLY : "Elderly",
DEAD : "Dead"
}
$(document).ready(function(){
$('#reborn').hide();
$('#deadHumanInformation').hide();
$('#reborn').click(function(){
$('#reborn').hide();
createNewHuman();
$('#deadHumanInformation').show();
});
});
window.setInterval(function(){
update();
draw();
}, interval);
function update(){
incrementDays(1);
updateAge();
updateStats();
checkDeath();
checkLifeStatus();
}
function draw(){
$('#firstDay').html(displayBeginDate);
$('#day').html(displayDateInGame);
$('#age').html(displayAge);
$('#status').html(human.lifeStatus);
$('#generation').html(human.generation);
$('#dateOfBirth').html(displayDateOfBirth);
$('#intelligence').html(human.intelligence.toFixed(2));
$('#strength').html(human.strength.toFixed(2));
}
function checkLifeStatus(){
if(human.lifeStatus == lifeStages.DEAD){
updateDeadHuman();
$('#reborn').show();
}else{
if(age == 0){
human.lifeStatus = lifeStages.NEW_BORN;
}else if(age >= 1){
if(age < 3){
human.lifeStatus = lifeStages.BABY;
}else if(age < 13){
human.lifeStatus = lifeStages.CHILD;
}else if(age < 20){
human.lifeStatus = lifeStages.TEENAGER;
}else if(age < 60){
human.lifeStatus = lifeStages.ADULT;
}else{
human.lifeStatus = lifeStages.ELDERLY;
}
}
}
}
function updateDeadHuman(){
deadHuman = new Human(human.dateOfBirth);
deadHuman.dateOfDeath = dateInGame;
deadHuman.lifeStatus = human.lifeStatus;
deadHuman.intelligence = human.intelligence;
deadHuman.strength = human.strength;
deadHuman.generation = human.generation;
deadHuman.deathChance = human.deathChance;
}
function createNewHuman(){
human = new Human(dateInGame.getTime(), deadHuman.generation + 1);
genericHumanStatus = new Task(genericHumanStatus.description, intperday=(deadHuman.intelligence / 1000), strperday=(deadHuman.strength / 1000));
displayDateOfBirth = convertDate(human.dateOfBirth);
}
function checkDeath(){
var randomNumber = Math.random() * 100;
human.deathChance = 0.00001 * (age * 7);
if(randomNumber < human.deathChance){
human.lifeStatus = lifeStages.DEAD;
human.dateOfDeath = dateInGame;
}
}
function updateStats(){
if(human.lifeStatus != lifeStages.DEAD){
updateIntelligence();
updateStrength();
}
}
function updateIntelligence(){
human.intelligence += genericHumanStatus.intperday;
}
function updateStrength(){
human.strength += genericHumanStatus.strperday;
}
function incrementDays(num){
days += num;
dateInGame = beginDate.addDays(days);
displayDateInGame = convertDate(dateInGame);
}
function _calculateAge(endDate) {
var ageDifMs = endDate - human.dateOfBirth;
var ageDate = new Date(ageDifMs);
age = Math.abs(ageDate.getUTCFullYear() - 1970);
}
function updateAge(){
if(human.lifeStatus == lifeStages.DEAD){
_calculateAge(human.dateOfDeath);
displayAge = getAge(human.dateOfBirth, human.dateOfDeath);
}else{
_calculateAge(dateInGame);
displayAge = getAge(human.dateOfBirth, dateInGame);
}
}
function convertDate(inputFormat) {
function pad(s) { return (s < 10) ? '0' + s : s; } // Add a 0 to the left when number is less than 10
var d = new Date(inputFormat);
return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
}
function getAge(firstDate,finalDate){
var startDate = new Date(firstDate);
var endDate = new Date(finalDate);
var year = endDate.getFullYear() - startDate.getFullYear();
if((endDate.getMonth() + 1) >= (startDate.getMonth() + 1)){
var month = (endDate.getMonth() + 1)-(startDate.getMonth() + 1);
}else{
var month = 12 + (endDate.getMonth() + 1)-(startDate.getMonth() + 1);
year--;
}
if(endDate.getDate() >= startDate.getDate()){
var day = endDate.getDate()-startDate.getDate();
}else{
var day = 31 + endDate.getDate()-startDate.getDate();
month--;
if(month < 0){
month = 11;
year--;
}
}
var fmtYear;
var fmtMonth;
var fmtDay;
if(year == 1){
fmtYear = "Year";
}else{
fmtYear = "Years";
}
if(month == 1){
fmtMonth = "Month";
}else{
fmtMonth = "Months";
}
if(day == 1){
fmtDay = "Day";
}else{
fmtDay = "Days";
}
var formatedAge = day + " " + fmtDay;
if (month > 0){
formatedAge = month + " " + fmtMonth + " " + formatedAge;
}
if (year > 0){
formatedAge = year + " " + fmtYear + " " + formatedAge;
}
return formatedAge;
}