Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Junk.Cameras;
- using Unity.Assertions;
- using Unity.Burst;
- using Unity.Burst.Intrinsics;
- using Unity.Entities;
- using Unity.Mathematics;
- using Unity.Rendering;
- using Unity.Transforms;
- using UnityEngine;
- namespace Junk.FirstPerson
- {
- //
- //
- //
- //
- //
- //
- //
- //
- //
- //
- //
- // something in this causes built player burst crash
- //
- //
- //
- //
- //
- //
- //
- //
- //
- [BurstCompile]
- public struct AddToPlayerInventoryJob : IJobChunk
- {
- public EntityCommandBuffer EntityCommandBuffer;
- public float DeltaTime;
- public EntityTypeHandle EntityHandle;
- public ComponentLookup<MaterialMeshInfo> MaterialMeshInfoLookup;
- public ComponentTypeHandle<ItemPickup> WeaponPickupHandle;
- public ComponentTypeHandle<Equipped> EquippedHandle;
- // optional chunk components
- public ComponentTypeHandle<WeaponGraphicsEntities> WeaponGraphicsEntitiesHandle;
- public ComponentTypeHandle<WeaponMuzzleModel> WeaponMuzzleModelHandle;
- // Player components
- public Entity PlayerEntity;
- public ComponentLookup<LocalTransform> LocalTransformLookup;
- public ComponentLookup<PlayerItemsInventory> PlayerItemsInventoryLookup;
- public BufferLookup<ItemSlot> ItemSlotBufferLookup;
- public BufferLookup<Child> ChildBufferLookup;
- public ComponentLookup<FirstPersonCamera> FirstPersonCameraLookup;
- public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
- {
- Assert.IsTrue(ChildBufferLookup.HasBuffer(PlayerEntity));
- Assert.IsTrue(LocalTransformLookup.HasComponent(PlayerEntity));
- Assert.IsTrue(PlayerItemsInventoryLookup.HasComponent(PlayerEntity));
- Assert.IsTrue(ItemSlotBufferLookup.HasBuffer(PlayerEntity));
- var playerChildren = ChildBufferLookup[PlayerEntity];
- var playerTransform = LocalTransformLookup[PlayerEntity];
- var playerInventory = PlayerItemsInventoryLookup[PlayerEntity];
- var playerSlots = ItemSlotBufferLookup[PlayerEntity];
- var entities = chunk.GetNativeArray(EntityHandle);
- var weaponPickups = chunk.GetNativeArray(ref WeaponPickupHandle);
- var entityEnumerator = new ChunkEntityEnumerator(useEnabledMask, chunkEnabledMask, chunk.Count);
- while (entityEnumerator.NextEntityIndex(out var entityIndex))
- {
- var entity = entities[entityIndex];
- var weaponPickup = weaponPickups[entityIndex];
- // Cant operate without a camera
- if (playerChildren.Length < 1)
- {
- Debug.Log($"playerChildren buffer length: {playerChildren.Length}");
- continue;
- }
- //
- // < --- burst crash here --- >
- // Player.log exerpt
- // Streamed scene with 215ms latency from C:/Users/thele/Documents/Repos/cyberjunk/bin/Pain Meat Pain_Data/StreamingAssets/EntityScenes/a4185ca62cee3a144826b19ec2b9d207.0.entities
- // playerChildren buffer length: 0
- // playerChildren buffer length: 0
- // Crash!!!
- if (playerChildren.Length < 1)
- Debug.Log($"playerChildren.Length < 1, continue ");
- Debug.Log($"Checking Camera");
- if (!HasCamera(playerChildren, out var cameraChild))
- {
- Debug.Log($"No camera found in playerChildren, continue");
- continue;
- }
- Debug.Log($"Camera found");
- /*
- // this is a bit of a janky check for a camera, should have a component with camera entity on it
- // disable this?
- if (playerChildren.Length < 1)
- {
- Debug.Log($"playerChildren.Length < 1, continue ");
- continue;
- }
- Debug.Log($"playerChildren.Length: {playerChildren.Length}");
- // Dont execute if maxed out inventory
- if(playerSlots.Length > playerInventory.MaxWeapons)
- {
- Debug.Log($"Inventory maxed out: {playerSlots.Length} > {playerInventory.MaxWeapons}, continue");
- continue;
- }
- // destroy event if entity already inside buffer
- var containedAlready = false;
- for (var j = 0; j < playerSlots.Length; j++)
- {
- if (playerSlots[j].ItemEntity.Equals(entity))
- {
- Debug.Log($"containedAlready");
- containedAlready = true;
- break;
- }
- }
- if (containedAlready)
- {
- Debug.Log($"Entity {entity} already in inventory, disabling pickup");
- weaponPickup = default;
- //weaponPickups[entityIndex] = weaponPickup; ??
- chunk.SetComponentEnabled(ref WeaponPickupHandle, entityIndex, false);
- continue;
- }
- Debug.Log($"Entity {entity} not in inventory, proceeding with pickup");
- weaponPickup.Player = PlayerEntity;
- Debug.Log($"Set weaponPickup.Player to {PlayerEntity}");
- // Set graphics status
- // Enable the arms model entity
- if (chunk.Has(ref WeaponGraphicsEntitiesHandle))
- {
- var weaponGraphicsEntities = chunk.GetNativeArray(ref WeaponGraphicsEntitiesHandle)[entityIndex];
- Debug.Log($"Enabling weapon graphics: Arms={weaponGraphicsEntities.Arms}, Weapon={weaponGraphicsEntities.Weapon}");
- MaterialMeshInfoLookup.SetComponentEnabled(weaponGraphicsEntities.Arms, true);
- MaterialMeshInfoLookup.SetComponentEnabled(weaponGraphicsEntities.Weapon, true);
- MaterialMeshInfoLookup.SetComponentEnabled(entity, false);
- //EntityCommandBuffer.RemoveComponent<DisableRendering>(weaponGraphicsEntities.Arms);
- //EntityCommandBuffer.RemoveComponent<DisableRendering>(weaponGraphicsEntities.Weapon);
- //EntityCommandBuffer.AddComponent<DisableRendering>(entity);
- }
- // Enable muzzle entity
- if (chunk.Has(ref WeaponMuzzleModelHandle))
- {
- var weaponMuzzle = chunk.GetNativeArray(ref WeaponMuzzleModelHandle)[entityIndex];
- Debug.Log($"Enabling muzzle entity: {weaponMuzzle.MuzzleEntity}");
- EntityCommandBuffer.SetEnabled(weaponMuzzle.MuzzleEntity, true);
- }
- // "Equip" the weapon by parenting to player cameraand adding equipped component, and reset its local position//
- Debug.Log($"Parenting entity {entity} to camera {cameraChild}");
- EntityCommandBuffer.AddComponent(entity, new Parent{Value = cameraChild });
- EntityCommandBuffer.SetComponent(entity, LocalTransform.FromPositionRotationScale(playerTransform.Position, quaternion.identity, 1));
- // Add an event to populate it to the player's buffer
- var newSlotIndex = playerSlots.Length;
- Debug.Log($"Adding item to slot {newSlotIndex}");
- playerSlots.Add(new ItemSlot
- {
- Index = newSlotIndex,
- ItemEntity = entity,
- ItemPickupEntity = Entity.Null,
- Selected = false
- });
- // Disable trigger
- Debug.Log($"Disabling trigger {weaponPickup.WeaponTriggerEntity} and rigidbody {weaponPickup.WeaponRigidbodyEntity}");
- EntityCommandBuffer.SetEnabled(weaponPickup.WeaponTriggerEntity, false);
- EntityCommandBuffer.SetEnabled(weaponPickup.WeaponRigidbodyEntity, false);
- // set equipped if no other weapons
- if (playerSlots.Length.Equals(1))
- {
- Debug.Log($"First weapon, setting as selected");
- var slotArray = playerSlots.AsNativeArray();
- var slot = slotArray[0];
- slot.Selected = true;
- slotArray[0] = slot;
- }
- weaponPickups[entityIndex] = weaponPickup;
- chunk.SetComponentEnabled(ref EquippedHandle, entityIndex, true);*/
- Debug.Log($"Completed pickup for entity {entity}");
- }
- }
- private bool HasCamera(in DynamicBuffer<Child> childBuffer, out Entity entity)
- {
- Debug.Log($"HasCamera method start");
- entity = Entity.Null;
- if (childBuffer.Length == 0)
- {
- Debug.Log($"No children to find a camera in");
- return false;
- }
- for (var j = 0; j < childBuffer.Length; j++)
- {
- var child = childBuffer[j].Value;
- var hasComponent = FirstPersonCameraLookup.HasComponent(child);
- if (!hasComponent)
- {
- Debug.Log($"Skipping child {child} because it doesn't have a FirstPersonCamera component");
- continue;
- }
- Debug.Log($"Found camera in child {child}");
- entity = child;
- return true;
- }
- Debug.Log($"No camera found after checking all children, returning false");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement