/// <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); }
/// <summary> /// Tries to create a new PlayerEntityManager. If successful, returns true and the value of the out /// parameter manager is set to the newly created PlayerEntityManager. /// </summary> /// <param name="ownerId">The id of the user to create a PlayerEntityManager for.</param> /// <param name="manager">The manager that will be created by this function.</param> /// <returns>Returns true if a new PlayerEntityManager was successfully created.</returns> private bool TryCreateManager(Guid ownerId, out PlayerEntityManager manager) { manager = _playerEntityManagerFactory.Create(ownerId, this); var entitySuccess = _worldEntities.TryGetValue(ownerId, out WorldEntity entity); if (entitySuccess) { manager.Entity = entity; } else { manager = null; return(false); } var success = _playerEntityManagers.TryAdd(ownerId, manager); if (success) { OnPlayerEntityManagerCreated?.Invoke(this, new PlayerEntityManagerCreatedEventArgs { CreatedManager = manager }); } return(success); }