예제 #1
0
        /// <summary>
        /// (c) Phoera
        /// </summary>
        public static T EnsureComponent <T>(this IMyEntity entity, Func <T> factory) where T : MyEntityComponentBase
        {
            T res;

            if (entity.TryGetComponent(out res))
            {
                return(res);
            }
            res = factory();
            if (res is MyGameLogicComponent)
            {
                if (entity.GameLogic?.GetAs <T>() == null)
                {
                    //"Added as game logic".ShowNotification();
                    entity.AddGameLogic(res as MyGameLogicComponent);
                    (res as MyGameLogicComponent).Init((MyObjectBuilder_EntityBase)null);
                }
            }
            else
            {
                //"Added as component".ShowNotification();
                entity.Components.Add(res);
                res.Init(null);
            }
            return(res);
        }