/// <summary> /// 获取值,对最后一个添加的组件操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="component"></param> /// <returns></returns> public T GetValue <T>(ComponentValue component) { T t = default(T); int componentId = component.ComponentId; if (ComponentIds.COMPONENT_MAX_COUNT > componentId) { NormalComponent normalComponent = this._allComponenArray[componentId]; if (normalComponent == null) { Debug.LogError("组件不存在,组件Id:" + componentId); return(t); } try { object o = normalComponent.PropertyArray[component.PropertyId].Getter(normalComponent.CurrentComponent); t = (T)o; return(t); } catch (Exception ex) { Debug.LogErrorFormat("获取属性{0},的时候出错,错误信息{1}", component.PropertyId, ex.Message); return(t); } } Debug.LogError("组件Id过大无法获取,组件Id:" + componentId); return(t); }
/// <summary> /// 拷贝共享组件 /// 从目标实体拷贝到实体 /// </summary> /// <returns></returns> public Entity CopySharedComponent(Entity entity) { for (int i = 0; i < ComponentIds.COMPONENT_MAX_COUNT; i++) { NormalComponent component = entity._allComponenArray[i]; if (component == null) { continue; } if (component.SharedId < 0) { continue; } if (this._allComponenArray[i] != null) { continue; } this._allComponenArray[i] = component; this._currentFlag.SetFlag(component.OperatorId); if (_changeComponentCallBack != null) { _changeComponentCallBack.Invoke(this, component); } } return(this); }
/// <summary> /// 设置组件值,对最后一个添加的组件操作 /// </summary> /// <param name="component"></param> /// <param name="value"></param> /// <returns></returns> public Entity SetValue(ComponentValue component, object value) { int componentId = component.ComponentId; if (ComponentIds.COMPONENT_MAX_COUNT > componentId) { NormalComponent normalComponent = this._allComponenArray[componentId]; if (normalComponent == null) { Debug.LogError("组件不存在,组件Id:" + componentId); return(this); } try { if (Observer.IsUseThread) { TSThread.Instance.SetValue(this, normalComponent, component.PropertyId, value); } else { normalComponent.PropertyArray[component.PropertyId].Setter(this, normalComponent, value); } return(this); } catch (Exception ex) { Debug.LogErrorFormat("设置属性{0},的时候出错,错误信息{1}", component.PropertyId, ex.Message); return(this); } } Debug.LogError("组件Id过大无法设置,组件Id:" + componentId); return(this); }
public Entity AddComponent(int componentId) { if (componentId > ComponentIds.COMPONENT_MAX_COUNT) { throw new Exception("组件Id有误" + componentId.ToString()); } NormalComponent component = _allComponenArray[componentId]; if (component != null) { throw new Exception("增加组件失败,组件已存在,组件类型:" + componentId.ToString()); } if (_getComponentFunc == null) { throw new Exception("无法获取组件!!!"); } component = _getComponentFunc(componentId); if (component == null) { throw new Exception("增加组件失败,组件ID查询失败,组件类型:" + componentId.ToString()); } component.PropertyArray = ILHelper.GetComponentProperty(componentId); SetDefaultValue(component); _allComponenArray[componentId] = component; this._currentFlag.SetFlag(component.OperatorId); if (_changeComponentCallBack != null) { _changeComponentCallBack.Invoke(this, component); } return(this); }
/// <summary> /// 设置值 /// </summary> /// <param name="entity"></param> /// <param name="component"></param> /// <param name="propertyId"></param> /// <param name="value"></param> public void SetValue(Entity entity, NormalComponent component, int propertyId, object value) { Begin : if (Interlocked.CompareExchange(ref _valueLock, 1, 0) == FALSE) { ValueModel valueModel = null; if (_indexQueue.Count < 1) { valueModel = new ValueModel(_valueModelPool.Count); _valueModelPool.Add(null); } else { int index = _indexQueue.Dequeue(); valueModel = _valueModelPool[index]; _valueModelPool[index] = null; } valueModel.CurrentEntity = entity; valueModel.CurrentComponent = component; valueModel.CurrentPropertyId = propertyId; valueModel.CurrentValue = value; _mainThreadList.Add(valueModel); Interlocked.Exchange(ref _valueLock, 0); } else { goto Begin; } }
private void SetDefaultValue(NormalComponent dto) { for (int i = 0; i < dto.PropertyArray.Length; i++) { TSProperty tSProperty = dto.PropertyArray[i]; tSProperty.Setter(null, dto, tSProperty.DefaultValue); } }
public bool Contains(NormalComponent component) { if (_componentList.Count > component.InstanceId) { return(_componentList[component.InstanceId] != null); } throw new Exception("该对象不属于对象池!"); }
/// <summary> /// 回收一个组件 /// </summary> /// <param name="component"></param> partial void RecoverComponent(NormalComponent component) { if (component == null) { throw new Exception("回收的组件有误!!!"); } _componentPoolArray[component.ComponentId].Enqueue(component); }
public void DataDrivenMethod(Entity entity, NormalComponent com) { if (entity == null) { return; } int sharedId = com.SharedId; SharedComponent sharedComponent = null; if (sharedId > 0) { sharedComponent = _sharedComponentDic[sharedId]; } int count = _systemReactiveDic.Count; for (int i = 0; i < count; i++) { ReactiveSystemDto dto = _systemReactiveDic[i]; ComponentFlag executeCondition = dto.CurrentSystem.ExecuteCondition; ComponentFlag reactiveCondition = dto.CurrentSystem.ReactiveCondition; ComponentFlag reactiveIgnoreCondition = dto.CurrentSystem.ReactiveIgnoreCondition; if (!reactiveCondition.HasFlag(com.OperatorId)) { continue; } if (sharedId > 0) { foreach (Entity setEntity in sharedComponent.SharedEntityHashSet) { if (setEntity.GetComponentFlag().HasFlag(reactiveIgnoreCondition)) { continue; } if (setEntity.GetComponentFlag().HasFlag(executeCondition)) { dto.EntityHashSet.Add(setEntity); } } } else { if (entity.GetComponentFlag().HasFlag(reactiveIgnoreCondition)) { continue; } if (entity.GetComponentFlag().HasFlag(executeCondition)) { dto.EntityHashSet.Add(entity); } } } }
public SharedComponent(NormalComponent com, int shardId) { if (com == null) { throw new Exception("组件实体为空"); } _currentComponent = com; _sharedId = shardId; _currentComponent.SharedId = shardId; _operatorId = _currentComponent.OperatorId; _componentId = _currentComponent.ComponentId; _sharedEntityHashSet = new HashSet <Entity>(); }
public void Init(int count) { for (int i = 0; i < 10; i++) { NormalComponent component = ComponentIds.GetComponent(_componentId); if (component == null) { throw new Exception("需要获取的组件不存在"); } component.InstanceId = _componentList.Count; _componentList.Add(component); _indexQueue.Enqueue(i); } }
/// <summary> /// 创建共享组件 /// </summary> /// <param name="componentId"></param>Int64 componentId /// <returns></returns> public SharedComponent CreateSharedComponent(int componentId) { NormalComponent component = GetComponent(componentId); if ((component.CurrentComponent as ISharedComponent) != null) { SharedComponent shared = new SharedComponent(component, Utils.GetSharedId()); _sharedComponentDic.Add(shared.SharedId, shared); return(shared); } else { throw new Exception("创建共享组件失败,这不是共享组件!!!"); } }
public bool Enqueue(NormalComponent component) { int index = component.InstanceId; if (_componentList.Count > index) { if (_componentList[index] == null) { _indexQueue.Enqueue(index); _componentList[index] = component; return(true); } return(false); } return(false); }
/// <summary> /// 从目标实体拷贝到实体 /// </summary> /// <returns></returns> public Entity CopyComponent(Entity entity) { for (int i = 0; i < ComponentIds.COMPONENT_MAX_COUNT; i++) { NormalComponent component = entity._allComponenArray[i]; if (component == null) { continue; } if (component.SharedId > 0) { continue; } if (this._allComponenArray[i] != null) { continue; } this.AddComponent(i); } for (int i = 0; i < ComponentIds.COMPONENT_MAX_COUNT; i++) { NormalComponent component = entity._allComponenArray[i]; if (component == null) { continue; } if (component.SharedId > 0) { continue; } if (this._allComponenArray[i] != null) { continue; } NormalComponent thisComponent = this._allComponenArray[i]; for (int j = 0; j < component.PropertyArray.Length; j++) { if (component.PropertyArray[i].DontCopy) { continue; } thisComponent.PropertyArray[i].Setter(this, thisComponent, component.PropertyArray[i].Getter(component.CurrentComponent)); } } return(this); }
public NormalComponent Dequeue() { NormalComponent component = null; if (_indexQueue.Count > 0) { int index = _indexQueue.Dequeue(); component = _componentList[index]; _componentList[index] = null; return(component); } component = ComponentIds.GetComponent(_componentId); if (component == null) { throw new Exception("需要获取的组件不存在"); } component.InstanceId = _componentList.Count; _componentList.Add(null); return(component); }
/// <summary> /// 删除组件 /// </summary> /// <param name="componentId"></param> /// <returns></returns> public Entity RemoveComponent(int componentId) { if (ComponentIds.COMPONENT_MAX_COUNT > componentId) { NormalComponent normal = _allComponenArray[componentId]; if (normal == null) { goto Log; } _allComponenArray[componentId] = null; _currentFlag.RemoveFlag(normal.OperatorId); if (_changeComponentCallBack != null) { _changeComponentCallBack.Invoke(this, normal); } return(this); } Log : Debug.LogError("删除组件失败,组件Id不存在,组件Id:" + componentId); return(this); }
partial void MatchEntity(Entity entity, NormalComponent component) { int sharedId = component.SharedId; if (!entity.GetComponentFlag().HasFlag(component.OperatorId)) { if (sharedId > 0) { if (_sharedComponentDic.ContainsKey(sharedId)) { _sharedComponentDic[sharedId].SharedEntityHashSet.Remove(entity); } } foreach (Group item in _matchGroupHashSet) { if (item.ComponentFlag.HasFlag(component.OperatorId)) { item.RemoveEntity(entity); } } RecoverComponent(component); } else { if (sharedId > 0) { if (_sharedComponentDic.ContainsKey(sharedId)) { _sharedComponentDic[sharedId].SharedEntityHashSet.Add(entity); } } foreach (Group item in _matchGroupHashSet) { if (entity.GetComponentFlag().HasFlag(item.ComponentFlag)) { item.AddEntity(entity); } } } }
/// <summary> /// 删除组件 /// </summary> /// <returns></returns> public Entity RemoveComponentAll() { for (int i = 0; i < _allComponenArray.Length; i++) { if (i == ComponentIds.ACTIVE || i == ComponentIds.LIFE_CYCLE || i == ComponentIds.POOL) { continue; } NormalComponent normalComponent = _allComponenArray[i]; if (normalComponent != null) { _allComponenArray[i] = null; _currentFlag.RemoveFlag(normalComponent.OperatorId); if (_changeComponentCallBack != null) { _changeComponentCallBack.Invoke(this, normalComponent); } } } return(this); }
partial void MatchEntity(Entity entity, NormalComponent component);
partial void RecoverComponent(NormalComponent component);