Example #1
0
 void OnDestroy()
 {
     NeverdawnCamera.RemoveTargetLerped(targetTransform);
 }
Example #2
0
    public void UpdateExplorationControls()
    {
        if (character != null)
        {
            if (currentAbility != null)
            {
                updateAbility();
                return;
            }

            if (inputModule.GetAxisUp(NeverdawnInputAxis.Trigger, NeverdawnInputAxisDirection.Positive))
            {
                if (quickSlotsPage != null)
                {
                    quickSlotsPage = null;
                    characterMenu.Close();
                }
            }



            if (inputModule.GetAxisDown(NeverdawnInputAxis.Trigger, NeverdawnInputAxisDirection.Positive))
            {
                if (character.isIdle)
                {
                    quickSlotsPage           = Instantiate(UIFactory.uiQuickMenuQuickSlotsPrefab);
                    quickSlotsPage.abilities = character.quickCastAbilities;

                    characterMenu.Open(quickSlotsPage);
                }
            }

            if (characterMenu.isOpen)
            {
                if (inputModule.GetButtonDown(NeverdawnInputButton.Cancel))
                {
                    characterMenu.GoBack();
                }

                return;
            }

            // DEBUG LVL UP
            if (inputModule.GetButtonDown(NeverdawnInputButton.Info))
            {
                character.LevelUp();
            }

            // Start pressed
            if (inputModule.GetButtonDown(NeverdawnInputButton.Start))
            {
                if (character.isIdle)
                {
                    characterMenu.NavigateInto(Instantiate(UIFactory.uiQuickMenuStartPrefab));
                }
            }

            //Select pressed
            if (inputModule.GetButtonDown(NeverdawnInputButton.Settings))
            {
                if (character.isIdle)
                {
                    GameController.instance.PauseGame();
                }
            }

            // Confirm pressed
            if (inputModule.GetButtonDown(NeverdawnInputButton.Confirm))
            {
                if (character.isIdle)
                {
                    if (currentInteractable != null)
                    {
                        UIQuickMenuInteractable page = Instantiate(UIFactory.uiQuickMenuInteractionCollectionPrefab);
                        page.SetInteractionCollection(currentInteractable);

                        characterMenu.NavigateInto(page);
                    }
                }
                else
                {
                    character.currentAction.ActionConfirm();
                }
            }

            // Cancel pressed
            if (inputModule.GetButtonDown(NeverdawnInputButton.Cancel))
            {
                if (!character.isIdle)
                {
                    character.currentAction.ActionCancel();
                }
                else
                {
                    characterMenu.NavigateInto(Instantiate(UIFactory.uiQuickMenuStartPrefab));
                }
            }

            // Switch character pressed
            if (inputModule.GetButtonDown(NeverdawnInputButton.SwitchCharacter))
            {
                if (character.isIdle)
                {
                    NeverdawnCamera.RemoveTargetLerped(characterTransform);

                    Character nextCharacter = GameController.instance.party.GetNextCharacter(this);

                    if (nextCharacter != null)
                    {
                        character            = nextCharacter;
                        preferredCharacterId = nextCharacter.id;
                    }


                    NeverdawnCamera.AddTargetLerped(characterTransform);

                    // UINeverdawnParty.UpdateView();

                    characterMenu = UICharacterMenus.GetMenu(character);
                }
            }

            // Movement
            if (character.isIdle)
            {
                Vector3 input = inputModule.normalizedDirection;
                smoothedInput = Vector3.RotateTowards(smoothedInput, input, Time.deltaTime * _inputSmoothing, 1.0f);

                // Get player input
                Vector2 fwdTurn = NeverdawnUtility.InputToForwardTurn(characterTransform, smoothedInput, 2.0f);

                // Rotate the player
                characterTransform.Rotate(Vector3.up, fwdTurn.y * Time.deltaTime * characterNavMeshAnimator.turnSpeed);

                // Find target position
                Vector3 targetPosition = characterTransform.position + characterNavMeshAnimator.moveSpeed * fwdTurn.x * characterTransform.forward * Time.deltaTime;

                NavMeshHit hit;

                // Check for valid position
                if (!NavMesh.Raycast(characterTransform.position, targetPosition, out hit, NavMesh.AllAreas))
                {
                    characterTransform.position = hit.position;
                }
                else
                {
                    if (NavMesh.SamplePosition(targetPosition, out hit, 0.1f, NavMesh.AllAreas))
                    {
                        characterTransform.position = hit.position;
                    }
                }

                RaycastHit rayHit;

                // experimental: stick to ground
                if (Physics.Raycast(characterTransform.position, Vector3.down, out rayHit, 0.5f, 1 << 9))
                {
                    characterTransform.position = rayHit.point;
                }
            }

            // Find closest interaction
            Collider[] colliders = Physics.OverlapSphere(character.transform.position, 1.0f, 1 << 10);

            float        minDistance   = float.MaxValue;
            Interactable closestHandle = null;

            for (int i = 0; i < colliders.Length; i++)
            {
                float        distance = Vector3.Distance(colliders[i].transform.position, character.transform.position);
                Interactable handle   = colliders[i].GetComponentInParent <Interactable>();

                if (distance < minDistance && handle != null && handle.enabled)
                {
                    closestHandle = handle;
                    minDistance   = distance;
                }
            }

            if (closestHandle != null)
            {
                updateInteraction(closestHandle);
            }
            else
            {
                updateInteraction(null);
            }
        }
    }
    public override void UpdateCombatControls()
    {
        if (GameController.state == GameState.Combat)
        {
            if (character.isIdle)
            {
                if (obstacle.enabled)
                {
                    obstacle.enabled = false;
                    return;
                }

                if (character.remainingActions > 0)
                {
                    RangedAttackAbility rangedAttack = character.GetCastableAbilities <RangedAttackAbility>()
                                                       .OrderByDescending(m => m.threatRating).FirstOrDefault();

                    if (rangedAttack != null)
                    {
                        target = CharacterUtils.GetClosestCharacter(GameController.instance.party, character.position);

                        if (target != null)
                        {
                            NeverdawnCamera.AddTargetLerped(target.transform);
                            hasFallback = false;

                            float distance = Vector3.Distance(character.position, target.position);

                            if (distance > rangedAttack.weapon.estimatedRange + character.remainingSteps)
                            {
                                if (character.remainingSteps > 0.0f)
                                {
                                    character.PushAction(new CharacterNavigateToAction(target.position, character.remainingSteps, false,
                                                                                       rangedAttack.weapon.estimatedRange - 0.1f));
                                }
                                else
                                {
                                    GameController.instance.combatController.EndTurn(this);
                                    return;
                                }
                            }
                            else
                            {
                                // Get positions and sort by strategic value (furthest away from target!)
                                List <Vector3> positions = sampleSurroundings(20, character.remainingSteps);
                                positions = positions.OrderByDescending(p => Vector3.Distance(p, target.position)).ToList();

                                // No position found yet!
                                bool    hasPosition    = false;
                                Vector3 targetPosition = Vector3.negativeInfinity;
                                Vector3 targetVelocity = Vector3.negativeInfinity;
                                Vector3 force;

                                foreach (Vector3 position in positions)
                                {
                                    Vector3 direction = target.position - position;
                                    direction.y = 0.0f;
                                    direction.Normalize();

                                    launchTransform.position = position;
                                    launchTransform.forward  = direction;

                                    Vector3 launchPosition = launchTransform.TransformPoint(rangedAttack.weapon.projectileSpawn);

                                    if (tryHitTarget(rangedAttack, launchPosition, target.frame, out force, 5))
                                    {
                                        targetPosition = position;
                                        targetVelocity = force;
                                        hasPosition    = true;
                                        break;
                                    }
                                }

                                if (hasPosition)
                                {
                                    rangedAttack.targetVelocity = targetVelocity;
                                    rangedAttack.caster         = character;

                                    character.PushAction(new CharacterNavigateToAction(targetPosition, character.remainingSteps, false));
                                    character.PushAction(rangedAttack.Prepare());
                                    character.PushAction(rangedAttack.Cast());
                                }
                                else
                                {
                                    if (hasFallback)
                                    {
                                        rangedAttack.targetVelocity = fallBackVelocity;
                                        rangedAttack.caster         = character;

                                        character.PushAction(new CharacterNavigateToAction(fallbackPosition, character.remainingSteps, false));
                                        character.PushAction(rangedAttack.Prepare());
                                        character.PushAction(rangedAttack.Cast());
                                    }
                                    else
                                    {
                                        GameController.instance.combatController.EndTurn(this);
                                    }
                                }
                            }
                        }
                        else
                        {
                            GameController.instance.combatController.EndTurn(this);
                        }
                    }
                    else
                    {
                        EndCombatTurn();
                    }
                }
                else
                {
                    if (target)
                    {
                        NeverdawnCamera.RemoveTargetLerped(target.transform);
                    }

                    EndCombatTurn();
                }
            }
        }


        if (character.isIdle)
        {
            if (!obstacle.enabled)
            {
                obstacle.enabled = true;
            }
        }
    }