public Cv_SoundInstanceData PlaySound2D(string soundResource, Cv_Entity entity, Vector2 listener, Vector2 emitter, bool looping = false, float volume = 1f, float pan = 0f, float pitch = 0f) { if (soundResource == null || soundResource == "" || entity == null) { Cv_Debug.Error("Error - No sound or entity defined when trying to play sound."); return(null); } var playSoundData = GetNewSoundInstance(soundResource, entity, volume, pan, pitch, looping); AddSoundToManager(playSoundData); m_Emitter.Position = new Vector3(emitter, 0) * DistanceFallOff; m_Listener.Forward = new Vector3(0, 0, -1); m_Listener.Up = new Vector3(0, 1, 0); m_Listener.Position = new Vector3(listener, 0) * DistanceFallOff; try { playSoundData.Instance.Apply3D(m_Listener, m_Emitter); playSoundData.Instance.Play(); playSoundData.Instance.Apply3D(m_Listener, m_Emitter); // This is necessary due to a bug in monogame where 3D sounds do //not work correctly unless Apply3D is called both before and after Play } catch (Exception e) { Cv_Debug.Error("Unable to play sound: " + soundResource + "\n" + e.ToString()); } return(playSoundData); }
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(); } } }
public Cv_CollisionShape(string shapeID, Vector2 point, float radius, Vector2?anchorPoint, float density, bool isSensor, bool isBullet, Cv_CollisionCategories categories, Cv_CollisionCategories collidesWith, Dictionary <int, string> directions) { ShapeID = shapeID; Points = new List <Vector2>(); Points.Add(point); if (anchorPoint == null) { AnchorPoint = Vector2.Zero; } else { AnchorPoint = (Vector2)anchorPoint; } Radius = radius; IsCircle = true; IsSensor = isSensor; IsBullet = isBullet; Density = density; Friction = 1f; CollisionCategories = categories; CollidesWith = collidesWith; m_CollisionDirections = directions; CircleOutlineTex = Cv_DrawUtils.CreateCircle((int)radius); Owner = null; }
public bool SoundIsPlaying(string soundResource, Cv_Entity entity = null) { if (entity != null) { if (m_EntityToInstancesMap.ContainsKey(entity.ID)) { var instances = m_EntityToInstancesMap[entity.ID]; foreach (var s in instances) { if (s.Instance.State == SoundState.Playing && s.Resource == soundResource) { return(true); } } } return(false); } if (m_SoundInstances.ContainsKey(soundResource)) { var instances = m_SoundInstances[soundResource]; foreach (var s in instances) { if (s.Instance.State == SoundState.Playing) { return(true); } } } return(false); }
public Cv_CollisionShape(string shapeID, List <Vector2> points, Vector2?anchorPoint, float density, bool isSensor, bool isBullet, Cv_CollisionCategories categories, Cv_CollisionCategories collidesWith, Dictionary <int, string> directions) { ShapeID = shapeID; Points = points; if (anchorPoint == null) { AnchorPoint = Vector2.Zero; } else { AnchorPoint = (Vector2)anchorPoint; } IsSensor = isSensor; IsBullet = isBullet; Density = density; Friction = 1f; CollisionCategories = categories; CollidesWith = collidesWith; m_CollisionDirections = directions; Owner = null; }
internal Cv_EventListenerHandle(int ID) { ListenerID = ID; Delegate = null; ScriptDelegate = ""; IsScriptListener = false; EventType = Cv_EventType.INVALID_EVENT; Entity = null; EventName = ""; Manager = null; }
public Cv_Entity GetEntity(Cv_EntityID entityID) { Cv_Entity ent = null; lock (Entities) { Entities.TryGetValue(entityID, out ent); } return(ent); }
public void Initialize(EditorApp app) { m_EditorApp = app; var sceneRoot = m_EditorApp.Logic.GetSceneRoot(m_EditorApp.Logic.GetSceneID("/Root")); m_EditorCamera = sceneRoot.GetEntity("/_editorCamera"); if (m_EditorCamera != null) { zoomTextBox.Text = ((int)(m_EditorCamera.GetComponent <Cv_CameraComponent>().Zoom * 100)).ToString() + "%"; } }
internal void DestroyEntity(Cv_Entity entity) { if (entity == null) { return; } foreach (var e in m_EntityList) //TODO(JM): this might get really slow with tons of entities. Optimize if it becomes a problem { if (e.ID != entity.ID && e.Parent == entity.ID && !e.DestroyRequested) { if (e.SceneRoot) { UnloadScene(e.SceneID); } else { DestroyEntity(e); } } } foreach (var e in m_EntitiesToAdd) //TODO(JM): this might get really slow with tons of entities. Optimize if it becomes a problem { if (e.ID != entity.ID && e.Parent == entity.ID && !e.DestroyRequested) { if (e.SceneRoot) { UnloadScene(e.SceneID); } else { DestroyEntity(e); } } } m_EntitiesToDestroy.Enqueue(entity); var destroyEntityEvent = new Cv_Event_DestroyEntity(entity.ID, this); Cv_EventManager.Instance.TriggerEvent(destroyEntityEvent); entity.OnDestroy(); Entities.Remove(entity.ID); EntitiesByPath.Remove(entity.EntityPath); entity.DestroyRequested = true; }
public Cv_SoundInstanceData FadeInSound2D(string soundResource, Cv_Entity entity, Vector2 listener, Vector2 emitter, float interval, bool looping = false, float volume = 1f, float pan = 0f, float pitch = 0f) { if (soundResource == null || soundResource == "" || entity == null) { Cv_Debug.Error("Error - No sound or entity defined when trying to play sound."); return(null); } var fadeSoundData = PlaySound2D(soundResource, entity, listener, emitter, looping, 0, pan, pitch); FadeSound(fadeSoundData, volume, interval); return(fadeSoundData); }
internal override void VExecuteFile(string resource, Cv_Entity runningEntity = null) { try { throw new NotImplementedException(); } catch (LuaException e) { Cv_Debug.Error("Error executing Lua script:\n" + e.ToString()); } catch (Exception e) { Cv_Debug.Error("Error executing Lua script:\n" + e.ToString()); } }
public void StopSound(string soundResource, Cv_Entity entity = null, bool immediate = false) { if (soundResource == null || soundResource == "") { Cv_Debug.Error("Error - No sound defined when trying to stop sound."); return; } if (entity != null) //If an entity is specified removes the first instance of that sound belonging to the entity { if (!m_EntityToInstancesMap.ContainsKey(entity.ID)) { return; } var instances = m_EntityToInstancesMap[entity.ID]; Cv_SoundInstanceData soundToStop = null; foreach (var s in instances) { if (s.Resource == soundResource) { soundToStop = s; break; } } if (soundToStop != null) { StopSound(soundToStop); } } else //Removes all instances of that sound { if (!m_SoundInstances.ContainsKey(soundResource)) { return; } m_SoundInstancesListCopy.Clear(); m_SoundInstancesListCopy.AddRange(m_SoundInstances[soundResource]); foreach (var soundToStop in m_SoundInstancesListCopy) { StopSound(soundToStop); } } }
public void ResumeSound(string soundResource, Cv_Entity entity = null) { if (soundResource == null || soundResource == "") { Cv_Debug.Error("Error - No sound defined when trying to resume sound."); return; } if (entity != null) //If an entity is specified resumes the first instance of that sound belonging to the entity { if (!m_EntityToInstancesMap.ContainsKey(entity.ID)) { return; } var instances = m_EntityToInstancesMap[entity.ID]; Cv_SoundInstanceData soundToResume = null; foreach (var s in instances) { if (s.Resource == soundResource) { soundToResume = s; break; } } if (soundToResume != null) { soundToResume.Instance.Resume(); soundToResume.Paused = false; } } else //Resumes all instances of that sound { if (!m_SoundInstances.ContainsKey(soundResource)) { return; } foreach (var s in m_SoundInstances[soundResource]) { s.Instance.Resume(); s.Paused = false; } } }
public void FadeOutSound(string soundResource, float interval, Cv_Entity entity = null) { if (soundResource == null || soundResource == "") { Cv_Debug.Error("Error - No sound defined when trying to stop sound."); return; } if (entity != null) //If an entity is specified fades the first instance of that sound belonging to the entity { if (!m_EntityToInstancesMap.ContainsKey(entity.ID)) { return; } var instances = m_EntityToInstancesMap[entity.ID]; Cv_SoundInstanceData soundToFade = null; foreach (var s in instances) { if (s.Resource == soundResource) { soundToFade = s; break; } } if (soundToFade != null) { FadeSound(soundToFade, 0, interval); } } else //Fades all instances of that sound { if (!m_SoundInstances.ContainsKey(soundResource)) { return; } var instances = m_SoundInstances[soundResource]; foreach (var s in instances) { FadeSound(s, 0, interval); } } }
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); }
public Cv_Entity GetEntity(string entityPath) { Cv_Entity ent = null; lock (Entities) { var path = entityPath; if (entityPath.Length > 0 && entityPath[0] != '/') { path = "/" + entityPath; } EntitiesByPath.TryGetValue(path, out ent); } return(ent); }
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); }
public void DestroyEntity(Cv_EntityID entityID) { lock (m_EntityList) lock (Entities) { Cv_Entity entity = null; var entityExists = false; entityExists = Entities.TryGetValue(entityID, out entity); if (entityExists && !entity.DestroyRequested) { if (entity.SceneRoot) { UnloadScene(entity.SceneID); return; } DestroyEntity(entity); } } }
public Cv_SoundInstanceData PlaySound(string soundResource, Cv_Entity entity, bool looping = false, float volume = 1f, float pan = 0f, float pitch = 0f) { if (soundResource == null || soundResource == "" || entity == null) { Cv_Debug.Error("Error - No sound or entity defined when trying to play sound."); return(null); } var playSoundData = GetNewSoundInstance(soundResource, entity, volume, pan, pitch, looping); AddSoundToManager(playSoundData); try { playSoundData.Instance.Play(); } catch (Exception e) { Cv_Debug.Error("Unable to play sound: " + soundResource + "\n" + e.ToString()); } return(playSoundData); }
protected override Cv_EntityComponent[] ModifyEntity(Cv_Entity entity, XmlNodeList overrides) { var components = new List <Cv_EntityComponent>(); foreach (XmlElement componentNode in overrides) { var componentID = Cv_EntityComponent.GetID(componentNode.Name); var component = entity.GetComponent(componentID); if (component != null) { component.VInitialize(componentNode); component.VOnChanged(); components.Add(component); } else { component = CreateComponent(componentNode); if (component != null) { if (component is GameComponent) { entity.AddComponent(((GameComponent)component).ComponentName, component); } else { entity.AddComponent(component.GetType().Name, component); } components.Add(component); //component.VPostInitialize(); } } } return(components.ToArray()); }
private Cv_SoundInstanceData GetNewSoundInstance(string soundResource, Cv_Entity entity, float volume, float pan, float pitch, bool looping) { Cv_SoundResource sound = Cv_ResourceManager.Instance.GetResource <Cv_SoundResource>(soundResource, entity.ResourceBundle); var soundData = sound.GetSoundData(); var soundInstance = soundData.Sound.CreateInstance(); var playSoundData = new Cv_SoundInstanceData(); playSoundData.Resource = soundResource; playSoundData.FadeRemainingTime = 0; playSoundData.FinalVolume = volume; playSoundData.Paused = false; playSoundData.Instance = soundInstance; playSoundData.Fading = false; playSoundData.EntityId = entity.ID; soundInstance.Volume = volume * GlobalSoundVolume; soundInstance.Pan = pan; soundInstance.Pitch = pitch; soundInstance.IsLooped = looping; return(playSoundData); }
private bool RemoveListener(string eventName, string onEvent, Cv_Entity entity) { Cv_Debug.Log("Events", "Attempting to remove listener from event type " + eventName); var success = false; var eType = Cv_Event.GetType(eventName); lock (m_ScriptEventListeners) { if (m_ScriptEventListeners.ContainsKey(eType)) { var listeners = m_ScriptEventListeners[eType]; success = listeners.RemoveAll(l => l.Delegate == onEvent && l.Entity == entity) > 0; } } if (success) { Cv_Debug.Log("Events", "Successfully removed listener from event type " + eType); } return(success); }
protected override void VGameOnUpdate(float time, float timeElapsed) { var cam = GetEntity(EditorView.EditorCamera); if (cam == null) { return; } var camTransf = cam.GetComponent <Cv_TransformComponent>(); var camSettings = cam.GetComponent <Cv_CameraComponent>(); var mouseState = Mouse.GetState(); var keyboardState = Keyboard.GetState(); var editorApp = ((EditorApp)Caravel); var mousePos = Cv_InputManager.Instance.GetMouseValues().MousePos; var mouseScroll = Cv_InputManager.Instance.GetMouseValues().MouseWheelVal; //MOUSE ACTIONS if (Cv_InputManager.Instance.CommandActive("mouseLeftClick", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EWindow != null && editorApp.EWindow.Focused && editorApp.EWindow.EditorForm.CanSelectEntities && mousePos.X > 0 && mousePos.Y > 0) { Cv_EntityID[] entities; EditorView.Pick(mousePos, out entities); if (Cv_InputManager.Instance.CommandActivated("mouseLeftClick", Cv_Player.One)) { if (!Cv_InputManager.Instance.CommandActive("alternateMode", Cv_Player.One)) { if (entities.Length > 0) { if (entities[0] != editorApp.EForm.CurrentEntity) { editorApp.EForm.SetSelectedEntity(entities[0]); } if (!m_bMovingEntity) { m_EntityBeingMoved = GetEntity(editorApp.EForm.CurrentEntity); } } else { var timer = new Cv_TimerProcess(100, DeselectEntity); Caravel.ProcessManager.AttachProcess(timer); } } else if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { if (!m_bMovingEntity) { m_EntityBeingMoved = GetEntity(editorApp.EForm.CurrentEntity); } } } if (Cv_InputManager.Instance.CommandActive("mouseMove", Cv_Player.One) && m_EntityBeingMoved != null) { m_bMovingEntity = true; var trasnfComp = m_EntityBeingMoved.GetComponent <Cv_TransformComponent>(); if (trasnfComp == null) { m_bMovingEntity = false; m_EntityBeingMoved = null; } else { var delta = m_DeltaBuffer + (mousePos - m_PrevMousePos); var deltaXscale = delta / (float)EditorView.EditorRenderer.Scale; var entityDelta = deltaXscale / camSettings.Zoom; var numStepsX = ((int)entityDelta.X) / EntityDragStepX; var numStepsY = ((int)entityDelta.Y) / EntityDragStepY; var finalDelta = new Vector3(numStepsX * EntityDragStepX, numStepsY * EntityDragStepY, 0); m_DeltaBuffer = delta; if (finalDelta != Vector3.Zero) { trasnfComp.SetPosition(trasnfComp.Position + finalDelta); if (finalDelta.X != 0) { m_DeltaBuffer = new Vector2(0, m_DeltaBuffer.Y); } if (finalDelta.Y != 0) { m_DeltaBuffer = new Vector2(m_DeltaBuffer.X, 0); } } } } } } else if (editorApp.Mode == EditorApp.EditorMode.CAMERA && editorApp.EWindow != null && editorApp.EWindow.Focused && m_PrevMousePos.X != -1) { var delta = m_PrevMousePos - mousePos; camTransf.SetPosition(camTransf.Position + new Vector3(delta, 0) / (camSettings.Zoom * (float)EditorView.EditorRenderer.Scale)); } else if (editorApp.Mode == EditorApp.EditorMode.CREATE) { if (Cv_InputManager.Instance.CommandActivated("mouseLeftClick", Cv_Player.One)) { var timer = new Cv_TimerProcess(100, PaintEntity); Caravel.ProcessManager.AttachProcess(timer); } } } else { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (m_bMovingEntity) { m_bMovingEntity = false; m_EntityBeingMoved = null; m_DeltaBuffer = Vector2.Zero; editorApp.EForm.UpdateEntityXml(); } } } if (Cv_InputManager.Instance.CommandActive("mouseWheelUp", Cv_Player.One) || Cv_InputManager.Instance.CommandActive("mouseWheelDown", Cv_Player.One)) { m_iIdleTime = 0; var delta = mouseScroll - m_iPreviousScrollValue; if (editorApp.Mode == EditorApp.EditorMode.CAMERA) { camSettings.Zoom += delta / 3000f; editorApp.EForm.UpdateTools(); } else if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EWindow != null && editorApp.EWindow.Focused && editorApp.EWindow.EditorForm.CanSelectEntities) { var entity = GetEntity(editorApp.EForm.CurrentEntity); if (entity != null) { var trasnfComp = entity.GetComponent <Cv_TransformComponent>(); if (trasnfComp != null) { m_bRotatingEntity = true; trasnfComp.SetRotation(trasnfComp.Rotation + delta / 3000f); } } } } } else if (m_bRotatingEntity) { if (m_iIdleTime > 1000) { m_bRotatingEntity = false; editorApp.EForm.UpdateEntityXml(); m_iIdleTime = 0; } else { m_iIdleTime += (int)(timeElapsed); } } //SAVE SHORTCUT if (Cv_InputManager.Instance.CommandActive("alternateMode", Cv_Player.One) && Cv_InputManager.Instance.CommandDeactivated("save", Cv_Player.One)) { if (editorApp.EForm.CurrentSceneFile != null && editorApp.EForm.CurrentSceneFile != "") { editorApp.EForm.SaveScene(); } } //MOVE ENTITIES WITH ARROWS if (Cv_InputManager.Instance.CommandActivated("Left", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { var e = GetEntity(editorApp.EForm.CurrentEntity); if (e != null && e.GetComponent <Cv_TransformComponent>() != null) { var pos = e.GetComponent <Cv_TransformComponent>().Position; e.GetComponent <Cv_TransformComponent>().SetPosition(new Vector3(pos.X - 1, pos.Y, pos.Z)); } } } } if (Cv_InputManager.Instance.CommandActivated("Right", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { var e = GetEntity(editorApp.EForm.CurrentEntity); if (e != null && e.GetComponent <Cv_TransformComponent>() != null) { var pos = e.GetComponent <Cv_TransformComponent>().Position; e.GetComponent <Cv_TransformComponent>().SetPosition(new Vector3(pos.X + 1, pos.Y, pos.Z)); } } } } if (Cv_InputManager.Instance.CommandActivated("Up", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { var e = GetEntity(editorApp.EForm.CurrentEntity); if (e != null && e.GetComponent <Cv_TransformComponent>() != null) { var pos = e.GetComponent <Cv_TransformComponent>().Position; e.GetComponent <Cv_TransformComponent>().SetPosition(new Vector3(pos.X, pos.Y - 1, pos.Z)); } } } } if (Cv_InputManager.Instance.CommandActivated("Down", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { var e = GetEntity(editorApp.EForm.CurrentEntity); if (e != null && e.GetComponent <Cv_TransformComponent>() != null) { var pos = e.GetComponent <Cv_TransformComponent>().Position; e.GetComponent <Cv_TransformComponent>().SetPosition(new Vector3(pos.X, pos.Y + 1, pos.Z)); } } } } //DELETE KEY if (Cv_InputManager.Instance.CommandActivated("Delete", Cv_Player.One)) { if (editorApp.Mode == EditorApp.EditorMode.TRANSFORM) { if (editorApp.EForm.CurrentEntity != Cv_EntityID.INVALID_ENTITY) { editorApp.EForm.RemoveEntityFromEditor(editorApp.EForm.CurrentEntity); editorApp.EForm.SetSelectedEntity(Cv_EntityID.INVALID_ENTITY); } } } m_PrevMousePos = mousePos; m_iPreviousScrollValue = mouseScroll; }
internal abstract void VExecuteStream(string resource, Stream stream, bool runInEditor, Cv_Entity runningEntity = null);
internal abstract void VExecuteString(string resource, string str, bool runInEditor, Cv_Event runningEvent, Cv_Entity runningEntity);
internal abstract void VExecuteFile(string resource, Cv_Entity runningEntity = null);
public abstract Cv_CollisionShape VAddTrigger(Cv_Entity gameEntity, Cv_ShapeData data);
public abstract Cv_CollisionShape VAddPointShape(Cv_Entity gameEntity, Cv_ShapeData data);
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); }
internal void OnUpdate(float time, float elapsedTime) { switch (State) { case Cv_GameState.Initializing: break; case Cv_GameState.WaitingForPlayers: if (ExpectedPlayers + ExpectedRemotePlayers <= HumanPlayersAttached) { //if (!string.IsNullOrEmpty(Caravel.GameOptions.Scene)) //{ ChangeState(Cv_GameState.LoadingScene); //}*/ } break; case Cv_GameState.LoadingScene: break; case Cv_GameState.WaitingForPlayersToLoadScene: if (ExpectedPlayers + ExpectedRemotePlayers <= HumanPlayersLoaded) { ChangeState(Cv_GameState.Running); } break; case Cv_GameState.Running: if (GamePhysics != null && !IsProxy) { GamePhysics.VOnUpdate(elapsedTime); GamePhysics.VSyncVisibleScene(); } break; default: Cv_Debug.Error("Unrecognized state."); break; } foreach (var gv in GameViews) { gv.VOnUpdate(time, elapsedTime); } foreach (var e in m_EntityList) { if (!e.DestroyRequested) { e.OnUpdate(elapsedTime); } } Cv_Entity toAdd = null; while (m_EntitiesToAdd.TryDequeue(out toAdd)) { lock (m_EntityList) { m_EntityList.Add(toAdd); } } Cv_Entity toRemove = null; while (m_EntitiesToDestroy.TryDequeue(out toRemove)) { lock (m_EntityList) { m_EntityList.Remove(toRemove); } toRemove.OnRemove(); } VGameOnUpdate(time, elapsedTime); }