public Cv_Event_RemoteSceneLoaded(string sceneResource, Cv_SceneID sceneID, string sceneName, string resourceBundle, object sender, float timeStamp = 0) : base(Cv_EntityID.INVALID_ENTITY, sender, timeStamp)
 {
     SceneID        = sceneID;
     SceneName      = sceneName;
     SceneResource  = sceneResource;
     ResourceBundle = resourceBundle;
 }
Exemple #2
0
 protected override Cv_Entity CreateEntity(string entityTypeResource,
                                           Cv_Entity.Cv_EntityID parent,
                                           Cv_Entity.Cv_EntityID serverEntityID,
                                           string resourceBundle,
                                           Cv_SceneID sceneID,
                                           string sceneName)
 {
     return(base.CreateEntity(entityTypeResource, parent, serverEntityID, resourceBundle, sceneID, sceneName));
 }
Exemple #3
0
        private List <Cv_Entity> CreateNestedEntities(XmlNodeList entities, Cv_EntityID parentId,
                                                      string resourceBundle = null, Cv_SceneID sceneID = Cv_SceneID.INVALID_SCENE)
        {
            List <Cv_Entity> entitiesCreated = new List <Cv_Entity>();

            if (entities != null)
            {
                foreach (XmlNode e in entities)
                {
                    var visible = true;

                    if (e.Attributes["visible"] != null)
                    {
                        visible = bool.Parse(e.Attributes["visible"].Value);
                    }

                    if (e.Name == "Entity")
                    {
                        var entity = InstantiateSceneEntity(false, e, resourceBundle, parentId, sceneID);

                        entitiesCreated.Add(entity);

                        var childEntities = e.SelectNodes("./Entity|./Scene");

                        if (childEntities.Count > 0)
                        {
                            entitiesCreated.AddRange(CreateNestedEntities(childEntities, entity.ID, resourceBundle, sceneID));
                        }
                    }
                    else
                    {
                        var name          = e.Attributes?["name"].Value;
                        var sceneResource = e.Attributes["resource"].Value;

                        if (sceneResource == null || sceneResource == "")
                        {
                            continue;
                        }

                        var sceneOverrides = GetSceneOverrides((XmlElement)e);

                        var createdEntities = LoadScene(sceneResource, resourceBundle, name, sceneOverrides, null, parentId);

                        if (createdEntities == null || createdEntities.Length <= 0)
                        {
                            Cv_Debug.Warning("Unable to load a sub scene that as part of another scene [" + name + "]");
                            continue;
                        }

                        entitiesCreated.AddRange(createdEntities);
                    }
                }
            }

            return(entitiesCreated);
        }
Exemple #4
0
        internal string GetScenePath(Cv_SceneID sceneID)
        {
            lock (m_Scenes)
            {
                if (m_Scenes.ContainsKey(sceneID))
                {
                    return(m_Scenes[sceneID].ScenePath);
                }
            }

            return(null);
        }
Exemple #5
0
        internal Cv_Entity GetSceneRoot(Cv_SceneID sceneID)
        {
            lock (m_Scenes)
            {
                if (m_Scenes.ContainsKey(sceneID))
                {
                    return(CaravelApp.Instance.Logic.GetEntity(m_Scenes[sceneID].SceneRoot));
                }
            }

            return(null);
        }
Exemple #6
0
 public Cv_Event_RequestNewEntity(string entityResource, Cv_SceneID sceneID, string sceneName, string entityName, string resourceBundle, bool visible,
                                  Cv_EntityID parentId, Cv_Transform?initialTransform, object sender,
                                  Cv_EntityID serverEntityID = Cv_EntityID.INVALID_ENTITY,
                                  Cv_GameViewID gameViewId   = Cv_GameViewID.INVALID_GAMEVIEW) : base(Cv_EntityID.INVALID_ENTITY, sender)
 {
     SceneID          = sceneID;
     SceneName        = sceneName;
     EntityName       = entityName;
     EntityResource   = entityResource;
     InitialTransform = (initialTransform != null ? initialTransform.Value : Cv_Transform.Identity);
     ServerEntityID   = serverEntityID;
     GameViewID       = gameViewId;
     Parent           = parentId;
 }
Exemple #7
0
        public Cv_Entity CreateEmptyEntity(string name,
                                           string resourceBundle,
                                           bool visible               = true,
                                           Cv_EntityID parentID       = Cv_EntityID.INVALID_ENTITY,
                                           XmlElement overrides       = null,
                                           Cv_Transform?transform     = null,
                                           Cv_SceneID sceneID         = Cv_SceneID.INVALID_SCENE,
                                           Cv_EntityID serverEntityID = Cv_EntityID.INVALID_ENTITY)
        {
            var entity = InstantiateNewEntity(false, null, name, resourceBundle, visible, parentID,
                                              overrides, transform, sceneID, serverEntityID);

            entity.PostLoad();
            return(entity);
        }
Exemple #8
0
        public void UnloadScene(Cv_SceneID sceneID = Cv_SceneID.INVALID_SCENE)
        {
            var scene = m_SceneManager.MainScene;

            if (sceneID != Cv_SceneID.INVALID_SCENE)
            {
                scene = sceneID;
            }
            else if (scene == Cv_SceneID.INVALID_SCENE)
            {
                return;
            }

            m_SceneManager.UnloadScene(m_SceneManager.GetScenePath(scene));
        }
Exemple #9
0
        protected override bool VGameOnLoadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
        {
            if (sceneName == "Root")
            {
                var scriptElement = sceneData.SelectNodes("Script").Item(0);

                if (scriptElement != null)
                {
                    CurrentScenePreLoadScript  = scriptElement.Attributes["preLoad"].Value;
                    CurrentScenePostLoadScript = scriptElement.Attributes["postLoad"].Value;
                    CurrentSceneUnLoadScript   = scriptElement.Attributes["unLoad"].Value;
                }
            }

            return(true);
        }
Exemple #10
0
        public Cv_Entity[] GetSceneEntities(Cv_SceneID sceneID)
        {
            var entityList = new List <Cv_Entity>();

            lock (Entities)
            {
                foreach (var e in Entities)
                {
                    if (e.Value.SceneID == sceneID)
                    {
                        entityList.Add(e.Value);
                    }
                }
            }

            return(entityList.ToArray());
        }
Exemple #11
0
        internal bool OnUnloadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName, string sceneResource, string resourceBundle)
        {
            VGameOnUnloadScene(sceneData, sceneID, sceneName);

            if (IsProxy)
            {
                var remoteSceneUnloadedEvent = new Cv_Event_RemoteSceneUnloaded(sceneResource, sceneID, sceneName, resourceBundle, this);
                Cv_EventManager.Instance.TriggerEvent(remoteSceneUnloadedEvent);
            }
            else
            {
                var sceneUnloadedEvent = new Cv_Event_SceneUnloaded(sceneResource, sceneID, sceneName, resourceBundle, this);
                Cv_EventManager.Instance.TriggerEvent(sceneUnloadedEvent);
            }

            return(true);
        }
Exemple #12
0
        private Cv_Entity InstantiateSceneEntity(bool sceneRoot, XmlNode e, string resourceBundle, Cv_EntityID parentId,
                                                 Cv_SceneID sceneID = Cv_SceneID.INVALID_SCENE, XmlElement sceneOverrides = null)
        {
            Cv_Entity entity             = null;
            var       entityTypeResource = e.Attributes["type"].Value;
            var       name    = e.Attributes?["name"].Value;
            var       visible = true;

            if (e.Attributes["visible"] != null)
            {
                visible = bool.Parse(e.Attributes["visible"].Value);
            }

            var overrides = e.OwnerDocument.CreateElement("Overrides");

            foreach (XmlNode o in e.ChildNodes)
            {
                var newNode = o.CloneNode(true);
                overrides.AppendChild(newNode);
            }

            if (sceneOverrides != null)
            {
                foreach (XmlNode o in sceneOverrides.ChildNodes)
                {
                    var newNode = o.CloneNode(true);
                    overrides.AppendChild(overrides.OwnerDocument.ImportNode(newNode, true));
                }
            }

            if (entityTypeResource != "")
            {
                entity = Caravel.Logic.InstantiateNewEntity(sceneRoot, entityTypeResource, name, resourceBundle,
                                                            visible, parentId, overrides,
                                                            null, sceneID, Cv_EntityID.INVALID_ENTITY);
            }
            else
            {
                entity = Caravel.Logic.InstantiateNewEntity(sceneRoot, null, name, resourceBundle,
                                                            visible, parentId, overrides,
                                                            null, sceneID, Cv_EntityID.INVALID_ENTITY);
            }

            return(entity);
        }
Exemple #13
0
 internal Cv_Entity(Cv_EntityID entityId, string resourceBundle, string sceneName, Cv_SceneID sceneID)
 {
     SceneName            = sceneName;
     SceneID              = sceneID;
     ID                   = entityId;
     EntityType           = "Unknown";
     EntityTypeResource   = "";
     EntityName           = "Unknown_" + ID;
     EntityPath           = "/" + EntityName;
     m_ComponentMap       = new Dictionary <Cv_ComponentID, Cv_EntityComponent>();
     m_ComponentList      = new List <Cv_EntityComponent>();
     m_ComponentsToAdd    = new List <Cv_EntityComponent>();
     m_ComponentsToRemove = new List <Cv_EntityComponent>();
     ResourceBundle       = resourceBundle;
     DestroyRequested     = false;
     Visible              = true;
     m_Children           = new List <Cv_Entity>();
 }
Exemple #14
0
        public void SetMainScene(Cv_SceneID sceneID)
        {
            var path = GetScenePath(sceneID);

            if (IsSceneLoaded(path))
            {
                m_SceneManager.MainScene = sceneID;

                if (!IsSceneLoaded(path))
                {
                    Cv_Debug.Warning("Main scene unloaded after being set.");
                }
            }
            else
            {
                Cv_Debug.Error("Trying to set a scene that is not loaded as the main scene.");
            }
        }
Exemple #15
0
        internal bool OnLoadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
        {
            lock (m_GameViews)
            {
                foreach (var gv in m_GameViews)
                {
                    if (gv.Type == Cv_GameViewType.Player)
                    {
                        var playerView = (Cv_PlayerView)gv;
                        playerView.LoadGame(sceneData);
                    }
                }
            }

            if (!VGameOnLoadScene(sceneData, sceneID, sceneName))
            {
                return(false);
            }

            return(true);
        }
Exemple #16
0
 public Cv_Entity GetSceneRoot(Cv_SceneID sceneID)
 {
     return(m_SceneManager.GetSceneRoot(sceneID));
 }
Exemple #17
0
        internal bool UnloadScene(string scenePath)
        {
            Cv_SceneInfo sceneInfo;

            if (!m_ScenePaths.TryGetValue(scenePath, out sceneInfo))
            {
                return(true);
            }

            Cv_XmlResource resource;

            resource = Cv_ResourceManager.Instance.GetResource <Cv_XmlResource>(sceneInfo.SceneResource, sceneInfo.ResourceBundle, Caravel.EditorRunning);

            var root = ((Cv_XmlData)resource.ResourceData).RootNode;

            if (root == null)
            {
                Cv_Debug.Error("Failed to unload scene resource file: " + sceneInfo.SceneResource);
                return(false);
            }

            if (!Caravel.Logic.OnPreUnloadScene(root, sceneInfo.ID, sceneInfo.SceneName))
            {
                Cv_Debug.Error("Failed to unload scene resource file: " + sceneInfo.SceneResource);
                return(false);
            }

            string unloadScript = null;

            var scriptElement = root.SelectNodes("Script").Item(0);

            if (scriptElement != null)
            {
                unloadScript = scriptElement.Attributes["unLoad"].Value;
            }

            if (unloadScript != null && unloadScript != "")
            {
                Cv_ScriptResource unLoadRes = Cv_ResourceManager.Instance.GetResource <Cv_ScriptResource>(unloadScript, sceneInfo.ResourceBundle);
                unLoadRes.RunScript();
            }

            var entitiesToRemove = Caravel.Logic.GetSceneEntities(sceneInfo.ID);

            foreach (var e in entitiesToRemove)
            {
                if (e != null && !e.DestroyRequested)
                {
                    Caravel.Logic.DestroyEntity(e);
                }
            }

            Caravel.Logic.OnUnloadScene(root, sceneInfo.ID, sceneInfo.SceneName, sceneInfo.SceneResource, sceneInfo.ResourceBundle);

            lock (m_Scenes)
            {
                m_Scenes.Remove(sceneInfo.ID);
                m_ScenePaths.Remove(scenePath);
            }

            if (MainScene == sceneInfo.ID && m_Scenes.Count > 0)
            {
                MainScene = m_Scenes.Keys.First();
            }

            return(true);
        }
Exemple #18
0
        protected internal Cv_Entity CreateEmptyEntity(Cv_EntityID parent, Cv_EntityID serverEntityID, string resourceBundle, Cv_SceneID sceneID, string sceneName)
        {
            Cv_EntityID entityId = serverEntityID;

            if (entityId == Cv_EntityID.INVALID_ENTITY)
            {
                lock (m_Mutex)
                {
                    entityId = GetNextEntityID();
                }
            }

            var entity = new Cv_Entity(entityId, resourceBundle, sceneName, sceneID);

            if (!entity.Initialize(null, null, parent))
            {
                Cv_Debug.Error("Failed to initialize empty entity.");
                return(null);
            }

            return(entity);
        }
Exemple #19
0
        internal Cv_Entity InstantiateNewEntity(bool isSceneRoot, string entityTypeResource, string name, string resourceBundle,
                                                bool visible, Cv_EntityID parentID, XmlElement overrides,
                                                Cv_Transform?transform, Cv_SceneID sceneID, Cv_EntityID serverEntityID)
        {
            if (!CanCreateEntity(name, serverEntityID))
            {
                return(null);
            }

            var scene     = sceneID == Cv_SceneID.INVALID_SCENE ? m_SceneManager.MainScene : sceneID;
            var sceneName = m_SceneManager.GetSceneName(scene);

            Cv_Debug.Assert(sceneName != null, "Trying to add an entity to an invalid scene [" + scene + ", " + name + "]");

            var path = "/" + name;

            if (isSceneRoot) //Scene roots are named after the scene id
            {
                path = "/" + sceneName;
                name = sceneName;
            }

            if (parentID == Cv_EntityID.INVALID_ENTITY)
            {
                if (!isSceneRoot)
                {
                    var sceneRoot = m_SceneManager.GetSceneRoot(scene);
                    path = sceneRoot.EntityPath + path;
                    Cv_Debug.Assert(sceneRoot != null, "Trying to add an entity to an invalid scene [" + scene + ", " + name + "]");
                    parentID = sceneRoot.ID;
                }
            }
            else
            {
                var parent = GetEntity(parentID);
                if (parent == null)
                {
                    Cv_Debug.Warning("Attempting to add an entity to a parent that doesn't exist.");
                    return(null);
                }

                if (parent.SceneID != scene && !isSceneRoot)
                {
                    scene     = parent.SceneID;
                    sceneName = parent.SceneName;

                    Cv_Debug.Warning("Attempting to add an entity of a scene to a parent that is not of the same scene [" + scene + ", " + name + "]. Adding to parent scene instead.");
                }

                path = parent.EntityPath + path;
            }

            Cv_Debug.Assert(!EntitiesByPath.ContainsKey(path), "All entities with the same parent must have a unique ID. Trying to add repeated entity [" + scene + ", " + name + "]");

            Cv_Entity entity = null;

            if (entityTypeResource != null)
            {
                entity = m_EntityFactory.CreateEntity(entityTypeResource, parentID, serverEntityID, resourceBundle, scene, sceneName);
            }
            else
            {
                entity = m_EntityFactory.CreateEmptyEntity(parentID, serverEntityID, resourceBundle, scene, sceneName);
            }

            if (entity != null)
            {
                entity.EntityName = name;
                entity.EntityPath = path;
                entity.Visible    = visible;
                entity.SceneRoot  = isSceneRoot;
                m_EntitiesToAdd.Enqueue(entity);

                lock (Entities)
                {
                    Entities.Add(entity.ID, entity);
                    EntitiesByPath.Add(entity.EntityPath, entity);
                }

                if (overrides != null)
                {
                    m_EntityFactory.ModifyEntity(entity, overrides.SelectNodes("./*[not(self::Entity|self::Scene)]"));
                }

                var tranformComponent = entity.GetComponent <Cv_TransformComponent>();
                if (tranformComponent != null && transform != null)
                {
                    tranformComponent.Transform = transform.Value;
                }

                LastEntityID = entity.ID;

                entity.PostInitialize();

                if (!IsProxy && State == Cv_GameState.Running)
                {
                    var requestNewEntityEvent = new Cv_Event_RequestNewEntity(null, scene, sceneName, entity.EntityName, resourceBundle, visible, parentID, transform, entity.ID);
                    Cv_EventManager.Instance.TriggerEvent(requestNewEntityEvent);
                }

                var newEntityEvent = new Cv_Event_NewEntity(entity.ID, this);
                Cv_EventManager.Instance.TriggerEvent(newEntityEvent);

                return(entity);
            }

            Cv_Debug.Error("Could not create entity with resource [" + resourceBundle + "].");
            return(null);
        }
Exemple #20
0
        virtual protected internal Cv_Entity CreateEntity(string entityTypeResource, Cv_EntityID parent, Cv_EntityID serverEntityID, string resourceBundle, Cv_SceneID sceneID, string sceneName)
        {
            Cv_XmlResource resource;

            resource = Cv_ResourceManager.Instance.GetResource <Cv_XmlResource>(entityTypeResource, resourceBundle, CaravelApp.Instance.EditorRunning);

            XmlElement root = ((Cv_XmlResource.Cv_XmlData)resource.ResourceData).RootNode;

            if (root == null)
            {
                Cv_Debug.Error("Failed to load entity resource file: " + entityTypeResource);
                return(null);
            }

            Cv_EntityID entityId = serverEntityID;

            if (entityId == Cv_EntityID.INVALID_ENTITY)
            {
                lock (m_Mutex)
                {
                    entityId = GetNextEntityID();
                }
            }

            var entity = new Cv_Entity(entityId, resourceBundle, sceneName, sceneID);

            if (!entity.Initialize(entityTypeResource, root, parent))
            {
                Cv_Debug.Error("Failed to initialize entity: " + entityTypeResource);
                return(null);
            }

            foreach (var componentNode in root.ChildNodes)
            {
                if (componentNode.GetType() != typeof(XmlElement))
                {
                    continue;
                }

                var component = CreateComponent((XmlElement)componentNode);

                if (component != null)
                {
                    entity.AddComponent(component);
                }
                else
                {
                    return(null);
                }
            }

            return(entity);
        }
Exemple #21
0
 internal bool OnPreUnloadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
 {
     VGameOnPreUnloadScene(sceneData, sceneID, sceneName);
     return(true);
 }
Exemple #22
0
 protected virtual bool VGameOnLoadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
 {
     return(true);
 }
Exemple #23
0
 protected virtual void VGameOnUnloadScene(XmlElement sceneData, Cv_SceneID sceneID, string sceneName)
 {
 }
Exemple #24
0
 public string GetScenePath(Cv_SceneID sceneID)
 {
     return(m_SceneManager.GetScenePath(sceneID));
 }
Exemple #25
0
 public string GetSceneResource(Cv_SceneID sceneID)
 {
     return(m_SceneManager.GetSceneResource(sceneID));
 }
Exemple #26
0
        internal Cv_Entity[] LoadScene(string sceneResource, string resourceBundle, string sceneName, XmlElement overrides,
                                       Cv_Transform?sceneTransform, Cv_EntityID parentID = Cv_EntityID.INVALID_ENTITY)
        {
            Cv_XmlResource resource;

            resource = Cv_ResourceManager.Instance.GetResource <Cv_XmlResource>(sceneResource, resourceBundle, Caravel.EditorRunning);

            var root = ((Cv_XmlData)resource.ResourceData).RootNode;

            if (root == null)
            {
                Cv_Debug.Error("Failed to load scene resource file: " + sceneResource);
                return(null);
            }

            if (!Caravel.Logic.OnPreLoadScene(root, sceneName))
            {
                return(null);
            }

            Cv_SceneID newID = (Cv_SceneID)m_iLastSceneID;

            m_iLastSceneID++;

            if (MainScene == Cv_SceneID.INVALID_SCENE)
            {
                MainScene = newID;
            }

            var scenePath = "/" + sceneName;
            var parent    = CaravelApp.Instance.Logic.GetEntity(parentID);

            if (parent != null)
            {
                scenePath = parent.EntityPath + scenePath;
            }

            Cv_SceneInfo info = new Cv_SceneInfo();

            info.SceneResource  = sceneResource;
            info.ResourceBundle = resourceBundle;
            info.InitTransform  = sceneTransform;
            info.SceneName      = sceneName;
            info.ID             = newID;
            info.ScenePath      = scenePath;
            info.parentID       = parentID;
            info.SceneRoot      = Cv_EntityID.INVALID_ENTITY;

            lock (m_Scenes)
            {
                m_Scenes.Add(newID, info);
                m_ScenePaths.Add(scenePath, info);
            }

            string preLoadScript  = null;
            string postLoadScript = null;

            var scriptElement = root.SelectNodes("Script").Item(0);

            if (scriptElement != null)
            {
                preLoadScript  = scriptElement.Attributes["preLoad"].Value;
                postLoadScript = scriptElement.Attributes["postLoad"].Value;
            }

            if (preLoadScript != null && preLoadScript != "")
            {
                Cv_ScriptResource preLoadRes = Cv_ResourceManager.Instance.GetResource <Cv_ScriptResource>(preLoadScript, resourceBundle);
                preLoadRes.RunScript();
            }

            var sceneRootNodes = root.SelectNodes("StaticEntities/Entity|StaticEntities/Scene");

            if (sceneRootNodes.Count > 1 || sceneRootNodes.Count <= 0 || sceneRootNodes.Item(0).Name == "Scene")
            {
                Cv_Debug.Error("Invalid scene root node for scene " + sceneName + "(" + sceneResource + ").\n Either the scene has multiple roots, no root or its root is not an entity.");
                return(null);
            }

            XmlElement overridesNode = overrides;

            if (sceneTransform != null)
            {
                if (overridesNode == null)
                {
                    XmlDocument doc = new XmlDocument();
                    overridesNode = doc.CreateElement("Overrides");
                }

                var transformNode = GenerateTransformXml(sceneTransform.Value, overridesNode.OwnerDocument);
                overridesNode.AppendChild(transformNode);
            }

            var sceneRootNode = sceneRootNodes.Item(0);
            var sceneRoot     = InstantiateSceneEntity(true, sceneRootNode, resourceBundle, parentID, newID, overridesNode);

            if (sceneRoot != null)
            {
                info.SceneRoot = sceneRoot.ID;

                var entitiesCreated = new List <Cv_Entity>();
                entitiesCreated.Add(sceneRoot);

                var entitiesNodes = sceneRootNode.SelectNodes("Entity|Scene");
                entitiesCreated.AddRange(CreateNestedEntities(entitiesNodes, sceneRoot.ID, resourceBundle, newID));

                if (!Caravel.Logic.OnLoadScene(root, newID, sceneName))
                {
                    if (MainScene == newID)
                    {
                        MainScene = Cv_SceneID.INVALID_SCENE;
                    }

                    var entitiesToRemove = Caravel.Logic.GetSceneEntities(newID);
                    foreach (var e in entitiesToRemove)
                    {
                        if (e != null && !e.DestroyRequested)
                        {
                            Caravel.Logic.DestroyEntity(e);
                        }
                    }

                    lock (m_Scenes)
                    {
                        m_Scenes.Remove(newID);
                        m_ScenePaths.Remove(scenePath);
                    }

                    return(null);
                }

                if (postLoadScript != null && postLoadScript != "")
                {
                    Cv_ScriptResource postLoadRes = Cv_ResourceManager.Instance.GetResource <Cv_ScriptResource>(postLoadScript, resourceBundle);
                    postLoadRes.RunScript();
                }

                return(entitiesCreated.ToArray());
            }

            lock (m_Scenes)
            {
                m_Scenes.Remove(newID);
                m_ScenePaths.Remove(scenePath);
            }

            return(null);
        }