Ejemplo n.º 1
0
        /// <summary>
        /// Tries to initiate battle against one of the WorldEntities provided.
        /// </summary>
        /// <param name="enemies">A list of WorldEntities that this AiEntityManager should try to initiate battle against.</param>
        /// <returns>Returns true if a battle was successfully initiated.</returns>
        private bool TryInitiateBattle(List <WorldEntity> enemies)
        {
            int  i             = 0;
            bool battleSuccess = false;

            while (i < enemies.Count && !battleSuccess)
            {
                if (!_mapBattleManager.TryGetBattle(enemies[i], out IBattleManager battleManager))
                {
                    battleSuccess = _mapBattleManager.CreateBattle(new List <WorldEntity> {
                        _entity
                    },
                                                                   new List <WorldEntity> {
                        enemies[i]
                    });
                }
                i++;
            }

            return(battleSuccess);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a battle for this player and returns the BattleManager responsible for handling this battle.
        /// <para>If a battle already exists, returns the BattleManager for the current battle.</para>
        /// </summary>
        /// <returns></returns>
        private void StartBattle()
        {
            lock (_lock)
            {
                // Player hasn't created a formation or is in combat, prevent from starting a battle
                if (_stateManager.GetPlayerState(PlayerId) != PlayerStateConstants.Free)
                {
                    return;
                }

                if (_contactsQueuedForBattle == null || _contactsQueuedForBattle.Count() <= 0)
                {
                    return;
                }

                // Find original target
                var target = _contactsQueuedForBattle.FirstOrDefault(contact =>
                {
                    return(contact.Id == _targetEntityId && contact.OwnerGuid.ToString() == _targetOwnerId);
                });

                // If target is not in the list of contacts or is already in a battle, cancels trying to start a battle
                if (target == null || _mapBattleManager.TryGetBattle(target, out IBattleManager throwaway))
                {
                    return;
                }

                IEnumerable <WorldEntity> defenders = null;

                // Get all ai WorldEntity contacts and set them as the target defenders
                if (target.OwnerGuid == GameplayConstants.AiId)
                {
                    defenders = _contactsQueuedForBattle.Where(entity =>
                    {
                        if (entity == target)
                        {
                            return(false);
                        }
                        return(entity.OwnerGuid == GameplayConstants.AiId &&
                               !_mapBattleManager.TryGetBattle(entity, out IBattleManager manager));
                    }).Take(GameplayConstants.MaxFormationsPerSide - 1)
                                .Append(target)
                                .ToList();
                }
                // Target is a player, get all of the target player's allies and set them as the defenders
                else
                {
                    var enemyPlayerManager = _playerEntityManagerStore.GetPlayerEntityManager(target.OwnerGuid);
                    if (enemyPlayerManager == null)
                    {
                        return;
                    }

                    defenders = enemyPlayerManager.AlliedEntities.Append(target).ToList();
                    if (defenders.Count() > GameplayConstants.MaxFormationsPerSide)
                    {
                        throw new Exception($"Too many people in the party of player with id {target.OwnerGuid}!");
                    }
                }

                var attackers = AlliedEntities.Append(Entity).ToList();

                var success = _mapBattleManager.CreateBattle(attackers, defenders);
            }
        }