예제 #1
0
    static public void PlayScenario(string levelName, string scenarioName)
    {
        if (levelName != Application.loadedLevelName)          // currentlyLoadedLevelName ){
        {
            Debug.LogWarning("ScenarioCtrl couldn't play because LoadLevelName not match.");
            return;
        }

        // Get ScenarioObject.
        GameObject scenarioObj    = GameObject.Find(scenarioName);
        bool       isFromResource = false;

        if (scenarioObj == null)
        {
            Debug.LogWarning("ScenarioNode object NOT FOUND in scene. now Find in Resources.");
            scenarioObj = ViNoGOExtensions.InstantiateFromResource(scenarioName, null);
            if (scenarioObj == null)
            {
                Debug.LogError("ScenarioNode :" + scenarioName + " also Not Found in Resources.");
            }
            else
            {
                isFromResource = true;
                ViNoGOExtensions.StripGameObjectName(scenarioObj, "(Clone)", "");
            }
        }
        ScenarioNode scenario = scenarioObj.GetComponent <ScenarioNode>();

        if (scenario != null)
        {
            if (m_LoadLevelAndStartScenario)
            {
                scenario.startFromSave = true;
            }
            if (!isFromResource)
            {
                scenario.Play();
            }
        }
        else
        {
            Debug.LogError("Scenario :" + scenarioName + "object not attached a ScenarioNode.");
        }
        m_LoadLevelAndStartScenario = false;
        m_LoadLevelAndNewGame       = false;
    }
예제 #2
0
    // ------------- Override --------------------.

    /// <summary>
    /// Handles the opcode.
    /// </summary>
    public override void OnUpdate()
    {
        if (IsFinish())
        {
            return;
        }

        // Pomp Message to MessagingHandler.
        if (m_MessagePompToMsghandler)
        {
            if (m_MessagingHandler != null)
            {
                bool handled = m_MessagingHandler.HandleOpcode(this);
                m_MessagePompToMsghandler = !handled;
            }
            return;
        }

        m_CanTextProgress = false;
        switch (code[pc])
        {
        case Opcode.STRING:             pc = ByteCodeReader.readString(code, pc + 1);        m_TextBuilder.Append(loadedString);   break;

        case Opcode.TEXT:               pc = ReadText(pc);                                                                                                                                            break;

        case Opcode.VAR:                pc = ByteCodeReader.readVar(code, pc, ref paramHash, ref m_TextBuilder, stubIndent);       break;

        case Opcode.TABLE:              pc = ByteCodeReader.readTable(code, pc, ref paramHash);                                                                     break;

        case Opcode.ASSIGN_STRING:
//			Debug.Log("OPCODE>ASSIGN_STRING");
            pc        = ByteCodeReader.readString(code, pc + 2);
            leftHand  = loadedString;
            pc        = ByteCodeReader.readString(code, pc + 1);
            rightHand = loadedString;
//			Debug.Log(  "Opcode.ASSIGN key=" + leftHand + " value=\"" + rightHand + "\"" );

// Assign Value to Hashtable ?.
#if false
            symbolTable[leftHand] = rightHand;

// Assign Value to FlagTable.
#else
            ScenarioNode scenario = ScenarioNode.Instance;
            if (scenario != null && scenario.flagTable != null)
            {
                scenario.flagTable.SetStringValue(leftHand, rightHand);
            }
#endif
            leftHand  = "";
            rightHand = "";
            break;

        case Opcode.NULL:              pc++;                                                                                                                                                                           break;

        case Opcode.MESSAGING:
            pc = ByteCodeReader.readString(code, pc + 2);
            messagingTargetName       = loadedString;
            m_MessagePompToMsghandler = true;
            bool isIgnoreObj = loadedString.Equals("env");
            if (!isIgnoreObj)
            {
                if (tweenDataCached.tweenTarget != null)
                {
                    if (!tweenDataCached.tweenTarget.name.Equals(messagingTargetName))
                    {
                        tweenDataCached.tweenTarget = GameObject.Find(messagingTargetName);
                    }
                }
                else
                {
                    tweenDataCached.tweenTarget = GameObject.Find(messagingTargetName);
                }
            }
            tweenDataCached.paramTable = paramHash;
            break;

        case Opcode.NODE:
            m_PrevNodeName = m_CurrNodeName;
            pc             = ByteCodeReader.readString(code, pc + 2);
            ViNoDebugger.Log("NODE", loadedString);

            m_CurrNodeName            = loadedString;
            m_NodePcMap[loadedString] = pc;
            SetCurrentNodeToScriptEngine();

            // Callback to ScriptBinder.
            scriptBinder.OnEnterNode(this);
            break;

        case Opcode.LOAD_RESOURCE:
            m_LoadedResourcePath = VirtualMachine.loadedTextLiteralString;
            m_LoadedResource     = UnityWrapper.LoadResource(m_LoadedResourcePath);
            pc++;
            break;

// TODO : Need to Test .
        case Opcode.PLAY_AUDIO_FROM_RESOURCE:
            m_LoadedResourcePath = VirtualMachine.loadedTextLiteralString;
            ISoundPlayer.Instance.PlayAudioClip(m_LoadedResource as AudioClip, m_LoadedResourcePath, ViNoConfig.prefsBgmVolume, 0f);
            m_LoadedResource = null;
            Resources.UnloadUnusedAssets();

            pc++;
            break;

        case Opcode.INSTANTIATE_AS_GAMEOBJECT:
            if (m_LoadedResource != null)
            {
                string parentName = VirtualMachine.loadedTextLiteralString;

                UnityWrapper.InstantiateAsGameObject(m_LoadedResource, parentName);

                m_LoadedResource = null;
                Resources.UnloadUnusedAssets();
            }
            else
            {
                Debug.LogError("Resource not loaded.");
            }
            pc++;
            break;

        case Opcode.DESTROY_OBJECT:
            string     goName = VirtualMachine.loadedTextLiteralString;
            GameObject go     = GameObject.Find(goName);                                // TODO : GO.Find is Slow .
            GameObject.Destroy(go);
            pc++;
            break;

        case Opcode.JUMP:               // Jump to loadedString Node  .
            ByteCodeReader.readString(code, pc + 2);
            GoToLabel(loadedString);
            ViNoDebugger.Log("NODE", "jump to :" + loadedString);
            break;

        case Opcode.IF:
            pc = ByteCodeReader.readString(code, pc + 2);
            string flagName = VirtualMachine.loadedString;
            Debug.Log("flag name :" + flagName);

            bool isOnOrOff = (code[pc] == 1) ? true : false;
            pc++;

            pc = ByteCodeReader.readString(code, pc + 1);
            string ifTarget = VirtualMachine.loadedString;
            Debug.Log("IF =>" + ifTarget);
            pc = ByteCodeReader.readString(code, pc + 1);
            string elseTarget = VirtualMachine.loadedString;
            Debug.Log("ELSE =>" + elseTarget);

            bool isFlagOn = ScenarioNode.Instance.flagTable.CheckFlagBy(flagName);
            if (isFlagOn == isOnOrOff)
            {
                Debug.Log("IF");
                GoToLabel(ifTarget);
            }
            else
            {
                Debug.Log("ELSE");
                GoToLabel(elseTarget);
            }
            break;

            // ----- Layer -----.
#if false
        case Opcode.LAYOPT:                             GOOptionNode.Do(paramHash);           pc++;   break;
#endif
        case Opcode.BEGIN_TRANSITION:   UnityWrapper.BeginTransition();         pc++;   break;

        case Opcode.END_TRANSITION:             UnityWrapper.EndTransition();           pc++;   break;

        case Opcode.SCENE_NODE:
            SceneData.SceneNodeData data = SceneCreator.CreateNodeData(paramHash);
            SceneCreator.Create(data);
            pc++;
            break;

        case Opcode.LOAD_SCENE:
//			bool destroy = ( code[ pc + 1 ] == 0 ) ? true : false ;
//			UnityWrapper.LoadScene( m_LoadedResource , destroy );
            LoadSceneNode.Do(m_LoadedResource, paramHash);
            m_LoadedResource = null;
            Resources.UnloadUnusedAssets();
            pc++;
            break;

        case Opcode.CLEAR_SCENE:
            GameObject advSceneRoot     = ViNoSceneManager.Instance.theSavedPanel;
            bool       immediateDestroy = false;
            SceneCreator.DestroyScene(advSceneRoot, immediateDestroy);
            pc++;
            break;

/*		case Opcode.PLAY_ANIMATION:
 *                      byte animationID = code[ pc + 1 ];
 *                      ViNoAnimationManager.Instance.PlayAnimation( (int)animationID );
 *                      pc+= 2;
 *                      break;
 * //*/

        // ----- Message -----.
        case Opcode.BR:
//			m_TextBuilder.Append( "\n" );
            pc++;           break;            //scriptBinder.BR( this );	pc++;		break;

        case Opcode.CM:         ClearMessage();                         pc++;           break;

        case Opcode.ER:
            AddToBacklog();
            ClearTextBuilder();
            if (m_MsgTargetTextBox != null)
            {
                m_MsgTargetTextBox.ClearMessage();
            }
            else
            {
                Debug.LogWarning("Current Message Target Not Set.");
            }
            pc++;
            break;

        case Opcode.PRINT:              scriptBinder.PRINT(this);                     pc++;           break;

        case Opcode.CURRENT:
            TriggerMessageEvent("OnMessageTargetChanged", code[pc + 1], "", true);
            break;

        case Opcode.SET_TEXT:
            TriggerMessageEvent("OnSetText", code[pc + 1], VirtualMachine.loadedTextLiteralString, true);
            m_CurrentText = m_MessageEventData.message;
            break;

        case Opcode.HIDE_MESSAGE:
            TriggerMessageEvent("OnHideMessage", code[pc + 1], "", false);
            break;

        // ------ System Opcode -----.
        case Opcode.START_WAIT:
            m_ElapsedSec = 0f;
            if (!string.IsNullOrEmpty(loadedTextLiteralString))
            {
                m_WaitSec = float.Parse(loadedTextLiteralString);
            }
            else
            {
                m_WaitSec = kWaitSec;
            }
            pc++;
            break;

        case Opcode.UPDATE_WAIT:
//			ViNoDebugger.Log( "VM" , "waiting ...");
            m_ElapsedSec += Time.deltaTime;
            if (m_ElapsedSec > m_WaitSec)
            {
                pc++;
            }
            break;

        case Opcode.STOP:
            // Wait Until Player choosing from options . or reached to the end of a leaf node .
            // Nothing to do...

            break;

        case Opcode.END:
            ViNoEventManager.Instance.TriggerEvent("OnFinishScenario");
            update = false;
            break;

        case Opcode.PLAY_SCENARIO:
            pc = ByteCodeReader.readString(code, pc + 2);
            string     scenarioName = loadedString;
            GameObject scenarioObj  = GOCache.SetActive(scenarioName, true);
            if (scenarioObj != null)
            {
                ScenarioNode s = scenarioObj.GetComponent <ScenarioNode>();
                s.Play();
            }
            break;

        case Opcode.FLAG_ON:
            if (ScenarioNode.Instance != null && ScenarioNode.Instance.flagTable != null)
            {
                ScenarioNode.Instance.flagTable.SetFlagBy(VirtualMachine.loadedTextLiteralString, true);
            }
            pc++;
            break;

        case Opcode.FLAG_OFF:
            if (ScenarioNode.Instance != null && ScenarioNode.Instance.flagTable != null)
            {
                ScenarioNode.Instance.flagTable.SetFlagBy(VirtualMachine.loadedTextLiteralString, false);
            }
            pc++;
            break;

        case Opcode.SELECTIONS:
            ISelectionsCtrl selCtrl = ISelectionsCtrl.Instance;
            if (selCtrl != null)
            {
                if (!selCtrl.IsActive())
                {
                    string title = VirtualMachine.loadedTextLiteralString;
                    selCtrl.SetTitle(title);
                    ISelectionsCtrl.Instance.ChangeActive(true);
                }
            }
            else
            {
                Debug.LogError("ISelectionsCtrl instance not found.");
            }
            pc++;
            break;

        case Opcode.LINK:               ISelectionsCtrl.Instance.AddSelection(ref paramHash); pc++;   break;

        case Opcode.WAIT_TOUCH:
            if (IScriptEngine.skip)
            {
                if (IScriptEngine.skipAlreadyPass && !VirtualMachine._ALREADY_PASS_THE_NODE)
                {
                    return;
                }

                _SkipText( );

                ISoundPlayer pl = ISoundPlayer.Instance;
                if (pl != null)
                {
                    if (pl.IsPlayingVoice())
                    {
                        pl.StopVoice();
                    }
                }
                m_CanTextProgress = true;
                return;
            }

            if (autoMode)
            {
                float dt = Time.deltaTime;
                m_TimeElapsed += dt;
                if (m_TimeElapsed > _AUTO_MODE_WAIT_TIME)
                {
                    m_TimeElapsed = 0f;
                    _SkipText();
                }
                return;
            }
            m_CanTextProgress = true;
            break;

//		case Opcode.PLAY_BGM:
//			break;

        // -----Audio -----.
        case Opcode.PLAY_SOUND:
            byte soundCategory = code[pc + 1];                  // 0: BGM 1:SE 2: VOICE.
            byte soundID       = code[pc + 2];

            UnityWrapper.PlaySound(soundCategory, soundID);
            if (soundCategory == 2)
            {
                SET_CURRENT_VOICE_ID(true, soundID);
            }
            pc += 3;
            break;

        case Opcode.STOP_SOUND:
            //TODO :

            pc++;
            break;

        case Opcode.STOP_VOICE:
            SET_CURRENT_VOICE_ID(false, 0);
            if (ISoundPlayer.Instance != null)
            {
                ISoundPlayer.Instance.StopVoice();
            }
            pc++;
            break;

        default:
            ViNoDebugger.LogError("VM", "PC : " + pc);
            break;
        }
    }
    public override void OnInspectorGUI()
    {
//		serializedObject.Update();

        ScenarioNode scenario = target as ScenarioNode;

        if (Application.isPlaying)
        {
            // Ping Current Node in playmode.
            IScriptEngine scr = IScriptEngine.Instance;
            if (scr != null)
            {
                GUICommon.DrawScriptEngineCommonInspector(scr);
            }
        }
//*/
        Color savedCol = GUI.backgroundColor;

        GUI.backgroundColor = Color.green;

        GUILayout.BeginHorizontal();

        scenario.m_PlayAtStart = EditorGUILayout.Toggle("Play at Start", scenario.m_PlayAtStart);                   //, GUILayout.Width( 15f ) );
//			EditorGUILayout.LabelField( "Start and Play" , GUILayout.Width( 100f ) );

        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        scenario.startFromSave = EditorGUILayout.Toggle("Start from SavePoint", scenario.startFromSave);                   //, GUILayout.Width( 15f ) );
//			EditorGUILayout.LabelField( "Start from Savepoint" , GUILayout.Width( 150f ) );

        GUILayout.EndHorizontal();

        scenario.flagTable = EditorGUILayout.ObjectField("Flag", scenario.flagTable, typeof(FlagTable), false) as FlagTable;

//*/

//		scenario.saveInfo = EditorGUILayout.ObjectField( "SaveInfo" , scenario.saveInfo , typeof( ViNoSaveInfo ) , false ) as ViNoSaveInfo;
//		scenario.soundData = EditorGUILayout.ObjectField( "SoundData" , scenario.soundData , typeof( ScriptableSoundData ) , false ) as ScriptableSoundData;

/*		if ( soundDataProp != null){
 *                      EditorGUILayout.PropertyField( soundDataProp );
 *              }
 * //*/

// Dont show now.
// Adv Script Compile Option.
#if false
        if (GUILayout.Button("Node Names"))
        {
            List <string> list = scenario.GetNodeTagsUnderMe();
            for (int i = 0; i < list.Count; i++)
            {
                Debug.Log("Node:" + list[i]);
            }
        }

//		scenario.execMode = (ScenarioExecMode)EditorGUILayout.EnumPopup( "ExecLevel" , scenario.execMode );

        EditorGUILayout.LabelField("Scenario Script");
        scenarioScript = EditorGUILayout.TextArea(scenarioScript);

        GUILayout.BeginHorizontal();

        scenario.useCompiledScript = EditorGUILayout.Toggle(scenario.useCompiledScript);
        EditorGUILayout.LabelField("useCompiled", GUILayout.Width(100f));

        scenario.compiledScriptFile = EditorGUILayout.ObjectField(scenario.compiledScriptFile, typeof(TextAsset), false) as TextAsset;

        GUILayout.EndHorizontal();

        if (GUILayout.Button("Compile"))
        {
            CompileAndSaveAsBinaryFile(scenario.name, scenario);
        }

        GUILayout.BeginHorizontal();

        scenario.m_CacheCode = EditorGUILayout.Toggle(scenario.m_CacheCode, GUILayout.Width(15f));
        EditorGUILayout.LabelField("IsCacheCode", GUILayout.Width(100f));

        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();

        scenario.m_PlayOnEnable = EditorGUILayout.Toggle(scenario.m_PlayOnEnable, GUILayout.Width(15f));
        EditorGUILayout.LabelField("PlayOnEnable", GUILayout.Width(100f));

        GUILayout.EndHorizontal();
#endif

/*			GUILayout.BeginHorizontal();
 *
 *                              scenario.useScenarioScript = EditorGUILayout.Toggle( "use Scenario Script" , scenario.useScenarioScript );// , GUILayout.Width( 15f ) );
 * //				EditorGUILayout.LabelField( "useScenarioScript" , GUILayout.Width( 100f ) );
 *
 *                      GUILayout.EndHorizontal();
 * //*/
/*			if( scenario.useScenarioScript ){
 *                              scenario.scenarioScript = EditorGUILayout.ObjectField( "TextAsset" , scenario.scenarioScript , typeof( TextAsset ) , false  ) as TextAsset;
 *                      }
 *                      else{
 * //*/
//		GUI.enabled = ! scenario.useScenarioScript;

        GUILayout.BeginHorizontal();
        scenario.startNode = (ViNode)EditorGUILayout.ObjectField("START NODE", scenario.startNode, typeof(ViNode), true);
        GUILayout.EndHorizontal();
//			}

//		GUI.enabled = true;

//		GUI.enabled = Application.isPlaying;
//		NodeGUI.OnGUI_ViNode( scenario , true , false );

        if (Application.isPlaying)
        {
            if (GUILayout.Button("PLAY"))
            {
                if (AssetDatabase.Contains(scenario.gameObject))
                {
                    GameObject clone = Instantiate(scenario.gameObject) as GameObject;
                    ViNoGOExtensions.StripGameObjectName(clone, "(Clone)", "");
                }
                else
                {
                    scenario.Play();
                }
            }
        }
        else
        {
            GUILayout.BeginHorizontal();

            GUILayout.Space(30f);

            if (AssetDatabase.Contains(scenario.gameObject))
            {
                if (GUILayout.Button("Edit"))
                {
                    EditorUtility.DisplayDialog("Note", "If you finish editing in your scene , please do not forget to apply changes to the prefab", "Ok");
                    GameObject obj = PrefabUtility.InstantiatePrefab(scenario.gameObject) as GameObject;                      //AttachedAsset( scenario.gameObject );
                    PrefabUtility.ReplacePrefab(obj, scenario.gameObject, ReplacePrefabOptions.ConnectToPrefab);

                    EditorGUIUtility.PingObject(obj);
                }
            }
            else
            {
/*				if( GUILayout.Button ( "Save as ScenarioScript" ) ){
 *              //			scenarioScript = KAGInterpreter._TEST_TO_KIRIKIRI_SCRIPT( scenario.gameObject );
 *                                      string text = ViNoToolkit.KAGInterpreter.ToScenarioScript( scenario.gameObject );
 *              //			System.Text.StringBuilder sb = new System.Text.StringBuilder();
 *              //			scenario.ToScenarioScript( ref sb );
 *                                      string filePath = Application.dataPath + "/" + scenario.name + ".txt";
 *                                      Debug.Log( "Saved as a Scenario Script at :" + filePath );
 *                                      File.WriteAllText( filePath , text );
 *                                      AssetDatabase.Refresh();
 *
 *              //		    scenario.scenarioScript = AssetDatabase.LoadAssetAtPath( filePath , typeof(TextAsset) ) as TextAsset;
 *              //			scenarioScript = sb.ToString();
 *              //			Debug.Log ( sb.ToString ());
 *                              }
 * //*/
            }

            GUILayout.Space(30f);

            GUILayout.EndHorizontal();
        }

//		GUI.enabled = true;

        GUI.backgroundColor = savedCol;
//		serializedObject.ApplyModifiedProperties();
    }