Last active
July 20, 2016 15:34
-
-
Save erikhazzard/43761063ebcae59285ad to your computer and use it in GitHub Desktop.
Entity Component System Tutorial
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
/* ========================================================================= | |
* | |
* Components.js | |
* This contains all components for the tutorial (ideally, components would | |
* each live in their own module) | |
* | |
* Components are just data. | |
* | |
* ========================================================================= */ | |
// Appearance | |
// -------------------------------------- | |
ECS.Components.Appearance = function componentAppearance ( params ){ | |
// Appearance specifies data for color and size | |
params = params || {}; | |
this.colors = params.colors; | |
if(!this.colors){ | |
// generate random color if not passed in (get 6 random hex values) | |
this.colors = { | |
r: 0, | |
g: 100, | |
b: 150 | |
}; | |
} | |
this.size = params.size || (1 + (Math.random() * 30 | 0)); | |
return this; | |
}; | |
ECS.Components.Appearance.prototype.name = 'appearance'; | |
// Health | |
// -------------------------------------- | |
ECS.Components.Health = function componentHealth ( value ){ | |
value = value || 20; | |
this.value = value; | |
return this; | |
}; | |
ECS.Components.Health.prototype.name = 'health'; | |
// Position | |
// -------------------------------------- | |
ECS.Components.Position = function componentPosition ( params ){ | |
params = params || {}; | |
// Generate random values if not passed in | |
// NOTE: For the tutorial we're coupling the random values to the canvas' | |
// width / height, but ideally this would be decoupled (the component should | |
// not need to know the canvas's dimensions) | |
this.x = params.x || 20 + (Math.random() * (ECS.$canvas.width - 20) | 0); | |
this.y = params.y || 20 + (Math.random() * (ECS.$canvas.height - 20) | 0); | |
return this; | |
}; | |
ECS.Components.Position.prototype.name = 'position'; | |
// playerControlled | |
// -------------------------------------- | |
ECS.Components.PlayerControlled = function componentPlayerControlled ( params ){ | |
this.pc = true; | |
return this; | |
}; | |
ECS.Components.PlayerControlled.prototype.name = 'playerControlled'; | |
// Collision | |
// -------------------------------------- | |
ECS.Components.Collision = function componentCollision ( params ){ | |
this.collides = true; | |
return this; | |
}; | |
ECS.Components.Collision.prototype.name = 'collision'; |
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
/* ========================================================================= | |
* | |
* Entity.js | |
* Definition of our "Entity". Abstractly, an entity is basically an ID. | |
* Here we implement an entity as a container of data (container of components) | |
* | |
* ========================================================================= */ | |
ECS.Entity = function Entity(){ | |
// Generate a pseudo random ID | |
this.id = (+new Date()).toString(16) + | |
(Math.random() * 100000000 | 0).toString(16) + | |
(Math.random() * 100000000 | 0) + | |
ECS.Entity.prototype._count; | |
// increment counter | |
ECS.Entity.prototype._count++; | |
// The component data will live in this object | |
this.components = {}; | |
return this; | |
}; | |
// keep track of entities created | |
ECS.Entity.prototype._count = 0; | |
ECS.Entity.prototype.addComponent = function addComponent ( component ){ | |
// Add component data to the entity | |
this.components[component.name] = component; | |
return this; | |
}; | |
ECS.Entity.prototype.removeComponent = function removeComponent ( component ){ | |
// Remove component data by removing the reference to it | |
delete this.components[component.name]; | |
return this; | |
}; | |
ECS.Entity.prototype.print = function print () { | |
// Function to print / log information about the entity | |
console.log(JSON.stringify(this, null, 4)); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment