-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from GGAlanSmithee/dev
Version 2 !
- Loading branch information
Showing
101 changed files
with
12,637 additions
and
8,150 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [ "es2015" ] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaVersion": 6 | ||
}, | ||
"extends": "eslint:recommended", | ||
"env": { | ||
"env": | ||
{ | ||
"node": true, | ||
"mocha": true | ||
"mocha": true, | ||
"es6": true | ||
}, | ||
"rules": | ||
{ | ||
"semi": [2, "never"] | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.git | ||
.c9 | ||
*.log | ||
node_modules | ||
coverage |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
.c9 | ||
.git | ||
.gitignore | ||
.eslintrc | ||
.babelrc | ||
build | ||
coverage | ||
examples | ||
test | ||
.gitignore | ||
.eslintrc | ||
test |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# GG-Entities | ||
|
||
## Updrading from version 1 to version 2 | ||
|
||
Version 2 has seen a lot of internal and external changes, but reading through these points, upgrading should be pretty straight-forward. The code is built to fail in such a way that it should be appearant what action you as a user need to take to fix it. If you find this to not be the case, please let me know! | ||
|
||
### Registering a component | ||
|
||
* v1 | ||
```javascript | ||
const comp = entityManager.registerComponent(1) | ||
``` | ||
|
||
* v2 | ||
```javascript | ||
const comp = entityManager.registerComponent('comp', 1) | ||
``` | ||
|
||
### Registering a system | ||
|
||
* v1 | ||
```javascript | ||
const sys = entityManager.registerLogicSystem(Entities.SelectorType.GetWith, comp1 | comp2, system) | ||
``` | ||
|
||
* v2 | ||
```javascript | ||
const sys = entityManager.registerLogicSystem('system', [ comp1, comp2 ], system) | ||
``` | ||
|
||
### Accessing an entity's components from within a system | ||
|
||
* v1 | ||
```javascript | ||
function logSystem(entities) { | ||
for (var entity of entities) { | ||
console.log(this[position][entity].x, this[position][entity].y) | ||
} | ||
} | ||
``` | ||
|
||
* v2 | ||
```javascript | ||
function logSystem(entities) => { | ||
for (const {entity, i} of entities) { | ||
console.log(i, entity[pos].x, entity[pos].y) | ||
} | ||
} | ||
``` | ||
|
||
### See the examples for more information on how to use GG-Entities v2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,98 @@ | ||
# Entities | ||
easy-to-use Entity-Component System in JavaScript | ||
# GG-Entities | ||
|
||
## This library is built using [babel][0] and it uses generators. To use it you must `require` or `include` the [polyfill][1] first | ||
easy-to-use Entity-Component System for browser and node | ||
|
||
## Version 2 | ||
|
||
Version 2 is out! See the [migration guide](/MIGRATE.md) for how you can transfer from a previous version. | ||
|
||
## Usage | ||
|
||
```javascript | ||
require('babel/polyfill'); // polyfill for regenerator runtime and maps | ||
var Entities = require('gg-entities'); | ||
import { EntityManager } from 'gg-entities' | ||
|
||
var entityManager = new Entities.EntityManager(); | ||
const entityManager = new EntityManager() | ||
|
||
// Create components | ||
var position = entityManager.registerComponent({ | ||
// 1. Register Components | ||
|
||
const pos = entityManager.registerComponent('position', { | ||
x : 10.0, | ||
y : 10.0 | ||
}); | ||
}) | ||
|
||
var velocity = entityManager.registerComponent(2.0); | ||
const vel = entityManager.registerComponent('velocity', 2.0) | ||
|
||
// Add a system for the created components | ||
var movementSystem = function(entities, delta) { | ||
for (var entity of entities) { | ||
var pos = this[position][entity]; | ||
var vel = this[velocity][entity]; | ||
// 2. Register Systems | ||
|
||
pos.x += vel * delta; | ||
function movementSystem(entities, { delta }) => { | ||
for (const {entity} of entities) { | ||
entity[pos].x += entity[vel] * delta | ||
} | ||
}; | ||
} | ||
|
||
entityManager.registerLogicSystem(Entities.SelectorType.GetWith, position | velocity, movementSystem); | ||
entityManager.registerRenderSystem('movement', [ pos, vel ], movementSystem) | ||
|
||
// Run the systems | ||
... | ||
function logSystem(entities) => { | ||
for (const {entity} of entities) { | ||
console.log(entity[pos].x, entity[pos].y) | ||
} | ||
} | ||
|
||
var delta = 0.16; | ||
entityManager.registerLogicSystem('log', [ pos ], logSystem) | ||
|
||
entityManager.onLogic(delta); | ||
entityManager.onRender(delta); | ||
// 3. Run the systems | ||
|
||
... | ||
entityManager.onLogic({ delta: 16 }) // invokes all logic systems (movementSystem) | ||
entityManager.onRender({ delta: 16 }) // invokes all render systems (logSystem) | ||
``` | ||
|
||
*See /examples for more usages* | ||
|
||
## Contributing / Building | ||
## Features | ||
|
||
* Easy to use | ||
* Configurable | ||
* 100% code coverage | ||
* Fast [TODO: add benchmarks and comparisons to back this claim] | ||
|
||
- **download [nodejs][2]** and install | ||
## Docs | ||
|
||
- **fork the repository** and clone it on your local computer | ||
- We are currently working on documenting the entire public API - comming soon! | ||
|
||
- **install dependencies** by running `npm i` in the repository folder | ||
## FAQ / Gotchas | ||
|
||
- **make changes** | ||
* Since a system is bound with the `EntityManager` as its context, a system must be a regular function (not a es6 arrow function) | ||
* | ||
## Tips and tricks | ||
|
||
- **run the build script** by typing `npm run build` in the console, eventually genenrating the file `dist/entities.js` | ||
* Using [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) and [computed properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) | ||
|
||
- **add tests** for the added functionality in `/test` and run `npm run test` | ||
Accessing an entity's components in a system usually looks like this | ||
|
||
```javascript | ||
function movementSystem(entities) { | ||
for (const { entity } of entities) { | ||
entity[POS_COMPONENT].x += entity[VEL_COMPONENT].x * delta | ||
entity[POS_COMPONENT].y += entity[VEL_COMPONENT].y * delta | ||
} | ||
} | ||
``` | ||
|
||
which can be a bit ugly, especially if your entity has a lot of components which are accessed multiple times. Using some ES6 (computed property keys) and ES7 (object spread) magic, we can make it a bit more concise: | ||
|
||
```javascript | ||
function movementSystem(entities) { | ||
for (const { entity: { [POS_COMPONENT]: pos, [VEL_COMPONENT]: vel } } of entities) { | ||
pos.x += vel.x * delta | ||
pos.y += vel.y * delta | ||
} | ||
} | ||
``` | ||
|
||
- **send a pull request** to the dev branch with an explanation of what it is that you have been done | ||
## Get involved | ||
|
||
- **thanks!!** for contributing | ||
- Got questions or want to leave feedback? [create an issue](https://github.com/GGAlanSmithee/Entities/issues/new) | ||
|
||
[0]: https://github.com/babel/babel | ||
[1]: https://babeljs.io/docs/usage/polyfill/ | ||
[2]: http://nodejs.org | ||
- Got improvements? Feel free to send a PR to the `dev` branch (don't forget to add tests) | ||
- `npm run build` *builds the project* | ||
- `npm run test` *runs the test suite* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const rollup = require('rollup') | ||
const babel = require('rollup-plugin-babel') | ||
|
||
rollup.rollup({ | ||
entry: 'src/index.js', | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
presets: [ 'es2015-rollup' ], | ||
plugins: [ 'transform-object-rest-spread' ], | ||
exclude: 'node_modules/**' | ||
}) | ||
] | ||
}).then(bundle => bundle.write({ | ||
dest: 'dist/gg-entities.js', | ||
sourceMap: 'inline', | ||
format: 'umd', | ||
moduleId: 'GGEntities', | ||
moduleName: 'GGEntities' | ||
})).then((c) => { | ||
const entitiesPath = path.join(__dirname, '../dist/gg-entities.js') | ||
|
||
const polyfill = fs.readFileSync(path.join(__dirname, '../node_modules/babel-polyfill/dist/polyfill.js'), 'utf8') | ||
const entities = fs.readFileSync(entitiesPath, 'utf8') | ||
|
||
fs.writeFile(entitiesPath, `${polyfill}\n\n${entities}`, error => { | ||
if (error) { | ||
console.error(error) | ||
} | ||
}) | ||
}).catch(error => { | ||
console.error(error) | ||
}) |
Oops, something went wrong.