public T AddComponent <T>() where T : Component, new() { Type typeofT = typeof(T); if (!ComponentsRegistry.ContainsKey(typeofT)) { EntityManager.RegisterAddEntityComponent(typeofT, this); } else if (typeofT.IsDefined(typeof(UniqueComponentAttribute), true)) { throw new Exception("Trying to adding a unique component to an entity but this entity already have the component."); } T c = new T { Entity = this }; Components.Add(c); c.Transform = Transform; c.Init(); ComponentsRegistry[typeofT] = true; return(c); }
public void AddComponent(ReplicaComponentsId componentId) { var component = new ObjectComponent { ComponentType = componentId }; var table = FdbEditor.Database.Tables.FirstOrDefault(t => t.Name == componentId.ToString()); var componentRow = new ComponentsRegistry(new Row(new[] { new Field(DataType.Integer, 0), new Field(DataType.Integer, 0), new Field(DataType.Integer, 0), })); component.RegistryRow = componentRow.DatabaseRow; componentRow.DatabaseTable = ObjectEditor.ComponentsTable; ObjectEditor.ComponentsTable.AppendRow(component.RegistryRow); componentRow.id = Lot; componentRow.component_type = (int)componentId; componentRow.component_id = table?.Rows.Select(r => (int)r.Fields[0].Value).Max() + 1 ?? default; if (table != default) { var fields = new Field[table.Structure.Length]; for (var index = 0; index < fields.Length; index++) { var field = table.Structure[index]; fields[index] = new Field(field.Type, 0); switch (field.Type) { case DataType.Nothing: fields[index].Value = 0; break; case DataType.Integer: fields[index].Value = 0; break; case DataType.Unknown1: fields[index].Value = 0; break; case DataType.Float: fields[index].Value = 0; break; case DataType.Text: fields[index].Value = ""; break; case DataType.Boolean: fields[index].Value = false; break; case DataType.Bigint: fields[index].Value = 0; break; case DataType.Unknown2: fields[index].Value = 0; break; case DataType.Varchar: fields[index].Value = ""; break; default: throw new ArgumentOutOfRangeException(); } } fields[0].Value = componentRow.component_id; component.ComponentRow = new Row(fields); table.AppendRow(component.ComponentRow); } Components.Add(component); }
public bool HasComponent <T>() where T : Component { Type typeofT = typeof(T); return(ComponentsRegistry.ContainsKey(typeofT) && ComponentsRegistry[typeofT]); }