コード例 #1
0
ファイル: AIUnitBehavior.cs プロジェクト: vincent2421/Game
    /// <summary>
    /// Initiates combat between this squad and the target.
    /// </summary>
    /// <param name="target">Target.</param>
    protected void beginCombatWithTarget(ActorBehavior target)
    {
        // Capture the combat camera.
        CombatSystemBehavior combatSystem = GameObject.Find("Combat Camera").GetComponent <CombatSystemBehavior>();

        if (combatSystem == null)
        {
            Debug.LogError("Unable to find a valid combat system in scene!");
            return;
        }

        // Capture the offensive and defensive combat squads
        CombatSquadBehavior offensiveSquad = actor.GetComponent <CombatSquadBehavior>();

        if (!offensiveSquad)
        {
            Debug.LogError("Attempted to enter combat with an invalid offensive squad!");
            return;
        }

        CombatSquadBehavior defensiveSquad = target.GetComponent <CombatSquadBehavior>();

        if (!defensiveSquad)
        {
            Debug.LogError("Attempted to enter combat with an invalid defensive squad!");
            return;
        }

        // Hide the target movement points.
        grid.HideMovePoints();

        // Begin combat!
        combatSystem.BeginCombat(offensiveSquad, defensiveSquad, grid);
    }
コード例 #2
0
    /// <summary>
    /// Waits for the left mouse button to be clicked, then performs a raycast to determine if the player has clicked
    /// on a valid movement point.
    /// </summary>
    /// <remarks>
    /// Transitions:
    ///     Player presses Escape key -> SelectingUnit
    ///     Player presses Space key -> SelectingTarget
    ///
    ///     Path to movement point cannot be found -> SelectingUnit
    ///     Player selects a valid movement point -> AwaitingMovement
    /// </remarks>
    private void updateSelectingMovement()
    {
        // Allow the player to press the Escape key to deselect their current unit.
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            deselectSquad();
            controlState = GridControlState.SelectingUnit;
            return;
        }

        // Allow the player to press the Space bar to skip the movement step.
        if (Input.GetKeyDown(KeyCode.Space))
        {
            selectedSquad.actorHasMovedThisTurn = true;
            grid.HideMovePoints();
            controlState = GridControlState.SelectingTarget;
            return;
        }

        // Wait for the left mouse button to be pressed.
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            // Perform raycasting and store a list of all objects that have been selected.
            List <RaycastHit> hits = new List <RaycastHit>();
            hits.AddRange(Physics.RaycastAll(ray));

            // Iterate over the selection list to determine if the player has clicked on a valid movementpoint.
            foreach (RaycastHit hitInfo in hits.OrderBy(l => l.distance))
            {
                // Retrieve the movepoint behavior from the hit target.
                MovePointBehavior movePoint = hitInfo.transform.GetComponent <MovePointBehavior>();
                if (movePoint == null || !movePoint.renderer.enabled)
                {
                    continue;
                }

                // Retrieve the combat squad behavior from the currently-selected squad.
                CombatSquadBehavior combatSquad = selectedSquad.GetComponent <CombatSquadBehavior>();
                if (combatSquad == null)
                {
                    Debug.LogError("Selected a unit with no combat squad attached!");
                }

                // Retrieve the maximum movement distance of the selected squad.
                int distance = (combatSquad == null ? 0 : combatSquad.Squad.Speed);

                // Mark the actor as having moved.
                selectedSquad.actorHasMovedThisTurn = true;

                // Retrieve a path from the starting point to the selected node.
                List <MovePointBehavior> pathList = startingPoint.FindPath(movePoint, distance, grid);
                if (pathList != null)
                {
                    selectedSquad.pathList = pathList;
                    selectedSquad.canMove  = true;

                    // Remove the squad's current movement point from the ignore list.
                    grid.ignoreList.Remove(startingPoint);

                    // Add the squad's target movement point to the ignore list.
                    grid.ignoreList.Add(movePoint);

                    // Hide visible nodes on the grid.
                    grid.HideMovePoints();

                    // Transition into the AwaitingMovement state.
                    controlState = GridControlState.AwaitingMovement;
                }
                else
                {
                    Debug.LogError("No path to target!");

                    // Deselect the current squad, but do not allow it to move again!
                    deselectSquad();

                    // Return to the SelectingUnit state.
                    controlState = GridControlState.SelectingUnit;
                }
            }
        }
    }