Пример #1
0
    private static bool TryUpdatePosition <T>(T entity, BiDiMap <Vector3, T> dict) where T : Unit
    {
        try
        {
            dict.RemoveBySecond(entity);
        }
        catch (ArgumentException)
        {
            // Do nothing if already missing
        }

        return(dict.TryAdd(entity.Target, entity));
    }
Пример #2
0
 private void initializeDict <T>(BiDiMap <Vector3, T> dict, float layer) where T : Unit
 {
     T[] units = GetComponentsInChildren <T>();
     foreach (T unit in units)
     {
         unit.Snap(layer);
         Vector3 snapped = unit.transform.position;
         T       previousCell;
         bool    previousCellFound = dict.TryGetByFirst(snapped, out previousCell);
         if (previousCellFound)
         {
             Debug.Log("Destroying " + previousCell.gameObject);
             dict.RemoveByFirst(snapped);
             Destroy(previousCell.gameObject);
         }
         dict.Add(snapped, unit);
         unit.OnTargetChangedEvent.AddListener(updatePosition);
         unit.OnDestroyEvent.AddListener(updateDestroyed);
     }
 }