/// <summary> /// Removes a component from the entity. /// </summary> /// <param name="name">Name of the component which is to be removed from the entity.</param> public void RemoveComponent(string name) { if (name == null || name.Length == 0) { throw new ArgumentNullException("Entity.RemoveComponent(): null name."); } IEntityComponent component = mComponents.Find(c => c.Name == name); if (component == null) { throw new ArgumentException("Entity.RemoveComponent(): component not found."); } else { component.Owner = null; component.OnRemove(); ResetComponents(); if (ComponentRemoved != null) { ComponentRemoved(this, component); } } mComponents.Remove(component); }
/// <summary> /// Adds the component. /// </summary> /// <param name="entity">The entity.</param> /// <param name="component">The component.</param> /// <param name="type">The type.</param> internal void AddComponent(Entity entity, IEntityComponent component, ComponentType type) { if (type.Id >= this.componentsByType.Capacity) { this.componentsByType.Set(type.Id, null); } Bag <IEntityComponent> components = this.componentsByType.Get(type.Id); if (components == null) { components = new Bag <IEntityComponent>(); this.componentsByType.Set(type.Id, components); } components.Set(entity.Index, component); entity.AddTypeBit(type.Bit); if (AddedComponentEvent != null) { AddedComponentEvent(entity, component); } this.entityWorld.RefreshEntity(entity); }
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()}"); }
public bool Add(Type type, IEntityComponent component) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (component == null) { throw new ArgumentNullException(nameof(component)); } if (type == typeof(IEntityComponent)) { return(false); } if (Contains(type)) { return(false); } _lookup = _lookup.Add(type, component); Sort(); return(true); }
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); }
/// <summary> /// Removes components marked for removal. /// </summary> internal void RemoveMarkedComponents() { foreach (Tuple <Entity, ComponentType> pair in this.componentsToBeRemoved) { Entity entity = pair.Item1; ComponentType componentType = pair.Item2; int entityId = entity.Index; Bag <IEntityComponent> components = this.componentsByType.Get(componentType.Id); if (components != null && entityId < components.Count) { IEntityComponent componentToBeRemoved = components.Get(entityId); if (RemovedComponentEvent != null && componentToBeRemoved != null) { RemovedComponentEvent(entity, componentToBeRemoved); } entity.RemoveTypeBit(componentType.Bit); this.entityWorld.RefreshEntity(entity); components.Set(entityId, null); } } this.componentsToBeRemoved.Clear(); }
public void AddComponent(IEntityComponent myComponent) { if (myComponent == null) { throw new ArgumentNullException("Componenet is null"); } if (componentList.Contains(myComponent)) { return; //take this code out if you want to have two of each component } componentList.Add(myComponent); ComponentsDictionary.Add(myComponent.name, myComponent); IEntityUpdateable updateable = myComponent as IEntityUpdateable; IEntityDrawable drawable = myComponent as IEntityDrawable; if (updateable != null) { updateableComponentList.Add(updateable); } if (drawable != null) { drawableComponentList.Add(drawable); } myComponent.Initialize(); myComponent.Start(); }
public static T GetSiblingComponent <T>(this IEntityComponent component) where T : IEntityComponent { Entity entity = Game.World.EntityManager.GetComponentEntity(component); return(entity.GetComponent <T>()); }
/// <summary> /// Attempts to add the component to the object. /// It should work fine if there aren't any conflicting component Ids. /// You can't add a type of component more than once. /// </summary> /// <param name="Component"> The Component to add. </param> /// <returns> Whether the operation succeded. </returns> public bool AddComponent(IEntityComponent Component) { uint key = Component.GetID(); Component.OnCreate(this); return(Components.TryAdd(key, Component)); }
/// <summary> /// Strips all components from the given entity. /// </summary> /// <param name="entity">Entity for which you want to remove all components</param> internal void RemoveComponentsOfEntity(Entity entity) { Debug.Assert(entity != null, "Entity must not be null."); entity.TypeBits = 0; this.entityWorld.RefreshEntity(entity); int entityId = entity.Index; for (int index = this.componentsByType.Count - 1; index >= 0; --index) { Bag <IEntityComponent> components = this.componentsByType.Get(index); if (components != null && entityId < components.Count) { IEntityComponent componentToBeRemoved = components.Get(entityId); if (RemovedComponentEvent != null && componentToBeRemoved != null) { RemovedComponentEvent(entity, componentToBeRemoved); } components.Set(entityId, null); } } }
/// <summary> /// Gets a component of the passed type attached to the entity with the specified id. /// </summary> /// <param name="entityId"> Id of the entity to get the component of. </param> /// <param name="componentType"> Type of the component to get. </param> /// <returns> The component, if there is one of the specified type attached to the entity, and null otherwise. </returns> /// <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 type is null.</exception> public IEntityComponent GetComponent(int entityId, Type componentType) { this.CheckEntityId(entityId); if (componentType == null) { throw new ArgumentNullException("componentType"); } // Get component manager. if (componentType.IsInterface()) { foreach (KeyValuePair <Type, ComponentManager> componentManagerPair in this.componentManagers) { if (componentType.IsAssignableFrom(componentManagerPair.Key)) { IEntityComponent component = componentManagerPair.Value.GetComponent(entityId); if (component != null) { return(component); } } } return(null); } ComponentManager componentManager; return(this.componentManagers.TryGetValue(componentType, out componentManager) ? componentManager.GetComponent(entityId) : null); }
/// <summary> /// <para> /// Issues the entity with the specified id for removal at the end of /// the current tick. /// </para> /// <para> /// If the entity is inactive, it is removed immediately and no /// further event is raised. /// </para> /// </summary> /// <param name="entityId"> Id of the entity to remove. </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> public void RemoveEntity(int entityId) { if (this.EntityIsInactive(entityId)) { this.inactiveEntities.Remove(entityId); return; } this.CheckEntityId(entityId); if (this.EntityIsBeingRemoved(entityId)) { return; } // Remove components. foreach (ComponentManager manager in this.componentManagers.Values) { IEntityComponent component = manager.GetComponent(entityId); if (component == null) { continue; } this.game.EventManager.QueueEvent( FrameworkEvent.ComponentRemoved, new EntityComponentData(entityId, component)); } this.OnEntityRemoved(entityId); this.removedEntities.Add(entityId); }
public void PlayerLanded(Player player, IEntityComponent component) { var chosenAction = ChooseAction((ActionBox)component); _context.Add(new ExecuteAction(chosenAction, player.Id)); _context.RenderCommunications.CurDescription = chosenAction.Descsription; }
public void TestGetComponentBeforeAdding() { this.testEntityId = this.entityManager.CreateEntity(); IEntityComponent entityComponent = this.entityManager.GetComponent( this.testEntityId, typeof(TestEntityComponent)); Assert.IsNull(entityComponent); }
public T GetComponent <T>() where T : new() { int component_type = IEntityComponent.GetComponentID <T>(); ulong ptr = WComponent(handle, component_type, 3); T result = new T(); ((InternalObject)(object)result).SetHandle(ptr); return(result); }
/// <summary> /// Add a component to the given entity. /// If the component's type does not already exist, /// add it to the bag of available component types. /// </summary> /// <typeparam name="T">Component type you want to add.</typeparam> /// <param name="entity">The entity to which you want to add the component.</param> /// <param name="component">The component instance you want to add.</param> internal void AddComponent <T>(Entity entity, IEntityComponent component) where T : IEntityComponent { Debug.Assert(entity != null, "Entity must not be null."); Debug.Assert(component != null, "Component must not be null."); ComponentType type = ComponentTypeManager.GetTypeFor <T>(); AddComponent(entity, component, type); }
internal void OnComponentRemoved(IEntityComponent comp) { var meth = ObjUtil.ExtractDelegate <Action <GameTime> >(comp, EntityConstants.MSG_UPDATE); if (meth != null) { _updateDelegates -= meth; } }
public int GetRent(int renteeId, int ownerId, IEntityComponent component, int tileId) { var property = _context.GetTileComponent <Property>(tileId); var owner = _context.GetPlayer(ownerId); var dice = _context.Dice(); var ownedPropertyInSet = _context.OwnedPropertiesInSet(owner, property.SetId); return(dice.Sum * 5 * (int)Math.Round(Math.Pow(2, ownedPropertyInSet.Count - 1))); }
/// <summary> /// Gets the component with the key value. /// </summary> /// <param name="idx"> The key value of the component. </param> /// <returns> The component or null if it wasn't found. </returns> public IEntityComponent GetComponent(uint idx) { IEntityComponent OutComponent = null; if (Components.TryGetValue(idx, out OutComponent)) { return(OutComponent); } return(null); }
/// <summary> /// Constructs a new data object holding information on a component /// event. /// </summary> /// <param name="entityId"> /// Id of the entity the component event has been fired for. /// </param> /// <param name="component"> /// Component that has been interacted with. /// </param> /// <exception cref="ArgumentNullException"> /// Passed component is null. /// </exception> public EntityComponentData(int entityId, IEntityComponent component) { if (component == null) { throw new ArgumentNullException("component"); } this.EntityId = entityId; this.Component = component; }
public IEntityComponent EntityComponentCreateOrUpdateWithModel(string entityId, CLASS_ID_COMPONENT classId, object data) { IDCLEntity entity = GetEntityForUpdate(entityId); if (entity == null) { Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {entityId} doesn't exist!"); return(null); } IEntityComponent newComponent = null; if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK) { OnPointerEvent.Model model = JsonUtility.FromJson <OnPointerEvent.Model>(data as string); classId = model.GetClassIdFromType(); } if (!entity.components.ContainsKey(classId)) { var factory = Environment.i.world.componentFactory; newComponent = factory.CreateComponent((int)classId) as IEntityComponent; if (newComponent != null) { entity.components.Add(classId, newComponent); OnComponentAdded?.Invoke(newComponent); newComponent.Initialize(this, entity); if (data is string json) { newComponent.UpdateFromJSON(json); } else { newComponent.UpdateFromModel(data as BaseModel); } } } else { newComponent = EntityComponentUpdate(entity, classId, data as string); } if (newComponent != null && newComponent is IOutOfSceneBoundariesHandler) { Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); } OnChanged?.Invoke(); Environment.i.platform.physicsSyncController.MarkDirty(); Environment.i.platform.cullingController.MarkDirty(); return(newComponent); }
/// <summary> /// Removes the component mapped 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 remove the component from. /// </param> /// <param name="component">Removed component.</param> /// <returns> /// Whether a component has been removed, or not. /// </returns> public bool RemoveComponent(int entityId, out IEntityComponent component) { if (this.components.TryGetValue(entityId, out component)) { this.components.Remove(entityId); this.OnComponentRemoved(entityId, component); return(true); } return(false); }
public void CleanUpEntityComponents() { DecentralandEntity entity = new DecentralandEntity(); IEntityComponent component = Substitute.For <IEntityComponent>(); entity.components.Add(CLASS_ID_COMPONENT.NONE, component); entity.Cleanup(); component.Received().Cleanup(); }
public int GetRent(int renteeId, int ownerId, IEntityComponent component, int tileId) { var station = component as TrainStation; var property = _context.GetTileComponent <Property>(tileId); var owner = _context.GetPlayer(ownerId); var ownedPropertyInSet = _context.OwnedPropertiesInSet(owner, property.SetId); int rentValue = station.BaseRent * (int)Math.Round(Math.Pow(2, ownedPropertyInSet.Count - 1)); return(rentValue); }
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); }
/// <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); }
/// <summary> /// Adds a component with the specified type to entity with the /// specified id and initializes it with the values taken from /// the passed attribute table. /// </summary> /// <param name="componentType">Type of the component to add.</param> /// <param name="entityId">Id of the entity to add the component to.</param> /// <param name="attributeTable">Attribute table to initialize the component with.</param> private void AddComponent(Type componentType, int entityId, IAttributeTable attributeTable) { // Create component. IEntityComponent component = (IEntityComponent)Activator.CreateInstance(componentType); // Init component. this.InitComponent(component, attributeTable); // Initialize component with the attribute table data. component.InitComponent(attributeTable); // Add component. this.AddComponent(entityId, component); }
/// <summary> /// Unregisters a component from this entity. /// </summary> /// <param name="componentId">The ID of the component to unregister.</param> public void UnregisterComponent(string componentId) { if (string.IsNullOrWhiteSpace(componentId)) { throw new ArgumentNullException(nameof(componentId)); } IEntityComponent resolvedComponent = this.componentList.FirstOrDefault(x => x.Id == componentId); if (resolvedComponent != null) { this.componentList.Remove(resolvedComponent); } }
private IEntityComponent AddComponent(Type componentType) { IEntityComponent instance = (IEntityComponent)Activator.CreateInstance(componentType); this.components.Add(instance); if (Application.isPlaying) { this.entity.AddComponent(instance); } return(instance); }
internal void OnComponentAdd(IEntityComponent comp) { var meth = ObjUtil.ExtractDelegate <Action <GameTime> >(comp, EntityConstants.MSG_UPDATE); if (meth != null) { _updateDelegates += meth; } if (_manager != null) { _manager.RegisterComponent(comp); } }
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()); }
/// <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); }
/// <summary> /// Attaches a component to this entity. /// </summary> /// <param name="component">The component to attach to the entity.</param> public void AttachComponent(IEntityComponent component) { // Don't allow null parameters. if (component == null) { throw new ArgumentNullException("Component is null."); } // Don't allow multiples of the same type of component String id = component.ID; for (int i = 0; i < components.Count; i++) { if (components[i].ID.Equals(id)) { //throw new ArgumentException("Component already exists in entity."); } } // Add the component components.Add(component); if (component is IEntityUpdateable) { updateableComponents.Add((IEntityUpdateable)component); } if (component is IEntityDrawable) { drawableComponents.Add((IEntityDrawable)component); } // Initialize the component if this entity has already been initialized. if (isInitialized) { component.Initialize(); component.Start(); } }
/// <summary> /// Removes a component from the entity /// </summary> /// <param name="component">The component to remove</param> /// <returns>Returns true if the component was removed. False otherwise.</returns> public bool DetachComponent(IEntityComponent component) { // Don't allow null parameters. if (component == null) { throw new ArgumentNullException("Component is null"); } bool removed = components.Remove(component); if (removed) { if (component is IEntityUpdateable) { updateableComponents.Remove((IEntityUpdateable) component); } if (component is IEntityDrawable) { drawableComponents.Remove((IEntityDrawable) component); } } return removed; }
/// <summary> /// Removes the component mapped 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 remove the component from. /// </param> /// <param name="component">Removed component.</param> /// <returns> /// Whether a component has been removed, or not. /// </returns> public bool RemoveComponent(int entityId, out IEntityComponent component) { if (this.components.TryGetValue(entityId, out component)) { this.components.Remove(entityId); this.OnComponentRemoved(entityId, component); return true; } return false; }
public bool RemoveComponent(IEntityComponent myComponent) { if (myComponent == null) { //throw a null exception } if (componentList.Remove(myComponent)) { IEntityUpdateable updateable = myComponent as IEntityUpdateable; IEntityDrawable drawable = myComponent as IEntityDrawable; if (updateable != null) { updateableComponentList.Remove(updateable); } if (drawable != null) { drawableComponentList.Remove(drawable); } return true; } return false; }
/// <summary> /// Adds a component to this entity. Only allows one component of each type /// to be in a single entity. /// </summary> /// <param name="aComponent">The component to add</param> public void AddComponent(IEntityComponent aComponent) { if (aComponent == null) { throw new ArgumentNullException("component is null"); } //if you would like more than one of any type of component into this entity, //remove this if block. if (_components.Contains(aComponent)) { return; } //add to master and lookup list _components.Add(aComponent); Components.Add(aComponent.Name, aComponent); IEntityUpdateable updateable = aComponent as IEntityUpdateable; IEntityDrawable drawable = aComponent as IEntityDrawable; //if the component can be updated, add it to that list if (updateable != null) { _updateableComponents.Add(updateable); updateable.UpdateOrderChanged += OnComponentUpdateOrderChanged; OnComponentUpdateOrderChanged(this, EventArgs.Empty); } //if the component can be draw, add it to that list if (drawable != null) { _drawableComponents.Add(drawable); drawable.DrawOrderChanged += OnComponentDrawOrderChanged; OnComponentDrawOrderChanged(this, EventArgs.Empty); } //if the entity has already initialized, call this item's initialize and start methods if (_isInitialized) { aComponent.Initialize(); aComponent.Start(); } }
public void AddComponent(IEntityComponent entityComponent) { _entityComponents.Add(entityComponent); }
/// <summary> /// Removes a component from the entity /// </summary> /// <param name="aComponent">The component to remove</param> /// <returns>true if a component was removed, false otherwise</returns> public bool RemoveComponent(IEntityComponent aComponent) { if (aComponent == null) { throw new ArgumentNullException("component was null"); } if (_components.Remove(aComponent)) { IEntityUpdateable updateable = aComponent as IEntityUpdateable; IEntityDrawable drawable = aComponent as IEntityDrawable; //if the component was updateable, remove it from that list if (updateable != null) { _updateableComponents.Remove(updateable); updateable.UpdateOrderChanged -= OnComponentUpdateOrderChanged; } //if the component was drawable, remove it from that list if (drawable != null) { _drawableComponents.Remove(drawable); drawable.DrawOrderChanged -= OnComponentDrawOrderChanged; } return true; } return false; }
public void AddComponent(IEntityComponent comp) { comp.Owner = this; _components.Add(comp); }