コード例 #1
0
        private T CreateActor <T>(SubclassOf <Actor> ActorClass, ActorScript actorScript) where T : Actor
        {
            Actor actor = null;

            if (ActorClass != null)
            {
                actor = Activator.CreateInstance(ActorClass) as Actor;
                actor.PostConstruct();
                if (actorScript != null)
                {
                    actorScript.ApplyToObject(actor);
                }
                else
                {
                    actor.PostScriptApplied();
                }
            }
            else if (actorScript != null)
            {
                actor = actorScript.CreateInstance <Actor>();
            }
            else
            {
                throw new ArgumentNullException(nameof(ActorClass), "Both Actor Class and ActorScript cannot be null.  Provide one.");
            }

            if (actor.Name == null)
            {
                int CompCount = AllActors.Count(x => x.GetType().Name == actor.GetType().Name);
                actor.Name = string.Format("{0}_{1}", actor.GetType().Name, CompCount);
            }

            _AllActors.Add(actor);


            List <ResourcePtr> ResourceCollector = new List <ResourcePtr>();

            actor.CollectResources(ResourceCollector);

            if (ResourceCollector.Count > 0)
            {
                FileSystem.BulkLoadAssets(ResourceCollector,
                                          () => FinishSpawningActor(actor), null);
            }
            else
            {
                FinishSpawningActor(actor);
            }

            return(actor as T);
        }
コード例 #2
0
ファイル: World.cs プロジェクト: RoyAwesome/Watertight2
        private T CreateActor <T>(SubclassOf <Actor> ActorClass, ActorScript actorScript) where T : Actor
        {
            Actor actor = null;

            if (ActorClass != null)
            {
                actor = Activator.CreateInstance(ActorClass) as Actor;
                actor.PostConstruct();
                if (actorScript != null)
                {
                    actorScript.ApplyToObject(actor);
                }
                else
                {
                    actor.PostScriptApplied();
                }
            }
            else if (actorScript != null)
            {
                actor = actorScript.CreateInstance <Actor>();
            }
            else
            {
                throw new ArgumentNullException(nameof(ActorClass), "Both Actor Class and ActorScript cannot be null.  Provide one.");
            }

            if (actor.Name == null)
            {
                int CompCount = AllActors.Count(x => x.GetType().Name == actor.GetType().Name);
                actor.Name = string.Format("{0}_{1}", actor.GetType().Name, CompCount);
            }

            _AllActors.Add(actor);

            //Init all constructor created actors
            foreach (var Property in actor.GetType().GetProperties())
            {
                if (typeof(ActorComponent).IsAssignableFrom(Property.PropertyType))
                {
                    ActorComponent comp = Property.GetValue(actor) as ActorComponent;

                    if (comp == null)
                    {
                        if (Property.SetMethod != null && Property.GetCustomAttribute <DontConstructComponentAttribute>(true) != null)
                        {
                            comp = Activator.CreateInstance(Property.PropertyType) as ActorComponent;
                            Property.SetValue(actor, comp);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (!comp.Registered)
                    {
                        comp.Register();
                    }
                }
            }

            List <ResourcePtr> ResourceCollector = new List <ResourcePtr>();

            actor.CollectResources(ResourceCollector);

            if (ResourceCollector.Count > 0)
            {
                FileSystem.BulkLoadAssets(ResourceCollector,
                                          () => FinishSpawningActor(actor), null);
            }
            else
            {
                FinishSpawningActor(actor);
            }

            return(actor as T);
        }
コード例 #3
0
ファイル: World.cs プロジェクト: RoyAwesome/Watertight2
 public T CreateActor <T>(SubclassOf <Actor> ActorClass) where T : Actor
 {
     return(CreateActor <T>(ActorClass, null));
 }