//////////////// internal CustomEntity Convert() { if (!this.IsInitialized) { throw new HamstarException("Not initialized."); } CustomEntity ent; Type entType = CustomEntityManager.GetTypeByName(this.MyTypeName); if (entType == null) { throw new HamstarException(this.MyTypeName + " does not exist."); } if (!entType.IsSubclassOf(typeof(CustomEntity))) { throw new HamstarException(entType.Name + " is not a valid CustomEntity."); } if (string.IsNullOrEmpty(this.OwnerPlayerUID)) { ent = CustomEntity.CreateRaw(entType, this.Core, this.Components); } else { ent = CustomEntity.CreateRaw(entType, this.Core, this.Components, this.OwnerPlayerUID); } ent.InternalOnClone(); return(ent); }
internal SerializableCustomEntity(CustomEntity ent) : base(null) { this.MyTypeName = SerializableCustomEntity.GetTypeName(ent); this.Core = ent.Core; this.Components = ent.InternalComponents; this.OwnerPlayerUID = ent.MyOwnerPlayerUID; this.OwnerPlayerWho = ent.MyOwnerPlayerWho; }
//////////////// public static CustomEntity GetEntityByWho(int whoAmI) { CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr; CustomEntity ent = null; mngr.WorldEntitiesByIndexes.TryGetValue(whoAmI, out ent); return(ent); }
//////////////// public static void RemoveEntity(CustomEntity ent) { if (ent == null) { throw new HamstarException("Null ent not allowed."); } CustomEntityManager.RemoveEntityByWho(ent.Core.whoAmI); }
public static bool IsInWorld(CustomEntity myent) { CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr; CustomEntity ent = null; mngr.WorldEntitiesByIndexes.TryGetValue(myent.Core.WhoAmI, out ent); return(ent == myent); }
public static void AddToWorld(CustomEntity ent) { CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr; int who = mngr.WorldEntitiesByIndexes.Count + 1; if (!ent.SyncFromClient && !ent.SyncFromServer) { who = -who; } CustomEntityManager.AddToWorld(who, ent); }
//////////////// internal void CopyChangesFrom(CustomEntity ent) // TODO: Copy changes only! { if (!ent.IsInitialized) { //throw new HamstarException( "!ModHelpers.CustomEntity.CopyChangesFrom(CustomEntity) - Parameter not initialized." ); throw new HamstarException("Parameter not initialized."); } this.CopyChangesFrom(ent.Core, ent.Components, ent.OwnerPlayer); if (ModHelpersMod.Instance.Config.DebugModeCustomEntityInfo) { LogHelpers.Alert("Synced from " + ent.ToString() + " for " + this.ToString()); } }
public static CustomEntity AddToWorld(int who, CustomEntity ent, bool skipSync = false) { //if( ent == null ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Null ent not allowed." ); } //if( !ent.IsInitialized ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Initialized ents only." ); } if (ent == null) { throw new HamstarException("Null ent not allowed."); } if (!ent.IsInitialized) { throw new HamstarException("Initialized ents only."); } CustomEntityManager mngr = ModHelpersMod.Instance.CustomEntMngr; CustomEntity realEnt = ent; if (mngr.WorldEntitiesByIndexes.ContainsKey(who)) { //throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - " // + "Attempting to add "+ent.ToString()+" to slot "+who+" occupied by "+mngr.WorldEntitiesByIndexes[who].ToString() ); throw new HamstarException("Attempting to add " + ent.ToString() + " to slot " + who + " occupied by " + mngr.WorldEntitiesByIndexes[who].ToString()); } if (ent is SerializableCustomEntity) { realEnt = ((SerializableCustomEntity)ent).Convert(); } Type compType; Type baseType = typeof(CustomEntityComponent); // Map entity to each of its components foreach (CustomEntityComponent component in realEnt.InternalComponents) { compType = component.GetType(); lock (CustomEntityManager.MyLock) { do { if (!mngr.WorldEntitiesByComponentType.ContainsKey(compType)) { mngr.WorldEntitiesByComponentType[compType] = new HashSet <int>(); } mngr.WorldEntitiesByComponentType[compType].Add(who); compType = compType.BaseType; } while(compType != baseType); } } realEnt.Core.whoAmI = who; mngr.WorldEntitiesByIndexes[who] = realEnt; realEnt.InternalOnAddToWorld(); // Sync also if (!skipSync) { if (Main.netMode == 1) { if (ent.SyncFromClient) { Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => { ent.SyncToAll(); return(false); }); } } else if (Main.netMode == 2) { if (ent.SyncFromServer) { Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => { ent.SyncToAll(); return(false); }); } } } if (ModHelpersMod.Instance.Config.DebugModeCustomEntityInfo) { LogHelpers.Alert("Set " + realEnt.ToString()); } return(realEnt); }
internal void InternalOnAddToWorld(CustomEntity ent) { this.OnAddToWorld(ent); }
//// internal void InternalOnEntityInitialize(CustomEntity ent) { this.OnEntityInitialize(ent); }
public static string GetTypeName(CustomEntity ent) { return(ent.GetType().Name); }
public virtual void UpdateServer(CustomEntity ent) { }
public virtual void UpdateClient(CustomEntity ent) { }
public virtual void UpdateSingle(CustomEntity ent) { }
protected virtual void OnAddToWorld(CustomEntity ent) { }
//////////////// protected virtual void OnEntityInitialize(CustomEntity ent) { }