/* OBJECT PROTOTYPES */
function Attributes() {
this.health = 0.0;
this.wealth = 0.0;
this.joy = 0.0;
}
function Person() {
this.name = "";
this.lifeSatisfaction = 0.0;
this.attributes = new Attributes();
// Calculate new attribute values and life satisfaction
this.Calc = function() {
this.lifeSatisfaction = (this.attributes.health + this.attributes.wealth + this.attributes.joy) / 3.0;
}
// Runs for every time-step interval
this.Step = function() {
this.Calc();
}
}
/* GLOBALS */
var PEOPLE = [];
/* OTHER FUNCTIONS */
function WriteConsole(text) {
$("#sim-console").append("
" + text + "
");
}
function ClearConsole() {
$("#sim-console").html("");
}
function Step() {
ClearConsole();
var len = PEOPLE.length;
for (var i = 0; i < len; i++) {
PEOPLE[i].Step();
WriteConsole(PEOPLE[i].name + " Life Satisfaction: " + PEOPLE[i].lifeSatisfaction.toFixed(1));
}
}
/* DOCUMENT-READY AKA INIT SCRIPT */
$(document).ready(function() {
// Set up the work area
$("#sim-box").append("");
$("#sim-box").append("");
$("#step-button").click(Step);
// Set up three test people
var p = new Person();
p.name = "Bob";
p.attributes.health = Math.random();
p.attributes.wealth = Math.random();
p.attributes.joy = Math.random();
PEOPLE.push(p);
p = new Person();
p.name = "Eddy";
p.attributes.health = Math.random();
p.attributes.wealth = Math.random();
p.attributes.joy = Math.random();
PEOPLE.push(p);
p = new Person();
p.name = "Sarah";
p.attributes.health = Math.random();
p.attributes.wealth = Math.random();
p.attributes.joy = Math.random();
PEOPLE.push(p);
// Calculate initial life satisfaction and print to console
var len = PEOPLE.length;
for (var i = 0; i < len; i++) {
PEOPLE[i].Calc();
WriteConsole(PEOPLE[i].name + " Life Satisfaction: " + PEOPLE[i].lifeSatisfaction.toFixed(1));
}
});