protected override void OnUpdate() { Entities .WithAll <InventoryItemReference>() .ForEach((Entity entity, DynamicBuffer <StartingInventoryItem> startingInventoryBuffer) => { NativeArray <StartingInventoryItem> startingInventory = startingInventoryBuffer.ToNativeArray(Allocator.Temp); NativeArray <(Entity item, int stack)> itemTransfers = new NativeArray <(Entity item, int stack)>(startingInventoryBuffer.Length, Allocator.Temp); var random = World.Random(); for (int i = 0; i < startingInventory.Length; i++) { var itemPrefab = _itemBankSystem.GetItemPrefab(startingInventory[i].ItemAssetId); itemTransfers[i] = (itemPrefab, random.NextInt(startingInventory[i].StacksMin, startingInventory[i].StacksMax)); } ItemTransationBatch itemTransationBatch = new ItemTransationBatch() { Source = null, Destination = entity, ItemsAndStacks = itemTransfers, OutResults = default }; CommonWrites.ExecuteItemTransaction(Accessor, itemTransationBatch); // Remove starting inventory buffer EntityManager.RemoveComponent <StartingInventoryItem>(entity); }) .WithoutBurst() .WithStructuralChanges() .Run(); }
public static bool RequestUseItem(ISimGameWorldReadWriteAccessor accessor, Entity actor, Entity item, GameAction.UseParameters parameters = null) { if (!CommonReads.CanUseItem(accessor, actor, item, out ItemUnavailablityReason unavailabilityReason)) { Log.Warning($"Instigator {accessor.GetNameSafe(actor)} cannot use item {accessor.GetNameSafe(item)} because of {unavailabilityReason}"); return(false); } accessor.TryGetComponent(item, out ItemAction itemAction); CommonWrites.RequestExecuteGameAction(accessor, item, itemAction, parameters); // reduce consumable amount if (accessor.GetComponent <StackableFlag>(item)) { CommonWrites.DecrementItem(accessor, item, actor); } // reduce instigator AP if (accessor.TryGetComponent(item, out ItemSettingAPCost itemActionPointCost) && itemActionPointCost.Value != 0) { CommonWrites.ModifyStatFix <ActionPoints>(accessor, actor, -itemActionPointCost.Value); } // Cooldown if (accessor.TryGetComponent(item, out ItemTimeCooldownData itemTimeCooldownData)) { accessor.SetOrAddComponent(item, new ItemCooldownTimeCounter() { Value = itemTimeCooldownData.Value }); } return(true); }
private void ExecuteEquipItemInput(PawnControllerInputEquipItem pawnInputEquipItem, Entity pawn) { // Pawn has inventory ? if (!EntityManager.HasComponent <InventoryItemReference>(pawn)) { return; } // Find chest inventory Entity chestEntity = pawnInputEquipItem.ChestEntity; if (!EntityManager.HasComponent <InventoryItemReference>(chestEntity)) { return; } // Get item buffer DynamicBuffer <InventoryItemReference> chestInventory = GetBuffer <InventoryItemReference>(chestEntity); if (chestInventory.Length <= pawnInputEquipItem.ItemIndex) { return; } Entity item = chestInventory[pawnInputEquipItem.ItemIndex].ItemEntity; CommonWrites.MoveItemAll(Accessor, item, source: chestEntity, destination: pawn); }
protected override void OnUpdate() { Entities .WithoutBurst() .WithStructuralChanges() .ForEach((Entity entity, ref Health health, ref FixTranslation translation, ref ExplodeOnDeath explodeOnDeath) => { if (health.Value <= 0) { CommonWrites.RequestExplosion(Accessor, entity, translation.Value, explodeOnDeath.Radius, explodeOnDeath.Damage, explodeOnDeath.DestroyTiles); } }).Run(); }
private void ExecuteUseItemInput(PawnControllerInputUseItem inputUseItem, Entity pawn) { void LogDiscardReason(string str) { Log.Info($"Discarding {inputUseItem.PawnController}'s input : {str}"); } if (!EntityManager.TryGetBuffer(pawn, out DynamicBuffer <InventoryItemReference> inventory)) { LogDiscardReason($"Pawn has no {nameof(DynamicBuffer<InventoryItemReference>)}."); return; } if (inputUseItem.ItemIndex < 0 || inputUseItem.ItemIndex >= inventory.Length) { LogDiscardReason($"Item {inputUseItem.ItemIndex} is out of range ({inventory.Length})."); return; } Entity item = inventory[inputUseItem.ItemIndex].ItemEntity; CommonWrites.RequestUseItem(Accessor, pawn, item, inputUseItem.GameActionData); }
protected override void OnUpdate() { // Clear Damage Applied Events GetSingletonBuffer <EventExplosion>().Clear(); DynamicBuffer <SystemRequestExplosion> explosionRequestBuffer = GetSingletonBuffer <SystemRequestExplosion>(); if (explosionRequestBuffer.Length > 0) { NativeArray <SystemRequestExplosion> requests = explosionRequestBuffer.ToNativeArray(Allocator.Temp); explosionRequestBuffer.Clear(); NativeList <DistanceHit> hits = new NativeList <DistanceHit>(Allocator.Temp); foreach (SystemRequestExplosion request in requests) { if (request.DestroyTiles) { DestroyTiles(request.Position, request.Radius); } if (CommonReads.Physics.OverlapCircle(Accessor, request.Position, request.Radius, hits)) { CommonWrites.RequestDamage(Accessor, hits, request.Damage); if (request.Damage > 0) { // request impulses for every hit foreach (DistanceHit hit in hits) { if (!HasComponent <PhysicsVelocity>(hit.Entity)) { continue; } _applyImpulseSystem.RequestImpulseRadial(new RadialImpulseRequestData() { StrengthMin = IMPULSE_MIN, StrengthMax = IMPULSE_MAX, IgnoreMass = false, Position = request.Position, Radius = request.Radius, Target = hit.Entity }); } } } _newExplosionEvents.Add(new EventExplosion() { Position = request.Position, Radius = request.Radius }); } var explosionEvents = GetSingletonBuffer <EventExplosion>(); foreach (EventExplosion evnt in _newExplosionEvents) { explosionEvents.Add(evnt); } _newExplosionEvents.Clear(); } }
protected override void OnUpdate() { Entities .ForEach((Entity portalEntity, in Portal portalData, in FixTranslation portalPos) => { fix2 portalPosition = portalPos.Value; fix2 portalNextPosition = portalData.NextPos; if (EntityManager.TryGetBuffer(portalEntity, out DynamicBuffer <EntitiesInsidePortalBufferData> entitiesInsidePortal) && EntityManager.TryGetBuffer(portalEntity, out DynamicBuffer <EntitiesTeleportedByPortalBufferData> entitiesTeleported) && EntityManager.TryGetBuffer(portalData.NextPortal, out DynamicBuffer <EntitiesInsidePortalBufferData> entitiesInsideNextPortal) && EntityManager.TryGetBuffer(portalData.NextPortal, out DynamicBuffer <EntitiesTeleportedByPortalBufferData> nextPortalTeleportedEntities)) { // when entity inside a portal, if we didn't just teleported it and was not teleported by other portal, teleport Entities .ForEach((Entity entity, ref FixTranslation entityPos) => { if (HasComponent <PhysicsVelocity>(entity)) { if (IsInsidePortal(entity, entitiesInsidePortal) && !WasTeleported(entity, nextPortalTeleportedEntities) && !WasTeleported(entity, entitiesTeleported)) { int entityIndex = GetIndexOfEntity(entity, entitiesTeleported); if (entityIndex == -1) { entitiesTeleported.Add(new EntitiesTeleportedByPortalBufferData() { entity = entity }); CommonWrites.RequestTeleport(Accessor, entity, portalNextPosition); } } } }) .WithoutBurst() .Run(); // Update entities inside portal Entities .ForEach((Entity entity, ref FixTranslation entityPos) => { if (HasComponent <PhysicsVelocity>(entity)) { fix2 pawnPosition = entityPos.Value; if (IsInsidePortalPositionRange(portalPosition, pawnPosition)) { // we're inside the portal, add us to the list if not already there int entityIndex = GetIndexOfEntity(entity, entitiesInsidePortal); if (entityIndex == -1) { entitiesInsidePortal.Add(new EntitiesInsidePortalBufferData() { entity = entity }); } } else { // we're not in the portal anymore, remove us if found int entityIndex = GetIndexOfEntity(entity, entitiesInsidePortal); if (entityIndex >= 0) { entitiesInsidePortal.RemoveAt(entityIndex); } // we exited the portal without teleporting, this means the other portal can now teleport us if (!WasTeleported(entity, entitiesTeleported)) { int entityIndexNextPortal = GetIndexOfEntity(entity, nextPortalTeleportedEntities); if (entityIndexNextPortal >= 0) { nextPortalTeleportedEntities.RemoveAt(entityIndexNextPortal); } } } } }) .WithoutBurst() .Run(); } })
// Add new cheat classes here! public void HandleCheat(SimCheatInput cheat) { switch (cheat) { case SimInputCheatToggleInvincible toggleInvicible: { Entity groupHeader = GetSingletonEntity <PlayerGroupDataTag>(); if (EntityManager.Exists(groupHeader)) { const int DISTANT_FUTURE = 9999999; if (TryGetComponent(groupHeader, out InvincibleUntilTime invincibleUntilTime)) { EntityManager.AddComponentData(groupHeader, new InvincibleUntilTime() { Time = invincibleUntilTime.Time == DISTANT_FUTURE ? 0 : DISTANT_FUTURE }); } else { EntityManager.AddComponentData(groupHeader, new InvincibleUntilTime() { Time = DISTANT_FUTURE }); } } break; } case SimInputCheatDamageSelf damagePlayer: { Entity player = CommonReads.FindPlayerEntity(Accessor, damagePlayer.PlayerId); if (EntityManager.Exists(player) && EntityManager.TryGetComponentData(player, out ControlledEntity pawn)) { if (damagePlayer.Damage > 0) { CommonWrites.RequestDamage(Accessor, pawn, damagePlayer.Damage); } else { CommonWrites.RequestHeal(Accessor, pawn, -damagePlayer.Damage); } } break; } case SimInputCheatAddAllItems addAllItems: { Entity player = CommonReads.FindPlayerEntity(Accessor, addAllItems.PlayerId); if (EntityManager.Exists(player) && EntityManager.TryGetComponentData(player, out ControlledEntity pawn)) { if (HasComponent <InventoryCapacity>(pawn)) { SetComponent <InventoryCapacity>(pawn, 999); } var itemBankSystem = Accessor.GetExistingSystem <GlobalItemBankSystem>(); var allItems = itemBankSystem.GetAllItemPrefabs(); NativeArray <(Entity item, int stack)> itemTransfers = new NativeArray <(Entity item, int stack)>(allItems.Length, Allocator.Temp); for (int i = 0; i < allItems.Length; i++) { itemTransfers[i] = (allItems[i], 99); } ItemTransationBatch itemTransationBatch = new ItemTransationBatch() { Source = null, Destination = pawn, ItemsAndStacks = itemTransfers, OutResults = default }; CommonWrites.ExecuteItemTransaction(Accessor, itemTransationBatch); } break; } case SimInputCheatTeleport teleport: { Entity player = CommonReads.FindPlayerEntity(Accessor, teleport.PlayerId); if (EntityManager.Exists(player) && EntityManager.TryGetComponentData(player, out ControlledEntity pawn)) { CommonWrites.RequestTeleport(Accessor, pawn, teleport.Destination); } break; } case SimInputCheatRemoveAllCooldowns _: { if (HasSingleton <NoCooldownTag>()) { EntityManager.DestroyEntity(GetSingletonEntity <NoCooldownTag>()); } else { Entity entity = EntityManager.CreateEntity(); EntityManager.AddComponentData(entity, new NoCooldownTag()); } break; } case SimInputCheatImpulseSelf impulseSelf: { Entity player = CommonReads.FindPlayerEntity(Accessor, impulseSelf.PlayerId); if (EntityManager.Exists(player) && EntityManager.TryGetComponentData(player, out ControlledEntity pawn)) { PhysicsVelocity vel = EntityManager.GetComponentData <PhysicsVelocity>(pawn); vel.Linear += impulseSelf.ImpulseValue; SetComponent <PhysicsVelocity>(pawn, vel); } break; } case SimInputCheatSoloPlay soloPlay: { Entity player = CommonReads.FindPlayerEntity(Accessor, soloPlay.PlayerId); if (!HasComponent <ControlledEntity>(player)) { return; } // _________________________________________ Find Pawns _________________________________________ // Entity currentPawn = Entity.Null; Entity newPawn = Entity.Null; int newPawnIndex = soloPlay.PawnIndex; if (EntityManager.TryGetComponentData(player, out ControlledEntity pawn)) { currentPawn = pawn; } Entities.ForEach((Entity entity, in PlayerGroupMemberIndex memberIndex) => { if (memberIndex == newPawnIndex) { newPawn = entity; } }).Run(); // _________________________________________ Update Possession _________________________________________ // if (currentPawn != Entity.Null) { SetComponent <Controllable>(currentPawn, default); } if (newPawn != Entity.Null) { SetComponent <Controllable>(newPawn, player); } SetComponent <ControlledEntity>(player, newPawn); // _________________________________________ Disable Auto Attack on Others _________________________________________ // Entities.WithAll <PlayerGroupMemberIndex>() .ForEach((Entity entity, DynamicBuffer <InventoryItemReference> inventory) => { // implementation note: Keep 'EntityManager.HasComponent', otherwise it will cause an "invalidated by a structural change" exception var items = inventory.ToNativeArray(Allocator.Temp); if (entity != newPawn) { foreach (var item in items) { if (EntityManager.HasComponent <AutoAttackProgress>(item.ItemEntity)) { EntityManager.RemoveComponent <AutoAttackProgress>(item.ItemEntity); } } } else { foreach (var item in items) { if (!EntityManager.HasComponent <AutoAttackProgress>(item.ItemEntity) && EntityManager.HasComponent <ShouldAutoAttack>(item.ItemEntity)) { EntityManager.AddComponent <AutoAttackProgress>(item.ItemEntity); } } } }).WithoutBurst() .WithStructuralChanges() .Run(); break; } } }