diff --git a/radiantcore/entity/EntityNode.cpp b/radiantcore/entity/EntityNode.cpp index bcdba3e6b3..0b22a5e922 100644 --- a/radiantcore/entity/EntityNode.cpp +++ b/radiantcore/entity/EntityNode.cpp @@ -137,7 +137,7 @@ void EntityNode::createAttachedEntities() // Construct and store the attached entity auto attachedEnt = GlobalEntityModule().createEntity(cls); assert(attachedEnt); - _attachedEnts.push_back(attachedEnt); + _attachedEnts.push_back({attachedEnt, a.offset}); // Set ourselves as the parent of the attached entity (for // localToParent transforms) @@ -156,8 +156,8 @@ void EntityNode::transformChanged() // Broadcast transformChanged to all attached entities so they can update // their position - for (auto attached: _attachedEnts) - attached->transformChanged(); + for (auto [node, offset]: _attachedEnts) + node->transformChanged(); } void EntityNode::onEntityClassChanged() @@ -395,7 +395,7 @@ void EntityNode::setRenderSystem(const RenderSystemPtr& renderSystem) _colourKey.setRenderSystem(renderSystem); // Make sure any attached entities have a render system too - for (IEntityNodePtr node: _attachedEnts) + for (auto [node, offset]: _attachedEnts) node->setRenderSystem(renderSystem); } diff --git a/radiantcore/entity/EntityNode.h b/radiantcore/entity/EntityNode.h index da5f7c1664..03319d14fe 100644 --- a/radiantcore/entity/EntityNode.h +++ b/radiantcore/entity/EntityNode.h @@ -87,7 +87,8 @@ class EntityNode : // sure that everything will play nicely with entities as children of other // entities, and (2) storing entity node pointers instead of generic node // pointers avoids some extra dynamic_casting. - using AttachedEntities = std::list; + using AttachedEntity = std::pair; + using AttachedEntities = std::list; AttachedEntities _attachedEnts; protected: @@ -198,8 +199,11 @@ class EntityNode : // Render all attached entities template void renderAttachments(RenderFunc func) const { - for (const IEntityNodePtr& ent: _attachedEnts) + for (auto [entityNode, offset]: _attachedEnts) { + // Before rendering the attached entity, ensure its offset is correct + entityNode->setLocalToParent(Matrix4::getTranslation(offset)); + // Attached entities might themselves have child nodes (e.g. func_static // which has its model as a child node), so we must traverse() the // attached entities, not just render them alone @@ -215,9 +219,8 @@ class EntityNode : return true; } }; - ChildRenderer cr(func); - ent->traverse(cr); + entityNode->traverse(cr); } }