new public void Remove(T removal)
    {
        base.Remove(removal);
        OnRemoval _event = this.removal;

        if (_event != null)
        {
            _event.Invoke(removal);
        }
    }
Beispiel #2
0
        /// <summary>
        /// Removes player from level, with all the buildings and units that belong to him.
        /// Player can be removed either by calling this or by calling the <see cref="ILevelManager.RemovePlayer(IPlayer)"/>.
        /// </summary>
        public void RemoveFromLevel()
        {
            if (IsRemovedFromLevel)
            {
                return;
            }
            IsRemovedFromLevel = true;

            try
            {
                OnRemoval?.Invoke(this);
            }
            catch (Exception e)
            {
                Urho.IO.Log.Write(LogLevel.Warning,
                                  $"There was an unexpected exception during the invocation of {nameof(OnRemoval)}: {e.Message}");
            }
            OnRemoval = null;

            //We need removeFromLevel to work during any phase of loading, where connect references may not have been called yet
            try
            {
                Plugin?.Dispose();
            }
            catch (Exception e)
            {
                Urho.IO.Log.Write(LogLevel.Error, $"Player plugin call {nameof(Plugin.Dispose)} failed with Exception: {e.Message}");
            }

            //Enumerate over copies of the collections, because with each removal the unit will get removed from the live collection.
            foreach (var unitsOfType in units.Values.ToArray())
            {
                foreach (var unit in unitsOfType.ToArray())
                {
                    unit.RemoveFromLevel();
                }
            }

            //Enumerate over copies of the collections, because with each removal the unit will get removed from the live collection.
            foreach (var buildingsOfType in buildings.Values.ToArray())
            {
                foreach (var building in buildingsOfType.ToArray())
                {
                    building.RemoveFromLevel();
                }
            }

            Level.RemovePlayer(this);
            if (!IsDeleted)
            {
                Remove();
                base.Dispose();
            }
        }