Exemplo n.º 1
0
    // note Awake, Start, Update, etc. made public virtual so can override in extensions

    public virtual void Awake()      // do non-primitive initializations for MonoBehavior's here
    {
        taskHandle    = new EventWaitHandle(false, EventResetMode.ManualReset);
        menuHandle    = new EventWaitHandle(false, EventResetMode.ManualReset);
        sensingHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
        w32Classes    = new W32ClassBatchUnregisterer();
        program       = new Program();
        player        = new Player("Player");
        npc           = new NPC(this, "NPC");
        interaction   = new Interaction(npc, player);
        interaction.setOk(false);          // suppress default Ok's
        // support calling Debug.Log from scripts
        getDisco().eval(
            "Debug = { Log : function (obj) { TextWindow.Program.Log(obj); }}", "DiscoUnity");

        discoThread = new Thread((ThreadStart) delegate {
            try {
                while (discoRunning)
                {
                    // start console if requested
                    if (Console && !formStarted)
                    {
                        formStarted = true;
                        //second parameter is called when the console Form is loaded
                        program.StartWindow(formThread, (sender, evt) => {
                            if (onWindows)
                            {
                                w32Classes.AddWindowClassesRecursively((Control)sender);
                            }
                            formLoaded = true;
                            form.Writer.WriteLine("    DiscoUnity " + VERSION);
                            console = new edu.wpi.disco.Console(null, interaction);
                            interaction.setConsole(console);
                            console.init(getDisco());
                            console.setReader(new Program.ConsoleReader(form));
                            form.shell.shellTextBox.GotFocus += (s, e) => {                             // improve window readability
                                if (!firstPrompt && form.shell.shellTextBox.IsCaretJustBeforePrompt())
                                {
                                    firstPrompt = false;
                                    form.Writer.Write(console.getPrompt());
                                }
                            };
                            interaction.start(true);                             // start console thread
                        });
                    }
                    // do Sensing on Unity thread
                    sensing = true;
                    sensingHandle.WaitOne();
                    sensingHandle.Reset();
                    UpdateDisco(getDisco());               // manage toplevel goals
                    getDisco().tick();                     // update conditions
                    // process player menu choice, if any
                    if (chosen != null)
                    {
                        getDisco().doneUtterance(chosen.task, formatted);
                        done(true, chosen.task, chosen.contributes);
                        chosen    = null;
                        formatted = null;
                    }
                    // process NPC response, if any
                    Agenda.Plugin.Item item = npc.respondIf(interaction, true);
                    if (item != null && !item.task.Equals(npc.getLastUtterance()))
                    {
                        npc.done(interaction, item);
                    }
                    if (Menu)
                    {
                        // update player menu options (including empty)
                        java.util.List newItems = player.generate(interaction);
                        if (!newItems.equals(items))
                        {
                            String [] choices = null;
                            if (!newItems.isEmpty())
                            {
                                choices = new String[newItems.size()];
                                int i   = 0;
                                java.util.Iterator l = newItems.iterator();
                                while (l.hasNext())
                                {
                                    choices[i++] = translate(((Agenda.Plugin.Item)l.next()).task);
                                }
                            }
                            lastMenu = null;                             // block choosing from old menu
                            menu     = choices;
                            items    = newItems;
                            newMenu  = true;
                            menuHandle.WaitOne();                             // execute on Unity thread
                            menuHandle.Reset();
                            lastMenu = choices;
                        }
                    }
                    // sleep for a while
                    Thread.Sleep(Sleep);
                }
            } catch (Exception e) {
                Debug.Log("Disco thread terminating: " + e);
                if (Tracing)
                {
                    Debug.Log(e.ToString());
                }
            }
        });
        discoThread.IsBackground = true;
        discoThread.Priority     = Priority;

        formThread = new Thread((ThreadStart) delegate {
            try {
                /*
                 *      while (true) { // for testing
                 *              program.WriteLine("Echo: " + program.ReadLine());
                 *      }
                 */
                form = program.GetForm();
                java.lang.System.setOut(new java.io.PrintStream(new Program.ConsoleOut(form.Writer), true));
                java.lang.System.setErr(java.lang.System.@out);
            } catch (Exception e) { Debug.Log("Console form thread terminating: " + e); }
        });
        formThread.IsBackground = true;
    }