Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 public void Resume()
 {
     if (State == Cv_ProcessState.Paused)
     {
         State = Cv_ProcessState.Running;
     }
     else
     {
         Cv_Debug.Warning("Attempting to resume a process that isn't paused");
     }
 }
Ejemplo n.º 3
0
 public void Pause()
 {
     if (State == Cv_ProcessState.Running)
     {
         State = Cv_ProcessState.Paused;
     }
     else
     {
         Cv_Debug.Warning("Attempting to pause a process that isn't running");
     }
 }
Ejemplo n.º 4
0
        private void OnPlaySound(Cv_Event eventData)
        {
            var playEvt = (Cv_Event_PlaySound)eventData;

            var entity = CaravelApp.Instance.Logic.GetEntity(playEvt.EntityID);

            if (playEvt.Fade && playEvt.Interval > 0)
            {
                if (playEvt.Volume <= 0)
                {
                    Caravel.SoundManager.FadeOutSound(playEvt.SoundResource, playEvt.Interval);
                    return;
                }

                if (entity == null)
                {
                    Cv_Debug.Error("Attempting to play sound without an entity associated.");
                    return;
                }

                if (playEvt.Localized)
                {
                    Caravel.SoundManager.FadeInSound2D(playEvt.SoundResource, entity, playEvt.Listener, playEvt.Emitter,
                                                       playEvt.Interval, playEvt.Looping, playEvt.Volume, playEvt.Pan, playEvt.Pitch);
                }
                else
                {
                    Caravel.SoundManager.FadeInSound(playEvt.SoundResource, entity, playEvt.Interval, playEvt.Looping,
                                                     playEvt.Volume, playEvt.Pan, playEvt.Pitch);
                }
            }
            else
            {
                if (entity == null)
                {
                    Cv_Debug.Warning("Attempting to play sound without an entity associated.");
                    return;
                }

                if (playEvt.Localized)
                {
                    Caravel.SoundManager.PlaySound2D(playEvt.SoundResource, entity, playEvt.Listener, playEvt.Emitter,
                                                     playEvt.Looping, playEvt.Volume, playEvt.Pan, playEvt.Pitch);
                }
                else
                {
                    Caravel.SoundManager.PlaySound(playEvt.SoundResource, entity, playEvt.Looping,
                                                   playEvt.Volume, playEvt.Pan, playEvt.Pitch);
                }
            }
        }
Ejemplo n.º 5
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.");
            }
        }
Ejemplo n.º 6
0
        public Cv_EventListenerHandle AddListener(string eventName, string onEvent, Cv_Entity entity)
        {
            Cv_Debug.Log("Events", "Attempting to add listener for event type " + eventName);

            Cv_EventType eType = Cv_Event.GetType(eventName);

            lock (m_ScriptEventListeners)
            {
                if (!m_ScriptEventListeners.ContainsKey(eType))
                {
                    m_ScriptEventListeners[eType] = new List <Cv_ScriptListener>();
                }

                var listeners = m_ScriptEventListeners[eType];

                foreach (var l in listeners)
                {
                    if (l.Delegate == onEvent && l.Entity == entity)
                    {
                        Cv_Debug.Warning("Attempting to double register a listener.");

                        return(Cv_EventListenerHandle.NullHandle);
                    }
                }

                listeners.Add(new Cv_ScriptListener(entity, onEvent));
            }

            Cv_Debug.Log("Events", "Successfully added listener for event type " + eventName);

            var handle = new Cv_EventListenerHandle(m_iEventHandleNum);

            m_iEventHandleNum++;

            handle.EventName        = eventName;
            handle.EventType        = eType;
            handle.ScriptDelegate   = onEvent;
            handle.IsScriptListener = true;
            handle.Entity           = entity;
            handle.Manager          = this;

            return(handle);
        }
Ejemplo n.º 7
0
        public Cv_EventListenerHandle AddListener <EventType>(NewEventDelegate callback) where EventType : Cv_Event
        {
            Cv_Debug.Log("Events", "Attempting to add listener for event type " + typeof(EventType).Name);

            Cv_EventType eType = Cv_Event.GetType <EventType>();

            lock (m_EventListeners)
            {
                if (!m_EventListeners.ContainsKey(eType))
                {
                    m_EventListeners[eType] = new List <NewEventDelegate>();
                }

                var listeners = m_EventListeners[eType];

                foreach (var l in listeners)
                {
                    if (l == callback)
                    {
                        Cv_Debug.Warning("Attempting to double register a listener.");
                        return(Cv_EventListenerHandle.NullHandle);
                    }
                }

                listeners.Add(callback);
            }

            Cv_Debug.Log("Events", "Successfully added listener for event type " + typeof(EventType).Name);

            var handle = new Cv_EventListenerHandle(m_iEventHandleNum);

            m_iEventHandleNum++;

            handle.EventType = eType;
            handle.EventName = typeof(EventType).Name;
            handle.Delegate  = callback;
            handle.Manager   = this;
            return(handle);
        }
Ejemplo n.º 8
0
 public void Warning(string val)
 {
     Cv_Debug.Warning(val);
 }
Ejemplo n.º 9
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);
        }