]> git.r.bdr.sh - rbdr/serpentity/blob - README.md
6f4ddce5fecd2c358f72f001f4657e3770631f4f
[rbdr/serpentity] / README.md
1 # Serpentity
2
3 Serpentity is a simple entity framework inspired by [Ash][ash].
4
5 Usage:
6
7 import Serpentity from '@serpentity/serpentity';
8
9 ## Instantiating an engine
10
11 const engine = new Serpentity();
12
13 Add entities or systems, systems are added with a priority (the smaller
14 the number, the earlier it will be called):
15
16 engine.addEntity(entityFactory());
17 engine.addSystem(new GameSystem(), priority);
18
19 Update all systems:
20
21 engine.update(dt);
22
23 Remove entities or systems:
24
25 engine.removeEntity(entityReference);
26 engine.removeSystem(systemReference);
27
28 ## Creating Entities
29
30 Entities are the basic object of Serpentity, and they do nothing.
31
32 import { Entity } from '@serpentity/serpentity';
33 const entity = new Entity();
34
35 All the behavior is added through components
36
37 ## Creating Components
38
39 Components define data that we can add to an entity. This data will
40 eventually be consumed by "Systems"
41
42 import { Component } from '@serpentity/serpentity';
43 const PositionComponent = class PositionComponent extends Component {
44 constructor(config) {
45
46 this.x = 0;
47 this.y = 0;
48
49 super(config);
50 }
51 };
52
53 You can add components to entities by using the add method:
54
55 entity.addComponent(new PositionComponent());
56
57
58 Systems can refer to entities by requesting nodes.
59
60 ## Working with Nodes
61
62 Nodes are sets of components that you define, so your system can require
63 entities that always follow the API defined in the node.
64
65 import { Node } from '@serpentity/serpentity';
66 const MovementNode = class MovementNode extends Node;
67 MovementNode.position = PositionComponent;
68 MovementNode.motion = MotionComponent;
69
70 You can then request an array of all the nodes representing entities
71 that comply with that API
72
73 engine.getNodes(MovementNode);
74
75 ## Creating Systems
76
77 Systems are called on every update, and they use components through nodes.
78
79 import { System } from '@serpentity/serpentity';
80 const TestSystem = class TestSystem extends System {
81 added(engine){
82
83 this.nodeList = engine.getNodes(MovementNode);
84 }
85
86 removed(engine){
87
88 this.nodeList = undefined;
89 }
90
91 update(dt){
92
93 for (const node of this.nodeList) {
94 console.log(`Current position is: ${node.position.x},${node.position.y}`);
95 }
96 }
97 };
98
99 ## That's it
100
101 Just run `engine.update(dt)` in your game loop :D
102
103 [ash]: http://www.ashframework.org/