From dd2c125664bdecc96b112a1db8c57249df8ff2cc Mon Sep 17 00:00:00 2001 From: Christian Treffs Date: Fri, 7 Aug 2020 10:50:31 +0200 Subject: [PATCH] Extend family stencil --- Sources/FirebladeECS/Stencils/Family.stencil | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sources/FirebladeECS/Stencils/Family.stencil b/Sources/FirebladeECS/Stencils/Family.stencil index 50a1c32..252f388 100644 --- a/Sources/FirebladeECS/Stencils/Family.stencil +++ b/Sources/FirebladeECS/Stencils/Family.stencil @@ -23,12 +23,20 @@ {% map components into compsTypeParams using comp %}{% if not maploop.first %}_ {% endif %}{{ comp|lowercase }}: {{ comp }}.Type{% endmap %} {% set CompsTypeParams %}{{compsTypeParams|join: ", "}}{% endset %} {% map components into compsNamedParams using comp %}{% if not maploop.first %}_ {% endif %}{{ comp|lowercase }}: {{ comp }}{% endmap %} -{% set CompsNamedParams %}{{compsNamedParams|join: ", "}}{% endset %} +{% set CompsNamedParams %}{{compsNamedParams|join: ", "}}{% endset %} +{% map components into compsNamedRParams using comp %}_ {{ comp|lowercase }}: R.{{ comp }}{% endmap %} +{% set CompsNamedRParams %}{{compsNamedRParams|join: ", "}}{% endset %} // MARK: - Family {{ idx }} public typealias Family{{ idx }}<{{ CompParams }}> = Family> where {{ CompsWhere }} +public protocol RequiringComponents{{ idx }}: FamilyRequirementsManaging where Components == ({{ CompParams }}) { + {% for comp in components %} + associatedtype {{ comp }}: Component + {% endfor %} +} + public struct Requires{{ idx }}<{{ CompParams }}>: FamilyRequirementsManaging where {{ CompsWhere }} { public let componentTypes: [Component.Type] @@ -56,6 +64,14 @@ public struct Requires{{ idx }}<{{ CompParams }}>: FamilyRequirementsManaging wh } } +extension Requires{{ idx }}: RequiringComponents{{ idx }} { } + +extension FamilyMemberBuilder where R: RequiringComponents{{ idx }} { + public static func buildBlock({{ CompsNamedRParams }}) -> (R.Components) { + return ({{ CompsLowercased }}) + } +} + extension Requires{{ idx }}: FamilyEncoding where {{ CompsWhereEncodable }} { public static func encode(components: ({{ CompParams }}), into container: inout KeyedEncodingContainer, using strategy: CodingStrategy) throws { {% if compEncodable.count == 1 %}