]>
Commit | Line | Data |
---|---|---|
81ed2430 BB |
1 | # Serpentity |
2 | ||
b3b840f8 | 3 | Serpentity is a simple entity framework inspired by [Ash][ash]. |
85861d67 BB |
4 | |
5 | Usage: | |
6 | ||
b3b840f8 | 7 | const Serpentity = require('serpentity'); |
85861d67 BB |
8 | |
9 | ## Instantiating an engine | |
10 | ||
b3b840f8 | 11 | const engine = new Serpentity(); |
85861d67 | 12 | |
42c62ebf BB |
13 | Add entities or systems, systems are added with a priority (the smaller |
14 | the number, the earlier it will be called): | |
85861d67 BB |
15 | |
16 | engine.addEntity(entityFactory()); | |
42c62ebf | 17 | engine.addSystem(new GameSystem(), priority); |
85861d67 BB |
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 | ||
b3b840f8 | 32 | const entity = new Serpentity.Entity(); |
85861d67 BB |
33 | |
34 | All the behavior is added through components | |
35 | ||
36 | ## Creating Components | |
37 | ||
38 | Components define data that we can add to an entity. This data will | |
39 | eventually be consumed by "Systems" | |
40 | ||
b3b840f8 RBR |
41 | const PositionComponent = class PositionComponent extends Serpentity.Component { |
42 | constructor(config) { | |
167b4bbc | 43 | |
b3b840f8 RBR |
44 | this.x = 0; |
45 | this.y = 0; | |
46 | ||
47 | super(config); | |
167b4bbc BB |
48 | } |
49 | }; | |
85861d67 | 50 | |
167b4bbc | 51 | You can add components to entities by using the add method: |
85861d67 | 52 | |
509e372f | 53 | entity.addComponent(new PositionComponent()); |
85861d67 BB |
54 | |
55 | ||
56 | Systems can refer to entities by requesting nodes. | |
57 | ||
58 | ## Working with Nodes | |
59 | ||
60 | Nodes are sets of components that you define, so your system can require | |
61 | entities that always follow the API defined in the node. | |
62 | ||
b3b840f8 | 63 | const MovementNode = class MovementNode extends Serpentity.Node; |
167b4bbc BB |
64 | MovementNode.position = PositionComponent; |
65 | MovementNode.motion = MotionComponent; | |
85861d67 BB |
66 | |
67 | You can then request an array of all the nodes representing entities | |
68 | that comply with that API | |
69 | ||
70 | engine.getNodes(MovementNode); | |
71 | ||
72 | ## Creating Systems | |
73 | ||
74 | Systems are called on every update, and they use components through nodes. | |
75 | ||
b3b840f8 RBR |
76 | const TestSystem = class TestSystem extends Serpentity.System { |
77 | added(engine){ | |
78 | ||
167b4bbc | 79 | this.nodeList = engine.getNodes(MovementNode); |
b3b840f8 RBR |
80 | } |
81 | ||
82 | removed(engine){ | |
83 | ||
167b4bbc BB |
84 | this.nodeList = undefined; |
85 | } | |
b3b840f8 RBR |
86 | |
87 | update(dt){ | |
88 | ||
89 | for (const node of this.nodeList) { | |
167b4bbc | 90 | console.log(`Current position is: ${node.position.x},${node.position.y}`); |
85861d67 | 91 | } |
167b4bbc BB |
92 | } |
93 | }; | |
85861d67 BB |
94 | |
95 | ## That's it | |
96 | ||
97 | Just run `engine.update(dt)` in your game loop :D | |
98 | ||
b3b840f8 | 99 | ![Travis CI Build Status][travis-ci-badge] |
ae99b55e BB |
100 | |
101 | [ash]: http://www.ashframework.org/ | |
b3b840f8 | 102 | [travis-ci-badge]: https://travis-ci.org/serpentity/serpentity.svg?branch=master |