Пример #1
0
    // Called when it's the computer's turn.
    private void MediatorUpdate()
    {
        // Actions that took place.
        List <IOperator> actions = root.systemActions;

        // Loop through the system actions...
        foreach (IOperator action in actions)
        {
            string playerLocation = KnowledgeAnnotator.GetLocation(problem.player, problem.initial);
            string npcBefore      = KnowledgeAnnotator.GetLocation(action.Actor, root.parent.state.Predicates);
            string npcAfter       = KnowledgeAnnotator.GetLocation(action.Actor, problem.initial);

            // If the player can see the action taking place...
            if (npcBefore.Equals(playerLocation) || npcAfter.Equals(playerLocation))
            {
                // If the NPC was at a different location when their action started or when their action ends...
                if (!npcBefore.Equals(playerLocation) || !npcAfter.Equals(playerLocation))
                {
                    // Find all the game objects colocated with the player.
                    List <GameObject> objs = stateManager.GetObjectsAt(playerLocation);

                    // Create a container for the NPC's game object.
                    GameObject actor = null;

                    // If there are objects colocated with the player...
                    if (objs != null)
                    {
                        // try to find the NPC.
                        actor = objs.Find(x => x.name.Equals(action.Actor));
                    }

                    // If the NPC was not found...
                    if (actor == null)
                    {
                        // Get the objects at the NPC location where the action started.
                        objs = stateManager.GetObjectsAt(npcBefore);

                        // Of there are objects colocated with the NPC's starting location...
                        if (objs != null)
                        {
                            // try to find the NPC.
                            actor = objs.Find(x => x.name.Equals(action.Actor));
                        }
                    }

                    // If we found the NPC...
                    if (actor != null)
                    {
                        // Get the NPC's script.
                        NPC npc = actor.GetComponent <NPC>();

                        // Move the NPC to their new location.
                        //npc.MoveTo(npcAfter);
                        npc.AcceptInstruction(action);

                        // Remove the NPC from their previous location.
                        stateManager.RemoveObject(action.Actor, npcBefore);
                    }
                    // ...if we didn't find the NPC...
                    else
                    {
                        // Find the two rooms.
                        GameObject beforeChunk = GameObject.Find(npcBefore);
                        GameObject openTile    = stateManager.GetOpenTile(beforeChunk);
                        GameObject afterChunk  = GameObject.Find(npcAfter);

                        // Instantiate the actor in the before room.
                        actor = Instantiate(Resources.Load(stateManager.Type(action.Actor), typeof(GameObject)), openTile.transform.position, Quaternion.identity) as GameObject;

                        // Get the NPC's script.
                        NPC npc = actor.GetComponent <NPC>();

                        // Move the NPC to their new location.
                        //npc.MoveTo(npcAfter);
                        npc.AcceptInstruction(action);

                        // Set the new object's parent.
                        actor.transform.parent = afterChunk.transform;

                        // Name the NPC game object.
                        actor.name = action.Actor;
                    }

                    // Add the NPC game object to the state manager.
                    stateManager.PutObject(actor, npcAfter);
                }
            }
        }

        frontierThread.Start();
    }