JS Function
by Sam Fereday
CSS
body {
background: #222;
}
JavaScript
/*
The challenge: Dwarven empire simulator - In text first.
*/
const addMethods = (funcs, ctx) => () => {
for (let i = 0; i < funcs.length; i++) {
funcs[i].call(ctx);
}
}
const update = (fn, ctx) => {
fn.call(ctx);
window.requestAnimationFrame(() => {
//update(fn);
});
}
const dist = (a, b) =>
Math.sqrt(a.x * b.x + a.y * b.y);
/*
Object stuff.
*/
const Entity = ({
name,
stats,
loc,
mode
}) => {
console.log("I am " + name + ".");
console.log("My stats are:");
console.log(stats);
console.log("I don't feel like doing anything.");
return {
name,
mode,
loc
}
}
const Tile = ({
name,
amount,
loc
}) => {
return {
name,
amount,
loc
}
}
/*
Set things up
*/
const locationTiles = [
Tile({
name: 'Gold',
amount: 1000,
loc: {
x: 1,
y: 1
}
})
]
const dwarves = [
Entity({
name: 'Doug',
loc: {
x: 10,
y: 10
},
mode: 0,
stats: {
health: {
max: 10,
current: 10
},
gold: {
max: 120,
current: 0
}
}
})
];
/*
App functions.
*/
const fn1 = (dwarves, locations) => () => {
console.log(dwarves);
console.log(locations);
dwarves.forEach(({
loc
}) => {
const t = locations.find(x => x.name === 'Gold');
const distance = dist({
x: loc.x,
y: loc.y
}, {
x: t.loc.x,
y: t.loc.y
});
if (distance > 1) {
}
});
}
/*
Create context and call update.
*/
const context = addMethods([
fn1(dwarves, locationTiles)
], this);
update(context, this);