コード例 #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
        public static void FixCamera()
        {
            GameObject mainCamera = GameObject.FindGameObjectWithTag("MainCamera");

            CameraBehavior cameraBehavior = mainCamera.GetComponent <CameraBehavior>();
            float          cameraSpeed    = cameraBehavior.MovementRate;

            // Destroy the camera in preparation for adding the new one.
            GameObject.DestroyImmediate(mainCamera);

            // Retrieve the MainCamera prefab from the asset database.
            GameObject newCamera = (GameObject)GameObject.Instantiate(AssetDatabase.LoadAssetAtPath(
                                                                          "Assets/Prefabs/Main Camera.prefab", typeof(GameObject)));

            newCamera.transform.name = "Main Camera";

            cameraBehavior = newCamera.GetComponent <CameraBehavior>();
            cameraBehavior.MovementRate = cameraSpeed;

            // Update the main camera property of the combat camera.
            CombatSystemBehavior combatSystem = (CombatSystemBehavior)GameObject.Find("Combat Camera").GetComponent <CombatSystemBehavior>();

            combatSystem.mainCamera = newCamera.camera;

            EditorUtility.SetDirty(combatSystem);

            // Disable all text renderers on the camera.
            MeshRenderer[] renderers = newCamera.GetComponentsInChildren <MeshRenderer>();
            foreach (MeshRenderer renderer in renderers)
            {
                if (renderer.gameObject.name == "MiniMapBG")
                {
                    continue;
                }

                renderer.enabled = false;
            }

            // Update the grid for the minimap.
            GridBehavior         grid        = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridBehavior>();
            MiniMapGridBehaviour minimapGrid = newCamera.GetComponentInChildren <MiniMapGridBehaviour>();

            minimapGrid.theGrid        = grid;
            minimapGrid.gameController = grid.GetComponent <GameControllerBehaviour>();

            EditorUtility.SetDirty(newCamera);

            Selection.activeGameObject = newCamera;

            Debug.Log("Camera fixed.");
        }
コード例 #3
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 enemy unit.
    /// </summary>
    /// <remarks>
    /// Transitions:
    ///     Player presses Escape key -> SelectingMovement
    ///     Player presses Space key -> SelectingUnit or AwaitingEnemy
    /// </remarks>
    private void updateSelectingTarget()
    {
        // Allow the player to press the escape key to undo their movement.
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            // Unflag the actor as having moved.
            selectedSquad.actorHasMovedThisTurn = false;

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

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

            // Forcibly move the actor to the starting point.
            selectedSquad.currentMovePoint   = startingPoint;
            selectedSquad.transform.position = startingPoint.transform.position;

            // Hide currently-visible movement nodes.
            grid.HideMovePoints();

            // Re-highlight valid movement nodes.
            startingPoint.HighlightValidNodes(selectedSquad, grid);

            // Release the current list of valid targets.
            validTargets = null;

            // Return to the SelectingMovement state.
            controlState = GridControlState.SelectingMovement;

            return;
        }

        // Allow the player to press the space bar to skip attacking.
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Hide the target movement points.
            grid.HideMovePoints();

            // Decrement the number of moves left for this turn.
            controller.leftToMoveThis--;

            // Switch to the SelectingUnits state if the player still has moves left, otherwise end the turn.
            controlState = (controller.leftToMoveThis > 0 ?
                            GridControlState.SelectingUnit :
                            GridControlState.AwaitingEnemy);

            // Deselect the current squad.
            deselectSquad();

            return;
        }

        if (validTargets == null)
        {
            // Highlight grid points in attack range.
            CombatSquadBehavior combatSquad = selectedSquad.GetComponent <CombatSquadBehavior>();
            if (combatSquad != null)
            {
                int depth = 0;
                int range = combatSquad.Squad.Range;

                selectedSquad.currentMovePoint.HighlightValidNodes(selectedSquad, grid, range);

                // Store a list of valid targets.
                validTargets = new List <MovePointBehavior>();
                selectedSquad.currentMovePoint.BuildGraph(range, depth, grid, ref validTargets, true);
            }
        }

        // 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))
            {
                // Capture the actor behavior on the hit object.
                ActorBehavior actor = hitInfo.transform.GetComponent <ActorBehavior>();
                if (actor == null)
                {
                    continue;
                }

                // Ensure that the unit has not moved and does not belong to the player.
                if (actor.theSide == enemySide)
                {
                    // Ensure the the enemy's move point is in the list of valid target move points.
                    if (validTargets.Contains(actor.currentMovePoint))
                    {
                        // 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 = selectedSquad.GetComponent <CombatSquadBehavior>();

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

                        CombatSquadBehavior defensiveSquad = actor.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);

                        // Transition to Awaiting Combat state.
                        controlState = GridControlState.AwaitingCombat;
                    }
                }
            }
        }
    }