You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remeber Components are just data. No logic at all. In this library I decided to go with Objects:
39
+
```JavaScript
40
+
constPositionComponent= {
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
+
constGravityComponent= {
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
+
constPullDownProcessor= {
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.
#### "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.
0 commit comments