Salut!
Je crée une entité mur avec une entité enfant tile comme ceci :
Ensuite je veux la cloner comme ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 static EntityId createWallModel(EntityFactory& factory, WallType type, EntityId tile, World& world) { EntityId wall = factory.createEntity("E_WALL"); EntityInfoComponent eic; eic.groupName = "E_WALL"; TransformMatrix tm; TransformComponent tc; tc.localBounds = physic::BoundingBox(0, 0, 0, 0, 0, 0); tc.position = math::Vec3f(0, 0, 0); tc.origin = tc.size * 0.5f; tc.center = tc.position + tc.origin; tm.setOrigin(tc.origin); tm.setRotation(math::Vec3f::zAxis, 0); tm.setTranslation(tc.center); math::Vec3f scale(1.f, 1.f, 1.f); tm.setScale(scale); tc.globalBounds = tc.localBounds.transform(tm); tc.transformMatrix = tm; WallTypeComponent wtc; wtc.wallType = type; addComponent(wall, eic, factory); addComponent(wall, tc, factory); addComponent(wall, wtc, factory); world.addChild(wall, tile, factory); return wall; }
J'ai donc écrit une fonction clone comme ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 EntityId wall = walls[WallType::TOP_LEFT]; EntityId clonedWall; clonedWall = componentMapping.clone<EntityInfoComponent, TransformComponent, WallTypeComponent, MeshComponent>(factory, wall);
Le problème est qu'il clone bien l'entité parent, mais quand je veux cloner l'entité enfant, une fois que je rentre dans la fonction clone_impl, getComponent me renvoie nullptr le std::cout ne s'affiche pas du coup il ne clone pas les composants.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 template <typename... Signature> EntityId clone(EntityFactory& factory, EntityId toClone, EntityId parent = -1) { EntityId clonedId = factory.createEntity(getComponent<EntityInfoComponent>(toClone)->groupName); if( getComponent<MeshComponent>(toClone) && getComponent<EntityInfoComponent>(getRoot(toClone))->groupName == "E_WALL") std::cout<<"mesh to clone "<<toClone<<std::endl; clone_impl<0, Signature...>(toClone, clonedId, factory); if (parent != -1) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[parent].push_back(clonedId); parentMapping[clonedId] = parent; } for (unsigned int i = 0; i < getChildren(toClone).size(); i++) { clone(factory, childrenMapping[toClone][i], clonedId); } if (parent == -1) return clonedId; } template <size_t I, typename... Components, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I < sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { //si l'entité possède le composant en question on le clône. if (getComponent<MeshComponent>(toCloneId) != nullptr && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL") std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } clone_impl<I+1, Components...>(toCloneId, clonedId, factory); } template <size_t I, typename... Components, class... D, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I == sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { if (getComponent<MeshComponent>(toCloneId) != nullptr && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL") std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } }
Voici le code source du fichier :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 #ifndef ODFAEG_ECS_HPP #define ODFAEG_ECS_HPP #include <vector> #include "../Graphics/ECS/components.hpp" #include <optional> #include "entityFactory.hpp" namespace odfaeg { namespace graphic { namespace ecs { template <typename T> std::vector<std::optional<T>>& get(){ static std::vector<std::optional<T>> tvec; return tvec; } template <typename T> void addComponent(EntityId entity, T component, EntityFactory& factory) { get<T>().resize(factory.getNbEntities()); get<T>()[entity] = component; } template <typename T> T* getComponent(EntityId entity) { if (entity >= get<T>().size() || !get<T>()[entity].has_value()) return nullptr; else return &get<T>()[entity].value(); } template <typename T> void removeComponent(EntityId entity, EntityFactory& factory) { typename std::vector<T>::iterator it; unsigned int i; for (i = 0, it = get<T>().begin(); it != get<T>().end();i++) { if (i == entity) it = get<T>().erase(it); else it++; } get<T>().resize(factory.getNbEntities()); } class ComponentMapping { std::vector<std::vector<EntityId>> childrenMapping; std::vector<std::optional<EntityId>> parentMapping; public : void updateSize(EntityFactory& factory) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); } void addChild(EntityId parentId, EntityId child, EntityFactory& factory) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[parentId].push_back(child); parentMapping[child] = parentId; } EntityId getRoot(EntityId entityId) { EntityId root = entityId; if (entityId >= parentMapping.size()) return root; while (parentMapping[root].has_value()) { root = parentMapping[root].value(); } return root; } std::vector<EntityId> getChildren(EntityId entity) { if (entity >= childrenMapping.size()) return std::vector<EntityId>(); return childrenMapping[entity]; } EntityId getParent(EntityId entity) { if(parentMapping[entity].has_value()); return parentMapping[entity].value(); return -1; } void setParent(EntityId entity, EntityId parent) { parentMapping[entity] = parent; } bool remove(EntityId entity) { for (unsigned int i = 0; i < childrenMapping[entity].size(); i++) { remove(childrenMapping[entity][i]); } childrenMapping[entity].clear(); std::vector<std::optional<EntityId>>::iterator it; for (it = parentMapping.begin(); it != parentMapping.end();) { if (it->has_value() && it->value() == entity) it = parentMapping.erase(it); else it++; } } template <typename... Signature> EntityId clone(EntityFactory& factory, EntityId toClone, EntityId parent = -1) { EntityId clonedId = factory.createEntity(getComponent<EntityInfoComponent>(toClone)->groupName); if( getComponent<MeshComponent>(toClone) && getComponent<EntityInfoComponent>(getRoot(toClone))->groupName == "E_WALL") std::cout<<"mesh to clone "<<toClone<<std::endl; clone_impl<0, Signature...>(toClone, clonedId, factory); if (parent != -1) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[parent].push_back(clonedId); parentMapping[clonedId] = parent; } for (unsigned int i = 0; i < getChildren(toClone).size(); i++) { clone(factory, childrenMapping[toClone][i], clonedId); } if (parent == -1) return clonedId; } template <size_t I, typename... Components, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I < sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { //si l'entité possède le composant en question on le clône. if (getComponent<MeshComponent>(toCloneId) != nullptr && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL") std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } clone_impl<I+1, Components...>(toCloneId, clonedId, factory); } template <size_t I, typename... Components, class... D, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I == sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { if (getComponent<MeshComponent>(toCloneId) != nullptr && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL") std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } } template <size_t I, typename... Components, class... D, class...E, class = typename std::enable_if_t<(sizeof...(Components) == 0)>> void clone_impl(EntityId tocloneId, EntityId clonedId, EntityFactory& factory) { } template <typename... Signature> EntityId merge(EntityFactory& factory, EntityId toMerge1, EntityId toMerge2, std::string groupName, EntityId parent=-1) { EntityId merged = factory.createEntity(groupName); EntityId merged1, merged2; if (parent == -1) { merged1 = factory.createEntity(getComponent<EntityInfoComponent>(toMerge1)->groupName); merged2 = factory.createEntity(getComponent<EntityInfoComponent>(toMerge2)->groupName); merge_node<0, Signature...>(merged, toMerge1, factory); merge_node<0, Signature...>(merged, toMerge2, factory); childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[merged].push_back(merged1); parentMapping[merged1] = merged; childrenMapping[merged].push_back(merged2); parentMapping[merged2] = merged; } if (parent != -1 && toMerge1 != -1) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[parent].push_back(merged); parentMapping[merged] = parent; merge_node<0, Signature...>(merged, toMerge1, factory); } if (parent != -1 && toMerge2 != -1) { childrenMapping.resize(factory.getNbEntities()); parentMapping.resize(factory.getNbEntities()); childrenMapping[parent].push_back(merged); parentMapping[merged] = parent; merge_node<0, Signature...>(merged, toMerge2, factory); } for (unsigned int i = 0; i < childrenMapping[toMerge1].size(); i++) { merge(factory, childrenMapping[toMerge1][i], -1, getComponent<EntityInfoComponent>(childrenMapping[toMerge1][i])->groupName, (parent == -1) ? merged1 : merged); } for (unsigned int i = 0; i < childrenMapping[toMerge2].size(); i++) { merge(factory, -1, childrenMapping[toMerge2][i], getComponent<EntityInfoComponent>(childrenMapping[toMerge2][i])->groupName, (parent == -1) ? merged2 : merged); } if (parent == -1) return merged; } template <size_t I, typename... Signature, class = typename std::enable_if_t<(sizeof...(Signature) != 0 && I < sizeof...(Signature)-1)>> void merge_node(EntityId entityId, EntityId toMergeId, EntityFactory& factory) { //si l'entité possède le composant en question on le clône. if (getComponent<std::tuple_element_t<I, std::tuple<Signature...>>>(toMergeId)) { addComponent(entityId, *getComponent<std::tuple_element_t<I, std::tuple<Signature...>>>(toMergeId), factory); } merge_node<I+1, Signature...>(entityId, toMergeId, factory); } template < size_t I, typename... Signature, class... D, class = typename std::enable_if_t<(sizeof...(Signature) != 0 && I == sizeof...(Signature)-1)>> void merge_node(EntityId entityId, EntityId toMergeId, EntityFactory& factory) { if (getComponent<std::tuple_element_t<I, std::tuple<Signature...>>>(toMergeId)) { addComponent(entityId, *getComponent<std::tuple_element_t<I, std::tuple<Signature...>>>(toMergeId), factory); } } template <size_t I, typename... Signature, class... D, class...E, class = typename std::enable_if_t<(sizeof...(Signature) == 0)>> void merge_node(EntityId tocloneId, EntityId clonedId, EntityFactory& factory) { } template <typename... Signature, typename System, typename... Params> void apply(System& system, std::vector<EntityId>& entities, std::tuple<Params...>& params) { for (unsigned int i = 0; i < entities.size(); i++) { this->template apply_impl<Signature...>(entities[i], system, params, std::index_sequence_for<Signature...>()); std::vector<EntityId> children = getChildren(entities[i]); std::vector<EntityId> addedChildren; while (children.size() > 0) { for (unsigned int i = 0; i < children.size(); i++) { addedChildren.push_back(children[i]); children = getChildren(children[i]); } for (unsigned int i = 0; i < children.size(); i++) { children.pop_back(); } } for (unsigned int i = 0; i < addedChildren.size(); i++) { this->template apply_impl<Signature...>(addedChildren[i], system, params, std::index_sequence_for<Signature...>()); } } } template <typename... Signature, typename System, size_t... I, typename... Params> void apply_impl(EntityId entityId, System& system, std::tuple<Params...>& params, std::index_sequence<I...>) { system.template operator()<Signature...>(entityId, params); } template <typename... Signature, typename System, typename... Params, class R> void apply(System& system, std::vector<EntityId>& entities, std::tuple<Params...>& params, std::vector<R>& ret) { for (unsigned int i = 0; i < entities.size(); i++) { this->template apply_impl<Signature...>(entities[i], system, params, std::index_sequence_for<Signature...>(), ret); std::vector<EntityId> children = getChildren(entities[i]); std::vector<EntityId> addedChildren; while (addedChildren.size() > 0) { for (unsigned int i = 0; i < children.size(); i++) { addedChildren.push_back(children[i]); children = getChildren(children[i]); } for (unsigned int i = 0; i < children.size(); i++) { children.pop_back(); } } for (unsigned int i = 0; i < addedChildren.size(); i++) { this->template apply_impl<Signature...>(addedChildren[i], system, params, std::index_sequence_for<Signature...>(), ret); } } } template <typename... Signature, typename System, size_t... I, typename... Params, typename R> void apply_impl(EntityId entityId, System& system, std::tuple<Params...>& params, std::index_sequence<I...>, std::vector<R>& rets) { rets.push_back(system.template operator()<Signature...>(entityId, params)); } }; } } } #endif
EDIT : j'ai fais des tests plus appronfondit pour voir et c'est has_value de std::option qui renvoie faux :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 template <size_t I, typename... Components, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I < sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { //si l'entité possède le composant en question on le clône. if (getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL" && getComponent<MeshComponent>(toCloneId, "test") != nullptr) std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } clone_impl<I+1, Components...>(toCloneId, clonedId, factory); } template <size_t I, typename... Components, class... D, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I == sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId, EntityFactory& factory) { if (getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL" && getComponent<MeshComponent>(toCloneId, "test") != nullptr) std::cout<<"cloned mesh component"<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId), factory); } }Pourquoi has_value de std::option renvoie true en dehors de la fonction clone_impl mais une fois que je rentre dans la fonction clone_impl ça change et il renvoie faux ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 template <typename T> T* getComponent(EntityId entity, std::string name = "") { if (name == "test") { std::cout<<"size : "<<get<T>().size()<<" entity : "<<entity<<std::endl; std::cout<<"has component ? "<<!get<T>()[entity].has_value()<<std::endl; } if (entity >= get<T>().size() || !get<T>()[entity].has_value()) { /*if (name == "test") std::cout<<"return nullptr"<<std::endl;*/ return nullptr; } else { /*if (name == "test") std::cout<<"return : "<<&get<T>()[entity].value()<<std::endl;*/ return &get<T>()[entity].value(); } }
EDIT 2 : la fonction value me donne une exception ça veut dire qu'il n'y a plus de composant dans mon std::vector dans la fonction clone_impl pourquoi ?
EDIT 3 : j'ai afficher les ids et ce n'est pas les mêmes :
Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 template <typename... Signature> EntityId clone(EntityFactory& factory, EntityId toClone, EntityId parent = -1) { EntityId clonedId = factory.createEntity(getComponent<EntityInfoComponent>(toClone)->groupName); if(getComponent<EntityInfoComponent>(getRoot(toClone))->groupName == "E_WALL" && getComponent<MeshComponent>(toClone) != nullptr) std::cout<<"to clone : "<<toClone<<std::endl; clone_impl<0, Signature...>(toClone, clonedId); if (parent != -1) { if (parent >= childrenMapping.size()) childrenMapping.resize(parent + 1); if (clonedId >= parentMapping.size()) parentMapping.resize(clonedId + 1); childrenMapping[parent].push_back(clonedId); parentMapping[clonedId] = parent; } for (unsigned int i = 0; i < getChildren(toClone).size(); i++) { clone(factory, childrenMapping[toClone][i], clonedId); } if (parent == -1) return clonedId; } template <size_t I, typename... Components, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I < sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId) { //si l'entité possède le composant en question on le clône. //if (getComponent<EntityInfoComponent>(toCloneId) != nullptr && (getComponent<EntityInfoComponent>(toCloneId)->groupName != "E_WALL") && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName != "E_TILE") std::cout<<"to clone impl : "<<toCloneId<<std::endl; /*if (getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL" && getComponent<MeshComponent>(toCloneId, "test") != nullptr) std::cout<<"cloned mesh component"<<std::endl;*/ if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)); } clone_impl<I+1, Components...>(toCloneId, clonedId); } template <size_t I, typename... Components, class... D, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I == sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId) { /*if (getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName == "E_WALL" && getComponent<MeshComponent>(toCloneId, "test") != nullptr) std::cout<<"cloned mesh component"<<std::endl;*/ //if (getComponent<EntityInfoComponent>(toCloneId) != nullptr && (getComponent<EntityInfoComponent>(toCloneId)->groupName != "E_WALL") && getComponent<EntityInfoComponent>(getRoot(toCloneId))->groupName != "E_TILE") std::cout<<"to clone impl : "<<toCloneId<<std::endl; if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)); } } template <size_t I, typename... Components, class... D, class...E, class = typename std::enable_if_t<(sizeof...(Components) == 0)>> void clone_impl(EntityId tocloneId, EntityId clonedId) { }
EDIT 4 : Résolu!!!
J'avais oublié de passer le template parameter pack lors du rappel de la fonction clone quel boulet et j'ai cherché pendant 1 jour à cause de ça!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 template <typename... Signature> EntityId clone(EntityFactory& factory, EntityId toClone, EntityId parent = -1) { EntityId clonedId = factory.createEntity(getComponent<EntityInfoComponent>(toClone)->groupName); clone_impl<0, Signature...>(toClone, clonedId); if (parent != -1) { if (parent >= childrenMapping.size()) childrenMapping.resize(parent + 1); if (clonedId >= parentMapping.size()) parentMapping.resize(clonedId + 1); childrenMapping[parent].push_back(clonedId); parentMapping[clonedId] = parent; } for (unsigned int i = 0; i < getChildren(toClone).size(); i++) { clone<Signature...>(factory, childrenMapping[toClone][i], clonedId); } if (parent == -1) return clonedId; } template <size_t I, typename... Components, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I < sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId) { //si l'entité possède le composant en question on le clône. if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)); } clone_impl<I+1, Components...>(toCloneId, clonedId); } template <size_t I, typename... Components, class... D, class = typename std::enable_if_t<(sizeof...(Components) != 0 && I == sizeof...(Components)-1)>> void clone_impl(EntityId toCloneId, EntityId clonedId) { if (getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)) { addComponent(clonedId, *getComponent<std::tuple_element_t<I, std::tuple<Components...>>>(toCloneId)); } } template <size_t I, typename... Components, class... D, class...E, class = typename std::enable_if_t<(sizeof...(Components) == 0)>> void clone_impl(EntityId tocloneId, EntityId clonedId) { }
Partager