Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pagoru committed Apr 8, 2021
1 parent ef2803f commit d277cac
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 288 deletions.
146 changes: 1 addition & 145 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,149 +22,5 @@ enum ComponentEnum {
MOB
}
```
Call the game function, and extract what you need.
- `entities`, can access the... entities.
- `onLoop`, is a function to be call from your [LoopWorker](https://github.com/voidpixel/LoopWorker/packages/399863) or DOM loop.
- `setSystems`, sets the systems of the game.
```Typescript
const {
entities,
onLoop,
setSystems
} = game<SystemEnum, ComponentEnum>();
```
Import the type, if you are cool, and create a function and return:
- `id`, entity unique identificator.
- `components`, array with every `component` you want to assign to the entity.
```Typescript
import {EntityFunction} from "darker-engine";

const spiderEntity: EntityFunction<ComponentEnum> = (
name: string
) => ({
id: name,
components: [
ComponentEnum.SPIDY,
ComponentEnum.MOB
]
});
```
Create your first system, you need to return some mandatory params:
- `id`, unique for every system, from the enum we created.
- `components`, a list of every component the entity need to activate
- `onAdd`, function that returns an entity addition :)
- `onRemove`, function that returns an entity removal :(
```Typescript
import {SystemFunction} from "darker-engine";

const spiderSystem: SystemFunction<SystemEnum, ComponentEnum> = () => {
const onAdd = (id: string) => {
console.log('Spidermaaaaannn!!', id);

const entity = entities.get(id);
// We can modify the component data...
entity.updateComponent(ComponentEnum.SPIDY, {
deathMessage: 'Mr. Stark? I don't feel so good... I don't wanna go...'
});

setTimeout(() => {
// And also delete one component
entity.removeComponent(ComponentEnum.SPIDY);
}, 3000);
}
const onRemove = (id: string) => {
console.log(entities.get(id).getComponent(ComponentEnum.WEB).deathMessage, id);
}
return {
id: SystemEnum.SPIDER_SYSTEM,
components: [
ComponentEnum.SPIDY,
ComponentEnum.MOB
],
onAdd,
onRemove
}
}
```
Add the systems and the entities to the game.
```Typescript
setSystems( spiderSystem() );

entities.add( spiderEntity('Tom_Holland') );
```
And don't forget to add your loop!!
```Typescript
import {LoopWorker} from "@voidpixel/loop-worker";

const loopWorker = new LoopWorker({ ticksPerSecond: 20 });
loopWorker.on('tick', (data) => onLoop(data.ms));
```


## Complete example code
```Typescript
import {game, EntityFunction, SystemFunction} from "darker-engine";
import {LoopWorker} from "@voidpixel/loop-worker";

enum SystemEnum {
SPIDER_SYSTEM
}
enum ComponentEnum {
SPIDY,
MOB
}

const {
entities,
onLoop,
setSystems
} = game<SystemEnum, ComponentEnum>();

const spiderEntity: EntityFunction<ComponentEnum> = (
name: string
) => ({
id: name,
data: {},
components: [
ComponentEnum.SPIDY,
ComponentEnum.MOB
]
});

const spiderSystem: SystemFunction<SystemEnum, ComponentEnum> = () => {
const onAdd = (id: string) => {
console.log('Spidermaaaaannn!!', id);

const entity = entities.get(id);
// We can modify the component data...
entity.updateComponent(ComponentEnum.SPIDY, {
deathMessage: 'Mr. Stark? I don\'t feel so good... I don\'t wanna go...'
});

setTimeout(() => {
// And also delete one component
entity.removeComponent(ComponentEnum.SPIDY);
}, 3000);
}
const onRemove = (id: string) => {
// And here the fun beguns
console.log(entities.get(id).getComponent(ComponentEnum.SPIDY).deathMessage, id);
}
return {
id: SystemEnum.SPIDER_SYSTEM,
components: [
ComponentEnum.SPIDY,
ComponentEnum.MOB
],
onAdd,
onRemove
}
}

setSystems( spiderSystem() );

entities.add( spiderEntity('Tom_Holland') );

const loopWorker = new LoopWorker({ ticksPerSecond: 20 });
loopWorker.on('tick', (data) => onLoop(data.ms));
```
TODO...
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "darker-engine",
"description": "ecs lightweight library",
"version": "1.1.3",
"version": "2.0.0",
"author": "darkaqua",
"license": "MIT",
"main": "./build/index.js",
Expand Down
58 changes: 0 additions & 58 deletions src/entities.ts

This file was deleted.

Loading

0 comments on commit d277cac

Please sign in to comment.