@@ -11,21 +11,6 @@ public sealed class EntityLayout
1111 public IEnumerable < KeyValuePair < Type , object > > Added => _added ;
1212 public IEnumerable < Type > Removed => _removed ;
1313
14- /// <summary>
15- /// Defines a component to be added.
16- /// Overwrites any previous Add or Remove of the same type.
17- /// </summary>
18- /// <typeparam name="T">Component type</typeparam>
19- /// <param name="component">The component to add</param>
20- /// <exception cref="BufferableException"></exception>
21- public void AddComponent < T > ( T ? component = default ) where T : unmanaged
22- {
23- var metaData = ComponentMetaData < T > . Instance ;
24- if ( metaData . Bufferable ) throw new BufferableException ( typeof ( T ) ) ;
25- _removed . Remove ( typeof ( T ) ) ;
26- _added [ typeof ( T ) ] = component ;
27- }
28-
2914 /// <summary>
3015 /// Defines a buffer component to be added.
3116 /// Overwrites any previous buffer Add or Remove of the same type.
@@ -40,31 +25,32 @@ public void AddBuffer<T>(ReadOnlySpan<T> components) where T : unmanaged
4025 if ( metaData . ZeroSize ) throw new ZeroSizeBufferException ( typeof ( T ) ) ;
4126 if ( ! metaData . Bufferable ) throw new InvalidBufferableException ( typeof ( T ) ) ;
4227 _removed . Remove ( typeof ( T ) ) ;
43- SetBufferComponents ( metaData . Type , components ) ;
28+ SetBuffer ( metaData . Type , components ) ;
4429 }
4530
46- /// <summary>
47- /// Clears Added and Removed components
48- /// </summary>
49- public void Clear ( )
50- {
51- _added . Clear ( ) ;
52- _removed . Clear ( ) ;
53- }
54-
5531 /// <summary>
56- /// Defines a component to be removed .
32+ /// Defines a component to be added .
5733 /// Overwrites any previous Add or Remove of the same type.
5834 /// </summary>
5935 /// <typeparam name="T">Component type</typeparam>
36+ /// <param name="component">The component to add</param>
6037 /// <exception cref="BufferableException"></exception>
61- public void RemoveComponent < T > ( ) where T : unmanaged
38+ public void AddComponent < T > ( T component = default ) where T : unmanaged
6239 {
6340 var metaData = ComponentMetaData < T > . Instance ;
6441 if ( metaData . Bufferable ) throw new BufferableException ( typeof ( T ) ) ;
65- _added . Remove ( typeof ( T ) ) ;
66- _removed . Add ( typeof ( T ) ) ;
67- }
42+ _removed . Remove ( typeof ( T ) ) ;
43+ SetComponent ( metaData . Type , component ) ;
44+ }
45+
46+ /// <summary>
47+ /// Clears Added and Removed components
48+ /// </summary>
49+ public void Clear ( )
50+ {
51+ _added . Clear ( ) ;
52+ _removed . Clear ( ) ;
53+ }
6854
6955 /// <summary>
7056 /// Defines a buffer component to be removed.
@@ -83,7 +69,21 @@ public void RemoveBuffer<T>() where T : unmanaged
8369 _removed . Add ( bufferType ) ;
8470 }
8571
86- private void SetBufferComponents < T > ( Type componentType , ReadOnlySpan < T > components ) where T : unmanaged
72+ /// <summary>
73+ /// Defines a component to be removed.
74+ /// Overwrites any previous Add or Remove of the same type.
75+ /// </summary>
76+ /// <typeparam name="T">Component type</typeparam>
77+ /// <exception cref="BufferableException"></exception>
78+ public void RemoveComponent < T > ( ) where T : unmanaged
79+ {
80+ var metaData = ComponentMetaData < T > . Instance ;
81+ if ( metaData . Bufferable ) throw new BufferableException ( typeof ( T ) ) ;
82+ _added . Remove ( typeof ( T ) ) ;
83+ _removed . Add ( typeof ( T ) ) ;
84+ }
85+
86+ private void SetBuffer < T > ( Type componentType , ReadOnlySpan < T > components ) where T : unmanaged
8787 {
8888 // re-use existing list if available
8989 List < T > list ;
@@ -103,6 +103,22 @@ private void SetBufferComponents<T>(Type componentType, ReadOnlySpan<T> componen
103103 list . Add ( components [ i ] ) ;
104104 }
105105 }
106+
107+ private void SetComponent < T > ( Type componentType , T component ) where T : unmanaged
108+ {
109+ // re-use existing boxed if available
110+ Boxed < T > boxed ;
111+ if ( ! _added . TryGetValue ( componentType , out var boxedObject ) )
112+ {
113+ boxed = ( Boxed < T > ) boxedObject ;
114+ }
115+ else
116+ {
117+ boxed = new Boxed < T > ( ) ;
118+ _added . Add ( typeof ( T ) , boxed ) ;
119+ }
120+ boxed . Value = component ;
121+ }
106122 }
107123}
108124
0 commit comments