예제 #1
0
        /// <summary>
        ///   Deinitializes the specified component.
        /// </summary>
        /// <param name="component">Component to deinitialize.</param>
        private void DeinitComponent(IEntityComponent component)
        {
            InspectorType inspectorType;

            if (!this.inspectorTypes.TryGetInspectorType(component.GetType(), out inspectorType))
            {
                this.game.Log.Warning(
                    "Entity component '" + component.GetType() + "' not flagged as inspector type, can't deinitialize.");
                return;
            }

            InspectorUtils.Deinit(this, inspectorType, component);
        }
        public string GetTileComponentString(IEntityComponent component)
        {
            if (component == null)
            {
                return("");
            }

            switch (component)
            {
            case Property prop:
                return(GetPropComponentString(prop));

            case PropertyDevelopment dev:
                return(GetDevComponentString(dev));

            case TrainStation train:
                return($"Base station rent: {train.BaseRent}");

            case UtilityProperty utility:
                return("");

            case ActionBox actionBox:
            case ActionTile actionTile:
            case FreeParking freeParking:
            case Go go:
            case Jail jail:
                return("");
            }
            return($"COULD NOT GET A STRING FOR {component.GetType().ShortTypeString()}");
        }
예제 #3
0
        public static void BindSingleValue(IEntityComponent component, string key, string value)
        {
            var  fieldInfo = component.GetType().GetField(key);
            Type fieldType = Nullable.GetUnderlyingType(fieldInfo.FieldType) ?? fieldInfo.FieldType;

            object safeValue = null;

            if (fieldType == typeof(Color))
            {
                safeValue = ColorTranslator.FromHtml(value);
            }
            else if (typeof(ICustomFieldSerialization).IsAssignableFrom(fieldType))
            {
                var instance = Activator.CreateInstance(fieldType);
                ((ICustomFieldSerialization)instance).Deserialize(value);
                safeValue = instance;
            }
            else if (fieldType.IsEnum)
            {
                safeValue = Enum.Parse(fieldType, value);
            }
            else if (value != null)
            {
                safeValue = Convert.ChangeType(value, fieldType);
            }

            fieldInfo.SetValue(component, safeValue);
        }
예제 #4
0
        /// <summary>
        /// Add the given component to the given entity.
        /// </summary>
        /// <param name="entity">Entity for which you want to add the component.</param>
        /// <param name="component">Component you want to add.</param>
        internal void AddComponent(Entity entity, IEntityComponent component)
        {
            Debug.Assert(entity != null, "Entity must not be null.");
            Debug.Assert(component != null, "Component must not be null.");

            ComponentType type = ComponentTypeManager.GetTypeFor(component.GetType());

            AddComponent(entity, component, type);
        }
예제 #5
0
        /// <summary>
        ///   Initializes the specified component with the specified attribute table.
        /// </summary>
        /// <param name="component">Component to initialize.</param>
        /// <param name="attributeTable">Attribute table which contains the data of the component.</param>
        private void InitComponent(IEntityComponent component, IAttributeTable attributeTable)
        {
            InspectorType inspectorType;

            if (!this.inspectorTypes.TryGetInspectorType(component.GetType(), out inspectorType))
            {
                this.game.Log.Warning(
                    "Entity component '" + component.GetType()
                    + "' not flagged as inspector type, can't initialize via reflection.");
                return;
            }

            var inspectorComponent = inspectorType.Attribute as InspectorComponentAttribute;

            if (inspectorComponent != null && inspectorComponent.InitExplicitly)
            {
                return;
            }

            InspectorUtils.InitFromAttributeTable(this, inspectorType, component, attributeTable);
        }
        public static bool NotIdentityOrProperty(this IEntityComponent component)
        {
            var type  = component.GetType();
            var check =
                type == typeof(Tile) ||
                type == typeof(Property) ||
                type == typeof(PropertyDevelopment) ||
                type == typeof(TrainStation) ||
                type == typeof(UtilityProperty);

            return(!check);
        }
예제 #7
0
        public static string Serialize(IEntityComponent component, int depth)
        {
            var stringBuilder = new StringBuilder();

            var componentType = component.GetType();

            stringBuilder.AppendLine($"[{componentType.Name}]");

            var fields = componentType.GetFields(BindingFlags.Public | BindingFlags.Instance).OrderBy(f => f.Name);

            foreach (var field in fields)
            {
                object value       = field.GetValue(component);
                string stringValue = null;

                Type fieldType = field.FieldType;

                if (IsNullOrDefault(value))
                {
                    stringValue = null;
                }
                else if (fieldType == typeof(Color))
                {
                    stringValue = ColorTranslator.ToHtml((Color)value);
                }
                else if (typeof(ICustomFieldSerialization).IsAssignableFrom(fieldType))
                {
                    stringValue = ((ICustomFieldSerialization)value).Serialize();
                }
                else if (fieldType == typeof(string) && ((string)value).Contains("\n"))
                {
                    stringBuilder.AppendLine($"{field.Name}: {"{"}");
                    stringBuilder.AppendLine(value.ToString());
                    stringBuilder.AppendLine("}");

                    stringValue = null;
                }
                else
                {
                    stringValue = value.ToString();
                }

                if (stringValue != null)
                {
                    stringBuilder.AppendLine($"{field.Name}: {stringValue}");
                }
            }

            return(stringBuilder.ToString());
        }
예제 #8
0
        /// <summary>
        /// Add a component to given Entity.
        /// </summary>
        /// <param name="entity">Given Entity.</param>
        /// <param name="component">Component to add.</param>
        public void AddComponent(Entity entity, IEntityComponent component)
        {
            Type componentType = component.GetType();

#if DEBUG
            if (HasComponent(entity, componentType))
            {
                throw new ComponentAlreadyExistsException();
            }
#endif
            if (componentType.IsComponentWithEntityId())
            {
                ((IEntityComponentWithEntityId)component).EntityId = entity.ID;
            }
            entitiesData[entity.ID].components.Add(componentType, component);
        }
예제 #9
0
        /// <summary>
        ///   Attaches the passed component to the entity with the specified id.
        /// </summary>
        /// <param name="entityId"> Id of the entity to attach the component to. </param>
        /// <param name="component"> Component to attach. </param>
        /// <param name="sendEvent">Indicates if an event should be send about the component adding.</param>
        /// <exception cref="ArgumentOutOfRangeException">Entity id is negative.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Entity id has not yet been assigned.</exception>
        /// <exception cref="ArgumentException">Entity with the specified id has already been removed.</exception>
        /// <exception cref="ArgumentNullException">Passed component is null.</exception>
        /// <exception cref="InvalidOperationException">There is already a component of the same type attached.</exception>
        public void AddComponent(int entityId, IEntityComponent component, bool sendEvent)
        {
            this.CheckEntityId(entityId);

            Type componentType = component.GetType();

            ComponentManager componentManager = this.GetComponentManager(componentType, true);

            componentManager.AddComponent(entityId, component);

            if (sendEvent)
            {
                this.game.EventManager.QueueEvent(
                    FrameworkEvent.ComponentAdded,
                    new EntityComponentData(entityId, component));
            }
        }
예제 #10
0
        /// <summary>
        ///   Attaches the passed component to the entity with the specified id.
        ///   Note that this manager does not check whether the specified id is valid.
        /// </summary>
        /// <param name="entityId">
        ///   Id of the entity to attach the component to.
        /// </param>
        /// <param name="component">
        ///   Component to attach.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///   Passed component is null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///   There is already a component of the same type attached.
        /// </exception>
        public void AddComponent(int entityId, IEntityComponent component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            if (this.components.ContainsKey(entityId))
            {
                throw new InvalidOperationException(
                          "There is already a component of type " + component.GetType() + " attached to entity with id "
                          + entityId + ".");
            }

            this.components.Add(entityId, component);
            this.OnComponentAdded(entityId, component);
        }
예제 #11
0
        /// <summary>
        ///   Attaches the passed component to the entity with the specified id.
        ///   Note that this manager does not check whether the specified id is valid.
        /// </summary>
        /// <param name="entityId">
        ///   Id of the entity to attach the component to.
        /// </param>
        /// <param name="component">
        ///   Component to attach.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///   Passed component is null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///   There is already a component of the same type attached.
        /// </exception>
        public void AddComponent(int entityId, IEntityComponent component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            if (this.components.ContainsKey(entityId))
            {
                throw new InvalidOperationException(
                    "There is already a component of type " + component.GetType() + " attached to entity with id "
                    + entityId + ".");
            }

            this.components.Add(entityId, component);
            this.OnComponentAdded(entityId, component);
        }
예제 #12
0
        /// <summary>
        /// Entities the manager removed component event.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="component">The component.</param>
        internal void EntityManagerRemovedComponentEvent(Entity entity, IEntityComponent component)
        {
            ComponentPoolable componentPoolable = component as ComponentPoolable;

            if (componentPoolable != null)
            {
                if (componentPoolable.PoolId < 0)
                {
                    return;
                }

                IComponentPool <ComponentPoolable> pool = this.entityWorld.GetPool(component.GetType());
                if (pool != null)
                {
                    pool.ReturnObject(componentPoolable);
                }
            }
        }
예제 #13
0
        /// <summary>
        ///   Adds the matching Unity component to the game object that represents
        ///   the entity with the passed id.
        /// </summary>
        /// <param name="e"> Game event that has occurred. </param>
        private void OnComponentAdded(GameEvent e)
        {
            Profiler.BeginSample("Component added");

            EntityComponentData eventArgs = (EntityComponentData)e.EventData;
            int entityId = eventArgs.EntityId;
            IEntityComponent component    = eventArgs.Component;
            GameObject       entityObject = this.entities[entityId];

            // Check if a behaviour has to be attached which visualizes the logic state.
            foreach (LogicToVisualMapping logicToVisualMapping in
                     this.logicVisualMappings.Where(mapping => mapping.LogicType == component.GetType()))
            {
                // NOTE: The component may already exist because we recycle existing entity objects and the old components
                // just get removed after the current update loop (see http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html).
                entityObject.AddComponent(logicToVisualMapping.VisualType);
            }

            Profiler.EndSample();
        }
예제 #14
0
        private void OnComponentRemoved(GameEvent e)
        {
            EntityComponentData eventArgs = (EntityComponentData)e.EventData;
            int entityId = eventArgs.EntityId;
            IEntityComponent component    = eventArgs.Component;
            GameObject       entityObject = this.entities[entityId];

            // Check if a behaviour has to be removed which visualizes the logic state.
            foreach (LogicToVisualMapping logicToVisualMapping in this.logicVisualMappings)
            {
                if (component.GetType() != logicToVisualMapping.LogicType)
                {
                    continue;
                }

                Component visualComponent = entityObject.GetComponent(logicToVisualMapping.VisualType);
                if (visualComponent != null)
                {
                    Destroy(visualComponent);
                }
                break;
            }
        }
예제 #15
0
        public Blueprint Save(int entityId)
        {
            AttributeTable attributeTable = new AttributeTable();
            List <Type>    componentTypes = new List <Type>();

            // Get all components.
            foreach (ComponentManager componentManager in this.componentManagers.Values)
            {
                IEntityComponent entityComponent = componentManager.GetComponent(entityId);
                if (entityComponent == null)
                {
                    continue;
                }

                componentTypes.Add(entityComponent.GetType());
                InspectorUtils.SaveToAttributeTable(this, entityComponent, attributeTable);
            }

            Blueprint blueprint = new Blueprint {
                AttributeTable = attributeTable, ComponentTypes = componentTypes
            };

            return(blueprint);
        }