コード例 #1
0
 public BaseEntity CreateInstance(EntityInfo info)
 {
     return(CreateInstance(info, EntityDictionary.Allocate()));
 }
コード例 #2
0
ファイル: Entities.cs プロジェクト: UAVXP/SharpLife-Game
        private void LoadEntity(List <KeyValuePair <string, string> > block, int index)
        {
            var className = GetClassName(block, index);

            var info = EntityRegistry.FindEntityByMapName(className);

            if (info == null)
            {
                throw new ArgumentException($"No entity class of name {className} exists");
            }

            BaseEntity entity = null;

            //The world always has edict 0, but alloc calls can never get that edict directly.
            if (index == 0)
            {
                entity = EntityRegistry.CreateInstance(info, EntityDictionary.Allocate(0));
            }
            else
            {
                entity = EntityRegistry.CreateInstance(info);
            }

            if (entity == null)
            {
                throw new ArgumentException($"Couldn't create instance of entity {className}");
            }

            //The entity can remove itself in Spawn, so keep the edict here to properly free it in case of problems
            var edict = entity.Edict();

            try
            {
                foreach (var kv in block)
                {
                    var key   = kv.Key;
                    var value = kv.Value;

                    //Don't do this multiple times
                    if (key != "classname")
                    {
                        //The engine does not allow values with the same content as the classname to be passed
                        //No reason to impose this restriction here

                        CheckKeyValue(entity, ref key, ref value);

                        if (!KeyValueUtils.TrySetKeyValue(entity, info, key, value))
                        {
                            entity.KeyValue(key, value);
                        }
                    }
                }

                Log.Message($"Spawning entity {entity.ClassName} ({entity.GetType().FullName})");
                Spawn(entity.Edict());

                //TODO: can check if the entity is a template and do stuff here
            }
            catch (Exception)
            {
                //On failure always free the edict
                //This will also free the entity instance if it has been assigned
                EntityDictionary.Free(edict);

                throw;
            }
        }