Last active
April 8, 2020 15:37
-
-
Save pcornier/562efab321ac69cc4a87c6f4517b770c to your computer and use it in GitHub Desktop.
very basic Node tree system
This file contains 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
const Node = function(...nodes) { | |
let children = nodes | |
let node = { | |
addChild: n => children.push(n), | |
register: (name, callback) => { | |
this[name] = callback | |
return node | |
}, | |
call: (name, ...args) => { | |
if (this[name]) this[name](args) | |
children.forEach(c => c.call(name, args)) | |
}, | |
} | |
return node | |
} | |
let root = new Node( | |
new Node( | |
new Node().register('update', () => console.log('with a shield')), | |
new Node().register('update', () => console.log('and a helmet')) | |
).register('update', () => console.log('I have an armor')), | |
new Node().register('update', () => console.log('And a sword !!')) | |
) | |
.call('update') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LUA version