]>
Commit | Line | Data |
---|---|---|
1 | # Serpentity | |
2 | ||
3 | Serpentity is a simple entity framework inspired by Ash. | |
4 | ||
5 | Usage: | |
6 | ||
7 | let Serpentity = require('serpentity'); | |
8 | ||
9 | or: | |
10 | ||
11 | <script src="/node_modules/serpentity/dist/serpentity.js"></script> | |
12 | ||
13 | ## Instantiating an engine | |
14 | ||
15 | let engine = Serpentity(); | |
16 | ||
17 | Add entities or systems, systems are added with a priority (the smaller | |
18 | the number, the earlier it will be called): | |
19 | ||
20 | engine.addEntity(entityFactory()); | |
21 | engine.addSystem(new GameSystem(), priority); | |
22 | ||
23 | Update all systems: | |
24 | ||
25 | engine.update(dt); | |
26 | ||
27 | Remove entities or systems: | |
28 | ||
29 | engine.removeEntity(entityReference); | |
30 | engine.removeSystem(systemReference); | |
31 | ||
32 | ## Creating Entities | |
33 | ||
34 | Entities are the basic object of Serpentity, and they do nothing. | |
35 | ||
36 | let entity = new Serpentity.Entity(); | |
37 | ||
38 | All the behavior is added through components | |
39 | ||
40 | ## Creating Components | |
41 | ||
42 | Components define data that we can add to an entity. This data will | |
43 | eventually be consumed by "Systems" | |
44 | ||
45 | let PositionComponent = class PositionComponent extends Serpentity.Component { | |
46 | constructor (config) { | |
47 | super(config); | |
48 | ||
49 | this.x = this.x || 0; | |
50 | this.y = this.y || 0; | |
51 | } | |
52 | }; | |
53 | ||
54 | You can add components to entities by using the add method: | |
55 | ||
56 | entity.addComponent(new PositionComponent()); | |
57 | ||
58 | ||
59 | Systems can refer to entities by requesting nodes. | |
60 | ||
61 | ## Working with Nodes | |
62 | ||
63 | Nodes are sets of components that you define, so your system can require | |
64 | entities that always follow the API defined in the node. | |
65 | ||
66 | let MovementNode = class MovementNode extends Serpentity.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 | let TestSystem = class TestSystem extends Serpentity.System { | |
80 | added (engine){ | |
81 | this.nodeList = engine.getNodes(MovementNode); | |
82 | }, | |
83 | removed (engine){ | |
84 | this.nodeList = undefined; | |
85 | } | |
86 | update (dt){ | |
87 | let node; | |
88 | for (node of this.nodeList) { | |
89 | console.log(`Current position is: ${node.position.x},${node.position.y}`); | |
90 | } | |
91 | } | |
92 | }; | |
93 | ||
94 | ## That's it | |
95 | ||
96 | Just run `engine.update(dt)` in your game loop :D | |
97 | ||
98 | ## TO-DO | |
99 | ||
100 | * Minimize code (Uglify does not support ES6 yet) | |
101 | * Check Performance | |
102 | ||
103 | [ash]: http://www.ashframework.org/ |