Advertisement
Guest User

dots

a guest
Jun 16th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1.     public class GameLogicSystemGroup : ComponentSystemGroup
  2.     {
  3.     }
  4.  
  5.     [UpdateInGroup(typeof(GameLogicSystemGroup))]
  6.     public class StatUpdateSystemGroup : ComponentSystemGroup
  7.     {
  8.     }
  9.  
  10.     [UpdateInGroup(typeof(GameLogicSystemGroup))]
  11.     [UpdateAfter(typeof(StatUpdateSystemGroup))]
  12.     public class SkillUpdateSystemGroup: ComponentSystemGroup
  13.     {
  14.     }
  15.  
  16.     [UpdateInGroup(typeof(GameLogicSystemGroup), OrderFirst = true)]
  17.     public class GameLogicStartBuffer : EntityCommandBufferSystem
  18.     {
  19.     }
  20.     [UpdateInGroup(typeof(GameLogicSystemGroup), OrderLast = true)]
  21.     public class GameLogicEndBuffer : EntityCommandBufferSystem
  22.     {
  23.     }
  24.  
  25.     public struct StatBlock
  26.     {
  27.         public int Str;
  28.         public int Dex;
  29.         public int Int;
  30.         public int Con;
  31.  
  32.         public static StatBlock operator +(StatBlock a, StatBlock b)
  33.         {
  34.             return new StatBlock()
  35.             {
  36.                 Str = a.Str + b.Str,
  37.                 Dex = a.Dex + b.Dex,
  38.                 Int = a.Int + b.Int,
  39.                 Con = a.Con + b.Con
  40.             };
  41.         }
  42.     }
  43.  
  44.     public struct TagBaseChanged : IComponentData
  45.     {
  46.     }
  47.  
  48.     public struct TagModsChanged : IComponentData
  49.     {
  50.     }
  51.  
  52.  
  53.     public struct BaseStats : IComponentData
  54.     {
  55.         public StatBlock Value;
  56.     }
  57.  
  58.     [InternalBufferCapacity(10)]
  59.     public struct StatMods : IBufferElementData
  60.     {
  61.         public StatBlock Value;
  62.     }
  63.  
  64.  
  65.     public struct TotalStatMods : IComponentData
  66.     {
  67.         public StatBlock Value;
  68.     }
  69.  
  70.     public struct TotalStats : IComponentData
  71.     {
  72.         public StatBlock Value;
  73.     }
  74.  
  75.     public struct SomeSkillA : IComponentData
  76.     {
  77.         public int Value;
  78.     }
  79.  
  80.     public struct SomeSkillB : IComponentData
  81.     {
  82.         public int Value;
  83.     }
  84.  
  85.     [UpdateInGroup(typeof(StatUpdateSystemGroup))]
  86.     public partial class SystemCalcMods : SystemBase
  87.     {
  88.         protected override void OnUpdate()
  89.         {
  90.             Entities.WithAll<TagModsChanged>().ForEach((ref TotalStatMods total, in DynamicBuffer<StatMods> mods) =>
  91.             {
  92.                 total.Value = new StatBlock();
  93.                 for (int i = 0; i < mods.Length; i++)
  94.                     total.Value += mods[i].Value;
  95.             }).ScheduleParallel(Dependency);
  96.         }
  97.     }
  98.  
  99.     [UpdateInGroup(typeof(StatUpdateSystemGroup))]
  100.     [UpdateAfter(typeof(SystemCalcMods))]
  101.     public partial class SystemCalcStats : SystemBase
  102.     {
  103.         private struct job_stats : IJobEntity
  104.         {
  105.             public void Execute(ref TotalStats total, in TotalStatMods mods, in BaseStats stats)
  106.             {
  107.                 total.Value = stats.Value + mods.Value;
  108.             }
  109.         }
  110.  
  111.         private EntityQuery query_mod, query_base, query_both;
  112.  
  113.         protected override void OnCreate()
  114.         {
  115.             query_base = EntityManager.CreateEntityQuery(ComponentType.ReadOnly<TagBaseChanged>(),
  116.                 ComponentType.Exclude<TagModsChanged>());
  117.             query_mod = EntityManager.CreateEntityQuery(ComponentType.Exclude<TagBaseChanged>(),
  118.                 ComponentType.ReadOnly<TagModsChanged>());
  119.             query_both = EntityManager.CreateEntityQuery(ComponentType.ReadOnly<TagBaseChanged>(),
  120.                 ComponentType.ReadOnly<TagModsChanged>());
  121.         }
  122.  
  123.         protected override void OnUpdate()
  124.         {
  125.             var job = new job_stats();
  126.             job.ScheduleParallel(query_base);
  127.             job.ScheduleParallel(query_mod);
  128.             job.ScheduleParallel(query_both);
  129.         }
  130.     }
  131.  
  132.     [UpdateInGroup(typeof(SkillUpdateSystemGroup))]
  133.     public partial class SystemCalcSkillA : SystemBase
  134.     {
  135.         protected override void OnUpdate()
  136.         {
  137.             Entities.ForEach((ref SomeSkillA skill, in TotalStats stats) =>
  138.             {
  139.                 skill.Value = 50 + (stats.Value.Str + stats.Value.Int) / 2;
  140.             }).ScheduleParallel(Dependency);
  141.         }
  142.     }
  143.  
  144.     [UpdateInGroup(typeof(SkillUpdateSystemGroup))]
  145.     public partial class SystemCalcSkillB : SystemBase
  146.     {
  147.         protected override void OnUpdate()
  148.         {
  149.             Entities.ForEach((ref SomeSkillB skill, in TotalStats stats) =>
  150.             {
  151.                 skill.Value = 25 + (stats.Value.Dex + stats.Value.Con + stats.Value.Str) / 3;
  152.             }).ScheduleParallel(Dependency);
  153.         }
  154.     }
  155.  
  156.  
  157.     [UpdateInGroup(typeof(GameLogicSystemGroup))]
  158.     public partial class SystemClearTagModsChanged : SystemBase
  159.     {
  160.         private GameLogicEndBuffer ecbSystem;
  161.  
  162.         protected override void OnCreate()
  163.         {
  164.             ecbSystem = World.GetOrCreateSystem<GameLogicEndBuffer>();
  165.         }
  166.  
  167.         protected override void OnUpdate()
  168.         {
  169.             var ecb = ecbSystem.CreateCommandBuffer().AsParallelWriter();
  170.             Entities.WithAll<TagModsChanged>().ForEach((Entity e, int index) =>
  171.             {
  172.                 ecb.RemoveComponent<TagModsChanged>(index, e);
  173.             });
  174.  
  175.             ecbSystem.AddJobHandleForProducer(Dependency);
  176.         }
  177.     }
  178.  
  179.     [UpdateInGroup(typeof(GameLogicSystemGroup))]
  180.     public partial class SystemClearTagBaseChanged : SystemBase
  181.     {
  182.         private GameLogicEndBuffer ecbSystem;
  183.  
  184.         protected override void OnCreate()
  185.         {
  186.             ecbSystem = World.GetOrCreateSystem<GameLogicEndBuffer>();
  187.         }
  188.  
  189.         protected override void OnUpdate()
  190.         {
  191.             var ecb = ecbSystem.CreateCommandBuffer().AsParallelWriter();
  192.             Entities.WithAll<TagBaseChanged>().ForEach((Entity e, int index) =>
  193.             {
  194.                 ecb.RemoveComponent<TagBaseChanged>(index, e);
  195.             });
  196.  
  197.             ecbSystem.AddJobHandleForProducer(Dependency);
  198.         }
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement