/// <summary>
        /// Creates a new WorldEntity and assigns it to the PlayerEntityManager.
        /// </summary>
        /// <param name="ownerId">The id of the player who will own this new entity.</param>
        /// <param name="activeFormation">The Formation this WorldEntity represents.</param>
        /// <returns></returns>
        public IReadOnlyWorldEntity AssignWorldEntity(Guid ownerId, Formation activeFormation)
        {
            var newManager = _playerEntityManagerFactory.Create(ownerId, this);
            var success    = _playerEntityManagers.TryAdd(ownerId, newManager);
            PlayerEntityManager manager;

            if (success)
            {
                manager = newManager;
                OnPlayerEntityManagerCreated?.Invoke(this, new PlayerEntityManagerCreatedEventArgs
                {
                    CreatedManager = newManager
                });
            }
            else
            {
                _playerEntityManagers.TryGetValue(ownerId, out manager);
            }

            manager.Entity = _worldEntityFactory.Create(ownerId, activeFormation);

            _worldEntities.AddOrUpdate(ownerId, manager.Entity, (id, entity) => manager.Entity);

            return(manager.Entity);
        }
Esempio n. 2
0
        /// <summary>
        /// Spawns a new WorldEntity and creates a new AiEntityManager to control it. Returns the AiEntityManager.
        /// </summary>
        /// <param name="spawnData">Contains data used to create the spawn WorldEntity.</param>
        /// <returns></returns>
        private AiEntityManager SpawnEntity(SpawnEntityData spawnData)
        {
            var spawnedEntity = _worldEntityFactory.Create(spawnData.FormationTemplate);
            var manager       = _aiEntityManagerFactory.Create(spawnedEntity, _mapManager, _mapBattleManager, spawnData);

            manager.RemovedFromMap += OnEntityRemovedFromMap;
            _managers.Add(manager);
            return(manager);
        }