11using Cameras . Components ;
2+ using Cameras . Messages ;
23using Collections . Generic ;
34using Rendering . Components ;
45using Simulation ;
1011
1112namespace Cameras . Systems
1213{
13- public class CameraSystem : ISystem , IDisposable
14+ public sealed partial class CameraSystem : SystemBase , IListener < CameraUpdate >
1415 {
16+ private readonly World world ;
1517 private readonly Operation operation ;
1618 private readonly Array < IsDestination > destinations ;
1719 private readonly int settingsType ;
1820 private readonly int matricesType ;
1921 private readonly int viewportType ;
2022 private readonly int ltwType ;
2123 private readonly int destinationType ;
24+ private readonly BitMask cameraComponents ;
2225
23- public CameraSystem ( Simulator simulator )
26+ public CameraSystem ( Simulator simulator , World world ) : base ( simulator )
2427 {
25- operation = new ( ) ;
28+ this . world = world ;
29+ operation = new ( world ) ;
2630 destinations = new ( ) ;
2731
28- Schema schema = simulator . world . Schema ;
32+ Schema schema = world . Schema ;
2933 settingsType = schema . GetComponentType < CameraSettings > ( ) ;
3034 matricesType = schema . GetComponentType < CameraMatrices > ( ) ;
3135 viewportType = schema . GetComponentType < IsViewport > ( ) ;
3236 ltwType = schema . GetComponentType < LocalToWorld > ( ) ;
3337 destinationType = schema . GetComponentType < IsDestination > ( ) ;
38+ cameraComponents = new ( settingsType , matricesType , viewportType ) ;
3439 }
3540
36- public void Dispose ( )
41+ public override void Dispose ( )
3742 {
3843 destinations . Dispose ( ) ;
3944 operation . Dispose ( ) ;
4045 }
4146
42- void ISystem . Update ( Simulator simulator , double deltaTime )
47+ void IListener < CameraUpdate > . Receive ( ref CameraUpdate message )
4348 {
44- World world = simulator . world ;
45-
4649 //find all destination components
4750 int capacity = ( world . MaxEntityValue + 1 ) . GetNextPowerOf2 ( ) ;
4851 if ( destinations . Length < capacity )
@@ -53,43 +56,43 @@ void ISystem.Update(Simulator simulator, double deltaTime)
5356 destinations . Clear ( ) ;
5457 Span < IsDestination > destinationsSpan = destinations . AsSpan ( ) ;
5558
59+ //collect destinations and make sure cameras have the matrix component
5660 ReadOnlySpan < Chunk > chunks = world . Chunks ;
57- foreach ( Chunk chunk in chunks )
61+ for ( int c = 0 ; c < chunks . Length ; c ++ )
5862 {
59- if ( chunk . Definition . ContainsComponent ( destinationType ) )
63+ Chunk chunk = chunks [ c ] ;
64+ if ( chunk . Count > 0 )
6065 {
61- ReadOnlySpan < uint > entities = chunk . Entities ;
62- ComponentEnumerator < IsDestination > components = chunk . GetComponents < IsDestination > ( destinationType ) ;
63- for ( int i = 0 ; i < entities . Length ; i ++ )
66+ Definition definition = chunk . Definition ;
67+ if ( definition . ContainsComponent ( destinationType ) )
6468 {
65- destinationsSpan [ ( int ) entities [ i ] ] = components [ i ] ;
69+ ReadOnlySpan < uint > entities = chunk . Entities ;
70+ ComponentEnumerator < IsDestination > components = chunk . GetComponents < IsDestination > ( destinationType ) ;
71+ for ( int i = 0 ; i < entities . Length ; i ++ )
72+ {
73+ destinationsSpan [ ( int ) entities [ i ] ] = components [ i ] ;
74+ }
6675 }
67- }
68- }
6976
70- //ensure cameras have a matrices component
71- foreach ( Chunk chunk in chunks )
72- {
73- Definition definition = chunk . Definition ;
74- if ( definition . ContainsComponent ( settingsType ) && ! definition . ContainsComponent ( matricesType ) )
75- {
76- operation . SelectEntities ( chunk . Entities ) ;
77+ if ( definition . ContainsComponent ( settingsType ) && ! definition . ContainsComponent ( matricesType ) )
78+ {
79+ operation . AppendMultipleEntitiesToSelection ( chunk . Entities ) ;
80+ }
7781 }
7882 }
7983
8084 if ( operation . Count > 0 )
8185 {
82- operation . AddComponentType < CameraMatrices > ( ) ;
83- operation . Perform ( world ) ;
86+ operation . AddComponentType ( matricesType ) ;
87+ operation . Perform ( ) ;
8488 operation . Reset ( ) ;
8589 }
8690
8791 chunks = world . Chunks ;
88- BitMask componentMask = new ( settingsType , matricesType , viewportType ) ;
89- foreach ( Chunk chunk in chunks )
92+ for ( int c = 0 ; c < chunks . Length ; c ++ )
9093 {
91- Definition definition = chunk . Definition ;
92- if ( definition . componentTypes . ContainsAll ( componentMask ) )
94+ Chunk chunk = chunks [ c ] ;
95+ if ( chunk . Definition . componentTypes . ContainsAll ( cameraComponents ) )
9396 {
9497 ReadOnlySpan < uint > entities = chunk . Entities ;
9598 ComponentEnumerator < CameraSettings > settingsComponents = chunk . GetComponents < CameraSettings > ( settingsType ) ;
@@ -110,18 +113,18 @@ void ISystem.Update(Simulator simulator, double deltaTime)
110113 IsDestination destination = destinationsSpan [ ( int ) destinationEntity ] ;
111114 if ( settings . orthographic )
112115 {
113- CalculateOrthographic ( world , entity , destination , ltwType , settings , ref matrices ) ;
116+ CalculateOrthographic ( entity , destination , settings , ref matrices ) ;
114117 }
115118 else
116119 {
117- CalculatePerspective ( world , entity , destination , ltwType , settings , ref matrices ) ;
120+ CalculatePerspective ( entity , destination , settings , ref matrices ) ;
118121 }
119122 }
120123 }
121124 }
122125 }
123126
124- private static void CalculatePerspective ( World world , uint entity , IsDestination destination , int ltwType , CameraSettings settings , ref CameraMatrices matrices )
127+ private void CalculatePerspective ( uint entity , IsDestination destination , CameraSettings settings , ref CameraMatrices matrices )
125128 {
126129 LocalToWorld ltw = world . GetComponentOrDefault ( entity , ltwType , LocalToWorld . Default ) ;
127130 Vector3 position = ltw . Position ;
@@ -137,7 +140,7 @@ private static void CalculatePerspective(World world, uint entity, IsDestination
137140 matrices = new ( projection , view ) ;
138141 }
139142
140- private static void CalculateOrthographic ( World world , uint entity , IsDestination destination , int ltwType , CameraSettings settings , ref CameraMatrices matrices )
143+ private void CalculateOrthographic ( uint entity , IsDestination destination , CameraSettings settings , ref CameraMatrices matrices )
141144 {
142145 LocalToWorld ltw = world . GetComponentOrDefault ( entity , ltwType , LocalToWorld . Default ) ;
143146 Vector3 position = ltw . Position ;
0 commit comments