Esempio n. 1
0
        ////////////////

        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);
        }
Esempio n. 2
0
 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;
 }
Esempio n. 3
0
        ////////////////

        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 7
0
        ////////////////

        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);
        }
Esempio n. 11
0
 public static string GetTypeName(CustomEntity ent)
 {
     return(ent.GetType().Name);
 }
Esempio n. 12
0
 public virtual void UpdateServer(CustomEntity ent)
 {
 }
Esempio n. 13
0
 public virtual void UpdateClient(CustomEntity ent)
 {
 }
Esempio n. 14
0
 public virtual void UpdateSingle(CustomEntity ent)
 {
 }
Esempio n. 15
0
 protected virtual void OnAddToWorld(CustomEntity ent)
 {
 }
Esempio n. 16
0
        ////////////////

        protected virtual void OnEntityInitialize(CustomEntity ent)
        {
        }