Created
January 26, 2016 04:15
-
-
Save slacktracer/63fff6e69c4e263ada3b to your computer and use it in GitHub Desktop.
Messing with traits.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import importedTrait from 'modules/importedTrait'; | |
const importedTrait = Trait({ | |
direction() { | |
return this.colours; | |
} | |
}); | |
const gameThingTrait = Trait({ | |
// gps: 1337, | |
render() { | |
this.fun += 1; | |
return this.fun; | |
}, | |
update() { | |
this.property += 2; | |
return this.property; | |
} | |
// , | |
// locate() { | |
// return ++this.gps; | |
// } | |
}); | |
const prototype = Object.create( | |
null, | |
Trait.compose( | |
gameThingTrait, | |
importedTrait | |
) | |
); | |
export default function Engine({ | |
colours = {}, | |
property = 30 | |
} = {}) { | |
const engine = Object.create(prototype); | |
engine.colours = colours; | |
engine.fun = 0; | |
engine.property = property; | |
return engine; | |
} | |
const o = Engine({ | |
colours: { | |
inside: '#fff', | |
outside: '#000' | |
}, | |
property: 40 | |
}); | |
// console.log(prototype); | |
console.log(o); | |
// console.log(o.direction()); | |
// console.log(o.render()); | |
// console.log(o.update()); | |
// console.log(o.locate()); | |
// console.log(o.gps); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hullTrait(x, y) { | |
const position = vec2.fromValues(x, y); | |
return Trait({ | |
alpha: undefined, | |
getPosition() { | |
return position; | |
}, | |
getLocation() { | |
return this.location; | |
}, | |
location: Trait.required | |
}); | |
} | |
function makeColorTrait(col) { | |
return Trait({ | |
alpha: Trait.required, | |
color: function() { return col; } | |
}); | |
} | |
function mainTrait(x, y, z) { | |
return Trait({ // and an anonymous point trait | |
getX: function() { return x; }, | |
getY: function() { return y; }, | |
z, | |
location: 'here', | |
set() { | |
if (this.alpha === undefined) { | |
console.log(this.alpha) | |
this.alpha = 1; | |
console.log(this.alpha) | |
} | |
this.alpha++; | |
return this.alpha; | |
}, | |
toString: function() { return ''+x+'@'+y; } | |
}); | |
} | |
function makePoint(x, y, z) { | |
return Object.create( // create an instance of a trait | |
null, // that inherits from Object.prototype | |
Trait.compose( // and is the composition of | |
makeColorTrait('red'), // a color trait | |
mainTrait(x, y, z), | |
hullTrait(10, 11) | |
) | |
); | |
} | |
const p = makePoint(0,2, 42); | |
console.log(p.color()) // 'red' | |
console.log(p.getPosition()); | |
console.log(p.getLocation()); | |
// p.location = 'there'; | |
console.log(p) | |
// p.alpha = 1; | |
console.log(p.getLocation()); | |
const b = p.getLocation; | |
console.log(p.z); | |
console.log(p.set()); | |
console.log(p.set()); | |
console.log(b()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment