Skip to content

Commit 09871d4

Browse files
committed
code examples
1 parent d5eed5a commit 09871d4

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

README.md

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ If you want a general explanation then head over to [Wikipedia](https://en.wikip
1010

1111
## Features
1212
- Gives the most basic features to get going
13-
- it's small (*1.62kb minified*)
13+
- it's small (*1.61kb minified*)
1414
- no dependencies
1515
- it has documentation
1616

@@ -28,11 +28,124 @@ In essence it works like this:
2828
4. You call the libraries update function.
2929
5. It will magically update every entity with the corresponding processor.
3030

31-
**Code examples TBD**
31+
#### Import the Module
32+
```JavaScript
33+
import {EntityComponentSystem} from "javascript-entity-component-system"
34+
const ECS = new EntityComponentSystem()
35+
```
36+
37+
#### "How do I define components?"
38+
Remeber Components are just data. No logic at all. In this library I decided to go with Objects:
39+
```JavaScript
40+
const PositionComponent = {
41+
name: "Position",
42+
state: {
43+
x: 0,
44+
y: 0
45+
}
46+
}
47+
```
48+
49+
The name property is **required**. It will define the name of the component and is important because it's an identifier for processors.
50+
The state property however is not required. You can also define Components to just "mark" entities.
51+
A component that doesn't have a state property could look like this:
52+
```JavaScript
53+
const GravityComponent = {
54+
name: "Gravity"
55+
}
56+
```
57+
58+
#### "How do I define Processors?"
59+
They are almost the same as Components. They are also Objects:
60+
```JavaScript
61+
const PullDownProcessor = {
62+
name: "PullDown",
63+
component: "Gravity",
64+
update(component, entities) {
65+
entities.forEach(entity => {
66+
// Do something on those entities
67+
})
68+
}
69+
}
70+
```
71+
72+
All the properties you see are **required**. The first is the name of the processor and the second one is the component it should act upon. Then there is something that is different from the component Object. It's the update function.
73+
74+
That function is being called every frame and basically inside of that you will write all your logic code. For example in this case this processor corresponds to the "Gravity" component. That means this processor has to pull (hence the name) entities down to earth.
75+
76+
This could be done like so:
77+
```JavaScript
78+
update(component, entities) {
79+
entities.forEach(entity => {
80+
if (ECS.entityHasComponent(entity, "Position")) {
81+
entity.y += 9.81
82+
}
83+
})
84+
```
85+
86+
The update function will get 2 arguments passed down from the system. The first is the component this processor acts on, it can be useful in some cases but most of the time you don't need it. The second argument are the entities that all have a "Gravity" Component attached. You don't have to filter away the components you need. It will be done for you by the system, nice and simple.
87+
88+
With those you then can iterate through them (I used a .forEach as you can see) and then apply logic.
89+
In some cases you will need to check for some other components, like here, you need a position component attached otherwise you can't apply gravity force to it.
90+
91+
You can do that with **ECS.entityHasComponent()**. There is some explanation in the docs. Feel free to read it up :)
92+
93+
#### "How do I register my components and processors in the system?"
94+
That's simple to do.
95+
96+
```JavaScript
97+
// Register Component
98+
ECS.addComponent(PositionComponent)
99+
ECS.addComponent(GravityComponent)
100+
101+
// Register Processor
102+
ECS.addProcessor(PullDownProcessor)
103+
```
104+
105+
Now they are registered in the system and ready for composing.
106+
107+
#### "How to create entities though?"
108+
That's simple too.
109+
You just compose them.
110+
111+
```JavaScript
112+
const Player = ECS.createEntity("Player", ["Position", "Gravity"])
113+
```
114+
115+
#### "Do we need to register those in the system too?"
116+
Yup.
117+
```JavaScript
118+
ECS.addEntity(Player)
119+
```
120+
121+
The decision why you need to manually do it was because you then can create the entity first, make some changes in some properties where you don't feel like creating a whole new component is overkill and then insert it into the system.
122+
This way gives more flexibility.
123+
124+
#### "Okay I got it but how does the system update my entites?"
125+
Now you're almost done.
126+
You now only need to call the update function of the system.
127+
```JavaScript
128+
// You should do this every frame. Preferrably inside your gameloop.
129+
ECS.update()
130+
```
131+
132+
And now everything is updating nicely :)
133+
134+
#### "How do I go from here?"
135+
Well the only thing you have to worry about is to make more components and processors that act upon them.
136+
137+
Please note though that this system is designed for continous logic. Gravity for example is something that always acts on some entity. Having a player get damage for couple of seconds is not continous. I am experimenting with some solutions for this and there are for sure some hacks you can do to make it work but I would suggest you only use this for continous effects and make something yourself for one-time-effects.
138+
139+
One solution would be to add Components that does damage the some entity and then dettach it from the entity again after couple of seconds but it wouldn't be all that easy. You would have to define couple of variables that keep track of the time that has elapsed etc.
140+
141+
However I do have some helper methods implemented in the library that can ease the pain to code one-time-effects and would like to know how you guys cope this.
142+
143+
Other than that have fun deving! :)
32144
33145
## Documentation
34146
[Docs](https://stuhl.github.io/javascript-entity-component-system/)
35147
36148
## Roadmap
37149
- Implement a method that will *freeze* a processor and keep component state
38150
- Solve the rendering problem
151+
- Making Error handling cosistent. Some methods lack error handling. Be careful about this.

0 commit comments

Comments
 (0)