public void RemoveComponent(int entityID, ECSComponent component) { OnComponentRemovedPre(entityID, component); GetComponentBag(entityID).Set(GetComponentID(component.GetType()), null); m_componentFactory.DeInitializeComponent(entityID, component); OnComponentRemovedPost(entityID, component); }
public override void OnComponentRemovedPre(int entityID, ECSComponent component) { if (component.GetType() == typeof(CCollider)) { RemoveEntity(entityID); } }
static void SetDataForComponent(ECSComponent comp, SerializedData data) { Type t = comp.GetType(); var field = findDataField(t); if (field != null) { field.SetValue(comp, data); } }
public virtual int DeInitializeComponent(int entityID, ECSComponent component) { if (component.GetType() == typeof(CTransform)) { CTransform cTransform = component as CTransform; GameObject.Destroy(cTransform.GameObject); } if (component.GetType() == typeof(CSpriteRenderer)) { CSpriteRenderer cRenderer = component as CSpriteRenderer; DestroyUnityComponent <SpriteRenderer>(entityID, cRenderer); } if (component.GetType() == typeof(CBoxCollider)) { CBoxCollider cBoxCollider = component as CBoxCollider; OnColliderRemoved(entityID, cBoxCollider.BoxCollider); DestroyUnityComponent <BoxCollider>(entityID, cBoxCollider); } if (component.GetType() == typeof(CMeshCollider)) { CMeshCollider cMeshCollider = component as CMeshCollider; OnColliderRemoved(entityID, cMeshCollider.Collider); DestroyUnityComponent <MeshCollider>(entityID, cMeshCollider); } if (component.GetType() == typeof(CSphereCollider)) { CSphereCollider cSphereCollider = component as CSphereCollider; OnColliderRemoved(entityID, cSphereCollider.Collider); DestroyUnityComponent <SphereCollider>(entityID, cSphereCollider); } if (component.GetType() == typeof(CRigidbody)) { CRigidbody cRigidbody = component as CRigidbody; DestroyUnityComponent <Rigidbody>(entityID, cRigidbody); } if (component.GetType() == typeof(CMeshRenderer)) { CMeshRenderer cMeshRenderer = component as CMeshRenderer; DestroyUnityComponent <MeshRenderer>(entityID, cMeshRenderer); DestroyUnityComponent <MeshFilter>(entityID, cMeshRenderer); } return(0); }
public void UnregisterECSComponent(ECSComponent component) { Type t = component.GetType(); IList comps; components.TryGetValue(t, out comps); if (comps != null) { comps.Remove(component); } allComponents.Remove(component); }
public void RegisterECSComponent(ECSComponent component) { Type t = component.GetType(); IList comps; components.TryGetValue(t, out comps); if (comps == null) { comps = MakeNewGenericList(t); components.Add(t, comps); } comps.Add(component); allComponents.Add(component); }
public void InitializePendingComponents(int entityID) { Bag <ECSComponent> bag = SafeGetPendingComponentBag(entityID).Clone(); bag.ResizeToFit(); ECSComponent component = null; List <ECSComponent> toInit = new List <ECSComponent>(bag.GetAll()); int attemptThreshold = -1; int START_THRESHOLD = 1; if (toInit.Count > 0) { START_THRESHOLD = 10; } attemptThreshold = START_THRESHOLD; while (toInit.Count > 0 && attemptThreshold > 0) { for (int i = toInit.Count - 1; i >= 0; i--) { component = toInit[i]; RemovePendingComponent(entityID, component); if (m_componentFactory.InitializeComponent(entityID, component) == 0) { SafeGetComponentBag(entityID).Set(SafeGetComponentID(component.GetType()), component); OnComponentAdded(entityID, component); toInit.RemoveAt(i); attemptThreshold = START_THRESHOLD; continue; } else { AddPendingComponent(entityID, component); attemptThreshold--; } } } ECSDebug.Assert(attemptThreshold > 0, " Reached attempt threshold, maybe two components are dependent on eachother?"); }
static SerializedData GetDataFromComponent(ECSComponent comp) { if (comp == null) { return(null); } Type t = comp.GetType(); var field = findDataField(t); if (field != null) { var data = field.GetValue(comp) as SerializedData; return(data); } else { return(null); } }
private void RemovePendingComponent(int entityID, ECSComponent component) { int componentID = GetComponentID(component.GetType()); SafeGetPendingComponentBag(entityID).Set(componentID, null); }
private void AddPendingComponent(int entityID, ECSComponent component) { int componentID = SafeGetComponentID(component.GetType()); SafeGetPendingComponentBag(entityID).Set(componentID, component); }
protected void OnComponentDeInitializeFailure(ECSComponent component, string reason) { ECSDebug.LogWarning("DeInitializing Component " + component.GetType().Name + " failed. Reason: " + reason); }
private T GetComponent <T>(ECSComponent component, int entityID) where T : ECSComponent { if (m_componentManager.HasComponent <T>(entityID) == false) { OnComponentInitializeFailure(component, "Cannot initialize component " + component.GetType().Name + " without component " + typeof(T).Name); return(null); } return(m_componentManager.GetComponent <T>(entityID)); }
public virtual int InitializeComponent(int entityID, ECSComponent component) { if (component.GetType() == typeof(CTransform)) { CTransform cTransform = component as CTransform; if (cTransform.GameObject == null) { if (cTransform.PrefabID == null) { cTransform.GameObject = new GameObject(cTransform.Name); } else { GameObject prefab = Resources.Load <GameObject>(cTransform.PrefabID); if (prefab == null) { OnComponentInitializeFailure(cTransform, "Prefab could not be found " + cTransform.PrefabID); return(1); } cTransform.GameObject = GameObject.Instantiate(prefab); } } if (cTransform.LayerID != -1) { cTransform.GameObject.layer = cTransform.LayerID; } if (cTransform.X.Equals(float.NaN)) { cTransform.X = cTransform.GameObject.transform.position.x; } if (cTransform.Y.Equals(float.NaN)) { cTransform.Y = cTransform.GameObject.transform.position.y; } if (cTransform.Z.Equals(float.NaN)) { cTransform.Z = cTransform.GameObject.transform.position.z; } if (cTransform.RotationX.Equals(float.NaN)) { cTransform.RotationX = cTransform.GameObject.transform.eulerAngles.x; } if (cTransform.RotationY.Equals(float.NaN)) { cTransform.RotationY = cTransform.GameObject.transform.eulerAngles.y; } if (cTransform.RotationZ.Equals(float.NaN)) { cTransform.RotationZ = cTransform.GameObject.transform.eulerAngles.z; } if (cTransform.ScaleX.Equals(float.NaN)) { cTransform.ScaleX = cTransform.GameObject.transform.localScale.x; } if (cTransform.ScaleY.Equals(float.NaN)) { cTransform.ScaleY = cTransform.GameObject.transform.localScale.y; } if (cTransform.ScaleZ.Equals(float.NaN)) { cTransform.ScaleZ = cTransform.GameObject.transform.localScale.z; } } if (component.GetType() == typeof(CSpriteRenderer)) { CSpriteRenderer cRenderer = component as CSpriteRenderer; CTransform cTransform = VerifyTransform(cRenderer, entityID); if (cTransform == null) { return(1); } cRenderer.SpriteRenderer = AddOrGetUnityComponent <SpriteRenderer>(cTransform); } if (component.GetType() == typeof(CRigidbody)) { CRigidbody cRigidbody = component as CRigidbody; CTransform cTransform = VerifyTransform(cRigidbody, entityID); if (cTransform == null) { return(1); } bool overwriteValues = (cTransform.GameObject.GetComponent <Rigidbody>() != null); cRigidbody.RigidBody = AddOrGetUnityComponent <Rigidbody>(cTransform); if (overwriteValues) { cRigidbody.IsKinematic = cRigidbody.RigidBody.isKinematic; cRigidbody.UseGravity = cRigidbody.RigidBody.useGravity; } } if (component.GetType() == typeof(CBoxCollider)) { CBoxCollider cBoxCollider = component as CBoxCollider; CTransform cTransform = VerifyTransform(cBoxCollider, entityID); if (cTransform == null) { return(1); } if (cBoxCollider.BoxCollider == null) { cBoxCollider.BoxCollider = AddOrGetUnityComponent <BoxCollider>(cTransform); } OnColliderAdded(cBoxCollider.BoxCollider, entityID); } if (component.GetType() == typeof(CMeshCollider)) { CMeshCollider collider = component as CMeshCollider; CTransform cTransform = VerifyTransform(collider, entityID); CMeshRenderer cMeshRenderer = GetComponent <CMeshRenderer>(collider, entityID); if (cMeshRenderer == null) { return(1); } if (cMeshRenderer.MeshFilter == null) { OnComponentInitializeFailure(collider, "Cannot create mesh collider without mesh filter"); return(1); } if (collider.Collider == null) { collider.Collider = AddOrGetUnityComponent <MeshCollider>(cTransform); collider.Collider.sharedMesh = null; collider.Collider.sharedMesh = cMeshRenderer.MeshFilter.mesh; if (collider.IsConvex) { collider.Collider.convex = true; } } OnColliderAdded(collider.Collider, entityID); } if (component.GetType() == typeof(CSphereCollider)) { CSphereCollider cSphereCollider = component as CSphereCollider; CTransform cTransform = VerifyTransform(cSphereCollider, entityID); if (cTransform == null) { return(1); } if (cSphereCollider.Collider == null) { cSphereCollider.Collider = AddOrGetUnityComponent <SphereCollider>(cTransform); if (cSphereCollider.PhysicsMaterialID != null && (cSphereCollider.Collider.material == null || cSphereCollider.Collider.material.name != cSphereCollider.PhysicsMaterialID)) { cSphereCollider.Collider.material = Resources.Load <PhysicMaterial>(cSphereCollider.PhysicsMaterialID); } } OnColliderAdded(cSphereCollider.Collider, entityID); } if (component.GetType() == typeof(CMeshRenderer)) { CMeshRenderer meshRenderer = component as CMeshRenderer; CTransform cTransform = VerifyTransform(meshRenderer, entityID); if (cTransform == null) { return(1); } if (meshRenderer.MeshRenderer == null) { meshRenderer.MeshRenderer = AddOrGetUnityComponent <MeshRenderer>(cTransform); if (meshRenderer.MaterialID != null && (meshRenderer.MeshRenderer.material == null || meshRenderer.MeshRenderer.material.name != meshRenderer.MaterialID)) { meshRenderer.MeshRenderer.material = Resources.Load <Material>(meshRenderer.MaterialID); } } if (meshRenderer.MeshFilter == null) { meshRenderer.MeshFilter = AddOrGetUnityComponent <MeshFilter>(cTransform); if (meshRenderer.MeshID != null && (meshRenderer.MeshFilter.mesh.name != meshRenderer.MeshID || meshRenderer.MeshFilter.mesh == null)) { Mesh mesh = Resources.Load <Mesh>(meshRenderer.MeshID); meshRenderer.MeshFilter.mesh = mesh; } meshRenderer.MeshID = meshRenderer.MeshFilter.mesh.name; } } return(0); }