コード例 #1
0
 public void SetScheduleEventDetails(NPCScheduleEvent npcScheduleEvent)
 {
     npcTargetScene                  = npcScheduleEvent.toSceneName;
     npcTargetGridPosition           = (Vector3Int)npcScheduleEvent.toGridCoordinate;
     npcTargetWorldPosition          = GetWorldPosition(npcTargetGridPosition);
     npcFacingDirectionAtDestination = npcScheduleEvent.npcFacingDirectionAtDestination;
     npcTargetAnimationClip          = npcScheduleEvent.animationAtDestination;
     ClearNPCEventAnimation();
 }
コード例 #2
0
    private void Update()
    {
        if (moveNPC)
        {
            moveNPC = false;

            NPCScheduleEvent npcScheduleEvent = new NPCScheduleEvent(0, 0, 0, 0, Weather.none, Season.none, sceneName, new GridCoordinate(finishPosition.x, finishPosition.y), eventAnimationClip);

            npcPath.BuildPath(npcScheduleEvent);
        }
    }
コード例 #3
0
    // Check until the moveNPC bool is triggered in the editor. Then create an NPCSchedule event for this movement we want to make, and build the path and have the NPC follow it
    private void Update()
    {
        if (moveNPC)
        {
            moveNPC = false;

            // Now scheduled event with the correct parameters
            NPCScheduleEvent npcScheduleEvent = new NPCScheduleEvent(0, 0, 0, 0, Weather.none, Season.none, sceneName, new GridCoordinate(finishPosition.x, finishPosition.y), eventAnimationClip);

            // Build the path from this npcScheduleEvent
            npcPath.BuildPath(npcScheduleEvent);
        }
    }
コード例 #4
0
    // Called from NPCPath, after we've built the path for the NPC to follow. This Sets all of the scheduled movement event details in this class
    // including the target scene, target grid/world positions, the direction the NPC faces at destination, and the animation clip to play
    // at the destination
    public void SetScheduleEventDetails(NPCScheduleEvent npcScheduleEvent)
    {
        // Set the target scene, the target drid and world positions we're walking to, the direction the NPC faces when they get there, and the animation to play
        // when they get there. These are all set in the npcScheduleEvent passed here from NPCPath
        npcTargetScene                  = npcScheduleEvent.toSceneName;
        npcTargetGridPosition           = (Vector3Int)npcScheduleEvent.toGridCoordinate;
        npcTargetWorldPosition          = GetWorldPosition(npcTargetGridPosition);
        npcFacingDirectionAtDestination = npcScheduleEvent.npcFacingDirectionAtDestination;
        npcTargetAnimationClip          = npcScheduleEvent.animationAtDestination;

        // Clear out the current NPC's event animations back to the blank one so we can start moving on the new path
        ClearNPCEventAnimation();
    }
コード例 #5
0
    // This method is called every time the game Minute advances. It will loop through all of the npcScheduleEvents in the npcScheduleEventList
    // And see if we have a match (in time, day, season, weather, and priority if times match). If we do, this will call NPCPath
    // to build the path with AStar, and move the NPC accordingly with NPCMovement
    private void GameTimeSystem_AdvanceMinute(int gameYear, Season gameSeason, int gameDay, string gameDayOfWeek, int gameHour, int gameMinute, int gameSecond)
    {
        // Now that we know a minute has just advanced, get the current game time in the form of HHMM
        int time = (gameHour * 100) + gameMinute;

        // Attempt to get a matching schedule for this particular gameTime that just advanced to
        NPCScheduleEvent matchingNPCScheduleEvent = null;

        // Loop through all of the NPCScheduleEvents in the sorted set, which is already in time and priority order
        foreach (NPCScheduleEvent npcScheduleEvent in npcScheduleEventSet)
        {
            // First check if the current npcScheduleEvent occurs at the same time as the current game time
            if (npcScheduleEvent.Time == time)
            {
                // Now that the time matches, check if the parameters (day/season/weather) also match. If they don't, continue on to the next NPCScheduleEvent in the Set
                if (npcScheduleEvent.day != 0 && npcScheduleEvent.day != gameDay)
                {
                    continue;
                }
                if (npcScheduleEvent.season != Season.none && npcScheduleEvent.season != gameSeason)
                {
                    continue;
                }
                if (npcScheduleEvent.weather != Weather.none && npcScheduleEvent.weather != GameManager.Instance.currentWeather)
                {
                    continue;
                }

                // If all of the above checks pass (so we are on the correct time, day, season, and weather), we have a match! Play the Scheduled event! (build path, move NPC)
                // Note these are already sorted by priority, so the first match is automatically the correctly prioritized match for several schedules at the same time.
                // This will set matchingNPCScheduleEvent to the current NPCScheduleEvent, break out of the for loop, and then build the path down below
                matchingNPCScheduleEvent = npcScheduleEvent;
                break;
            }
            // If the time hasn't occured yet, dont do anything at all (The set is sorted by time, so if the current elements time hasn't occured yet, none of them will have!)
            else if (npcScheduleEvent.Time > time)
            {
                break;
            }
        }

        // Now, as long as we have found a matching event above (matchingNPCScheduleEvent isn't null anymore), Build the path with npcPath which takes
        // care of building the path with AStar, and moving the NPC with NPCMovement along the path, just by passing in this NPCScheduleEvent that matches the current time
        if (matchingNPCScheduleEvent != null)
        {
            npcPath.BuildPath(matchingNPCScheduleEvent);
        }
    }
コード例 #6
0
ファイル: NPCSchedule.cs プロジェクト: teradebu/FarmingKit
    private void GameTimeSystem_AdvanceMinute(int gameYear, Season gameSeason, int gameDay, string gameDayOfWeek, int gameHour, int gameMinute, int gameSecond)
    {
        int time = (gameHour * 100) + gameMinute;

        //Attempt to get matching schedule

        NPCScheduleEvent matchingNPCScheduleEvent = null;

        foreach (NPCScheduleEvent npcScheduleEvent in npcScheduleEventSet)
        {
            if (npcScheduleEvent.Time == time)
            {
                //Time match now check if parameters match
                if (npcScheduleEvent.day != 0 && npcScheduleEvent.day != gameDay)
                {
                    continue;
                }

                if (npcScheduleEvent.season != Season.none && npcScheduleEvent.season != gameSeason)
                {
                    continue;
                }

                if (npcScheduleEvent.weather != Weather.none && npcScheduleEvent.weather != GameManager.Instance.currentWeather)
                {
                    continue;
                }

                //Schdule matches
                //Debug.Log("Schedule Matches!" + npcScheduleEvent);
                matchingNPCScheduleEvent = npcScheduleEvent;
                break;
            }
            else if (npcScheduleEvent.Time > time)
            {
                break;
            }
        }

        //Now test is matchingSchedule != null and do something;
        if (matchingNPCScheduleEvent != null)
        {
            //Build path for matching schedule
            npcPath.BuildPath(matchingNPCScheduleEvent);
        }
    }
コード例 #7
0
ファイル: NPCPath.cs プロジェクト: zackrcarson/MyFarmingGame
    // This method is called from AStarTest or NPCScheduler to build the path. This method then calls NPCManager.BuildPath(), which in turn calls AStar.BuildPath()
    // To ultimately find the path taken and populate npcMovementStepStack with steps to take.
    public void BuildPath(NPCScheduleEvent npcScheduleEvent)
    {
        // First clear out the npcMovementStepStack in case it has extra steps from earlier builds
        ClearPath();

        // If the scheduled event is for the same scene as the current NPC scene. For now, we are only moving the NPC within the current scene. If it's not, go to the
        // next else if statement to move to a new scene
        if (npcScheduleEvent.toSceneName == npcMovement.npcCurrentScene)
        {
            // If we are in the same scene as the scheduled event, get the current (start) and target grid positions for the movement event, the former from the npcMovement, and
            // the latter from the scheduleEvent
            Vector2Int npcCurrentGridPosition = (Vector2Int)npcMovement.npcCurrentGridPosition;

            Vector2Int npcTargetGridPosition = (Vector2Int)npcScheduleEvent.toGridCoordinate;

            // Build the path with NPCManager -> AStar and add the corresponding movement steps to the movement step stack
            NPCManager.Instance.BuildPath(npcScheduleEvent.toSceneName, npcCurrentGridPosition, npcTargetGridPosition, npcMovementStepStack);
        }
        // Else, if the movement event is for a location in another scene, we need to build Paths from the current NPC position, to the scene exit position specified in
        // the SceneRoute, then a new path from the new scene entrance position to where the NPC targets (or to another scene if there is a 3-scene scene route between the scenes)
        else if (npcScheduleEvent.toSceneName != npcMovement.npcCurrentScene)
        {
            SceneRoute sceneRoute;

            // Get the corresponding sceneRoute for the matching schedule
            sceneRoute = NPCManager.Instance.GetSceneRoute(npcMovement.npcCurrentScene.ToString(), npcScheduleEvent.toSceneName.ToString());

            // Check if a valid SceneRoute has been found in the NPCManager SceneRouteDictionary
            if (sceneRoute != null)
            {
                // Loop through all of the scene paths the NPC will need to traverse to get to the toSceneName, backwards
                // For each ScenePath in the SceneRoute, calculate the to and from grid positions (if it's a starting scene, fromGrid is the NPCs current position, and toGrid is that scenes
                // exit point specified in the scenePath. If it's a middle scene, fromGrid is the scene entrance point, and toGrid is the scene exit point. If it's the ending scene, the
                // fromGrid is the scene entrance point, and the toGrid is the target destination specified in the NPCScheduleEvent
                for (int i = sceneRoute.scenePathList.Count - 1; i >= 0; i--)
                {
                    int toGridX, toGridY, fromGridX, fromGridY;

                    ScenePath scenePath = sceneRoute.scenePathList[i];

                    // Check if this current ScenePath is the final destination (we set it up so final destinations have toGridCells of (999999, 999999), while the maxGridDims are smaller at 99999)
                    // - when this happens we know to suibstitute the final destination as the one the NPCScheduleEvent wants
                    if (scenePath.toGridCell.x >= Settings.maxGridWidth || scenePath.toGridCell.y >= Settings.maxGridHeight)
                    {
                        // If so, set up the toGrid cell as the final destination of the NPCScheduleEvent we are looking at
                        toGridX = npcScheduleEvent.toGridCoordinate.x;
                        toGridY = npcScheduleEvent.toGridCoordinate.y;
                    }
                    else
                    {
                        // If it's not the final destination, use the specified scenePath to position to move to (i.e. the scene exit position the NPC will move to to transfer to the next scene)
                        toGridX = scenePath.toGridCell.x;
                        toGridY = scenePath.toGridCell.y;
                    }

                    // Check if this current ScenePath is the  starting position (we set it up so starting positions have toGridCells of (999999, 999999), while the maxGridDims are smaller at 99999)
                    // - when this happens we know to suibstitute the starting position as the one the NPCs current position
                    if (scenePath.fromGridCell.x >= Settings.maxGridWidth || scenePath.fromGridCell.y >= Settings.maxGridHeight)
                    {
                        // If so, set up the NPCs current position cell as the starting location of the first path
                        fromGridX = npcMovement.npcCurrentGridPosition.x;
                        fromGridY = npcMovement.npcCurrentGridPosition.y;
                    }
                    else
                    {
                        // If it's not the starting position, use the specified scenePath to position to move from (i.e. the scene entrance position the NPC will move to to transfer to the next scene)
                        fromGridX = scenePath.fromGridCell.x;
                        fromGridY = scenePath.fromGridCell.y;
                    }

                    // To and From grid positions in THIS current Scene (computed above from the current scene path we're looking at)
                    Vector2Int fromGridPosition = new Vector2Int(fromGridX, fromGridY);
                    Vector2Int toGridPosition   = new Vector2Int(toGridX, toGridY);

                    // Build the path for this current ScenePath in the sceneRoute, and add the steps to the movementStepStack (not cleared inbetween scenePaths! All of the scenes
                    // will get added onto the top of the movementStepStack). So our movementStepStack will have multiple built paths stacked on top of eachother,
                    // one for each scene we need to traverse. Then the NPCmovement will take all of the steps in turn, from each scene in order to get to the destination
                    // scene and grid position
                    NPCManager.Instance.BuildPath(scenePath.sceneName, fromGridPosition, toGridPosition, npcMovementStepStack);

                    // Then move on to the next ScenePath (for the next scene) in the loop and repeat all of the steps to add more to the Step Stack under a new step name
                }
            }
        }

        // If stack count is > 1 (i.e. has steps beyond the starting one), update the times and the pop off the 1st item which is the starting position
        if (npcMovementStepStack.Count > 1)
        {
            // This method will loop through all of the steps in the npcMovementStepStack, and populate the gameTime that the NPC needs to
            // be to that step by.
            UpdateTimesOnPath();

            // discard starting step (we're already on the starting step)
            //npcMovementStepStack.Pop();  // I removed this line so the NPC doesn't jump a square for the very first step...

            // Set the schedule event details in NPC movement, so it knows how to move the NPC, and what facing direction/animation to play when they get there
            npcMovement.SetScheduleEventDetails(npcScheduleEvent);
        }
    }
コード例 #8
0
ファイル: NPCPath.cs プロジェクト: teradebu/FarmingKit
    public void BuildPath(NPCScheduleEvent npcScheduleEvent)
    {
        ClearPath();

        //If schedule event is for the same scene as the current NPC scene
        if (npcScheduleEvent.toSceneName == npcMovement.npcCurrentScene)
        {
            Vector2Int npcCurrentGridPosition = (Vector2Int)npcMovement.npcCurrentGridPosition;

            Vector2Int npcTargetGridPosition = (Vector2Int)npcScheduleEvent.toGridCoordinate;

            //Build path and add movement steps to movement step stack
            NPCManager.Instance.BuildPath(npcScheduleEvent.toSceneName, npcCurrentGridPosition, npcTargetGridPosition, npcMovementStepStack);
        }
        //else if the schedule event is for a location in another scene
        else if (npcScheduleEvent.toSceneName != npcMovement.npcCurrentScene)
        {
            SceneRoute sceneRoute;

            //Get scene route matchingSchedule
            sceneRoute = NPCManager.Instance.GetSceneRoute(npcMovement.npcCurrentScene.ToString(), npcScheduleEvent.toSceneName.ToString());

            //Has a valid scene route been found?
            if (sceneRoute != null)
            {
                //Loop through scene paths in reverse order

                for (int i = sceneRoute.scenePathList.Count - 1; i >= 0; i--)
                {
                    int toGridX, toGridY, fromGridX, fromGridY;

                    ScenePath scenePath = sceneRoute.scenePathList[i];

                    //Check if this is the final destination
                    if (scenePath.toGridCell.x >= Settings.maxGridWidth || scenePath.toGridCell.y >= Settings.maxGridHeight)
                    {
                        //If so use final destination grid cell
                        toGridX = npcScheduleEvent.toGridCoordinate.x;
                        toGridY = npcScheduleEvent.toGridCoordinate.y;
                    }
                    else
                    {
                        //else use scene path to position
                        toGridX = scenePath.toGridCell.x;
                        toGridY = scenePath.toGridCell.y;
                    }

                    //Check if this is the starting position
                    if (scenePath.fromGridCell.x >= Settings.maxGridWidth || scenePath.fromGridCell.y >= Settings.maxGridHeight)
                    {
                        //if so use npc position
                        fromGridX = npcMovement.npcCurrentGridPosition.x;
                        fromGridY = npcMovement.npcCurrentGridPosition.y;
                    }
                    else
                    {
                        //else use scene path from position
                        fromGridX = scenePath.fromGridCell.x;
                        fromGridY = scenePath.fromGridCell.y;
                    }

                    Vector2Int fromGridPosition = new Vector2Int(fromGridX, fromGridY);

                    Vector2Int toGridPosition = new Vector2Int(toGridX, toGridY);

                    //Build path and add movement steps to movement step stack
                    NPCManager.Instance.BuildPath(scenePath.sceneName, fromGridPosition, toGridPosition, npcMovementStepStack);
                }
            }
        }

        //If stack count >1, update times and then pop off 1st item which is the starting position
        if (npcMovementStepStack.Count > 1)
        {
            UpdateTimesOnPath();
            npcMovementStepStack.Pop();//discard starting step

            //Set schedule event details in NPC movement
            npcMovement.SetScheduleEventDetails(npcScheduleEvent);
        }
    }