]>
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 | ||
efee4ba1 | 7 | import Serpentity from '@serpentity/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 | ||
efee4ba1 BB |
32 | import { Entity } from '@serpentity/serpentity'; |
33 | const entity = new Entity(); | |
85861d67 BB |
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 | ||
efee4ba1 BB |
42 | import { Component } from '@serpentity/serpentity'; |
43 | const PositionComponent = class PositionComponent extends Component { | |
b3b840f8 | 44 | constructor(config) { |
167b4bbc | 45 | |
b3b840f8 RBR |
46 | this.x = 0; |
47 | this.y = 0; | |
48 | ||
49 | super(config); | |
167b4bbc BB |
50 | } |
51 | }; | |
85861d67 | 52 | |
167b4bbc | 53 | You can add components to entities by using the add method: |
85861d67 | 54 | |
509e372f | 55 | entity.addComponent(new PositionComponent()); |
85861d67 BB |
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 | ||
efee4ba1 BB |
65 | import { Node } from '@serpentity/serpentity'; |
66 | const MovementNode = class MovementNode extends Node; | |
167b4bbc BB |
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 | ||
efee4ba1 BB |
79 | import { System } from '@serpentity/serpentity'; |
80 | const TestSystem = class TestSystem extends System { | |
b3b840f8 RBR |
81 | added(engine){ |
82 | ||
167b4bbc | 83 | this.nodeList = engine.getNodes(MovementNode); |
b3b840f8 RBR |
84 | } |
85 | ||
86 | removed(engine){ | |
87 | ||
167b4bbc BB |
88 | this.nodeList = undefined; |
89 | } | |
b3b840f8 RBR |
90 | |
91 | update(dt){ | |
92 | ||
93 | for (const node of this.nodeList) { | |
167b4bbc | 94 | console.log(`Current position is: ${node.position.x},${node.position.y}`); |
85861d67 | 95 | } |
167b4bbc BB |
96 | } |
97 | }; | |
85861d67 BB |
98 | |
99 | ## That's it | |
100 | ||
101 | Just run `engine.update(dt)` in your game loop :D | |
102 | ||
ae99b55e | 103 | [ash]: http://www.ashframework.org/ |