/// <summary> /// Gives quantity from giver to receiver without exchanging the entities. /// </summary> public static bool TryGiveQuantity(GameContext context, int[] receiverIds, int[] giverIds) { Log("TryGiveQuantity"); bool gaveAnything = false; GameEntity[] giverEntities = GameLinkUtils.GetEntitiesWithIds(context, giverIds); foreach (GameEntity giverEntity in giverEntities) { int giftId = GetNextOccupantId(giverEntity.receiver); if (giftId == kEmpty) { continue; } GameEntity gift = context.GetEntityWithId(giftId); DebugUtil.Assert(gift != null, "ReceiverUtils.TryOccupy: Expected gift was not null. Gift ID=" + giftId); if (gift == null) { continue; } if (!gift.hasQuantity || gift.quantity.value == 0) { continue; } foreach (int receiverId in receiverIds) { GameEntity receiver = context.GetEntityWithId(receiverId); ReceiverComponent component = receiver.receiver; if (!Filter(component, gift)) { continue; } if (!receiver.hasQuantity) { continue; } receiver.ReplaceQuantity(receiver.quantity.value + gift.quantity.value); gift.ReplaceQuantity(0); gaveAnything = true; break; } } return(gaveAnything); }
/// <summary> /// Receiver might be an input or an output of a transmitter. /// As in a Petri Net, only transmits if all receivers are occupied. /// Unlike a Petri Net, only transmits if to outputs with an empty occupant position. /// Also unlike a Petri Net, preserves the input entity if it would be a suitable output. /// /// Expects each input and output has a receiver, or else Entitas throws an exception. /// </summary> /// /// <param name="giverIds"> /// Selects first acceptable giver or gift in that giver's receiver. Sets that giver ID to empty if selected. /// </param> public static void TryOccupy(GameContext context, int[] receiverIds, int[] giverIds) { Log("TryOccupy"); GameEntity[] giverEntities = GameLinkUtils.GetEntitiesWithIds(context, giverIds); foreach (GameEntity giverEntity in giverEntities) { int giftId = GetNextOccupantId(giverEntity.receiver); if (giftId == kEmpty) { continue; } RemoveOccupant(giverEntity, giftId); GameEntity gift = context.GetEntityWithId(giftId); DebugUtil.Assert(gift != null, "ReceiverUtils.TryOccupy: Expected gift was not null. Gift ID=" + giftId); if (gift == null) { continue; } foreach (int receiverId in receiverIds) { GameEntity receiver = context.GetEntityWithId(receiverId); ReceiverComponent component = receiver.receiver; if (IsFull(component)) { continue; } if (Filter(component, giverEntity)) { AddOccupant(receiver, giverEntity.id.value); break; } if (Filter(component, gift)) { AddOccupant(receiver, giftId); break; } } } }