public Bag <ECSComponent> ParseComponentData(JSONNode archetypeData)
        {
            Bag <ECSComponent> components         = new Bag <ECSComponent>();
            JSONNode           data               = null;
            JSONNode           componentDataArray = m_dataLocator.GetComponentData(archetypeData);
            Type componentType = null;

            for (int i = 0; i < componentDataArray.Count; i++)
            {
                data = componentDataArray[i];

                componentType = GetComponentTypeByName(m_dataLocator.GetComponentName(data));

                if (componentType == null)
                {
                    continue;
                }

                ECSComponent component = JsonUtility.FromJson(data.ToString(), componentType) as ECSComponent;
                ECSDebug.Assert(component != null, "Failed to create Component " + m_dataLocator.GetComponentName(data));

                components.Add(component);
            }

            components.ResizeToFit();

            return(components);
        }
Exemplo n.º 2
0
 public void LogWarning(object v)
 {
     if (IsDebugOn)
     {
         ECSDebug.LogWarning(v);
     }
 }
Exemplo n.º 3
0
 public void LogError(object v)
 {
     if (IsDebugOn)
     {
         ECSDebug.LogError(v);
     }
 }
Exemplo n.º 4
0
        public string GetComponentName(JSONNode componentData)
        {
            string componentName = componentData["ComponentName"];

            ECSDebug.Assert(componentName != null, "Validate JSON file, might have misspelt 'ComponentName'");
            return(componentName);
        }
        private Dictionary <string, Type> CacheComponentTypesFromJSON(string[] componentNames)
        {
            Dictionary <string, Type> returnedTypes = new Dictionary <string, Type>();

            foreach (string componentName in componentNames)
            {
                foreach (var pair in m_assemblyTypes)
                {
                    Type[] allTypes = pair.Value;

                    foreach (Type type in allTypes)
                    {
                        if (type.Name == componentName)
                        {
                            m_componentClasses.Add(componentName, type);
                            continue;
                        }
                    }
                }

                ECSDebug.Assert(m_componentClasses.ContainsKey(componentName), "Tried to cache type " + componentName + " but could not find it. Does it exist?");
            }

            return(returnedTypes);
        }
 private int[] CreateQuery(Type[] args)
 {
     ECSDebug.Log("Created query with args " + args.Length);
     m_queries[args]            = new Bag <int>();
     m_queries[args].NULL_VALUE = -1;
     RefreshQuery(args);
     return(m_queries[args].GetAll().Clone() as int[]);
 }
        public JSONNode GetArchetypeData(string v)
        {
            JSONNode data = m_dataLocator.GetEntityContainer(m_parsedJSON)[v];

            ECSDebug.Assert(data != null, "Archetype not found " + v);

            return(data);
        }
        public void Provide(string json)
        {
            m_parsedJSON = JSONNode.Parse(json);
            ECSDebug.Assert(json != "" && json != null && m_parsedJSON != null, "JSON is not Valid");

            Bag <string> componentsChecked = GetAllComponentNames(m_parsedJSON);

            CacheComponentTypesFromJSON(componentsChecked.GetAll());
        }
        private Type GetComponentTypeByName(string componentName)
        {
            if (m_componentClasses.ContainsKey(componentName) == false)
            {
                ECSDebug.LogError("Component Type not found " + componentName);
                return(null);
            }

            return(m_componentClasses[componentName]);
        }
        private void SortSystems()
        {
            m_sortedSystems = new List <ECSSystem>();

            Bag <ECSSystem> bag = m_systems.Clone();

            bag.ResizeToFit();

            List <ECSSystem> systems         = new List <ECSSystem>(bag.GetAll());
            List <ECSSystem> disabledSystems = new List <ECSSystem>();

            for (int i = systems.Count - 1; i >= 0; i--)
            {
                if (systems[i].IsEnabled() == false)
                {
                    disabledSystems.Add(systems[i]);
                    systems.RemoveAt(i);
                }
            }

            int       highestPriority       = int.MinValue;
            ECSSystem highestPriotitySystem = null;

            while (systems.Count > 0)
            {
                highestPriority       = int.MinValue;
                highestPriotitySystem = null;

                foreach (ECSSystem system in systems)
                {
                    if (system.UpdatePriority > highestPriority)
                    {
                        highestPriority       = system.UpdatePriority;
                        highestPriotitySystem = system;
                    }
                }

                systems.Remove(highestPriotitySystem);
                m_sortedSystems.Add(highestPriotitySystem);
            }

            foreach (ECSSystem disabledSystem in disabledSystems)
            {
                m_sortedSystems.Add(disabledSystem);
            }

            for (int i = 0; i < m_sortedSystems.Count; i++)
            {
                ECSDebug.Log(i + " - " + m_sortedSystems[i]);
            }
        }
Exemplo n.º 11
0
        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?");
        }
Exemplo n.º 12
0
        public ECSEntity SetupEntity(ECSEntity e, string archetype)
        {
            ECSDebug.Assert(m_parser != null, "Cannot create Entity from Archetype > JSON not provided!");

            JSONNode archetypeData = m_parser.GetArchetypeData(archetype);

            string baseArchetype = m_dataLocator.GetBaseArchetype(archetypeData);

            if (baseArchetype != null)
            {
                archetypeData = m_parser.OverwriteBaseArchetypeData(m_parser.GetArchetypeData(baseArchetype).AsObject, archetypeData.AsObject);
            }

            Bag <ECSComponent> components = m_parser.ParseComponentData(archetypeData);

            ComponentPreProcessing(archetype, components);
            m_componentManager.AddComponents(e.EntityID, components);

            return(e);
        }
Exemplo n.º 13
0
        public bool HasComponent(int entityID, Type type)
        {
            if (GetComponentID(type) == -1)
            {
                ECSDebug.LogWarning("[HasComponent " + type.Name + "] Component not yet registered " + type.Name.ToString());
                return(false);
            }

            if (HasComponentBag(entityID) == false)
            {
                return(false);
            }

            Bag <ECSComponent> bag = GetComponentBag(entityID);

            if (bag.Get(GetComponentID(type)) == null)
            {
                return(false);
            }

            return(true);
        }
        public void DestroyEntity(int entityID)
        {
            ECSEntity entity = m_entityBag.Get(entityID);

            if (entity == null)
            {
                ECSDebug.LogWarning("Tried to destroy entity " + entityID + " but is already destroyed");
                return;
            }

            if (OnEntityDestroyedPre != null)
            {
                OnEntityDestroyedPre(entity);
            }

            m_entityFactory.DestroyEntity(entity);
            m_entityBag.Set(entityID, null);

            if (OnEntityDestroyedPost != null)
            {
                OnEntityDestroyedPost(entity);
            }
        }
Exemplo n.º 15
0
 protected void LogForce(object v)
 {
     ECSDebug.LogForce(v);
 }
Exemplo n.º 16
0
 private Bag <ECSComponent> GetComponentBag(int entityID)
 {
     ECSDebug.Assert(m_entityComponents.ContainsKey(entityID) == true, "Entity does not have a component bag! > " + entityID);
     return(m_entityComponents[entityID]);
 }
Exemplo n.º 17
0
 protected void Log(object v)
 {
     ECSDebug.Log(SYSTEM_LOG_PREFIX + v);
 }
Exemplo n.º 18
0
 public void Assert(bool condition, object v)
 {
     ECSDebug.Assert(condition, v);
 }
 protected void OnComponentDeInitializeFailure(ECSComponent component, string reason)
 {
     ECSDebug.LogWarning("DeInitializing Component " + component.GetType().Name + " failed. Reason: " + reason);
 }
 public ECSEntity SetupEntity(ECSEntity e, string archetype)
 {
     ECSDebug.LogWarning("EntityFactory was not provided.. Using " + GetType().Name);
     return(e);
 }
Exemplo n.º 21
0
 private void LogWarning()
 {
     ECSDebug.LogWarning("ECSComponentFactory not provided.. using " + GetType().Name);
 }
 public ECSEntity CreateEntity()
 {
     ECSDebug.LogWarning("EntityFactory was not provided.. Using " + GetType().Name);
     return(new ECSEntity());
 }
Exemplo n.º 23
0
 protected void Assert(bool condition, object v)
 {
     ECSDebug.Assert(condition, SYSTEM_LOG_PREFIX + v);
 }
 public void DestroyEntity(ECSEntity entity)
 {
     ECSDebug.LogWarning("EntityFactory was not provided.. Using " + GetType().Name);
 }