Skip to content

Commit

Permalink
Merge pull request #58 from GGAlanSmithee/dev
Browse files Browse the repository at this point in the history
Version 2 !
  • Loading branch information
GGAlanSmithee authored Apr 4, 2017
2 parents a45fae0 + e978810 commit 7bb8cd1
Show file tree
Hide file tree
Showing 101 changed files with 12,637 additions and 8,150 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015" ]
}
13 changes: 11 additions & 2 deletions .eslintrc
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"]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.git
.c9
*.log
node_modules
coverage
7 changes: 4 additions & 3 deletions .npmignore
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
98 changes: 97 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,104 @@
## v2.0.0

Updated README.md
Added MIGRATE.md
Added start of docs
Updated package.json

## v2.0.0-beta9 (2017-03-09)

Updated dependencies
Changed bundling to have `gg-entities.js` be es5 compatible
Added es6 module package `gg-entities.module.js`
Changed so that `jsnext:main` and `module` entries point to `gg-entities.module.js`
Updated README.md

## v2.0.0-beta8 (2016-08-29)

Reverts changes back to v2.0.0-beta4

## v2.0.0-beta7 (2016-08-22)

Removed support for unregistering components and initializers
Removed `EntityManager.unregisterComponent`
Removed `ComponentManager.unregisterComponent`
Removed `EntityFactory.unregisterInitializer`

Removed all usage of named components
Removed `name` parameter from `EntityManager.registerComponent`

## v2.0.0-beta4 / v2.0.0-beta5 / v2.0.0-beta6 (2016-08-03)

Added support for unregistering a component as well as a component initializer
Added `EntityManager.unregisterComponent`
Added `ComponentManager.unregisterComponent`
Added `EntityFactory.unregisterInitializer`

## v2.0.0-beta2 / v2.0.0-beta3 (2016-08-01)

Added support for getting / adding / removing a component by name (in addition to by id)
Added `name` parameter to `EntityManager.registerComponent`
`component` parameter of `EntityManager.addComponent` can now be either a string (name of component) or an id
`component` parameter of `EntityManager.removeComponent` can now be either a string (name of component) or an id
`components` parameter of `EntityManager.registerSystem` can now be either an array of strings (names of components) or an bitmask
`components` parameter of `EntityManager.registerInitSystem` can now be either an array of strings (names of components) or an bitmask
`components` parameter of `EntityManager.registerLogicSystem` can now be either an array of strings (names of components) or an bitmask
`components` parameter of `EntityManager.registerRenderSystem` can now be either an array of strings (names of components) or an bitmask
`component` parameter of `EntityManager.registerInitializer` can now be either a string (name of component) or an id
`component` parameter of `EntityManager.withComponent` can now be either a string (name of component) or an id

## v2.0.0-beta1 (2016-07-23)

Removed all default exports
Changed default dist file to be in es6
Moved the es5 compatible build to `dist/gg-entities.es5.js`
The es5 build now includes `babel-polyfill`
Moved promise related functions out of the `EventHandler` class (still in file as free functions)
Updated all npm dependencies to latest

## v2.0.0-alpha3 (2016-04-30)

Added `EntityManager.registerConfiguration(key)`
Removed `EntityManager.createConfiguration()`

Registered configurations are now stored internally in the `EntityManager`
and can be used to create an entity by passing `key` to `EntityManager.create(key)`

## v2.0.0-alpha2.1 (2016-04-24)

Fixes `jsnext:main` to point to the correct location

## v2.0.0-alpha2 (2016-04-24)

Removed `delta` argument from `on(Init|Logic|Render)` in favor of passing delta as a part of `opts`
Removed `SelectorType`. The idea is that the user either specifies a list of components to select entities by them, or emits components to select all entities.
`EntityManager.Entities` is now a list of actual objects with a `components` property that is a list of the components that currently defines the entity.
The `components` of an `entity` is now a first class property of the acutal entity, instead of an array on the entity manager.

Exchanged `componentId` (int) argument with `key` (string) argument of `ComponentManager.newComponent`.
Added `key` (string) argument to `ComponentManager.registerComponent`.
`ComponentManager.registerComponent` now returns the `key` argument.

Exchanged `type` (`SelectorType`) argument with `key` (string) argument to `SystemManager.registerSystem`.
`components` argument of `SystemManager.registerSystem` is now an `Array<string>`, used to be an `int`.

Exchanged `componentId` (int) argument with `key` (string) argument of `EntityFactory.registerInitializer`.
Exchanged `componentId` (int) argument with `key` (string) argument of `EntityFactory.withComponent`.

`components` argument of `EntityManager.newEntity` is now an `Array<string>`, used to be an `int`.
`components` argument of `EntityManager.*getEntities` is now an `Array<string>`, used to be an `int`.
Removed `selector` argument from `EntityManager.*getEntities`.
Updated all proxy methods of `EntityManager` to match updates in respective manager class.

## v2.0.0-alpha1 (2016-04-09)

Added possibility to register init systems as well as added `onInit` method to the `EntityManager`

## v1.1.0 (2016-04-08)

### Feature

Added the ability to pass in an aditional parameter `opts` to the `onRender` and `onLogic` methods of the `entityManager`. This parameter will be passed as the last argument to the registered logic and render systems that is invoked.
Added the ability to pass in an aditional parameter `opts` to the `onRender` and `onLogic` methods of the `EntityManager`. This parameter will be passed as the last argument to the registered logic and render systems that is invoked.

### Example

Expand Down
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 GGAlanSmithee
Copyright (c) 2016 GGAlanSmithee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

51 changes: 51 additions & 0 deletions MIGRATE.md
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
104 changes: 68 additions & 36 deletions README.md
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*
35 changes: 35 additions & 0 deletions build/bundle.js
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)
})
Loading

0 comments on commit 7bb8cd1

Please sign in to comment.