]>
Commit | Line | Data |
---|---|---|
81ed2430 BB |
1 | # Serpentity |
2 | ||
167b4bbc | 3 | Serpentity is a simple entity framework inspired by Ash. |
85861d67 BB |
4 | |
5 | Usage: | |
6 | ||
167b4bbc BB |
7 | let Serpentity = require('serpentity'); |
8 | ||
9 | or: | |
10 | ||
11 | <script src="/node_modules/serpentity/dist/serpentity.js"></script> | |
85861d67 BB |
12 | |
13 | ## Instantiating an engine | |
14 | ||
167b4bbc | 15 | let engine = Serpentity(); |
85861d67 | 16 | |
42c62ebf BB |
17 | Add entities or systems, systems are added with a priority (the smaller |
18 | the number, the earlier it will be called): | |
85861d67 BB |
19 | |
20 | engine.addEntity(entityFactory()); | |
42c62ebf | 21 | engine.addSystem(new GameSystem(), priority); |
85861d67 BB |
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 | ||
167b4bbc | 36 | let entity = new Serpentity.Entity(); |
85861d67 BB |
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 | ||
167b4bbc BB |
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 | }; | |
85861d67 | 53 | |
167b4bbc | 54 | You can add components to entities by using the add method: |
85861d67 | 55 | |
509e372f | 56 | entity.addComponent(new PositionComponent()); |
85861d67 BB |
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 | ||
167b4bbc BB |
66 | let MovementNode = class MovementNode extends Serpentity.Node; |
67 | MovementNode.position = PositionComponent; | |
68 | MovementNode.motion = MotionComponent; | |
85861d67 BB |
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 | ||
167b4bbc BB |
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}`); | |
85861d67 | 90 | } |
167b4bbc BB |
91 | } |
92 | }; | |
85861d67 BB |
93 | |
94 | ## That's it | |
95 | ||
96 | Just run `engine.update(dt)` in your game loop :D | |
97 | ||
98 | ## TO-DO | |
99 | ||
167b4bbc BB |
100 | * Minimize code (Uglify does not support ES6 yet) |
101 | * Check Performance | |
ae99b55e BB |
102 | |
103 | [ash]: http://www.ashframework.org/ |