]> git.r.bdr.sh - rbdr/serpentity/blame - README.md
Prepare bower with a concatenated file.
[rbdr/serpentity] / README.md
CommitLineData
81ed2430
BB
1# Serpentity
2
ae99b55e 3Serpentity is a simple entity framework inspired by [Ash][ash].
85861d67
BB
4
5Usage:
6
c2c83a18 7 require('serpentity');
85861d67
BB
8
9## Instantiating an engine
10
c2c83a18 11 var engine = Serpentity();
85861d67 12
42c62ebf
BB
13Add entities or systems, systems are added with a priority (the smaller
14the number, the earlier it will be called):
85861d67
BB
15
16 engine.addEntity(entityFactory());
42c62ebf 17 engine.addSystem(new GameSystem(), priority);
85861d67
BB
18
19Update all systems:
20
21 engine.update(dt);
22
23Remove entities or systems:
24
25 engine.removeEntity(entityReference);
26 engine.removeSystem(systemReference);
27
28## Creating Entities
29
30Entities are the basic object of Serpentity, and they do nothing.
31
971ff307 32 var entity = new Serpentity.Entity();
85861d67
BB
33
34All the behavior is added through components
35
36## Creating Components
37
38Components define data that we can add to an entity. This data will
39eventually be consumed by "Systems"
40
41 Class("PositionComponent").inherits(Serpentity.Component)({
42 prototype : {
43 x : 0,
44 y : 0
45 }
46 });
47
48You can add components to entities by using the add method:
49
50 entity.add(new PositionComponent());
51
52
53Systems can refer to entities by requesting nodes.
54
55## Working with Nodes
56
57Nodes are sets of components that you define, so your system can require
58entities that always follow the API defined in the node.
59
60 Class("MovementNode").inherits(Serpentity.Node)({
61 types : {
62 position : PositionComponent,
63 motion : MotionComponent
64 }
65 });
66
67You can then request an array of all the nodes representing entities
68that comply with that API
69
70 engine.getNodes(MovementNode);
71
72## Creating Systems
73
74Systems are called on every update, and they use components through nodes.
75
c2c83a18
BB
76 Class("TestSystem").inherits(Serpentity.System)({
77 prototype : {
78 added : function added(engine){
79 this.nodeList = engine.getNodes(MovementNode);
80 },
81 removed : function removed(engine){
82 this.nodeList = undefined;
83 }
84 update : function update(dt){
85 this.nodeList.forEach(function (node) {
86 console.log("Current position is: " + node.position.x + "," + node.position.y);
87 });
88 }
85861d67 89 }
c2c83a18 90 });
85861d67
BB
91
92## That's it
93
94Just run `engine.update(dt)` in your game loop :D
95
eecd25c9
BB
96## Checking it in the frontend (dev).
97
74892a8f
BB
98You can link the bower package to test it out locally.
99Spawn a python server (`python -m SimpleHTTPServer`) to see
100the test page in `http://localhost:8000/browser_test/`
eecd25c9
BB
101
102
85861d67
BB
103## TO-DO
104
105* Removing components
106* Implement the ashteroids demo (Serpentoids)
107* Actually check performance
ae99b55e
BB
108
109[ash]: http://www.ashframework.org/