Exemple #1
0
        public bool AbortEvent <EventType>(bool allOfType = false) where EventType : Cv_Event
        {
            Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "EventManager must have an active event queue.");
            Cv_EventType eType = Cv_Event.GetType <EventType>();

            var success = false;

            Cv_Debug.Log("Events", "Attempting to abort event type " + typeof(EventType).Name);

            var queue = m_EventQueues[m_iActiveQueue];

            if (allOfType)
            {
                if (queue.Remove(queue.First(e => e.Type == eType)))
                {
                    success = true;
                }
            }
            else
            {
                while (queue.Remove(queue.First(e => e.Type == eType)))
                {
                    success = true;
                }
            }

            if (success)
            {
                Cv_Debug.Log("Events", "Successfully aborted event type " + typeof(EventType).Name);
            }

            return(success);
        }
Exemple #2
0
        public void ModifyEntity(Cv_EntityID entityID, XmlNodeList overrides)
        {
            Cv_Debug.Assert(m_EntityFactory != null, "Entity factory should not be null.");

            if (m_EntityFactory == null)
            {
                return;
            }

            Cv_Entity ent = null;

            lock (Entities)
            {
                Entities.TryGetValue(entityID, out ent);
            }

            if (ent != null)
            {
                var components = m_EntityFactory.ModifyEntity(ent, overrides);

                foreach (var component in components)
                {
                    component.VPostInitialize();
                    component.VPostLoad();
                }
            }
        }
        internal override void VExecuteStream(string resource, Stream stream, bool runInEditor, Cv_Event runningEvent)
        {
            Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "ScriptManager must have an active script queue.");

            if (stream == null)
            {
                Cv_Debug.Error("Invalid stream in VExecuteStream.");
                return;
            }

            string code;

            using (StreamReader reader = new StreamReader(stream))
            {
                stream.Position = 0;
                code            = reader.ReadToEnd();
            }

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

            Cv_ScriptExecutionRequest request = new Cv_ScriptExecutionRequest();

            request.Code        = code;
            request.Resource    = resource;
            request.Entity      = entity;
            request.Event       = runningEvent;
            request.RunInEditor = runInEditor;

            lock (m_QueuedScriptLists[m_iActiveQueue])
            {
                m_QueuedScriptLists[m_iActiveQueue].AddLast(request);
            }

            Cv_Debug.Log("LuaScript", "Queued script " + resource + " for entity " + (entity != null ? entity.EntityName : "[null]"));
        }
        internal override void VExecuteString(string resource, string str, bool runInEditor, Cv_Event runningEvent, Cv_Entity runningEntity)
        {
            Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "ScriptManager must have an active script queue.");

            if (str == null)
            {
                Cv_Debug.Error("Invalid script in VExecuteString.");
                return;
            }

            Cv_ScriptExecutionRequest request = new Cv_ScriptExecutionRequest();

            request.Code        = str;
            request.Resource    = resource;
            request.Entity      = runningEntity;
            request.Event       = runningEvent;
            request.RunInEditor = runInEditor;

            lock (m_QueuedScriptLists[m_iActiveQueue])
            {
                m_QueuedScriptLists[m_iActiveQueue].AddLast(request);
            }

            Cv_Debug.Log("LuaScript", "Queued script " + resource + " for entity " + (runningEntity != null ? runningEntity.EntityName : "[null]"));
        }
Exemple #5
0
        public bool QueueEvent(Cv_Event newEvent, bool threadSafe = false)
        {
            if (!threadSafe)
            {
                Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "EventManager must have an active event queue.");

                if (newEvent == null)
                {
                    Cv_Debug.Error("Invalid event in QueueEvent.");
                    return(false);
                }

                if (newEvent.WriteToLog)
                {
                    Cv_Debug.Log("Events", "Attempting to queue event " + newEvent.VGetName() + " for entity " + newEvent.EntityID);
                }

                lock (m_EventListeners)
                {
                    if (m_EventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                lock (m_ScriptEventListeners)
                {
                    if (m_ScriptEventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                Cv_Debug.Log("Events", "Skipping event " + newEvent.VGetName() + " since there are no listeners for it.");
                return(false);
            }
            else
            {
                m_RealTimeEventQueue.Enqueue(newEvent);
                return(true);
            }
        }
Exemple #6
0
        private bool CanCreateEntity(string entityName, Cv_EntityID serverEntityID)
        {
            Cv_Debug.Assert(m_EntityFactory != null, "Entity factory should not be null.");
            Cv_Debug.Assert(entityName != null, "Entity must have a name.");
            Cv_Debug.Assert(m_SceneManager.MainScene != Cv_SceneID.INVALID_SCENE, "Must have loaded a scene before creating entity.");

            if (!IsProxy && serverEntityID != Cv_EntityID.INVALID_ENTITY)
            {
                return(false);
            }
            else if (IsProxy && serverEntityID == Cv_EntityID.INVALID_ENTITY)
            {
                return(false);
            }

            return(true);
        }
Exemple #7
0
        private void OnNewEntityRequest(Cv_Event eventData)
        {
            Cv_Debug.Assert(IsProxy, "Should only enter RequestNewEntityCallback when game logic is a proxy.");
            if (!IsProxy)
            {
                return;
            }

            Cv_Event_RequestNewEntity data = (Cv_Event_RequestNewEntity)eventData;
            var bundle = data.EntityResourceBundle;

            if (data.EntityResource != null)
            {
                CreateEntity(data.EntityResource, data.EntityName, bundle, data.Visible, data.Parent, null, data.InitialTransform, data.SceneID, data.ServerEntityID);
            }
            else
            {
                CreateEmptyEntity(data.EntityName, bundle, data.Visible, data.Parent, null, data.InitialTransform, data.SceneID, data.ServerEntityID);
            }
        }
Exemple #8
0
        protected override bool VInheritedInit(XmlElement componentData)
        {
            Cv_Debug.Assert(componentData != null, "Must have valid component data.");

            var fontNode = componentData.SelectNodes("Font").Item(0);

            if (fontNode != null)
            {
                FontResource = fontNode.Attributes["resource"].Value;
            }

            var textNode = componentData.SelectNodes("Text").Item(0);

            if (textNode != null)
            {
                Text = textNode.Attributes["text"].Value;
            }

            var literalTextNode = componentData.SelectNodes("LiteralText").Item(0);

            if (literalTextNode != null)
            {
                LiteralText = bool.Parse(literalTextNode.Attributes["status"].Value);
            }

            var hAlignNode = componentData.SelectNodes("HorizontalAlignment").Item(0);

            if (hAlignNode != null)
            {
                HorizontalAlignment = (Cv_TextAlign)Enum.Parse(typeof(Cv_TextAlign), hAlignNode.Attributes["value"].Value);
            }

            var vAlignNode = componentData.SelectNodes("VerticalAlignment").Item(0);

            if (vAlignNode != null)
            {
                VerticalAlignment = (Cv_TextAlign)Enum.Parse(typeof(Cv_TextAlign), vAlignNode.Attributes["value"].Value);
            }

            return(true);
        }
        public override bool VInitialize(XmlElement componentData)
        {
            Cv_Debug.Assert(componentData != null, "Must have valid component data.");

            m_OldTransform = Transform;

            var positionNode = componentData.SelectNodes("Position").Item(0);

            if (positionNode != null)
            {
                float x, y, z;

                x = int.Parse(positionNode.Attributes["x"].Value);
                y = int.Parse(positionNode.Attributes["y"].Value);
                z = int.Parse(positionNode.Attributes["z"].Value);

                var position = new Vector3(x, y, z);
                Position = position;
            }

            var rotationNode = componentData.SelectNodes("Rotation").Item(0);

            if (rotationNode != null)
            {
                float rad;

                rad = float.Parse(rotationNode.Attributes["radians"].Value, CultureInfo.InvariantCulture);

                var rotation = rad;
                Rotation = rotation;
            }

            var scaleNode = componentData.SelectNodes("Scale").Item(0);

            if (scaleNode != null)
            {
                float x, y;

                x = float.Parse(scaleNode.Attributes["x"].Value, CultureInfo.InvariantCulture);
                y = float.Parse(scaleNode.Attributes["y"].Value, CultureInfo.InvariantCulture);

                var scale = new Vector2(x, y);
                Scale = scale;
            }

            var originNode = componentData.SelectNodes("Origin").Item(0);

            if (originNode != null)
            {
                float x, y;

                x = (float)double.Parse(originNode.Attributes["x"].Value, CultureInfo.InvariantCulture);
                y = (float)double.Parse(originNode.Attributes["y"].Value, CultureInfo.InvariantCulture);

                x = Math.Max(0, Math.Min(1, x));
                y = Math.Max(0, Math.Min(1, y));
                var origin = new Vector2(x, y);
                Origin = origin;
            }

            return(true);
        }
        protected override bool VInheritedInit(XmlElement componentData)
        {
            Cv_Debug.Assert(componentData != null, "Must have valid component data.");

            var textureNode = componentData.SelectNodes("Texture").Item(0);

            if (textureNode != null)
            {
                Texture = textureNode.Attributes["resource"].Value;
            }

            var velocityNode = componentData.SelectNodes("EmitterVelocity").Item(0);

            if (velocityNode != null)
            {
                var x = int.Parse(velocityNode.Attributes["x"].Value);
                var y = int.Parse(velocityNode.Attributes["y"].Value);
                EmitterVelocity = new Vector2(x, y);
            }

            var variationNode = componentData.SelectNodes("EmitterVariation").Item(0);

            if (variationNode != null)
            {
                var x = int.Parse(variationNode.Attributes["x"].Value);
                var y = int.Parse(variationNode.Attributes["y"].Value);
                EmitterVariation = new Vector2(x, y);
            }

            var gravityNode = componentData.SelectNodes("Gravity").Item(0);

            if (gravityNode != null)
            {
                var x = int.Parse(gravityNode.Attributes["x"].Value);
                var y = int.Parse(gravityNode.Attributes["y"].Value);
                Gravity = new Vector2(x, y);
            }

            var particlesPerSecondNode = componentData.SelectNodes("ParticlesPerSecond").Item(0);

            if (particlesPerSecondNode != null)
            {
                ParticlesPerSecond = int.Parse(particlesPerSecondNode.Attributes["value"].Value);
            }

            var particleLifetimeNode = componentData.SelectNodes("ParticleLifetime").Item(0);

            if (particleLifetimeNode != null)
            {
                ParticleLifeTime = float.Parse(particleLifetimeNode.Attributes["value"].Value, CultureInfo.InvariantCulture);
            }

            var maxParticlesNode = componentData.SelectNodes("MaxParticles").Item(0);

            if (maxParticlesNode != null)
            {
                MaxParticles = int.Parse(maxParticlesNode.Attributes["value"].Value);
            }

            XmlElement finalColorNode = (XmlElement)componentData.SelectSingleNode("FinalColor");

            if (finalColorNode != null)
            {
                int r, g, b, a;

                r = int.Parse(finalColorNode.Attributes["r"].Value);
                g = int.Parse(finalColorNode.Attributes["g"].Value);
                b = int.Parse(finalColorNode.Attributes["b"].Value);

                a = 255;

                if (finalColorNode.Attributes["a"] != null)
                {
                    a = int.Parse(finalColorNode.Attributes["a"].Value);
                }

                FinalColor = new Color(r, g, b, a);
            }

            var colorMiddlePoint = componentData.SelectNodes("ColorEaseBias").Item(0);

            if (colorMiddlePoint != null)
            {
                var value = float.Parse(colorMiddlePoint.Attributes["value"].Value, CultureInfo.InvariantCulture);
                ColorChangePoint = (float)Math.Max(Math.Min(value, 1f), 0f);
            }

            var initialScaleNode = componentData.SelectNodes("InitialParticleScale").Item(0);

            if (initialScaleNode != null)
            {
                var x = float.Parse(initialScaleNode.Attributes["x"].Value, CultureInfo.InvariantCulture);
                var y = float.Parse(initialScaleNode.Attributes["y"].Value, CultureInfo.InvariantCulture);
                InitialScale = new Vector2(x, y);
            }

            var finalScaleNode = componentData.SelectNodes("FinalParticleScale").Item(0);

            if (finalScaleNode != null)
            {
                var x = float.Parse(finalScaleNode.Attributes["x"].Value, CultureInfo.InvariantCulture);
                var y = float.Parse(finalScaleNode.Attributes["y"].Value, CultureInfo.InvariantCulture);
                FinalScale = new Vector2(x, y);
            }

            var scaleMiddlePoint = componentData.SelectNodes("ScaleEaseBias").Item(0);

            if (scaleMiddlePoint != null)
            {
                var value = float.Parse(scaleMiddlePoint.Attributes["value"].Value, CultureInfo.InvariantCulture);
                ScaleChangePoint = (float)Math.Max(Math.Min(value, 1f), 0f);
            }

            var initialRotationNode = componentData.SelectNodes("InitialParticleRotation").Item(0);

            if (initialRotationNode != null)
            {
                InitialRotation = float.Parse(initialRotationNode.Attributes["value"].Value, CultureInfo.InvariantCulture);
            }

            var finalRotationNode = componentData.SelectNodes("FinalParticleRotation").Item(0);

            if (finalRotationNode != null)
            {
                FinalRotation = float.Parse(finalRotationNode.Attributes["value"].Value, CultureInfo.InvariantCulture);
            }

            var rotationMiddlePoint = componentData.SelectNodes("RotationEaseBias").Item(0);

            if (rotationMiddlePoint != null)
            {
                var value = float.Parse(rotationMiddlePoint.Attributes["value"].Value, CultureInfo.InvariantCulture);
                RotationChangePoint = (float)Math.Max(Math.Min(value, 1f), 0f);
            }

            return(true);
        }
Exemple #11
0
 public void Fail()
 {
     Cv_Debug.Assert(State == Cv_ProcessState.Running || State == Cv_ProcessState.Paused, "Only running processes can fail.");
     State = Cv_ProcessState.Failed;
 }
Exemple #12
0
 public void Succeed()
 {
     Cv_Debug.Assert(State == Cv_ProcessState.Running || State == Cv_ProcessState.Paused, "Only running processes can succeed.");
     State = Cv_ProcessState.Succeeded;
 }
Exemple #13
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 #14
0
        protected override bool VInheritedInit(XmlElement componentData)
        {
            Cv_Debug.Assert(componentData != null, "Must have valid component data.");

            var textureNode = componentData.SelectNodes("Texture").Item(0);

            if (textureNode != null)
            {
                Texture = textureNode.Attributes["resource"].Value;
            }

            var animationNode = componentData.SelectNodes("Animation").Item(0);

            if (animationNode != null)
            {
                if (animationNode.Attributes["fx"] != null)
                {
                    FrameX = int.Parse(animationNode.Attributes["fx"].Value);
                }

                if (animationNode.Attributes["fy"] != null)
                {
                    FrameY = int.Parse(animationNode.Attributes["fy"].Value);
                }

                if (animationNode.Attributes["loop"] != null)
                {
                    Looping = bool.Parse(animationNode.Attributes["loop"].Value);
                }

                if (animationNode.Attributes["speed"] != null)
                {
                    Speed = int.Parse(animationNode.Attributes["speed"].Value);
                }

                if (animationNode.Attributes["startFrame"] != null)
                {
                    StartFrame = int.Parse(animationNode.Attributes["startFrame"].Value);
                }

                if (animationNode.Attributes["endFrame"] != null)
                {
                    EndFrame = int.Parse(animationNode.Attributes["endFrame"].Value);
                }

                var subAnimations = animationNode.SelectNodes("SubAnimation");
                m_SubAnimations.Clear();
                m_CurrAnim = null;

                foreach (XmlElement subAnimation in subAnimations)
                {
                    var anim = new Cv_SpriteSubAnimation();

                    anim.ID         = subAnimation.Attributes["id"].Value;
                    anim.Speed      = int.Parse(subAnimation.Attributes["speed"].Value);
                    anim.StartFrame = int.Parse(subAnimation.Attributes["startFrame"].Value);
                    anim.EndFrame   = int.Parse(subAnimation.Attributes["endFrame"].Value);

                    AddAnimation(anim);
                }

                if (m_SubAnimations.Count > 0 && animationNode.Attributes["defaultAnim"] != null)
                {
                    var defaultAnim = animationNode.Attributes["defaultAnim"].Value;
                    m_sDefaultAnim = defaultAnim;
                    SetAnimation(defaultAnim);
                    CurrentFrame = m_CurrAnim != null ? m_CurrAnim.Value.StartFrame : 0;
                }

                if (StartFrame != null)
                {
                    CurrentFrame = StartFrame.Value;
                }
            }

            var mirroredNode = componentData.SelectNodes("Mirrored").Item(0);

            if (mirroredNode != null)
            {
                Mirrored = bool.Parse(mirroredNode.Attributes["status"].Value);
            }

            var scriptNode = componentData.SelectNodes("OnEndScript").Item(0);

            if (scriptNode != null)
            {
                OnEndScript = scriptNode.Attributes["resource"].Value;
            }

            return(true);
        }
Exemple #15
0
 private XmlElement GetSceneOverrides(XmlElement sceneElement)
 {
     Cv_Debug.Assert(sceneElement != null, "Must have valid scene element.");
     return((XmlElement)sceneElement.SelectSingleNode("Overrides"));
 }