internal void OnComponentRemoved(IEntityComponent comp) { var meth = ObjUtil.ExtractDelegate <Action <GameTime> >(comp, EntityConstants.MSG_UPDATE); if (meth != null) { _updateDelegates -= meth; } }
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 IEntityComponent AddComponent(Type tp) { if (tp == null) { throw new ArgumentNullException("tp"); } if (!typeof(IEntityComponent).IsAssignableFrom(tp)) { throw new ArgumentException("Type must implement IEntityComponent."); } IEntityComponent comp = this.GetComponent(tp); if (comp != null) { return(comp); } try { comp = System.Activator.CreateInstance(tp) as IEntityComponent; } catch { return(null); } if (comp is EntityComponent) { _lst.Add(comp); (comp as EntityComponent).OnAddedToEntity(_owner); } else { var meth = ObjUtil.ExtractDelegate <Action <Entity> >(comp, EntityConstants.MSG_ONADDEDTOENTITY); if (meth != null) { _lst.Add(comp); meth(_owner); } else { throw new EntityComponentMalformedException("Custom rolled IEntityComponents must contain a 'OnAddedToEntity' method present as a member."); } } _owner.OnComponentAdd(comp); return(comp); }
public T AddComponent <T>() where T : class, IEntityComponent { T comp = this.GetComponent <T>(); if (comp != null) { return(comp); } try { comp = System.Activator.CreateInstance <T>(); } catch { return(null); } if (comp is EntityComponent) { _lst.Add(comp); (comp as EntityComponent).OnAddedToEntity(_owner); } else { var meth = ObjUtil.ExtractDelegate <Action <Entity> >(comp, EntityConstants.MSG_ONADDEDTOENTITY); if (meth != null) { _lst.Add(comp); meth(_owner); } else { throw new EntityComponentMalformedException("Custom rolled IEntityComponents must contain a 'OnAddedToEntity' method present as a member."); } } _owner.OnComponentAdd(comp); return(comp); }
/// <summary> /// Call a method with shape of delegate T. Note this method can be slow so don't overuse. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="msg"></param> /// <param name="args"></param> public void SendMessage <T>(string msg, params object[] args) where T : class { Delegate messageReceiver = null; foreach (var comp in this.Components) { var meth = ObjUtil.ExtractDelegate <T>(comp, msg); if (meth != null) { messageReceiver = Delegate.Combine(messageReceiver as Delegate, meth as Delegate); } } if (messageReceiver != null) { try { messageReceiver.DynamicInvoke(args); } catch { } } }