예제 #1
0
 public void InitPersos()
 {
     if (loader != null)
     {
         for (int i = 0; i < loader.persos.Count; i++)
         {
             PersoBehaviour unityBehaviour = loader.persos[i].Gao.AddComponent <PersoBehaviour>();
             unityBehaviour.perso = loader.persos[i];
             unityBehaviour.Init();
         }
     }
 }
예제 #2
0
    async Task InitPersos()
    {
        if (loader != null)
        {
            for (int i = 0; i < loader.persos.Count; i++)
            {
                detailedState = "Initializing persos: " + i + "/" + loader.persos.Count;
                await WaitIfNecessary();

                Perso          p = loader.persos[i];
                PersoBehaviour unityBehaviour = p.Gao.AddComponent <PersoBehaviour>();
                unityBehaviour.controller = this;
                if (loader.globals != null && loader.globals.spawnablePersos != null)
                {
                    if (loader.globals.spawnablePersos.IndexOf(p) > -1)
                    {
                        unityBehaviour.IsAlways           = true;
                        unityBehaviour.transform.position = new Vector3(i * 10, -1000, 0);
                    }
                }
                if (!unityBehaviour.IsAlways)
                {
                    if (p.sectInfo != null && p.sectInfo.off_sector != null)
                    {
                        unityBehaviour.sector = sectorManager.sectors.FirstOrDefault(s => s.sector != null && s.sector.SuperObject.offset == p.sectInfo.off_sector);
                    }
                    else
                    {
                        SectorComponent sc = sectorManager.GetActiveSectorWrapper(p.Gao.transform.position);
                        unityBehaviour.sector = sc;
                    }
                }
                else
                {
                    unityBehaviour.sector = null;
                }
                unityBehaviour.perso = p;
                unityBehaviour.Init();

                // Scripts
                if (p.Gao)
                {
                    if (p.brain != null && p.brain.mind != null && p.brain.mind.AI_model != null)
                    {
                        if (p.brain.mind.AI_model.behaviors_normal != null)
                        {
                            GameObject intelParent = new GameObject("Rule behaviours");
                            intelParent.transform.parent = p.Gao.transform;
                            Behavior[] normalBehaviors = p.brain.mind.AI_model.behaviors_normal;
                            int        iter            = 0;
                            foreach (Behavior behavior in normalBehaviors)
                            {
                                string     shortName   = behavior.GetShortName(p.brain.mind.AI_model, Behavior.BehaviorType.Intelligence, iter);
                                GameObject behaviorGao = new GameObject(shortName);
                                behaviorGao.transform.parent = intelParent.transform;
                                foreach (Script script in behavior.scripts)
                                {
                                    GameObject scriptGao = new GameObject("Script");
                                    scriptGao.transform.parent = behaviorGao.transform;
                                    ScriptComponent scriptComponent = scriptGao.AddComponent <ScriptComponent>();
                                    scriptComponent.SetScript(script, p);
                                }
                                if (behavior.firstScript != null)
                                {
                                    ScriptComponent scriptComponent = behaviorGao.AddComponent <ScriptComponent>();
                                    scriptComponent.SetScript(behavior.firstScript, p);
                                }
                                if (iter == 0)
                                {
                                    behaviorGao.name += " (Init)";
                                }
                                if ((behavior.scripts == null || behavior.scripts.Length == 0) && behavior.firstScript == null)
                                {
                                    behaviorGao.name += " (Empty)";
                                }
                                iter++;
                            }
                        }
                        if (p.brain.mind.AI_model.behaviors_reflex != null)
                        {
                            GameObject reflexParent = new GameObject("Reflex behaviours");
                            reflexParent.transform.parent = p.Gao.transform;
                            Behavior[] reflexBehaviors = p.brain.mind.AI_model.behaviors_reflex;
                            int        iter            = 0;
                            foreach (Behavior behavior in reflexBehaviors)
                            {
                                string     shortName   = behavior.GetShortName(p.brain.mind.AI_model, Behavior.BehaviorType.Reflex, iter);
                                GameObject behaviorGao = new GameObject(shortName);
                                behaviorGao.transform.parent = reflexParent.transform;
                                foreach (Script script in behavior.scripts)
                                {
                                    GameObject scriptGao = new GameObject("Script");
                                    scriptGao.transform.parent = behaviorGao.transform;
                                    ScriptComponent scriptComponent = scriptGao.AddComponent <ScriptComponent>();
                                    scriptComponent.SetScript(script, p);
                                }
                                if (behavior.firstScript != null)
                                {
                                    ScriptComponent scriptComponent = behaviorGao.AddComponent <ScriptComponent>();
                                    scriptComponent.SetScript(behavior.firstScript, p);
                                }
                                if ((behavior.scripts == null || behavior.scripts.Length == 0) && behavior.firstScript == null)
                                {
                                    behaviorGao.name += " (Empty)";
                                }
                                iter++;
                            }
                        }
                        if (p.brain.mind.AI_model.macros != null)
                        {
                            GameObject macroParent = new GameObject("Macros");
                            macroParent.transform.parent = p.Gao.transform;
                            Macro[] macros = p.brain.mind.AI_model.macros;
                            int     iter   = 0;

                            foreach (Macro macro in macros)
                            {
                                GameObject behaviorGao = new GameObject(macro.GetShortName(p.brain.mind.AI_model, iter));
                                behaviorGao.transform.parent = macroParent.transform;
                                ScriptComponent scriptComponent = behaviorGao.AddComponent <ScriptComponent>();
                                scriptComponent.SetScript(macro.script, p);
                                iter++;
                            }
                        }
                    }
                }
            }
            // Initialize DSGVars after all persos have their perso behaviours
            for (int i = 0; i < loader.persos.Count; i++)
            {
                Perso    p   = loader.persos[i];
                Moddable mod = null;
                if (p.SuperObject != null && p.SuperObject.Gao != null)
                {
                    mod = p.SuperObject.Gao.GetComponent <Moddable>();
                    if (mod != null)
                    {
                        mod.persoBehaviour = p.Gao.GetComponent <PersoBehaviour>();
                    }
                }
                if (p.Gao && p.brain != null && p.brain.mind != null && p.brain.mind.AI_model != null)
                {
                    // DsgVars
                    if (p.brain.mind.dsgMem != null || p.brain.mind.AI_model.dsgVar != null)
                    {
                        DsgVarComponent dsgVarComponent = p.Gao.AddComponent <DsgVarComponent>();
                        dsgVarComponent.SetPerso(p);
                        if (mod != null)
                        {
                            mod.dsgVarComponent = dsgVarComponent;
                        }
                    }
                    // Dynam
                    if (p.dynam != null && p.dynam.dynamics != null)
                    {
                        DynamicsMechanicsComponent dynamicsBehaviour = p.Gao.AddComponent <DynamicsMechanicsComponent>();
                        dynamicsBehaviour.SetDynamics(p.dynam.dynamics);
                    }
                    // Mind
                    if (p.brain != null && p.brain.mind != null)
                    {
                        MindComponent mindComponent = p.Gao.AddComponent <MindComponent>();
                        mindComponent.Init(p, p.brain.mind);
                        if (mod != null)
                        {
                            mod.mindComponent = mindComponent;
                        }
                    }
                    // Custom Bits
                    if (p.stdGame != null)
                    {
                        CustomBitsComponent c = p.Gao.AddComponent <CustomBitsComponent>();
                        c.stdGame = p.stdGame;
                        if (Settings.s.engineVersion == Settings.EngineVersion.R3)
                        {
                            c.hasAiCustomBits = true;
                        }
                        c.Init();
                    }
                }
            }
        }
        if (loader is OpenSpace.Loader.R2ROMLoader)
        {
            OpenSpace.Loader.R2ROMLoader romLoader = loader as OpenSpace.Loader.R2ROMLoader;
            if (romPersos.Count > 0)
            {
                for (int i = 0; i < romPersos.Count; i++)
                {
                    detailedState = "Initializing persos: " + i + "/" + romPersos.Count;
                    await WaitIfNecessary();

                    ROMPersoBehaviour unityBehaviour = romPersos[i];
                    unityBehaviour.controller = this;

                    /*if (loader.globals != null && loader.globals.spawnablePersos != null) {
                     *      if (loader.globals.spawnablePersos.IndexOf(p) > -1) {
                     *              unityBehaviour.IsAlways = true;
                     *              unityBehaviour.transform.position = new Vector3(i * 10, -1000, 0);
                     *      }
                     * }*/
                    if (!unityBehaviour.IsAlways)
                    {
                        SectorComponent sc = sectorManager.GetActiveSectorWrapper(unityBehaviour.transform.position);
                        unityBehaviour.sector = sc;
                    }
                    else
                    {
                        unityBehaviour.sector = null;
                    }

                    /*Moddable mod = null;
                     * if (p.SuperObject != null && p.SuperObject.Gao != null) {
                     *      mod = p.SuperObject.Gao.GetComponent<Moddable>();
                     *      if (mod != null) {
                     *              mod.persoBehaviour = unityBehaviour;
                     *      }
                     * }*/
                    unityBehaviour.Init();

                    var iteratorPerso = unityBehaviour.perso;

                    // Of sound brain and AI model?
                    if (iteratorPerso.brain?.Value?.aiModel?.Value != null)
                    {
                        var aiModel = iteratorPerso.brain.Value.aiModel.Value;

                        // DsgVars
                        if (iteratorPerso.brain?.Value?.dsgMem?.Value != null || aiModel.dsgVar?.Value != null)
                        {
                            DsgVarComponent dsgVarComponent = unityBehaviour.gameObject.AddComponent <DsgVarComponent>();
                            dsgVarComponent.SetPerso(iteratorPerso);
                        }

                        // Comports
                        if (aiModel.comportsIntelligence.Value != null)
                        {
                            aiModel.comportsIntelligence.Value.CreateGameObjects("Rule", unityBehaviour.gameObject, iteratorPerso);
                        }
                        if (aiModel.comportsReflex.Value != null)
                        {
                            aiModel.comportsReflex.Value.CreateGameObjects("Reflex", unityBehaviour.gameObject, iteratorPerso);
                        }
                    }
                }
            }
            if (romLoader.level != null && romLoader.level.spawnablePersos.Value != null && romLoader.level.num_spawnablepersos > 0)
            {
                GameObject spawnableParent = new GameObject("Spawnable persos");
                for (int i = 0; i < romLoader.level.num_spawnablepersos; i++)
                {
                    detailedState = "Initializing spawnable persos: " + i + "/" + romLoader.level.num_spawnablepersos;
                    await WaitIfNecessary();

                    OpenSpace.ROM.SuperObjectDynamic sod = romLoader.level.spawnablePersos.Value.superObjects[i];
                    GameObject sodGao = sod.GetGameObject();
                    if (sodGao != null)
                    {
                        ROMPersoBehaviour unityBehaviour = sodGao.GetComponent <ROMPersoBehaviour>();
                        unityBehaviour.controller = this;
                        unityBehaviour.IsAlways   = true;
                        unityBehaviour.transform.SetParent(spawnableParent.transform);
                        unityBehaviour.transform.position   = new Vector3(i * 10, -1000, 0);
                        unityBehaviour.transform.rotation   = Quaternion.identity;
                        unityBehaviour.transform.localScale = Vector3.one;
                        if (!unityBehaviour.IsAlways)
                        {
                            SectorComponent sc = sectorManager.GetActiveSectorWrapper(unityBehaviour.transform.position);
                            unityBehaviour.sector = sc;
                        }
                        else
                        {
                            unityBehaviour.sector = null;
                        }
                        unityBehaviour.Init();

                        var iteratorPerso = unityBehaviour.perso;

                        // Of sound brain and AI model?
                        if (iteratorPerso.brain?.Value?.aiModel?.Value != null)
                        {
                            var aiModel = iteratorPerso.brain.Value.aiModel.Value;

                            // DsgVars
                            if (iteratorPerso.brain?.Value?.dsgMem?.Value != null || aiModel.dsgVar?.Value != null)
                            {
                                DsgVarComponent dsgVarComponent = unityBehaviour.gameObject.AddComponent <DsgVarComponent>();
                                dsgVarComponent.SetPerso(iteratorPerso);
                            }

                            // Comports
                            if (aiModel.comportsIntelligence.Value != null)
                            {
                                aiModel.comportsIntelligence.Value.CreateGameObjects("Rule", unityBehaviour.gameObject, iteratorPerso);
                            }
                            if (aiModel.comportsReflex.Value != null)
                            {
                                aiModel.comportsReflex.Value.CreateGameObjects("Reflex", unityBehaviour.gameObject, iteratorPerso);
                            }
                        }
                    }
                }
            }
        }
    }