Выполнить существующий код, через заданный интервал - C#
Формулировка задачи:
Здравствуйте.
Дано:
Есть в наличии 2 файла, первый выполняется единыжды, второй выполняется по интервалу.
1)
2)
Задача:
скопировать из второго файла код, отвечающий за интервал и вставить в первый,
так, чтобы если значение интервала явно не указано, то код должен выполняться один раз,
а если указано, то через промежуток указанный в интервале.
Помогите пожалуйста правильно это сделать, не повредив структуру языка.
using OpenRA.Primitives; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Player receives a unit for free once the building is placed. This also works for structures.", "If you want more than one unit to appear copy this section and assign IDs like FreeActor@2, ...")] public class FreeActorInfo : ITraitInfo { [ActorReference, FieldLoader.Require] [Desc("Name of the actor.")] public readonly string Actor = null; [Desc("Offset relative to the top-left cell of the building.")] public readonly CVec SpawnOffset = CVec.Zero; [Desc("Which direction the unit should face.")] public readonly int Facing = 0; public virtual object Create(ActorInitializer init) { return new FreeActor(init, this); } } public class FreeActor { public FreeActor(ActorInitializer init, FreeActorInfo info) { if (init.Contains<FreeActorInit>() && !init.Get<FreeActorInit>().ActorValue) return; init.Self.World.AddFrameEndTask(w => { w.CreateActor(info.Actor, new TypeDictionary { new ParentActorInit(init.Self), new LocationInit(init.Self.Location + info.SpawnOffset), new OwnerInit(init.Self.Owner), new FacingInit(info.Facing), }); }); } } public class FreeActorInit : IActorInit<bool> { [FieldFromYamlKey] public readonly bool ActorValue = true; public FreeActorInit() { } public FreeActorInit(bool init) { ActorValue = init; } public bool Value(World world) { return ActorValue; } } public class ParentActorInit : IActorInit<Actor> { public readonly Actor ActorValue; public ParentActorInit(Actor parent) { ActorValue = parent; } public Actor Value(World world) { return ActorValue; } } }
using System; using System.Linq; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Lets the actor spread resources around it in a circle.")] class SeedsResourceInfo : UpgradableTraitInfo { public readonly int Interval = 75; public readonly string ResourceType = "Ore"; public readonly int MaxRange = 100; public override object Create(ActorInitializer init) { return new SeedsResource(init.Self, this); } } class SeedsResource : UpgradableTrait<SeedsResourceInfo>, ITick, ISeedableResource { readonly SeedsResourceInfo info; readonly ResourceType resourceType; readonly ResourceLayer resLayer; public SeedsResource(Actor self, SeedsResourceInfo info) : base(info) { this.info = info; resourceType = self.World.WorldActor.TraitsImplementing<ResourceType>() .FirstOrDefault(t => t.Info.Name == info.ResourceType); if (resourceType == null) throw new InvalidOperationException("No such resource type `{0}`".F(info.ResourceType)); resLayer = self.World.WorldActor.Trait<ResourceLayer>(); } int ticks; public void Tick(Actor self) { if (IsTraitDisabled) return; if (--ticks <= 0) { Seed(self); ticks = info.Interval; } } public void Seed(Actor self) { var cell = Util.RandomWalk(self.Location, self.World.SharedRandom) .Take(info.MaxRange) .SkipWhile(p => !self.World.Map.Contains(p) || (resLayer.GetResource(p) == resourceType && resLayer.IsFull(p))) .Cast<CPos?>().FirstOrDefault(); if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value)) resLayer.AddResource(resourceType, cell.Value, 1); } } }
Решение задачи: «Выполнить существующий код, через заданный интервал»
textual
Листинг программы
* * * * * * * * * * * using OpenRA.Primitives; using OpenRA.Traits; using System; using System.Timers; namespace OpenRA.Mods.Common.Traits { [Desc("Player receives a unit for free once the building is placed. This also works for structures.", "If you want more than one unit to appear copy this section and assign IDs like FreeActor@2, ...")] public class FreeActorInfo : ITraitInfo { [ActorReference, FieldLoader.Require] [Desc("Name of the actor.")] public readonly string Actor = null; public readonly int Interval = 0; [Desc("Offset relative to the top-left cell of the building.")] public readonly CVec SpawnOffset = CVec.Zero; [Desc("Which direction the unit should face.")] public readonly int Facing = 0; public virtual object Create(ActorInitializer init) { return new FreeActor(init, this); } } public class FreeActor { private static System.Timers.Timer aTimer; public static void Main() { SetTimer(); aTimer.Stop(); aTimer.Dispose(); } private static void SetTimer() { // Create a timer with a 5 second interval. aTimer = new System.Timers.Timer(5); // Hook up the Elapsed event for the timer. aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true; } private static void OnTimedEvent(Object source, ElapsedEventArgs e) { public FreeActor(ActorInitializer init, FreeActorInfo info) { if (init.Contains<FreeActorInit>() && !init.Get<FreeActorInit>().ActorValue) return; init.Self.World.AddFrameEndTask(w => { w.CreateActor(info.Actor, new TypeDictionary { new ParentActorInit(init.Self), new LocationInit(init.Self.Location + info.SpawnOffset), new OwnerInit(init.Self.Owner), new FacingInit(info.Facing), }); }); } } } public class FreeActorInit : IActorInit<bool> { [FieldFromYamlKey] public readonly bool ActorValue = true; public FreeActorInit() { } public FreeActorInit(bool init) { ActorValue = init; } public bool Value(World world) { return ActorValue; } } public class ParentActorInit : IActorInit<Actor> { public readonly Actor ActorValue; public ParentActorInit(Actor parent) { ActorValue = parent; } public Actor Value(World world) { return ActorValue; } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д