Exemplo n.º 1
0
 private void registerEvents()
 {
     StartOfTurnEvent.RegisterListener(onStartOfTurn);
     DiceRollEvent.RegisterListener(onDiceRoll);
     PlayerDecisionEvent.RegisterListener(onPlayerAction);
     PlayerWonEvent.RegisterListener(onPlayerWon);
 }
Exemplo n.º 2
0
    private void onStartOfTurn(StartOfTurnEvent data)
    {
        string text      = "<#" + ColorUtility.ToHtmlStringRGB(data.player.GetColor()) + ">Player (" + data.player.GetPlayerID() + ")'s Turn ! </color>";
        var    component = Instantiate(TextComp, transform);

        component.GetComponent <TextComponent>().textToDisplay = text;
        uiQueue.Enqueue(component);
    }
Exemplo n.º 3
0
    private void invokeStartOfTurnEvent()
    {
        StartOfTurnEvent turnEvent = new StartOfTurnEvent()
        {
            player = currentPlayerTurn
        };

        turnEvent.FireEvent();
    }
Exemplo n.º 4
0
        /// <summary>
        /// Starts the turn for a group.
        /// </summary>
        private void StartTurn()
        {
            var affectedEntities   = new List <IReadOnlyCombatEntity>();
            var activatedAbilities = new List <IReadOnlyAbility>();
            var activeEntities     = new List <ActiveEntities>();
            var actionPointData    = new Dictionary <int, IEnumerable <ActionPointData> >();

            lock (_key)
            {
                if (!_isBattleActive)
                {
                    return;
                }

                _battle.IsDefenderTurn = !_battle.IsDefenderTurn;
                IEnumerable <Formation> activeGroup = null;
                if (_battle.IsDefenderTurn)
                {
                    activeGroup = _battle.Defenders;
                }
                else
                {
                    activeGroup = _battle.Attackers;
                }

                List <DelayedAbility> startOfTurnAbilities = new List <DelayedAbility>();
                List <DelayedAbility> delayedAbilities     = null;
                if (_battle.IsDefenderTurn)
                {
                    delayedAbilities = _battle.DefenderDelayedAbilities;
                }
                else
                {
                    delayedAbilities = _battle.AttackerDelayedAbilities;
                }

                // Decrement turns left and queues delayed abilities to be activated
                for (int i = delayedAbilities.Count() - 1; i >= 0; i++)
                {
                    delayedAbilities[i].TurnsLeft--;
                    if (delayedAbilities[i].TurnsLeft == 0)
                    {
                        startOfTurnAbilities.Add(delayedAbilities[i]);
                        delayedAbilities.RemoveAt(i);
                    }
                }

                // Activates delayed abilities
                if (startOfTurnAbilities != null && startOfTurnAbilities.Count() > 0)
                {
                    foreach (var ability in startOfTurnAbilities)
                    {
                        var entities = _abilityManager.PerformDelayedAbility(ability);
                        affectedEntities.AddRange(entities);
                        activatedAbilities.Add(ability.BaseAbility);
                    }
                }

                // Increase action points, choose active entities, and apply status effects to all formations that are active
                foreach (var formation in activeGroup)
                {
                    affectedEntities.AddRange(ApplyStatusEffects(formation));

                    var data = IncreaseActionPoints(formation);
                    actionPointData.Add(formation.Id, data);

                    var actives = ChooseActiveEntities(formation);
                    activeEntities.Add(new ActiveEntities
                    {
                        EntityIds   = actives.Select(ae => ae.Id).ToList(),
                        FormationId = formation.Id,
                        OwnerId     = formation.OwnerId
                    });
                    _battle.ActionsLeftPerFormation.Add(formation, actives);
                }
                affectedEntities       = affectedEntities.Distinct().ToList();
                _battle.TurnExpiration = DateTime.Now.AddSeconds(GameplayConstants.SecondsPerTurn);

                if (!_battle.IsDefenderTurn)
                {
                    _battle.Round++;
                }
            }

            Task.Run(() => StartOfTurnEvent?.Invoke(this, new StartOfTurnEventArgs
            {
                DelayedAbilities = activatedAbilities,
                AffectedEntities = affectedEntities,
                ActionPointData  = actionPointData,
                TurnExpiration   = _battle.TurnExpiration,
                ActiveEntities   = activeEntities,
                IsDefendersTurn  = _battle.IsDefenderTurn,
                ParticipantIds   = _participantIds
            }));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Starts a Battle instance between the attackers and defenders.
        /// <para>Returns null if a Battle instance already exists for this manager.</para>
        /// </summary>
        /// <param name="attackers">The WorldEntities who initiated combat.</param>
        /// <param name="defenders">The WorldEntities who are being initiated on.</param>
        /// <returns></returns>
        public IReadOnlyBattle StartBattle(List <WorldEntity> attackers, List <WorldEntity> defenders)
        {
            Battle battle = null;
            List <ActiveEntities> activeEntities = null;

            lock (_key)
            {
                if (_battle != null)
                {
                    return(null);
                }
                if (_isBattleActive)
                {
                    return(null);
                }

                _participantIds = attackers.Union(defenders)
                                  .Where(entity => entity.OwnerGuid != GameplayConstants.AiId)
                                  .Select(entity => entity.OwnerGuid.ToString())
                                  .ToList();

                _aiParticipantIds = attackers.Union(defenders)
                                    .Where(entity => entity.OwnerGuid == GameplayConstants.AiId)
                                    .Select(entity => entity.Id)
                                    .ToList();

                var attackingFormations = attackers.Select(entity => entity.ActiveFormation)
                                          .ToList();
                var defendingFormations = defenders.Select(entity => entity.ActiveFormation)
                                          .ToList();

                var actionsPerFormation = new Dictionary <Formation, List <CombatEntity> >();
                activeEntities = new List <ActiveEntities>();
                foreach (var attacker in attackingFormations)
                {
                    InitializeFormation(attacker, true);
                    var activeE = ChooseActiveEntities(attacker);
                    actionsPerFormation.Add(attacker, activeE);
                    activeEntities.Add(new ActiveEntities
                    {
                        EntityIds   = activeE.Select(e => e.Id).ToList(),
                        FormationId = attacker.Id,
                        OwnerId     = attacker.OwnerId
                    });
                }

                foreach (var defender in defendingFormations)
                {
                    InitializeFormation(defender, false, true);
                }

                var nextTurnStartDate = DateTime.Now.AddSeconds(GameplayConstants.SecondsPerTurn);

                battle = new Battle
                {
                    Attackers               = attackingFormations,
                    Defenders               = defendingFormations,
                    TurnExpiration          = nextTurnStartDate,
                    Round                   = 1,
                    ActionsLeftPerFormation = actionsPerFormation,
                    IsDefenderTurn          = false
                };

                _battle         = battle;
                _isBattleActive = true;
            }

            Task.Run(() => StartOfTurnEvent?.Invoke(this, new StartOfTurnEventArgs
            {
                TurnExpiration  = battle.TurnExpiration,
                ActiveEntities  = activeEntities,
                IsDefendersTurn = false,
                ParticipantIds  = _participantIds
            }));

            return(battle);
        }