コード例 #1
0
ファイル: ActorPreview.cs プロジェクト: zgrsgr/OpenRA-SC
        public Func <WRot> GetOrientation()
        {
            var facingInfo = Actor.TraitInfoOrDefault <IFacingInfo>();

            if (facingInfo == null)
            {
                return(() => WRot.Zero);
            }

            // Dynamic facing takes priority
            var dynamicInit = dict.GetOrDefault <DynamicFacingInit>();

            if (dynamicInit != null)
            {
                // TODO: Account for terrain slope
                var getFacing = dynamicInit.Value(null);
                return(() => WRot.FromFacing(getFacing()));
            }

            // Fall back to initial actor facing if an Init isn't available
            var facingInit = dict.GetOrDefault <FacingInit>();
            var facing     = facingInit != null?facingInit.Value(null) : facingInfo.GetInitialFacing();

            var orientation = WRot.FromFacing(facing);

            return(() => orientation);
        }
コード例 #2
0
        public DamageState GetDamageState()
        {
            var health = dict.GetOrDefault <HealthInit>();

            if (health == null)
            {
                return(DamageState.Undamaged);
            }

            var hf = health.Value(null);

            if (hf <= 0)
            {
                return(DamageState.Dead);
            }
            if (hf < 0.25f)
            {
                return(DamageState.Critical);
            }
            if (hf < 0.5f)
            {
                return(DamageState.Heavy);
            }

            if (hf < 0.75f)
            {
                return(DamageState.Medium);
            }

            if (hf < 1.0f)
            {
                return(DamageState.Light);
            }
            return(DamageState.Undamaged);
        }
コード例 #3
0
ファイル: Turreted.cs プロジェクト: RAunplugged/OpenRA
        void IActorPreviewInitModifier.ModifyActorPreviewInit(Actor self, TypeDictionary inits)
        {
            var facings = inits.GetOrDefault <DynamicTurretFacingsInit>();

            if (facings == null)
            {
                facings = new DynamicTurretFacingsInit();
                inits.Add(facings);
            }

            Func <int> bodyFacing    = () => facing.Facing;
            var        dynamicFacing = inits.GetOrDefault <DynamicFacingInit>();
            var        staticFacing  = inits.GetOrDefault <FacingInit>();

            if (dynamicFacing != null)
            {
                bodyFacing = dynamicFacing.Value(self.World);
            }
            else if (staticFacing != null)
            {
                bodyFacing = () => staticFacing.Value(self.World);
            }

            // Freeze the relative turret facing to its current value
            var facingOffset = TurretFacing - bodyFacing();

            facings.Value(self.World).Add(Name, () => bodyFacing() + facingOffset);
        }
コード例 #4
0
        public void GetOrDefault()
        {
            var td = new TypeDictionary <IServiceCollection, IServiceProvider>
            {
                [typeof(ServiceCollection)] = typeof(ServiceProvider)
            };

            td.GetOrDefault <ServiceCollection>().ShouldNotBeNull();
            td.GetOrDefault <IServiceCollection>().ShouldBeNull();
        }
コード例 #5
0
ファイル: ActorGlobal.cs プロジェクト: dnqbob/OpenRA
        public Actor Create(string type, bool addToWorld, LuaTable initTable)
        {
            var initDict = new TypeDictionary();

            // Convert table entries into ActorInits
            foreach (var kv in initTable)
            {
                using (kv.Key)
                    using (kv.Value)
                        initDict.Add(CreateInit(kv.Key.ToString(), kv.Value));
            }

            var owner = initDict.GetOrDefault <OwnerInit>();

            if (owner == null)
            {
                throw new LuaException("Tried to create actor '{0}' with an invalid or no owner init!".F(type));
            }

            // The actor must be added to the world at the end of the tick
            var a = Context.World.CreateActor(false, type, initDict);

            if (addToWorld)
            {
                Context.World.AddFrameEndTask(w => w.Add(a));
            }

            return(a);
        }
コード例 #6
0
        void IActorPreviewInitModifier.ModifyActorPreviewInit(Actor self, TypeDictionary inits)
        {
            Func <WAngle> bodyFacing    = () => facing.Facing;
            var           dynamicFacing = inits.GetOrDefault <DynamicFacingInit>();
            var           staticFacing  = inits.GetOrDefault <FacingInit>();

            if (dynamicFacing != null)
            {
                bodyFacing = dynamicFacing.Value;
            }
            else if (staticFacing != null)
            {
                bodyFacing = () => staticFacing.Value;
            }

            // Freeze the relative turret facing to its current value
            var facingOffset = WAngle.FromFacing(TurretFacing) - bodyFacing();

            inits.Add(new DynamicTurretFacingInit(Info, () => bodyFacing() + facingOffset));
        }
コード例 #7
0
        public Actor Create(string type, bool addToWorld, LuaTable initTable)
        {
            var initDict = new TypeDictionary();

            // Convert table entries into ActorInits
            foreach (var kv in initTable)
            {
                using (kv.Key)
                    using (kv.Value)
                    {
                        // Find the requested type
                        var typeName = kv.Key.ToString();
                        var initType = Game.ModData.ObjectCreator.FindType(typeName + "Init");
                        if (initType == null)
                        {
                            throw new LuaException("Unknown initializer type '{0}'".F(typeName));
                        }

                        // Cast it up to an IActorInit<T>
                        var genericType = initType.GetInterfaces()
                                          .First(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IActorInit <>));
                        var innerType = genericType.GetGenericArguments().First();
                        var valueType = innerType.IsEnum ? Enum.GetUnderlyingType(innerType) : innerType;

                        // Try and coerce the table value to the required type
                        object value;
                        if (!kv.Value.TryGetClrValue(valueType, out value))
                        {
                            throw new LuaException("Invalid data type for '{0}' (expected '{1}')".F(typeName, valueType.Name));
                        }

                        // Construct the ActorInit. Phew!
                        var test = initType.GetConstructor(new[] { innerType }).Invoke(new[] { value });
                        initDict.Add(test);
                    }
            }

            var owner = initDict.GetOrDefault <OwnerInit>();

            if (owner == null)
            {
                throw new LuaException("Tried to create actor '{0}' with an invalid or no owner init!".F(type));
            }

            // The actor must be added to the world at the end of the tick
            var a = Context.World.CreateActor(false, type, initDict);

            if (addToWorld)
            {
                Context.World.AddFrameEndTask(w => w.Add(a));
            }

            return(a);
        }
コード例 #8
0
ファイル: Turreted.cs プロジェクト: huwpascoe/OpenRA
        public void ModifyDeathActorInit(Actor self, TypeDictionary init)
        {
            var facings = init.GetOrDefault <TurretFacingsInit>();

            if (facings == null)
            {
                facings = new TurretFacingsInit();
                init.Add(facings);
            }

            facings.Value(self.World).Add(Name, TurretFacing);
        }
コード例 #9
0
        public T Get <T>() where T : IGlobalModData
        {
            var module = modules.GetOrDefault <T>();

            // Lazily create the default values if not explicitly defined.
            if (module == null)
            {
                module = (T)Game.ModData.ObjectCreator.CreateBasic(typeof(T));
                modules.Add(module);
            }

            return(module);
        }
コード例 #10
0
        public void ModifyDeathActorInit(Actor self, TypeDictionary init)
        {
            var facings = init.GetOrDefault <TurretFacingsInit>();

            if (facings == null)
            {
                facings = new TurretFacingsInit(new Dictionary <string, int>());
                init.Add(facings);
            }

            if (!facings.Value.ContainsKey(Name))
            {
                facings.Value.Add(Name, TurretFacing);
            }
        }
コード例 #11
0
ファイル: Manifest.cs プロジェクト: hadow/Commander
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Get <T>() where T : IGlobalModData
        {
            if (!customDataLoaded)
            {
                throw new InvalidOperationException("Attempted to call Manifest.Get() before loading custom data");
            }

            var module = modules.GetOrDefault <T>();

            if (module == null)
            {
                module = (T)WarGame.ModData.ObjectCreator.CreateBasic(typeof(T));
                modules.Add(module);
            }
            return(module);
        }
コード例 #12
0
ファイル: Manifest.cs プロジェクト: shakhmetov/OpenRA
        /// <summary>Load a cached IGlobalModData instance.</summary>
        public T Get <T>() where T : IGlobalModData
        {
            if (!customDataLoaded)
            {
                throw new InvalidOperationException("Attempted to call Manifest.Get() before loading custom data!");
            }

            var module = modules.GetOrDefault <T>();

            // Lazily create the default values if not explicitly defined.
            if (module == null)
            {
                module = (T)Game.ModData.ObjectCreator.CreateBasic(typeof(T));
                modules.Add(module);
            }

            return(module);
        }
コード例 #13
0
        public void UnitProducedByOther(Actor self, Actor producer, Actor produced, string productionType, TypeDictionary init)
        {
            if (IsTraitDisabled)
            {
                return;
            }

            // No recursive cloning!
            if (producer.Owner != self.Owner || productionType == Info.ProductionType)
            {
                return;
            }

            var ci = produced.Info.TraitInfoOrDefault <CloneableInfo>();

            if (ci == null || !Info.CloneableTypes.Overlaps(ci.Types))
            {
                return;
            }

            var factionInit = init.GetOrDefault <FactionInit>();

            // Stop as soon as one production trait successfully produced
            foreach (var p in productionTraits)
            {
                if (!string.IsNullOrEmpty(Info.ProductionType) && !p.Info.Produces.Contains(Info.ProductionType))
                {
                    continue;
                }

                var inits = new TypeDictionary
                {
                    new OwnerInit(self.Owner),
                    factionInit ?? new FactionInit(BuildableInfo.GetInitialFaction(produced.Info, p.Faction))
                };

                if (p.Produce(self, produced.Info, Info.ProductionType, inits, 0))
                {
                    return;
                }
            }
        }
コード例 #14
0
 public T GetOrDefault <T>() where T : ActorInit, ISingleInstanceInit
 {
     return(Dict.GetOrDefault <T>());
 }
コード例 #15
0
 public T TraitInfoOrDefault <T>() where T : ITraitInfoInterface
 {
     return(traits.GetOrDefault <T>());
 }