real crate spawning
This commit is contained in:
@@ -11,17 +11,49 @@ namespace OpenRa.Traits
|
||||
public readonly int CrateMaximum = 255; // Maximum number of crates
|
||||
public readonly int CrateRadius = 3; // Radius of crate effect TODO: This belongs on the crate effect itself
|
||||
public readonly int CrateRegen = 180; // Average time (seconds) between crate spawn
|
||||
public readonly float WaterCrateChance = 0.2f; // Change of generating a water crate instead of a land crate
|
||||
|
||||
public object Create(Actor self) { return new CrateSpawner(self); }
|
||||
public readonly float WaterCrateChance = .2f; // Chance of generating a water crate instead of a land crate
|
||||
|
||||
public object Create(Actor self) { return new CrateSpawner(); }
|
||||
}
|
||||
|
||||
class CrateSpawner
|
||||
// assumption: there is always at least one free water cell, and one free land cell.
|
||||
|
||||
class CrateSpawner : ITick
|
||||
{
|
||||
Actor self;
|
||||
public CrateSpawner(Actor self)
|
||||
List<Actor> crates = new List<Actor>();
|
||||
int ticks = 0;
|
||||
|
||||
public void Tick(Actor self)
|
||||
{
|
||||
this.self = self;
|
||||
if (--ticks <= 0)
|
||||
{
|
||||
var info = self.Info.Traits.Get<CrateSpawnerInfo>();
|
||||
ticks = info.CrateRegen * 25; // todo: randomize
|
||||
|
||||
crates.RemoveAll(c => !c.IsInWorld);
|
||||
|
||||
while (crates.Count < info.CrateMinimum)
|
||||
SpawnCrate(self, info);
|
||||
if (crates.Count < info.CrateMaximum)
|
||||
SpawnCrate(self, info);
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnCrate(Actor self, CrateSpawnerInfo info)
|
||||
{
|
||||
var inWater = Game.SharedRandom.NextDouble() < info.WaterCrateChance;
|
||||
var umt = inWater ? UnitMovementType.Float : UnitMovementType.Wheel;
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
var p = new int2(Game.SharedRandom.Next(128), Game.SharedRandom.Next(128));
|
||||
if (self.World.IsCellBuildable(p, umt))
|
||||
{
|
||||
self.World.AddFrameEndTask(
|
||||
w => crates.Add(w.CreateActor("crate", p, self.Owner)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user