Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Created June 4, 2011 08:03
Show Gist options
  • Save iaincarsberg/1007712 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1007712 to your computer and use it in GitHub Desktop.
Example of thornys entity system interaction
require('thorny/base')('./config/default.json')(function ($) {
// Grab a copy of the entity system object
var entitySystem = $('thorny entity-system');
// Make the actor template
entitySystem.makeEntity($.defined('template'))
.addTag('actor')
.addComponent('position')
.addComponent('keys')
.addComponent('render-2d');
// Make the zombie template
entitySystem.makeEntity($.defined('template'))
.useTag('actor')
.addTag('zombie')
.addComponent('target');
// Load the map
entitySystem.makeEntity()
.addComponent('level', 'some_map.json');
.addTag('level');
// Create an event trigger, that spawns zombies
entitySystem.makeEntity()
.useTag('event-trigger', $.defined('concrete'))
.updateComponent('bounds', {
x1: 0,
y1: 0,
x2: 10,
y2: 10
})
.updateComponent('triggered', function (entity) {
var
location = entity.getComponent('position'),
i;
// Spawn 5 zombies
for (i=0; i < 5; i++) {
entitySystem.makeEntity()
.useTag('zombie', $.defined('concrete'))
.updateComponent('target', entitySystem.getTag('player'))
.updateComponent('position', {x: location.x + ((i * 10) - 25), y: location.y + 50})
.spawn(entitySystem.getTag('level'));
};
})
.spawn(entitySystem.getTag('level'));
// Spawn the player.
entitySystem.makeEntity()
.useTag('actor', $.defined('concrete'))
.addTag('player')
.updateComponent('position', {x: 50, y: 30})
.updateComponent('keys', {
'up': 'w',
'down': 's',
'left': 'a',
'right': 'd',
})
.spawn(entitySystem.getTag('level'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment