private void ReplaceSelectedIfReceiver(Collider2D collider) { if (collider == null || collider.gameObject == null) { return; } GameEntity entity = GameLinkUtils.GetEntity(collider.gameObject); if (entity == null) { return; } if (!entity.hasReceiver) { return; } if (!ReceiverUtils.Filter(entity.receiver, m_ComponentIndex)) { return; } GameEntity selectedEntity = m_Context.CreateEntity(); selectedEntity.isSelected = true; ReceiverUtils.AddOccupant(entity, selectedEntity.id.value); }
private static void InitializeNavAgentView(GameEntity traveler, NavTilemapController navController) { var travelerObject = GameLinkUtils.GetObject(traveler); var navAgentView = travelerObject.AddComponent <NavAgentComponentView>(); navAgentView.Initialize(); var navComponent = navAgentView.Component; var navAgent = navComponent.agent; navAgent.nav = navController; }
private void OnTriggerEnter2D(Collider2D other) { int otherId = GameLinkUtils.GetId(other.gameObject); if (otherId < 0) { return; } TriggerEnter(otherId); }
public override void Initialize() { if (m_InputObjects != null) { m_Component.inputIds = GameLinkUtils.TryLinkIds(m_InputObjects); } if (m_OutputObjects != null) { m_Component.outputIds = GameLinkUtils.TryLinkIds(m_OutputObjects); } base.Initialize(); }
public void OnBeforeDestroy(GameEntity entity) { GameObject entityObject = GameLinkUtils.GetObject(entity.id.value); DebugUtil.Assert(entityObject != null, "Expected link at before destroy entity=" + entity); if (entityObject == null) { return; } UnityEngine.Object.Destroy(entityObject); }
/// <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> /// During initialization, converts filter component names to indexes. /// Otherwise, if the component indexes changed after editing this view, the indexes would mismatch. /// </summary> public override void Initialize() { InitializeOccupants(); if (m_FilterComponentNames != null) { ToComponentIndexes(m_FilterComponentNames, ref m_Component.filterComponentIndexes); } base.Initialize(); GameEntity receiverEntity = GameLinkUtils.GetEntity(gameObject); receiverEntity.AddReceiverListener(this); }
public void OnSpawn(GameEntity entity, GameObject prefab) { GameObject origin = GameLinkUtils.GetObject(entity.id.value); DebugUtil.Assert(origin != null, "Expected link at spawn entity=" + entity + ". Spawning at world origin. prefab=" + prefab); if (origin == null) { UnityEngine.Object.Instantiate(prefab); return; } GameObject clone = UnityEngine.Object.Instantiate(prefab, origin.transform); clone.transform.SetParent(null, true); }
protected override void Execute(List <GameEntity> entities) { foreach (GameEntity attractor in entities) { var trigger = attractor.triggerEnter; GameEntity traveler = m_Context.GetEntityWithId(trigger.otherId); if (!traveler.hasNavAgent) { InitializeNavAgentView(traveler, attractor.navAgent.agent.nav); } SetDestinationIfIsCloser(traveler.navAgent.agent, GameLinkUtils.GetObject(attractor).transform.position); } }
/// <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; } } } }
protected void TryLink() { if (m_Entity != null) { return; } m_Entity = GameLinkUtils.TryLink(gameObject); if (m_OnDestroyEntity == null) { m_OnDestroyEntity = DestroyObject; } m_Entity.OnDestroyEntity -= m_OnDestroyEntity; m_Entity.OnDestroyEntity += m_OnDestroyEntity; }
private void InitializeOccupants() { Debug.Assert(m_OccupantObjects != null, "Expected Unity Editor serializes occupant objects as an empty collection."); if (m_OccupantObjects == null) { return; } Array.Resize(ref m_Component.occupantIds, m_OccupantObjects.Length); int index = -1; foreach (GameObject occupantObject in m_OccupantObjects) { GameEntity occupantEntity = GameLinkUtils.TryLink(occupantObject); m_Component.occupantIds[++index] = occupantEntity == null ? ReceiverUtils.kEmpty : occupantEntity.id.value; } m_Component.availableIndex = ReceiverUtils.GetNextAvailableIndex(m_Component); }
public void OnReceiver(GameEntity receiver, HashSet <int> newFilterComponentIndexes, int[] newOccupantIds, int newAvailableIndex) { GameLinkUtils.TryAddChildren(receiver.id.value, newOccupantIds, silentIfChildIsUnlinked: true); }