Refactor RandomDelay:

Allow different types of random
and reflect other use cases by renaming.
This commit is contained in:
Matthias Mailänder
2021-09-20 13:39:22 +02:00
committed by abcdefg30
parent 3a7aeb5324
commit a2a668077c
6 changed files with 10 additions and 10 deletions

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Mods.Cnc.Traits
public TransformsNearResources(Actor self, TransformsNearResourcesInfo info) public TransformsNearResources(Actor self, TransformsNearResourcesInfo info)
{ {
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>(); resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
delay = Common.Util.RandomDelay(self.World, info.Delay); delay = Common.Util.RandomInRange(self.World.SharedRandom, info.Delay);
this.info = info; this.info = info;
} }

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Traits
public KillsSelf(Actor self, KillsSelfInfo info) public KillsSelf(Actor self, KillsSelfInfo info)
: base(info) : base(info)
{ {
lifetime = Util.RandomDelay(self.World, info.Delay); lifetime = Util.RandomInRange(self.World.SharedRandom, info.Delay);
} }
protected override void TraitEnabled(Actor self) protected override void TraitEnabled(Actor self)

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Traits.Render
: base(info) : base(info)
{ {
wsb = self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == Info.Body); wsb = self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == Info.Body);
ticks = Util.RandomDelay(self.World, info.Interval); ticks = Util.RandomInRange(self.World.SharedRandom, info.Interval);
} }
void ITick.Tick(Actor self) void ITick.Tick(Actor self)
@@ -50,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits.Render
if (--ticks <= 0) if (--ticks <= 0)
{ {
wsb.PlayCustomAnimation(self, Info.Sequences.Random(Game.CosmeticRandom)); wsb.PlayCustomAnimation(self, Info.Sequences.Random(Game.CosmeticRandom));
ticks = Util.RandomDelay(self.World, Info.Interval); ticks = Util.RandomInRange(self.World.SharedRandom, Info.Interval);
} }
} }

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Traits.Sound
public AmbientSound(Actor self, AmbientSoundInfo info) public AmbientSound(Actor self, AmbientSoundInfo info)
: base(info) : base(info)
{ {
delay = Util.RandomDelay(self.World, info.Delay); delay = Util.RandomInRange(self.World.SharedRandom, info.Delay);
loop = Info.Interval.Length == 0 || (Info.Interval.Length == 1 && Info.Interval[0] == 0); loop = Info.Interval.Length == 0 || (Info.Interval.Length == 1 && Info.Interval[0] == 0);
} }
@@ -71,7 +71,7 @@ namespace OpenRA.Mods.Common.Traits.Sound
{ {
StartSound(self); StartSound(self);
if (!loop) if (!loop)
delay = Util.RandomDelay(self.World, Info.Interval); delay = Util.RandomInRange(self.World.SharedRandom, Info.Interval);
} }
} }
@@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits.Sound
currentSounds.Clear(); currentSounds.Clear();
} }
protected override void TraitEnabled(Actor self) { delay = Util.RandomDelay(self.World, Info.Delay); } protected override void TraitEnabled(Actor self) { delay = Util.RandomInRange(self.World.SharedRandom, Info.Delay); }
protected override void TraitDisabled(Actor self) { StopSound(); } protected override void TraitDisabled(Actor self) { StopSound(); }
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self) { StopSound(); } void INotifyRemovedFromWorld.RemovedFromWorld(Actor self) { StopSound(); }

View File

@@ -93,7 +93,7 @@ namespace OpenRA.Mods.Common.Traits
if (spawnPoint == null) if (spawnPoint == null)
return; return;
spawnCountdown = Util.RandomDelay(self.World, info.SpawnInterval); spawnCountdown = Util.RandomInRange(self.World.SharedRandom, info.SpawnInterval);
do do
{ {

View File

@@ -198,7 +198,7 @@ namespace OpenRA.Mods.Common
} }
} }
public static int RandomDelay(World world, int[] range) public static int RandomInRange(MersenneTwister random, int[] range)
{ {
if (range.Length == 0) if (range.Length == 0)
return 0; return 0;
@@ -206,7 +206,7 @@ namespace OpenRA.Mods.Common
if (range.Length == 1) if (range.Length == 1)
return range[0]; return range[0];
return world.SharedRandom.Next(range[0], range[1]); return random.Next(range[0], range[1]);
} }
public static string FriendlyTypeName(Type t) public static string FriendlyTypeName(Type t)