Esempio n. 1
0
 public void RemoveComponent <T>(Entity entity) where T : ProvenceComponent
 {
     try{
         if (entity == null)
         {
             return;
         }
         if (componentDictionary.ContainsKey(typeof(T)))
         {
             if (componentDictionary[typeof(T)].ContainsKey(entity))
             {
                 ComponentHandle <T> handle = new ComponentHandle <T>(entity, componentDictionary[typeof(T)][entity] as T, world);
                 componentDictionary[typeof(T)].Remove(entity);
                 if (componentDictionary[typeof(T)].Count == 0)
                 {
                     componentDictionary.Remove(typeof(T));
                 }
                 world.eventManager.Raise <ComponentRemoved <T> >(new ComponentRemoved <T>(handle));
                 world.eventManager.Raise <CacheUpdate <T> >(new CacheUpdate <T>(world, GetAllComponentsAsDictionary <T>()));
             }
         }
     }catch (System.Exception e) {
         Debug.Log("Improper reference to component: " + e);
     }
 }
Esempio n. 2
0
        public ComponentHandle <T> GetOrCreateComponent <T>(Entity entity) where T : ProvenceComponent, new()
        {
            ComponentHandle <T> handle = GetComponent <T>(entity);

            if (handle == null)
            {
                handle = AddComponent <T>(entity);
            }
            return(handle);
        }
Esempio n. 3
0
        protected void RemoveChild(RemoveChild args)
        {
            ComponentHandle <Parent> parentHandle = world.GetComponent <Parent>(args.parent);

            if (parentHandle != null && parentHandle.component.children.Contains(args.child))
            {
                parentHandle.component.children.Remove(args.child);
                world.RemoveEntity(args.child);
            }
        }
Esempio n. 4
0
 protected void LoadGameObjects(WakeSystemEvent args)
 {
     foreach (GameObject gameObject in UnityEngine.Object.FindObjectsOfType <GameObject>())
     {
         EntityHandle entityHandle = world.LookUpEntity((Entity)gameObject.name);
         if (entityHandle != null)
         {
             ComponentHandle <UnityGameObject> objectHandle = entityHandle.GetOrCreateComponent <UnityGameObject>();
             objectHandle.component.gameObject = gameObject;
         }
     }
 }
Esempio n. 5
0
        protected void ChildRemoved(ComponentRemoved <Child> args)
        {
            ComponentHandle <Parent> parentHandle = world.GetComponent <Parent>(args.handle.component.parent);

            if (parentHandle != null)
            {
                parentHandle.component.children.Remove(args.handle.entity);
            }
            ComponentHandle <UnityGameObject> objectHandle = world.GetComponent <UnityGameObject>(args.handle.entity);

            if (objectHandle != null && objectHandle.component.gameObject != null)
            {
                objectHandle.component.gameObject.transform.parent = null;
            }
        }
Esempio n. 6
0
 protected void ChildAdded(ComponentAdded <Child> args)
 {
     if (args.handle.component.parent != null)
     {
         EntityHandle parentEntityHandle = world.LookUpEntity(args.handle.component.parent);
         if (parentEntityHandle != null)
         {
             ComponentHandle <Parent> parentHandle = parentEntityHandle.GetOrCreateComponent <Parent>();
             parentHandle.component.children.Add(args.handle.entity);
             GameObject parentObj = parentEntityHandle.GetOrCreateComponent <UnityGameObject>().component.gameObject;
             GameObject childObj  = world.GetOrCreateComponent <UnityGameObject>(args.handle.entity).component.gameObject;
             childObj.transform.parent = parentObj.transform;
         }
     }
 }
Esempio n. 7
0
        public HashSet <ComponentHandle <T> > GetAllComponentsByBase <T>() where T : ProvenceComponent
        {
            HashSet <ComponentHandle <T> > children = new HashSet <ComponentHandle <T> >();

            foreach (Type keyType in componentDictionary.Keys)
            {
                if (keyType.IsAssignableFrom(typeof(T)))
                {
                    foreach (KeyValuePair <Entity, ProvenceComponent> kvp in componentDictionary[typeof(T)])
                    {
                        ComponentHandle <T> componentHandle = new ComponentHandle <T>(kvp.Key, kvp.Value as T, world);
                        children.Add(componentHandle);
                    }
                }
            }
            return(children);
        }
Esempio n. 8
0
        public ComponentHandle <T> AddComponent <T>(Entity entity, T component) where T : ProvenceComponent
        {
            if (component == null || entity == null)
            {
                return(null);
            }
            Type componentType = component.GetType();

            if (!componentDictionary.ContainsKey(componentType))
            {
                componentDictionary[componentType] = new Dictionary <Entity, ProvenceComponent>();
                world.systemManager.AddRequiredSystems(component.requiredSystems);
            }
            componentDictionary[componentType][entity] = component as T;
            ComponentHandle <T> handle = new ComponentHandle <T>(entity, component, world);

            world.eventManager.Raise <ComponentAdded <T> >(new ComponentAdded <T>(handle));
            world.eventManager.Raise <CacheUpdate <T> >(new CacheUpdate <T>(world, GetAllComponentsAsDictionary <T>()));
            return(handle);
        }
Esempio n. 9
0
        protected void CameraAdded(ComponentAdded <ProvenceCamera> args)
        {
            ComponentHandle <UnityGameObject> objectHandle = world.GetOrCreateComponent <UnityGameObject>(args.handle.entity);

            if (objectHandle != null && objectHandle.component.gameObject != null)
            {
                GameObject gameObject = objectHandle.component.gameObject;
                Camera     camera     = gameObject.GetComponent <Camera>();
                if (camera == null)
                {
                    camera = gameObject.AddComponent <Camera>();
                }
                args.handle.component.camera = camera;
                ProvenceCameraHook hook = gameObject.GetComponent <ProvenceCameraHook>();
                if (hook == null)
                {
                    hook = gameObject.AddComponent <ProvenceCameraHook>();
                }
                hook.Init(args.handle.world, args.handle.entity);
            }
        }
Esempio n. 10
0
        protected void MainCameraAdded(ComponentAdded <MainCamera> args)
        {
            ComponentHandle <ProvenceCamera> cameraHandle = world.GetOrCreateComponent <ProvenceCamera>(args.handle.entity);

            if (cameraHandle.component.camera == null)
            {
                cameraHandle = world.AddComponent <ProvenceCamera>(args.handle.entity);
            }
            cameraHandle.component.camera.gameObject.tag = "MainCamera";
            cameraHandle.component.camera.gameObject.SetActive(true);
            foreach (ComponentHandle <MainCamera> mainHandle in world.componentManager.GetAllComponents <MainCamera>())
            {
                if (mainHandle.entity != args.handle.entity)
                {
                    ProvenceCamera pCamera = world.GetComponent <ProvenceCamera>(mainHandle.entity).component;
                    pCamera.camera.tag = "Untagged";
                    pCamera.camera.gameObject.SetActive(false);
                    world.RemoveComponent <MainCamera>(mainHandle.entity);
                }
            }
        }
Esempio n. 11
0
 public ComponentRemoved(ComponentHandle <T> handle)
 {
     this.handle = handle;
 }
Esempio n. 12
0
 public ComponentAdded(ComponentHandle <T> handle)
 {
     this.handle = handle;
 }