Advertisement
thelebaron

Untitled

Jul 4th, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.24 KB | None | 0 0
  1. using Junk.Cameras;
  2. using Unity.Assertions;
  3. using Unity.Burst;
  4. using Unity.Burst.Intrinsics;
  5. using Unity.Entities;
  6. using Unity.Mathematics;
  7. using Unity.Rendering;
  8. using Unity.Transforms;
  9. using UnityEngine;
  10.  
  11. namespace Junk.FirstPerson
  12. {
  13.     //
  14.     //
  15.     //
  16.     //
  17.     //
  18.     //
  19.     //
  20.     //
  21.     //
  22.     //
  23.     //
  24.     // something in this causes built player burst crash
  25.     //
  26.     //
  27.     //
  28.     //
  29.     //
  30.     //
  31.     //
  32.     //
  33.     //
  34.     [BurstCompile]
  35.     public struct AddToPlayerInventoryJob : IJobChunk
  36.     {
  37.         public EntityCommandBuffer               EntityCommandBuffer;
  38.         public float                             DeltaTime;
  39.         public EntityTypeHandle                  EntityHandle;
  40.         public ComponentLookup<MaterialMeshInfo> MaterialMeshInfoLookup;
  41.         public ComponentTypeHandle<ItemPickup>   WeaponPickupHandle;
  42.         public ComponentTypeHandle<Equipped>     EquippedHandle;
  43.         // optional chunk components
  44.         public ComponentTypeHandle<WeaponGraphicsEntities> WeaponGraphicsEntitiesHandle;
  45.         public ComponentTypeHandle<WeaponMuzzleModel>      WeaponMuzzleModelHandle;
  46.        
  47.         // Player components
  48.         public Entity                                PlayerEntity;
  49.         public ComponentLookup<LocalTransform>       LocalTransformLookup;
  50.         public ComponentLookup<PlayerItemsInventory> PlayerItemsInventoryLookup;
  51.         public BufferLookup<ItemSlot>                ItemSlotBufferLookup;
  52.         public BufferLookup<Child>                   ChildBufferLookup;
  53.         public ComponentLookup<FirstPersonCamera>    FirstPersonCameraLookup;
  54.        
  55.         public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
  56.         {
  57.             Assert.IsTrue(ChildBufferLookup.HasBuffer(PlayerEntity));
  58.             Assert.IsTrue(LocalTransformLookup.HasComponent(PlayerEntity));
  59.             Assert.IsTrue(PlayerItemsInventoryLookup.HasComponent(PlayerEntity));
  60.             Assert.IsTrue(ItemSlotBufferLookup.HasBuffer(PlayerEntity));
  61.  
  62.             var playerChildren  = ChildBufferLookup[PlayerEntity];
  63.             var playerTransform = LocalTransformLookup[PlayerEntity];
  64.             var playerInventory = PlayerItemsInventoryLookup[PlayerEntity];
  65.             var playerSlots     = ItemSlotBufferLookup[PlayerEntity];
  66.            
  67.             var entities      = chunk.GetNativeArray(EntityHandle);
  68.             var weaponPickups = chunk.GetNativeArray(ref WeaponPickupHandle);
  69.                
  70.             var entityEnumerator = new ChunkEntityEnumerator(useEnabledMask, chunkEnabledMask, chunk.Count);
  71.  
  72.             while (entityEnumerator.NextEntityIndex(out var entityIndex))
  73.             {
  74.                 var entity       = entities[entityIndex];
  75.                 var weaponPickup = weaponPickups[entityIndex];
  76.  
  77.                 // Cant operate without a camera
  78.                 if (playerChildren.Length < 1)
  79.                 {
  80.                     Debug.Log($"playerChildren buffer length: {playerChildren.Length}");
  81.                     continue;
  82.                 }
  83.                
  84.                 //
  85.                 // < --- burst crash here --- >
  86.                 // Player.log exerpt
  87.                 // Streamed scene with  215ms latency from C:/Users/thele/Documents/Repos/cyberjunk/bin/Pain Meat Pain_Data/StreamingAssets/EntityScenes/a4185ca62cee3a144826b19ec2b9d207.0.entities
  88.                 // playerChildren buffer length: 0
  89.                 // playerChildren buffer length: 0
  90.                 // Crash!!!
  91.                
  92.                 if (playerChildren.Length < 1)
  93.                     Debug.Log($"playerChildren.Length < 1, continue ");
  94.                
  95.                
  96.                 Debug.Log($"Checking Camera");
  97.                 if (!HasCamera(playerChildren, out var cameraChild))
  98.                 {
  99.                     Debug.Log($"No camera found in playerChildren, continue");
  100.                     continue;
  101.                 }
  102.                
  103.                 Debug.Log($"Camera found");
  104.                
  105.                 /*
  106.                 // this is a bit of a janky check for a camera, should have a component with camera entity on it
  107.                 // disable this?
  108.                 if (playerChildren.Length < 1)
  109.                 {
  110.                     Debug.Log($"playerChildren.Length < 1, continue ");
  111.                     continue;
  112.                 }
  113.  
  114.                 Debug.Log($"playerChildren.Length: {playerChildren.Length}");
  115.  
  116.                 // Dont execute if maxed out inventory
  117.                 if(playerSlots.Length > playerInventory.MaxWeapons)
  118.                 {
  119.                     Debug.Log($"Inventory maxed out: {playerSlots.Length} > {playerInventory.MaxWeapons}, continue");
  120.                     continue;
  121.                 }
  122.  
  123.                 // destroy event if entity already inside buffer
  124.                 var containedAlready = false;
  125.                 for (var j = 0; j < playerSlots.Length; j++)
  126.                 {
  127.                     if (playerSlots[j].ItemEntity.Equals(entity))
  128.                     {
  129.                         Debug.Log($"containedAlready");
  130.                         containedAlready = true;
  131.                         break;
  132.                     }
  133.                 }
  134.  
  135.                 if (containedAlready)
  136.                 {
  137.                     Debug.Log($"Entity {entity} already in inventory, disabling pickup");
  138.                     weaponPickup = default;
  139.                     //weaponPickups[entityIndex] = weaponPickup; ??
  140.                     chunk.SetComponentEnabled(ref WeaponPickupHandle, entityIndex, false);
  141.                     continue;
  142.                 }
  143.  
  144.                 Debug.Log($"Entity {entity} not in inventory, proceeding with pickup");
  145.  
  146.                 weaponPickup.Player = PlayerEntity;
  147.                 Debug.Log($"Set weaponPickup.Player to {PlayerEntity}");
  148.  
  149.                 // Set graphics status
  150.                 // Enable the arms model entity
  151.                 if (chunk.Has(ref WeaponGraphicsEntitiesHandle))
  152.                 {
  153.                     var weaponGraphicsEntities = chunk.GetNativeArray(ref WeaponGraphicsEntitiesHandle)[entityIndex];
  154.                     Debug.Log($"Enabling weapon graphics: Arms={weaponGraphicsEntities.Arms}, Weapon={weaponGraphicsEntities.Weapon}");
  155.                     MaterialMeshInfoLookup.SetComponentEnabled(weaponGraphicsEntities.Arms, true);
  156.                     MaterialMeshInfoLookup.SetComponentEnabled(weaponGraphicsEntities.Weapon, true);
  157.                     MaterialMeshInfoLookup.SetComponentEnabled(entity, false);
  158.                     //EntityCommandBuffer.RemoveComponent<DisableRendering>(weaponGraphicsEntities.Arms);
  159.                     //EntityCommandBuffer.RemoveComponent<DisableRendering>(weaponGraphicsEntities.Weapon);
  160.                     //EntityCommandBuffer.AddComponent<DisableRendering>(entity);
  161.                 }
  162.                 // Enable muzzle entity
  163.                 if (chunk.Has(ref WeaponMuzzleModelHandle))
  164.                 {
  165.                     var weaponMuzzle = chunk.GetNativeArray(ref WeaponMuzzleModelHandle)[entityIndex];
  166.                     Debug.Log($"Enabling muzzle entity: {weaponMuzzle.MuzzleEntity}");
  167.                     EntityCommandBuffer.SetEnabled(weaponMuzzle.MuzzleEntity, true);
  168.                 }
  169.  
  170.                 // "Equip" the weapon by parenting to player cameraand adding equipped component, and reset its local position//
  171.                 Debug.Log($"Parenting entity {entity} to camera {cameraChild}");
  172.                 EntityCommandBuffer.AddComponent(entity, new Parent{Value = cameraChild });
  173.                 EntityCommandBuffer.SetComponent(entity, LocalTransform.FromPositionRotationScale(playerTransform.Position, quaternion.identity, 1));
  174.  
  175.                 // Add an event to populate it to the player's buffer
  176.                 var newSlotIndex = playerSlots.Length;
  177.                 Debug.Log($"Adding item to slot {newSlotIndex}");
  178.                 playerSlots.Add(new ItemSlot
  179.                 {
  180.                     Index            = newSlotIndex,
  181.                     ItemEntity       = entity,
  182.                     ItemPickupEntity = Entity.Null,
  183.                     Selected         = false
  184.                 });
  185.  
  186.                 // Disable trigger
  187.                 Debug.Log($"Disabling trigger {weaponPickup.WeaponTriggerEntity} and rigidbody {weaponPickup.WeaponRigidbodyEntity}");
  188.                 EntityCommandBuffer.SetEnabled(weaponPickup.WeaponTriggerEntity, false);
  189.                 EntityCommandBuffer.SetEnabled(weaponPickup.WeaponRigidbodyEntity, false);
  190.  
  191.                 // set equipped if no other weapons
  192.                 if (playerSlots.Length.Equals(1))
  193.                 {
  194.                     Debug.Log($"First weapon, setting as selected");
  195.                     var slotArray = playerSlots.AsNativeArray();
  196.                     var slot      = slotArray[0];
  197.                     slot.Selected = true;
  198.                     slotArray[0]  = slot;
  199.                 }
  200.  
  201.                 weaponPickups[entityIndex] = weaponPickup;
  202.                 chunk.SetComponentEnabled(ref EquippedHandle, entityIndex, true);*/
  203.                 Debug.Log($"Completed pickup for entity {entity}");
  204.             }
  205.         }
  206.        
  207.         private bool HasCamera(in DynamicBuffer<Child> childBuffer, out Entity entity)
  208.         {
  209.             Debug.Log($"HasCamera method start");
  210.             entity = Entity.Null;
  211.  
  212.             if (childBuffer.Length == 0)
  213.             {
  214.                 Debug.Log($"No children to find a camera in");
  215.                 return false;
  216.             }
  217.  
  218.             for (var j = 0; j < childBuffer.Length; j++)
  219.             {
  220.                 var child = childBuffer[j].Value;
  221.                
  222.                 var hasComponent = FirstPersonCameraLookup.HasComponent(child);
  223.  
  224.                 if (!hasComponent)
  225.                 {
  226.                     Debug.Log($"Skipping child {child} because it doesn't have a FirstPersonCamera component");
  227.                     continue;
  228.                 }
  229.  
  230.                 Debug.Log($"Found camera in child {child}");
  231.                 entity = child;
  232.                 return true;
  233.             }
  234.  
  235.             Debug.Log($"No camera found after checking all children, returning false");
  236.             return false;
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement