/// <summary>
        /// Spawn a new instance of a managed entity
        /// </summary>
        /// <param name="name">Name.</param>
        public static T Spawn <T>(Vector3 pos, Quaternion rot, float scale = 1f) where T : Entity, new()
        {
            EntityClass cls = ClassRegistry.GetEntityClass <T>();

            if (cls == null)
            {
                Log.Warning("[EntityFramework] Could not find managed CESharp entity class for {0}", typeof(T).Name);
                return(null);
            }

            SEntitySpawnParams sParams = new SEntitySpawnParams();

            sParams.pClass = cls.NativeClass;

            sParams.vPosition = pos;
            sParams.qRotation = rot;
            sParams.vScale    = new Vec3(scale);
            IEntity pEnt = Global.gEnv.pEntitySystem.SpawnEntity(sParams);

            Entity spawnedEntity = null;

            if (s_managedEntities.TryGetValue(pEnt.GetId(), out spawnedEntity))
            {
                return(spawnedEntity as T);
            }

            return(null);
        }
        /// <summary>
        /// Spawn a new instance of a managed entity
        /// </summary>
        /// <param name="name">Name.</param>
        public static T SpawnWithComponent <T>(Vector3 pos, Quaternion rot, float scale = 1f) where T : EntityComponent, new()
        {
            SEntitySpawnParams spawnParams = new SEntitySpawnParams();

            string className;

            if (EntityComponent._componentClassMap.TryGetValue(typeof(T), out className))
            {
                spawnParams.pClass = Global.gEnv.pEntitySystem.GetClassRegistry().FindClass(className);
            }

            if (spawnParams.pClass == null)
            {
                spawnParams.pClass = Global.gEnv.pEntitySystem.GetClassRegistry().GetDefaultClass();
            }

            spawnParams.vPosition = pos;
            spawnParams.qRotation = rot;
            spawnParams.vScale    = new Vec3(scale);

            IEntity nativeEntity = Global.gEnv.pEntitySystem.SpawnEntity(spawnParams);
            var     entity       = new Entity(nativeEntity, nativeEntity.GetId());

            if (entity != null)
            {
                return(entity.GetOrCreateComponent <T>());
            }

            return(null);
        }
 /// <summary>
 /// Translate 'OnSpawn' callback to managed 'Spawn' event.
 /// </summary>
 public override void OnSpawn(IEntity pEntity, SEntitySpawnParams arg1)
 {
     if (Spawn != null)
     {
         Spawn(pEntity, arg1);
     }
 }
 /// <summary>
 /// Translate 'OnReused' callback to managed 'Reused' event.
 /// </summary>
 public override void OnReused(IEntity pEntity, SEntitySpawnParams arg1)
 {
     if (Reused != null)
     {
         Reused(pEntity, arg1);
     }
 }
        /// <summary>
        /// Translate 'OnBeforeSpawn' callback to managed 'BeforeSpawn' event.
        /// </summary>
        public override bool OnBeforeSpawn(SEntitySpawnParams arg0)
        {
            if (BeforeSpawn != null)
            {
                BeforeSpawn(arg0);
            }

            return(true);
        }
        /// <summary>
        /// Spawn a new instance of the given CESharp entity.
        /// </summary>
        /// <param name="name">Name.</param>
        public static T Spawn <T> (string name = null) where T : BaseEntity
        {
            EntityClass cls = s_entityClassRegistry.GetEntityClass <T> ();

            if (cls == null)
            {
                Log.Warning("[EntityFramework] Could not find managed CESharp entity class for {0}", typeof(T).Name);
                return(null);
            }

            if (typeof(T).IsAbstract)
            {
                Log.Warning("[EntityFramework] Cannot instantiate {0} as it is an abstract class.", typeof(T).Name);
                return(null);
            }

            int i = 1;

            while (String.IsNullOrEmpty(name))
            {
                string test = cls.Description.sName + i.ToString();
                if (Global.gEnv.pEntitySystem.FindEntityByName(test) == null)
                {
                    name = test;
                }
                i++;
            }

            SEntitySpawnParams sParams = new SEntitySpawnParams();

            sParams.sName  = name;
            sParams.pClass = cls.NativeClass;
            IEntity pEnt = Global.gEnv.pEntitySystem.SpawnEntity(sParams);

            if (s_managedEntities.ContainsKey(pEnt.GetId()))
            {
                if (s_managedEntities [pEnt.GetId()].GetType() == typeof(T))
                {
                    return(s_managedEntities [pEnt.GetId()] as T);
                }
                else
                {
                    Log.Error("[EntityFramework] Entity {0} already registered, but with a different type ({1} instead of {2})", pEnt.GetId(), pEnt.GetType().FullName, typeof(T).FullName);
                    return(null);
                }
            }
            else
            {
                Log.Info("[EntityFramework] Spawn entity: {0} ({1})", name, cls.ProtoType.Name);
                T instance = (T)cls.CreateInstance(pEnt);
                cls.NativeClass.LoadScript(true);
                s_managedEntities.Add(pEnt.GetId(), instance);
                return(instance);
            }
        }
        /// <summary>
        /// Called when CRYENGINE's entity system is spawning an entity. Will check if the entity class is registered with the managed entity class registry and if yes, it will create a managed CESharp entity instance.
        /// </summary>
        private static void OnSpawn(IEntity pEntity, SEntitySpawnParams spawnParams)
        {
            if (spawnParams == null)
            {
                return;
            }

            EntityClass managedClass = ClassRegistry.GetEntityClass(spawnParams.pClass);

            if (managedClass == null)
            {
                return;
            }

            managedClass.CreateManagedInstance(pEntity);
        }
        /// <summary>
        /// Retrieve the managed entity class for a set of entity spawn parameters.
        /// </summary>
        /// <returns>'Null' if the intended entity class is not a managed entity class - otherwise the managed entity class.</returns>
        /// <param name="spawnParams">Entity spawn paramters.</param>
        public EntityClass GetEntityClass(SEntitySpawnParams spawnParams)
        {
            if (spawnParams == null || spawnParams.pClass == null)
            {
                return(null);
            }

            string className = spawnParams.pClass.GetName();

            if (!_registry.ContainsKey(className))
            {
                return(null);
            }

            return(_registry [className]);
        }
        /// <summary>
        /// Called when CRYENGINE's entity system is spawning an entity. Will check if the entity class is registered with the managed entity class registry and if yes, it will create a managed CESharp entity instance.
        /// </summary>
        private static void OnSpawn(IEntity pEntity, SEntitySpawnParams arg1)
        {
            if (s_managedEntities.ContainsKey(pEntity.GetId()))
            {
                return;
            }

            EntityClass managedClass = s_entityClassRegistry.GetEntityClass(arg1);

            if (managedClass == null)
            {
                return;
            }

            Log.Info("[EntityFramework] Spawn entity: {0} ({1})", arg1.sName, managedClass.ProtoType.Name);
            s_managedEntities.Add(pEntity.GetId(), managedClass.CreateInstance(pEntity, arg1.HasEntityNode() ? arg1.entityNode : null));
        }
        /// <summary>
        /// Spawns an empty without any components
        /// </summary>
        /// <param name="name">Name of the entity</param>
        /// <param name="position">Initial position of the entity</param>
        /// <param name="rotation">Initial rotation of the entity</param>
        /// <param name="scale">Initial scale of the entity</param>
        /// <returns></returns>
        public static Entity Spawn(string name, Vector3 position, Quaternion rotation, Vector3 scale)
        {
            var spawnParams = new SEntitySpawnParams();

            spawnParams.sName  = name;
            spawnParams.pClass = Global.gEnv.pEntitySystem.GetClassRegistry().GetDefaultClass();

            spawnParams.vPosition = position;
            spawnParams.qRotation = rotation;
            spawnParams.vScale    = scale;

            var nativeEntity = Global.gEnv.pEntitySystem.SpawnEntity(spawnParams);

            if (nativeEntity != null)
            {
                return(new Entity(nativeEntity, nativeEntity.GetId()));
            }

            return(null);
        }
Exemple #11
0
        /// <summary>
        /// Instantiates an Entity wrapper class, along with a CRYENGINE IEntity object. The IEntity is already setup by various properties as well as physicallized.
        /// Registers the created Entity class as a unique wrapper for the created IEntity object.
        /// </summary>
        /// <param name="pos">Position.</param>
        /// <param name="rot">Rotation.</param>
        /// <param name="scale">Scale.</param>
        /// <param name="model">Model.</param>
        /// <param name="material">Material.</param>
        /// <typeparam name="T">Optionally the Type of a class inheriting by Entity.</typeparam>
        public static T Instantiate <T>(Vec3 pos, Quat rot, float scale = 1.0f, string model = null, string material = null)
            where T : Entity
        {
            lock (_createLock)
            {
                SEntitySpawnParams spawnParams = new SEntitySpawnParams()
                {
                    pClass    = Env.EntitySystem.GetClassRegistry().GetDefaultClass(),
                    vPosition = pos,
                    vScale    = new Vec3(scale)
                };

                _newEntity = Env.EntitySystem.SpawnEntity(spawnParams, true);
                if (model != null)
                {
                    _newEntity.LoadGeometry(0, model);
                }
                _newEntity.SetRotation(rot);

                if (material != null)
                {
                    IMaterial mat = Env.Engine.GetMaterialManager().LoadMaterial(material);
                    _newEntity.SetMaterial(mat);
                }

                var physics = new SEntityPhysicalizeParams()
                {
                    density = 1,
                    mass    = 0f,
                    type    = (int)EPhysicalizationType.ePT_Rigid,
                };
                _newEntity.Physicalize(physics);

                return(Activator.CreateInstance <T>());
            }
        }
 /// <summary>
 /// Not implemented!
 /// </summary>
 public override void InitArchetypeEntity(IEntity entity, string archetypeName, SEntitySpawnParams spawnParams)
 {
     // TODO: Implement?
 }