Created
October 4, 2017 18:49
-
-
Save mflux/01c76b71d990c6b044aaee15ed69184b to your computer and use it in GitHub Desktop.
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 R from 'ramda'; | |
function newId(){ | |
let d = new Date().getTime(); | |
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
const r = (d + Math.random()*16)%16 | 0; | |
d = Math.floor(d/16); | |
return (c=='x' ? r : (r&0x3|0x8)).toString(16); | |
}); | |
return uuid; | |
} | |
export const newWorld = () => ({}); | |
export const newEntity = () => ({}); | |
export const addEntity = R.curry( function( world, entity ){ | |
const id = newId(); | |
world[ id ] = entity; | |
entity._id = id; | |
return entity; | |
}); | |
export const removeEntity = R.curry( function( world, entity ){ | |
delete world[ entity._id ]; | |
return entity; | |
}); | |
export const getEntityById = R.curry( function( world, id ){ | |
return world[ id ]; | |
}); | |
export const lookupEntityId = R.curry( function( world, entity ){ | |
for( let id in world ){ | |
if( world[ id ] === entity ){ | |
return id; | |
} | |
} | |
}); | |
export const createComponent = function( componentType, componentData ){ | |
const component = new componentType( componentData ); | |
return component; | |
}; | |
export const addComponent = R.curry( function( entity, component ){ | |
entity[ component.name ] = component; | |
return component; | |
}); | |
export const removeComponent = R.curry( function( entity, component ){ | |
delete entity[ component.name ]; | |
return component; | |
}); | |
export const getEntities = R.curry(function( world, ...components ){ | |
return R.filter( hasKeys( components ), R.values( world ) ); | |
}); | |
export const getEntityList = R.curry( function( world ){ | |
return R.values( world ); | |
}); | |
const hasKeys = R.curry( function( keys, object ){ | |
const objectKeys = R.keys( object ); | |
return R.all( function( keyName ){ | |
if( keyName.charAt( 0 ) === '!' ){ | |
return ( object[ keyName.slice(1,keyName.length) ] === undefined ); | |
} | |
else{ | |
return ( object[ keyName ] !== undefined ); | |
} | |
}, keys ); | |
}); | |
export const defineComponent = function( componentTemplate ){ | |
const componentType = function( data ){ | |
const initializer = componentTemplate.init.bind( this ); | |
initializer( data ); | |
}; | |
componentType.prototype.name = componentTemplate.name; | |
return componentType; | |
}; | |
export const world = function( entities ){ | |
return { | |
addEntity: addEntity( entities ), | |
removeEntity: removeEntity( entities ), | |
getEntities: ( ...args ) => getEntities( entities, ...args ), | |
get: () => entities, | |
clone: () => R.clone( entities ), | |
getEntityById: getEntityById( entities ), | |
lookupEntityId: lookupEntityId( entities ), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment