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); }
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); }
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); }
public void GetOrDefault() { var td = new TypeDictionary <IServiceCollection, IServiceProvider> { [typeof(ServiceCollection)] = typeof(ServiceProvider) }; td.GetOrDefault <ServiceCollection>().ShouldNotBeNull(); td.GetOrDefault <IServiceCollection>().ShouldBeNull(); }
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); }
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)); }
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); }
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); }
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); }
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); } }
/// <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); }
/// <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); }
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; } } }
public T GetOrDefault <T>() where T : ActorInit, ISingleInstanceInit { return(Dict.GetOrDefault <T>()); }
public T TraitInfoOrDefault <T>() where T : ITraitInfoInterface { return(traits.GetOrDefault <T>()); }