]>
Commit | Line | Data |
---|---|---|
1 | Serpentity is a simple entity framework inspired by Ash. | |
2 | ||
3 | Usage: | |
4 | ||
5 | require('serpentity'); | |
6 | ||
7 | ## Instantiating an engine | |
8 | ||
9 | var engine = Serpentity(); | |
10 | ||
11 | Add entities or systems: | |
12 | ||
13 | engine.addEntity(entityFactory()); | |
14 | engine.addSystem(new GameSystem()); | |
15 | ||
16 | Update all systems: | |
17 | ||
18 | engine.update(dt); | |
19 | ||
20 | Remove entities or systems: | |
21 | ||
22 | engine.removeEntity(entityReference); | |
23 | engine.removeSystem(systemReference); | |
24 | ||
25 | ## Creating Entities | |
26 | ||
27 | Entities are the basic object of Serpentity, and they do nothing. | |
28 | ||
29 | var entity = new Serpentity.entity(); | |
30 | ||
31 | All the behavior is added through components | |
32 | ||
33 | ## Creating Components | |
34 | ||
35 | Components define data that we can add to an entity. This data will | |
36 | eventually be consumed by "Systems" | |
37 | ||
38 | Class("PositionComponent").inherits(Serpentity.Component)({ | |
39 | prototype : { | |
40 | x : 0, | |
41 | y : 0 | |
42 | } | |
43 | }); | |
44 | ||
45 | You can add components to entities by using the add method: | |
46 | ||
47 | entity.add(new PositionComponent()); | |
48 | ||
49 | ||
50 | Systems can refer to entities by requesting nodes. | |
51 | ||
52 | ## Working with Nodes | |
53 | ||
54 | Nodes are sets of components that you define, so your system can require | |
55 | entities that always follow the API defined in the node. | |
56 | ||
57 | Class("MovementNode").inherits(Serpentity.Node)({ | |
58 | types : { | |
59 | position : PositionComponent, | |
60 | motion : MotionComponent | |
61 | } | |
62 | }); | |
63 | ||
64 | You can then request an array of all the nodes representing entities | |
65 | that comply with that API | |
66 | ||
67 | engine.getNodes(MovementNode); | |
68 | ||
69 | ## Creating Systems | |
70 | ||
71 | Systems are called on every update, and they use components through nodes. | |
72 | ||
73 | Class("TestSystem").inherits(Serpentity.System)({ | |
74 | prototype : { | |
75 | added : function added(engine){ | |
76 | this.nodeList = engine.getNodes(MovementNode); | |
77 | }, | |
78 | removed : function removed(engine){ | |
79 | this.nodeList = undefined; | |
80 | } | |
81 | update : function update(dt){ | |
82 | this.nodeList.forEach(function (node) { | |
83 | console.log("Current position is: " + node.position.x + "," + node.position.y); | |
84 | }); | |
85 | } | |
86 | } | |
87 | }); | |
88 | ||
89 | ## That's it | |
90 | ||
91 | Just run `engine.update(dt)` in your game loop :D | |
92 | ||
93 | ## TO-DO | |
94 | ||
95 | * Removing components | |
96 | * Implement the ashteroids demo (Serpentoids) | |
97 | * Actually check performance |