Exemplo n.º 1
0
        public void Tick()
        {
            Vector2Int currentMousePosition = LevelGrid.MouseToGridCoordinates();

            GameplayEntity selectedPlayerEntity = m_manager.GetEntityAtPosition(currentMousePosition, m_selectablePlayer);

            if (Input.GetMouseButtonDown(0) &&
                selectedPlayerEntity != null &&
                selectedPlayerEntity != m_activeEntity &&
                !selectedPlayerEntity.Visual.AttackTargetSelection.activeSelf)
            {
                m_activeEntity?.Visual.ShowSelection(false);

                m_activeEntity = selectedPlayerEntity;

                m_activeEntity.Visual.ShowSelection(true);

                if (m_activeEntity.TurnState.CanAttack)
                {
                    m_DropdownOptions.Clear();
                    for (int i = 0; i < m_activeEntity.Skills.Count; i++)
                    {
                        m_DropdownOptions.Add(new Dropdown.OptionData(m_activeEntity.Skills[i].Name));
                    }

                    m_skillSelected = 0;
                    Dropdown dropdown = m_manager.Ui.SkillsDropdown;
                    dropdown.ClearOptions();
                    dropdown.AddOptions(m_DropdownOptions);
                    dropdown.value = m_skillSelected;

                    dropdown.RefreshShownValue();

                    CanvasGroup canvasGroup = dropdown.GetComponentInChildren <CanvasGroup>();//<-fix for unity bug
                    if (canvasGroup != null)
                    {
                        canvasGroup.alpha = 1;
                    }

                    m_manager.Ui.SkillsDropdownCanvasGroup.alpha = 1;
                    UpdateSkillTarget();
                }
                else
                {
                    m_manager.Ui.SkillsDropdownCanvasGroup.alpha = 0;
                    ToggleTarget(false);
                    m_targetableEntities.Clear();
                }

                m_forceRepath = true;
                return;
            }

            if (m_activeEntity != null)
            {
                if (m_activeEntity.TurnState.CanAttack)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        GameplayEntity entity = m_manager.GetEntityAtPosition(currentMousePosition, m_targetableEntities);
                        if (entity != null)
                        {
                            //reset
                            m_manager.Service.HideAllBreadCrumbs();
                            ToggleTarget(false);
                            //attack state
                            m_activeEntity.Visual.ShowSelection(false);
                            m_activeEntity.Skills[m_skillSelected].Execute(entity);
                            m_activeEntity.TurnState.CanAttack = false;
                            ShouldEnd = true;
                            return;
                        }
                    }
                }

                if (m_activeEntity.TurnState.CanMove)
                {
                    if (m_forceRepath || currentMousePosition != m_previousMouse)
                    {
                        m_manager.Pathfinder.Navigate(m_activeEntity.Visual.GridPosition, currentMousePosition, m_pathBuffer);

                        m_activeEntity.ValidateMovement(m_pathBuffer);

                        m_manager.Service.HideAllBreadCrumbs();

                        for (int i = 1; i < m_pathBuffer.Count; i++)
                        {
                            Vector2Int coord = m_pathBuffer[i];
                            m_manager.Service.ShowBreadCrumb(coord.x, coord.y, true, i * .05f);
                        }

                        m_previousMouse = currentMousePosition;
                        m_forceRepath   = false;
                    }

                    if (Input.GetMouseButtonDown(0) && m_pathBuffer.Count >= 2)
                    {
                        //move
                        ToggleTarget(false);
                        m_activeEntity.Visual.ShowSelection(false);
                        m_manager.StateManager.RegisterState <EntityMoveState>().Move(m_activeEntity, m_pathBuffer);
                        m_activeEntity.TurnState.CanMove = false;
                        ShouldEnd = true;
                        return;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Begin()
        {
            if (m_manager.PlayerEntities.Count == 0)
            {
                //no target left skip turn
                SkipTurn();
                return;
            }
            for (int i = 0; i < m_manager.EnemiesEntities.Count; i++)
            {
                GameplayEntity entity = m_manager.EnemiesEntities[i];
                if (entity.Status.Health > 0)
                {
                    //get closest enemy
                    if (entity.TurnState.CanAttack)
                    {
                        int randomness = Random.Range(0, entity.Skills.Count);

                        m_alternateAttacks.Clear();

                        for (int j = 0; j < entity.Skills.Count; j++)
                        {
                            ISkill skill = entity.Skills[(j + randomness) % entity.Skills.Count];

                            skill.GetValidTargets(m_targetableEntities);

                            //filter targets
                            for (int k = 0; k < m_targetableEntities.Count; k++)
                            {
                                GameplayEntity targetEntity = m_targetableEntities[k];

                                if (skill.Type == ESkillType.Offensive &&
                                    targetEntity.Visual.Type == EntityType.Enemy)
                                {
                                    m_targetableEntities.RemoveAt(k);
                                    k--;
                                }
                                else if (targetEntity.Visual.Type == EntityType.Obstacle)
                                {
                                    m_alternateAttacks.Add(new KeyValuePair <ISkill, GameplayEntity>(skill, targetEntity));
                                    m_targetableEntities.RemoveAt(k);
                                    k--;
                                }
                            }

                            if (m_targetableEntities.Count > 0)
                            {
                                GameplayEntity bestTarget = m_targetableEntities[0];
                                for (int k = 1; k < m_targetableEntities.Count; k++)
                                {
                                    GameplayEntity target = m_targetableEntities[k];
                                    if (target.Status.Health < bestTarget.Status.Health)
                                    {
                                        bestTarget = target;
                                    }
                                }

                                //do attack here
                                skill.Execute(bestTarget);
                                entity.TurnState.CanAttack = false;
                                return;
                            }
                        }

                        if (!entity.TurnState.CanMove)
                        {
                            bool noPathToPlayer = true;
                            for (int j = 0; j < m_manager.PlayerEntities.Count; j++)
                            {
                                if (m_manager.Pathfinder.Navigate(entity.Visual.GridPosition, m_manager.PlayerEntities[j].Visual.GridPosition, m_pathBuffer))
                                {
                                    noPathToPlayer = false;
                                    break;
                                }
                            }

                            if (noPathToPlayer)
                            {
                                //no enemies in target, attack whatever
                                if (m_alternateAttacks.Count > 0)
                                {
                                    KeyValuePair <ISkill, GameplayEntity> alternate = m_alternateAttacks[Random.Range(0, m_alternateAttacks.Count)];

                                    alternate.Key.Execute(alternate.Value);
                                    entity.TurnState.CanAttack = false;
                                    return;
                                }
                            }
                        }
                    }

                    if (entity.TurnState.CanMove)
                    {
                        GameplayEntity bestTarget = m_manager.PlayerEntities[0];
                        m_manager.Pathfinder.Navigate(entity.Visual.GridPosition, bestTarget.Visual.GridPosition, m_bestPathBuffer);

                        for (int j = 1; j < m_manager.PlayerEntities.Count; j++)
                        {
                            GameplayEntity target = m_manager.PlayerEntities[j];
                            m_manager.Pathfinder.Navigate(entity.Visual.GridPosition, target.Visual.GridPosition, m_pathBuffer);

                            if (m_pathBuffer.Count < m_bestPathBuffer.Count)
                            {
                                m_bestPathBuffer.Clear();
                                m_bestPathBuffer.AddRange(m_pathBuffer);
                            }
                        }

                        entity.TurnState.CanMove = false;

                        if (m_bestPathBuffer.Count >= 2)
                        {
                            entity.ValidateMovement(m_bestPathBuffer);
                            m_manager.StateManager.RegisterState <EntityMoveState>().Move(entity, m_bestPathBuffer);
                            return;
                        }
                    }
                }
            }

            SkipTurn();
        }