Beispiel #1
0
        void onEntityCreated(IContext context, IEntityExt entity)
        {
            var entityBehaviour = _entityBehaviourPool.Count > 0
                ? _entityBehaviourPool.Pop()
                : new GameObject().AddComponent <EntityBehaviour>();

            entityBehaviour.Init(context, entity, _entityBehaviourPool);
            entityBehaviour.transform.SetParent(_gameObject.transform, false);
        }
 void onEntityReleased(IEntityExt e)
 {
     _entity.OnEntityReleased -= onEntityReleased;
     gameObject.SetActive(false);
     gameObject.hideFlags = HideFlags.HideInHierarchy;
     _entityBehaviourPool.Push(this);
     _cachedName = null;
     name        = string.Empty;
 }
 public void Init(IContext context, IEntityExt entity, Stack <EntityBehaviour> entityBehaviourPool)
 {
     _context                  = context;
     _entity                   = entity;
     _entityBehaviourPool      = entityBehaviourPool;
     _entity.OnEntityReleased += onEntityReleased;
     gameObject.hideFlags      = HideFlags.None;
     gameObject.SetActive(true);
     Update();
 }
Beispiel #4
0
        public void Unlink()
        {
            if (_entity == null)
            {
                throw new Exception("EntityLink is already unlinked!");
            }

            _entity.Release(this);
            _entity = null;
        }
Beispiel #5
0
        public void Link(IEntityExt entity)
        {
            if (_entity != null)
            {
                throw new Exception("EntityLink is already linked to " + _entity + "!");
            }

            _entity = entity;
            _entity.Retain(this);
        }
        void switchToContext()
        {
            if (_context != null)
            {
                _context.Reset();
            }
            var targetContext = _allContexts[_contextIndex];

            _context = (IContext)Activator.CreateInstance(targetContext.GetType());
            _entity  = (IEntityExt)_context.GetType().GetMethod("CreateEntity").Invoke(_context, null);
        }
Beispiel #7
0
        public static EntityLink Link(this GameObject gameObject, IEntityExt entity)
        {
            var link = gameObject.GetEntityLink();

            if (link == null)
            {
                link = gameObject.AddComponent <EntityLink>();
            }

            link.Link(entity);
            return(link);
        }
        public static void DrawComponents(IEntityExt entity)
        {
            var unfoldedComponents    = getUnfoldedComponents(entity);
            var componentMemberSearch = getComponentMemberSearch(entity);

            EditorLayout.BeginVerticalBox();
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Components (" + entity.GetComponents().Length + ")", EditorStyles.boldLabel);
                    if (EditorLayout.MiniButtonLeft("▸"))
                    {
                        for (int i = 0; i < unfoldedComponents.Length; i++)
                        {
                            unfoldedComponents[i] = false;
                        }
                    }

                    if (EditorLayout.MiniButtonRight("▾"))
                    {
                        for (int i = 0; i < unfoldedComponents.Length; i++)
                        {
                            unfoldedComponents[i] = true;
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();

                var index = drawAddComponentMenu(entity);
                if (index >= 0)
                {
                    var componentType = entity.contextInfo.componentTypes[index];
                    var component     = entity.CreateComponent(index, componentType);
                    entity.AddComponent(index, component);
                }

                EditorGUILayout.Space();

                componentNameSearchString = EditorLayout.SearchTextField(componentNameSearchString);

                EditorGUILayout.Space();

                var indices    = entity.GetComponentIndices();
                var components = entity.GetComponents();
                for (int i = 0; i < components.Length; i++)
                {
                    DrawComponent(unfoldedComponents, componentMemberSearch, entity, indices[i], components[i]);
                }
            }
            EditorLayout.EndVerticalBox();
        }
Beispiel #9
0
        static string[] getComponentMemberSearch(IEntityExt entity)
        {
            string[] componentMemberSearch;
            if (!contextToComponentMemberSearch.TryGetValue(entity.contextInfo.name, out componentMemberSearch))
            {
                componentMemberSearch = new string[entity.totalComponents];
                for (int i = 0; i < componentMemberSearch.Length; i++)
                {
                    componentMemberSearch[i] = string.Empty;
                }
                contextToComponentMemberSearch.Add(entity.contextInfo.name, componentMemberSearch);
            }

            return(componentMemberSearch);
        }
Beispiel #10
0
        static bool[] getUnfoldedComponents(IEntityExt entity)
        {
            bool[] unfoldedComponents;
            if (!contextToUnfoldedComponents.TryGetValue(entity.contextInfo.name, out unfoldedComponents))
            {
                unfoldedComponents = new bool[entity.totalComponents];
                for (int i = 0; i < unfoldedComponents.Length; i++)
                {
                    unfoldedComponents[i] = true;
                }
                contextToUnfoldedComponents.Add(entity.contextInfo.name, unfoldedComponents);
            }

            return(unfoldedComponents);
        }
        static int drawAddComponentMenu(IEntityExt entity)
        {
            var componentInfos = getComponentInfos(entity)
                                 .Where(info => !entity.HasComponent(info.index))
                                 .ToArray();
            var componentNames = componentInfos
                                 .Select(info => info.name)
                                 .ToArray();
            var index = EditorGUILayout.Popup("Add Component", -1, componentNames);

            if (index >= 0)
            {
                return(componentInfos[index].index);
            }

            return(-1);
        }
        /// Adds all components from the blueprint to the entity.
        /// When 'replaceComponents' is set to true entity.ReplaceComponent()
        /// will be used instead of entity.AddComponent().
        public static void ApplyBlueprint(this IEntityExt entity, Blueprint blueprint,
                                          bool replaceComponents = false)
        {
            var componentsLength = blueprint.components.Length;

            for (int i = 0; i < componentsLength; i++)
            {
                var componentBlueprint = blueprint.components[i];
                if (replaceComponents)
                {
                    entity.ReplaceComponent(componentBlueprint.index,
                                            componentBlueprint.CreateComponent(entity));
                }
                else
                {
                    entity.AddComponent(componentBlueprint.index,
                                        componentBlueprint.CreateComponent(entity));
                }
            }
        }
Beispiel #13
0
        static GUIStyle getColoredBoxStyle(IEntityExt entity, int index)
        {
            GUIStyle[] styles;
            if (!contextToColoredBoxStyles.TryGetValue(entity.contextInfo.name, out styles))
            {
                styles = new GUIStyle[entity.totalComponents];
                for (int i = 0; i < styles.Length; i++)
                {
                    var hue            = (float)i / (float)entity.totalComponents;
                    var componentColor = Color.HSVToRGB(hue, 0.7f, 1f);
                    componentColor.a = 0.15f;
                    var style = new GUIStyle(GUI.skin.box);
                    style.normal.background = createTexture(2, 2, componentColor);
                    styles[i] = style;
                }
                contextToColoredBoxStyles.Add(entity.contextInfo.name, styles);
            }

            return(styles[index]);
        }
        public static void DrawEntity(IEntityExt entity)
        {
            var bgColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Destroy Entity"))
            {
                entity.Destroy();
            }

            GUI.backgroundColor = bgColor;

            DrawComponents(entity);

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("Retained by (" + entity.retainCount + ")", EditorStyles.boldLabel);

            var safeAerc = entity.aerc as SafeAERC;

            if (safeAerc != null)
            {
                EditorLayout.BeginVerticalBox();
                {
                    foreach (var owner in safeAerc.owners.OrderBy(o => o.GetType().Name))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(owner.ToString());
                            if (EditorLayout.MiniButton("Release"))
                            {
                                entity.Release(owner);
                            }

                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
                EditorLayout.EndVerticalBox();
            }
        }
Beispiel #15
0
        static ComponentInfo[] getComponentInfos(IEntityExt entity)
        {
            ComponentInfo[] infos;
            if (!contextToComponentInfos.TryGetValue(entity.contextInfo.name, out infos))
            {
                var contextInfo = entity.contextInfo;
                var infosList   = new List <ComponentInfo>(contextInfo.componentTypes.Length);
                for (int i = 0; i < contextInfo.componentTypes.Length; i++)
                {
                    infosList.Add(new ComponentInfo {
                        index = i,
                        name  = contextInfo.componentNames[i],
                        type  = contextInfo.componentTypes[i]
                    });
                }
                infos = infosList.ToArray();
                contextToComponentInfos.Add(entity.contextInfo.name, infos);
            }

            return(infos);
        }
Beispiel #16
0
        public Blueprint(string contextIdentifier, string name, IEntityExt entity)
        {
            this.contextIdentifier = contextIdentifier;
            this.name = name;

            if (entity != null)
            {
                var allComponents    = entity.GetComponents();
                var componentIndices = entity.GetComponentIndices();
                components = new ComponentBlueprint[allComponents.Length];
                for (int i = 0; i < allComponents.Length; i++)
                {
                    components[i] = new ComponentBlueprint(
                        componentIndices[i], allComponents[i]
                        );
                }
            }
            else
            {
                components = new ComponentBlueprint[0];
            }
        }
Beispiel #17
0
        public void Serialize(IEntityExt entity)
        {
            var blueprint = new Blueprint(entity.contextInfo.name, name, entity);

            Serialize(blueprint);
        }
        public static void DrawComponent(bool[] unfoldedComponents, string[] componentMemberSearch, IEntityExt entity, int index, IComponent component)
        {
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();

            if (EditorLayout.MatchesSearchString(componentName.ToLower(), componentNameSearchString.ToLower()))
            {
                var boxStyle = getColoredBoxStyle(entity, index);
                EditorGUILayout.BeginVertical(boxStyle);
                {
                    if (!Attribute.IsDefined(componentType, typeof(DontDrawComponentAttribute)))
                    {
                        var memberInfos = componentType.GetPublicMemberInfos();
                        EditorGUILayout.BeginHorizontal();
                        {
                            if (memberInfos.Count == 0)
                            {
                                EditorGUILayout.LabelField(componentName, EditorStyles.boldLabel);
                            }
                            else
                            {
                                unfoldedComponents[index] = EditorLayout.Foldout(unfoldedComponents[index], componentName, foldoutStyle);
                                if (unfoldedComponents[index])
                                {
                                    componentMemberSearch[index] = memberInfos.Count > 5
                                        ? EditorLayout.SearchTextField(componentMemberSearch[index])
                                        : string.Empty;
                                }
                            }

                            if (EditorLayout.MiniButton("-"))
                            {
                                entity.RemoveComponent(index);
                            }
                        }
                        EditorGUILayout.EndHorizontal();

                        if (unfoldedComponents[index])
                        {
                            var newComponent = entity.CreateComponent(index, componentType);
                            component.CopyPublicMemberValues(newComponent);

                            var changed         = false;
                            var componentDrawer = getComponentDrawer(componentType);
                            if (componentDrawer != null)
                            {
                                EditorGUI.BeginChangeCheck();
                                {
                                    componentDrawer.DrawComponent(newComponent);
                                }
                                changed = EditorGUI.EndChangeCheck();
                            }
                            else
                            {
                                foreach (var info in memberInfos)
                                {
                                    if (EditorLayout.MatchesSearchString(info.name.ToLower(), componentMemberSearch[index].ToLower()))
                                    {
                                        var memberValue = info.GetValue(newComponent);
                                        var memberType  = memberValue == null ? info.type : memberValue.GetType();
                                        if (DrawObjectMember(memberType, info.name, memberValue, newComponent, info.SetValue))
                                        {
                                            changed = true;
                                        }
                                    }
                                }
                            }

                            if (changed)
                            {
                                entity.ReplaceComponent(index, newComponent);
                            }
                            else
                            {
                                entity.GetComponentPool(index).Push(newComponent);
                            }
                        }
                    }
                    else
                    {
                        EditorGUILayout.LabelField(componentName, "[DontDrawComponent]", EditorStyles.boldLabel);
                    }
                }
                EditorLayout.EndVerticalBox();
            }
        }
Beispiel #19
0
        public IComponent CreateComponent(IEntityExt entity)
        {
            if (_type == null)
            {
                _type = fullTypeName.ToType();

                if (_type == null)
                {
                    throw new ComponentBlueprintException(
                              "Type '" + fullTypeName +
                              "' doesn't exist in any assembly!",
                              "Please check the full type name."
                              );
                }

                if (!_type.ImplementsInterface <IComponent>())
                {
                    throw new ComponentBlueprintException(
                              "Type '" + fullTypeName +
                              "' doesn't implement IComponent!",
                              typeof(ComponentBlueprint).Name +
                              " only supports IComponent."
                              );
                }
            }

            var component = entity.CreateComponent(index, _type);

            if (_componentMembers == null)
            {
                var memberInfos = _type.GetPublicMemberInfos();
                _componentMembers = new Dictionary <string, PublicMemberInfo>(
                    memberInfos.Count
                    );
                for (int i = 0; i < memberInfos.Count; i++)
                {
                    var info = memberInfos[i];
                    _componentMembers.Add(info.name, info);
                }
            }

            for (int i = 0; i < members.Length; i++)
            {
                var member = members[i];

                PublicMemberInfo memberInfo;
                if (_componentMembers.TryGetValue(member.name, out memberInfo))
                {
                    memberInfo.SetValue(component, member.value);
                }
                else
                {
                    Console.WriteLine(
                        "Could not find member '" + member.name +
                        "' in type '" + _type.FullName + "'!\n" +
                        "Only non-static public members are supported."
                        );
                }
            }

            return(component);
        }