예제 #1
0
    public void UpdateLivePreview()
    {
        Reader reader = MapLoader.Loader.livePreviewReader;

        foreach (SuperObject so in MapLoader.Loader.superObjects)
        {
            if (!(so.data is Perso))
            {
                continue;
            }

            if (so.off_matrix == null)
            {
                continue;
            }
            Pointer.Goto(ref reader, so.off_matrix);
            so.matrix = Matrix.Read(MapLoader.Loader.livePreviewReader, so.off_matrix);
            if (so.data != null && so.data.Gao != null)
            {
                so.data.Gao.transform.localPosition = so.matrix.GetPosition(convertAxes: true);
                so.data.Gao.transform.localRotation = so.matrix.GetRotation(convertAxes: true);
                so.data.Gao.transform.localScale    = so.matrix.GetScale(convertAxes: true);

                if (so.data is Perso)
                {
                    Perso perso = (Perso)so.data;

                    PersoBehaviour pb = perso.Gao.GetComponent <PersoBehaviour>();
                    if (pb != null)
                    {
                        Pointer.Goto(ref reader, perso.p3dData.offset);
                        perso.p3dData.UpdateCurrentState(reader);

                        // State offset changed?
                        if (perso.p3dData.stateCurrent != null)
                        {
                            pb.SetState(perso.p3dData.stateCurrent.index);
                            pb.autoNextState = true;
                        }
                    }

                    MindComponent mc = perso.Gao.GetComponent <MindComponent>();
                    if (mc != null)
                    {
                        Mind mind = mc.mind;
                        Pointer.DoAt(ref reader, mind.Offset, () => {
                            mind.UpdateCurrentBehaviors(reader);
                        });
                    }

                    DsgVarComponent dsgVarComponent = perso.Gao.GetComponent <DsgVarComponent>();
                    if (dsgVarComponent != null)
                    {
                        dsgVarComponent.SetPerso(perso);
                    }

                    CustomBitsComponent customBitsComponent = perso.Gao.GetComponent <CustomBitsComponent>();
                    if (customBitsComponent != null)
                    {
                        Pointer.Goto(ref reader, perso.off_stdGame);
                        perso.stdGame = StandardGame.Read(reader, perso.off_stdGame);
                        customBitsComponent.stdGame = perso.stdGame;
                        customBitsComponent.Init();
                    }

                    DynamicsMechanicsComponent dnComponent = perso.Gao.GetComponent <DynamicsMechanicsComponent>();
                    if (dnComponent != null)
                    {
                        Pointer.DoAt(ref reader, perso.off_dynam, () => {
                            perso.dynam = Dynam.Read(reader, perso.off_dynam);
                        });

                        dnComponent.SetDynamics(perso.dynam.dynamics);
                    }
                }
            }
        }

        Perso camera = loader.persos.FirstOrDefault(p => p != null && p.namePerso.Equals("StdCamer"));

        if (camera != null)
        {
            SuperObject cameraSO = camera.SuperObject;
            Pointer.Goto(ref reader, cameraSO.off_matrix);
            cameraSO.matrix = Matrix.Read(reader, cameraSO.off_matrix);
            camera.Gao.transform.localPosition = cameraSO.matrix.GetPosition(convertAxes: true);
            camera.Gao.transform.localRotation = cameraSO.matrix.GetRotation(convertAxes: true);
            camera.Gao.transform.localScale    = cameraSO.matrix.GetScale(convertAxes: true);

            Camera.main.transform.position = camera.Gao.transform.position;
            Camera.main.transform.rotation = camera.Gao.transform.rotation * Quaternion.Euler(0, 180, 0);
        }
    }
예제 #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);
                            }
                        }
                    }
                }
            }
        }
    }