ActorInitializer, in preparation for next change (bob)

This commit is contained in:
Chris Forbes
2010-06-19 14:28:30 +12:00
parent 572cdc9dbf
commit db465e1fdd
106 changed files with 174 additions and 227 deletions

View File

@@ -36,9 +36,9 @@ namespace OpenRA
public readonly ActorInfo Info;
public readonly World World;
public readonly uint ActorID;
[Sync]
public readonly uint ActorID;
[Sync]
public int2 Location;
[Sync]
public Player Owner;
@@ -49,13 +49,15 @@ namespace OpenRA
public Group Group;
public Actor(World world, string name, int2 location, Player owner)
{
{
World = world;
ActorID = world.NextAID();
Location = location;
CenterLocation = Traits.Util.CenterOfCell(Location);
Owner = owner;
var init = new ActorInitializer( this );
if (name != null)
{
if (!Rules.Info.ContainsKey(name.ToLowerInvariant()))
@@ -65,9 +67,9 @@ namespace OpenRA
Health = this.GetMaxHP();
foreach (var trait in Info.TraitsInConstructOrder())
traits.Add(trait.Create(this));
}
traits.Add(trait.Create(init));
}
Size = Lazy.New(() =>
{
var si = Info.Traits.GetOrDefault<SelectableInfo>();
@@ -254,5 +256,16 @@ namespace OpenRA
var o = obj as Actor;
return ( o != null && o.ActorID == ActorID );
}
}
public class ActorInitializer
{
public readonly Actor self;
public World world { get { return self.World; } }
public ActorInitializer( Actor actor )
{
this.self = actor;
}
}
}

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Traits
public readonly bool MuzzleFlash = false;
public readonly int FireDelay = 0;
public virtual object Create(Actor self) { return new AttackBase(self); }
public virtual object Create(ActorInitializer init) { return new AttackBase(init.self); }
}
public class AttackBase : IIssueOrder, IResolveOrder, ITick

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Traits
public readonly string LongDesc = "";
public readonly string[] Owner = { };
public virtual object Create(Actor self) { return new Valued(); }
public virtual object Create(ActorInitializer init) { return new Valued(); }
}
class BuildableInfo : ValuedInfo
@@ -40,7 +40,7 @@ namespace OpenRA.Traits
public readonly int BuildPaletteOrder = 9999;
public readonly string Hotkey = null;
public override object Create(Actor self) { return new Buildable(); }
public override object Create(ActorInitializer init) { return new Buildable(); }
}
class Valued { } /* halfway to buildable */

View File

@@ -54,7 +54,7 @@ namespace OpenRA.Traits
public readonly string DamagedSound = "kaboom1.aud";
public readonly string DestroyedSound = "kaboom22.aud";
public object Create(Actor self) { return new Building(self); }
public object Create(ActorInitializer init) { return new Building(init.self); }
}
public class Building : INotifyDamage, IResolveOrder, ITick, IRenderModifier

View File

@@ -30,7 +30,8 @@ namespace OpenRA.Traits
public readonly float CloakDelay = 1.2f; // Seconds
public readonly string CloakSound = "subshow1.aud";
public readonly string UncloakSound = "subshow1.aud";
public object Create(Actor self) { return new Cloak(self); }
public object Create(ActorInitializer init) { return new Cloak(init.self); }
}
public class Cloak : IRenderModifier, INotifyAttack, ITick, INotifyDamage

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Traits
public readonly int Ammo = 0;
public readonly int PipCount = 0;
public object Create(Actor self) { return new LimitedAmmo(self); }
public object Create(ActorInitializer init) { return new LimitedAmmo(init.self); }
}
public class LimitedAmmo : INotifyAttack, IPips

View File

@@ -24,13 +24,13 @@ using OpenRA.GameRules;
namespace OpenRA.Traits
{
public class MobileInfo : ITraitInfo
public class MobileInfo : ITraitInfo, ITraitPrerequisite<Unit>
{
public readonly UnitMovementType MovementType = UnitMovementType.Wheel;
public readonly int WaitAverage = 60;
public readonly int WaitSpread = 20;
public object Create(Actor self) { return new Mobile(self); }
public object Create(ActorInitializer init) { return new Mobile(init.self); }
}
public class Mobile : IIssueOrder, IResolveOrder, IOccupySpace, IMovement

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Traits
{
class HiddenUnderFogInfo : ITraitInfo
{
public object Create(Actor self) { return new HiddenUnderFog(self); }
public object Create(ActorInitializer init) { return new HiddenUnderFog(init.self); }
}
class HiddenUnderFog : IRenderModifier

View File

@@ -10,7 +10,8 @@ namespace OpenRA.Traits
public readonly int InitialCash = 10000;
public readonly int InitialOre = 0;
public readonly int AdviceInterval = 250;
public object Create(Actor self) { return new PlayerResources(self); }
public object Create(ActorInitializer init) { return new PlayerResources(init.self); }
}
public class PlayerResources : ITick
@@ -19,8 +20,7 @@ namespace OpenRA.Traits
int AdviceInterval;
public PlayerResources(Actor self)
{
var p = self.Owner;
Owner = p;
Owner = self.Owner;
Cash = self.Info.Traits.Get<PlayerResourcesInfo>().InitialCash;
Ore = self.Info.Traits.Get<PlayerResourcesInfo>().InitialOre;
AdviceInterval = self.Info.Traits.Get<PlayerResourcesInfo>().AdviceInterval;

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Traits
{
public readonly float BuildSpeed = 0.4f;
public readonly int LowPowerSlowdown = 3;
public object Create(Actor self) { return new ProductionQueue(self); }
public object Create(ActorInitializer init) { return new ProductionQueue(init.self); }
}
class ProductionQueue : IResolveOrder, ITick

View File

@@ -24,20 +24,16 @@ using OpenRA.GameRules;
namespace OpenRA.Traits
{
public class ProductionInfo : ITraitInfo
public class ProductionInfo : TraitInfo<Production>
{
public readonly int[] SpawnOffset = null;
public readonly int[] ProductionOffset = null;
public readonly int[] ExitOffset = null;
public readonly string[] Produces = { };
public virtual object Create(Actor self) { return new Production(self); }
}
public class Production : IIssueOrder, IResolveOrder, ITags
{
public Production( Actor self ) { }
public virtual int2? CreationLocation( Actor self, ActorInfo producee )
{
var pos = (1 / 24f * self.CenterLocation).ToInt2();

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Traits
{
public readonly int[] RallyPoint = { 1, 3 };
public object Create(Actor self) { return new RallyPoint(self); }
public object Create(ActorInitializer init) { return new RallyPoint(init.self); }
}
public class RallyPoint : IRender, IIssueOrder, IResolveOrder, ITick

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Traits
public class RenderBuildingInfo : RenderSimpleInfo
{
public readonly bool HasMakeAnimation = true;
public override object Create(Actor self) { return new RenderBuilding(self);}
public override object Create(ActorInitializer init) { return new RenderBuilding(init.self);}
}
public class RenderBuilding : RenderSimple, INotifyDamage, INotifySold

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Traits
{
class RenderBuildingTurretedInfo : RenderBuildingInfo
{
public override object Create(Actor self) { return new RenderBuildingTurreted(self); }
public override object Create(ActorInitializer init) { return new RenderBuildingTurreted(init.self); }
}
class RenderBuildingTurreted : RenderBuilding, INotifyBuildComplete

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Traits
{
public readonly string Image = null;
public readonly string Palette = null;
public abstract object Create(Actor self);
public abstract object Create(ActorInitializer init);
}
public abstract class RenderSimple : IRender, ITick

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Traits
{
public class RenderUnitInfo : RenderSimpleInfo
{
public override object Create(Actor self) { return new RenderUnit(self); }
public override object Create(ActorInitializer init) { return new RenderUnit(init.self); }
}
public class RenderUnit : RenderSimple, INotifyDamage

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Traits
{
class RenderUnitTurretedInfo : RenderUnitInfo
{
public override object Create(Actor self) { return new RenderUnitTurreted(self); }
public override object Create(ActorInitializer init) { return new RenderUnitTurreted(init.self); }
}
class RenderUnitTurreted : RenderUnit

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Traits
public readonly PipType PipColor = PipType.Yellow;
public readonly int Capacity = 0;
public readonly string DeathWeapon = null;
public object Create(Actor self) { return new StoresOre(self, this); }
public object Create(ActorInitializer init) { return new StoresOre(init.self, this); }
}
class StoresOre : IPips, INotifyCapture, INotifyDamage

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Traits
public readonly string SelectTargetSound = null;
public readonly string LaunchSound = null;
public abstract object Create(Actor self);
public abstract object Create(ActorInitializer init);
public SupportPowerInfo() { OrderName = GetType().Name + "Order"; }
}

View File

@@ -108,9 +108,9 @@ namespace OpenRA.Traits
public Renderable WithPos(float2 newPos) { return new Renderable(Sprite, newPos, Palette, ZOffset); }
}
public interface ITraitInfo { object Create(Actor self); }
public interface ITraitInfo { object Create(ActorInitializer init); }
public class TraitInfo<T> : ITraitInfo where T : new() { public object Create(Actor self) { return new T(); } }
public class TraitInfo<T> : ITraitInfo where T : new() { public virtual object Create(ActorInitializer init) { return new T(); } }
public interface ITraitPrerequisite<T> { }

View File

@@ -22,7 +22,7 @@ using OpenRA.Traits.Activities;
namespace OpenRA.Traits
{
class TransformsOnDeployInfo : ITraitInfo
class TransformsOnDeployInfo : TraitInfo<TransformsOnDeploy>
{
public readonly string TransformsInto = null;
public readonly int[] Offset = null;
@@ -30,14 +30,10 @@ namespace OpenRA.Traits
public readonly bool TransferHealthPercentage = true; // Set to false to transfer the absolute health
public readonly string[] TransformSounds = null;
public readonly string[] NoTransformSounds = null;
public object Create(Actor self) { return new TransformsOnDeploy(self); }
}
class TransformsOnDeploy : IIssueOrder, IResolveOrder
{
public TransformsOnDeploy(Actor self) { }
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
if (mi.Button == MouseButton.Right && self == underCursor)

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Traits
public readonly int ROT = 255;
public readonly int InitialFacing = 128;
public object Create(Actor self) { return new Turreted(self); }
public object Create(ActorInitializer init) { return new Turreted(init.self); }
}
public class Turreted : ITick

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Traits
public readonly int ROT = 255;
public readonly int Speed = 1;
public object Create( Actor self ) { return new Unit( self ); }
public object Create( ActorInitializer init ) { return new Unit( init.self ); }
}
public class Unit : INotifyDamage

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Traits
{
public readonly string[] BibTypes = {"bib3", "bib2", "bib1"};
public readonly int[] BibWidths = {2,3,4};
public object Create(Actor self) { return new BibLayer(self, this); }
public object Create(ActorInitializer init) { return new BibLayer(init.self, this); }
}
class BibLayer: IRenderOverlay, ILoadWorldHook

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Traits
{
public class BuildingInfluenceInfo : ITraitInfo
{
public object Create( Actor self ) { return new BuildingInfluence( self ); }
public object Create( ActorInitializer init ) { return new BuildingInfluence( init.world ); }
}
public class BuildingInfluence
@@ -34,17 +34,17 @@ namespace OpenRA.Traits
Actor[,] influence;
Map map;
public BuildingInfluence( Actor self )
public BuildingInfluence( World world )
{
map = self.World.Map;
map = world.Map;
blocked = new bool[map.MapSize.X, map.MapSize.Y];
influence = new Actor[map.MapSize.X, map.MapSize.Y];
self.World.ActorAdded +=
world.ActorAdded +=
a => { if (a.traits.Contains<Building>())
ChangeInfluence(a, a.traits.Get<Building>(), true); };
self.World.ActorRemoved +=
world.ActorRemoved +=
a => { if (a.traits.Contains<Building>())
ChangeInfluence(a, a.traits.Get<Building>(), false); };
}

View File

@@ -20,15 +20,13 @@
namespace OpenRA.Traits
{
public class CountryInfo : ITraitInfo
public class CountryInfo : TraitInfo<Country>
{
public readonly string Name = null;
public readonly string Race = null;
/* todo: icon,... */
public object Create(Actor self) { return new CountryInfo(); }
}
class Country { /* we're only interested in the Info */ }
public class Country { /* we're only interested in the Info */ }
}

View File

@@ -36,16 +36,16 @@ namespace OpenRA.Traits
public readonly int[] DisplayColor = null;
public readonly bool Playable = true;
public object Create(Actor self) { return new PlayerColorPalette(self, this); }
public object Create(ActorInitializer init) { return new PlayerColorPalette(init.world, this); }
public Color Color { get { return Util.ArrayToColor(DisplayColor); } }
}
public class PlayerColorPalette
{
public PlayerColorPalette(Actor self, PlayerColorPaletteInfo info)
public PlayerColorPalette(World world, PlayerColorPaletteInfo info)
{
var wr = self.World.WorldRenderer;
var wr = world.WorldRenderer;
var pal = wr.GetPalette(info.BasePalette);
var newpal = new Palette(pal, new PlayerColorRemap(
Util.ArrayToColor(info.Color1),

View File

@@ -26,10 +26,7 @@ using System.Drawing;
namespace OpenRA.Traits
{
public class ResourceLayerInfo : ITraitInfo
{
public object Create(Actor self) { return new ResourceLayer(self); }
}
public class ResourceLayerInfo : TraitInfo<ResourceLayer> { }
public class ResourceLayer: IRenderOverlay, ILoadWorldHook, ICustomTerrain
{
@@ -39,7 +36,7 @@ namespace OpenRA.Traits
public ResourceType[] resourceTypes;
CellContents[,] content;
public ResourceLayer(Actor self)
public ResourceLayer()
{
sr = Game.renderer.SpriteRenderer;
}

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Traits
public Sprite[][] Sprites;
public object Create(Actor self) { return new ResourceType(this); }
public object Create(ActorInitializer init) { return new ResourceType(this); }
}
public class ResourceType

View File

@@ -24,10 +24,7 @@ using System.Linq;
namespace OpenRA.Traits
{
class ScreenShakerInfo : ITraitInfo
{
public object Create( Actor self ) { return new ScreenShaker(); }
}
class ScreenShakerInfo : TraitInfo<ScreenShaker> {}
public class ScreenShaker : ITick
{

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Traits
{
public class ShroudInfo : ITraitInfo
{
public object Create(Actor self) { return new Shroud(self); }
public object Create(ActorInitializer init) { return new Shroud(init.world); }
}
public class Shroud
@@ -41,14 +41,14 @@ namespace OpenRA.Traits
public Rectangle? exploredBounds;
public event Action Dirty = () => { };
public Shroud(Actor self)
public Shroud(World world)
{
map = self.World.Map;
map = world.Map;
visibleCells = new int[map.MapSize.X, map.MapSize.Y];
exploredCells = new bool[map.MapSize.X, map.MapSize.Y];
self.World.ActorAdded += AddActor;
self.World.ActorRemoved += RemoveActor;
world.ActorAdded += AddActor;
world.ActorRemoved += RemoveActor;
}
// cache of positions that were added, so no matter what crazy trait code does, it

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Traits
public readonly string Type = "Scorch";
public readonly string[] Types = {"sc1", "sc2", "sc3", "sc4", "sc5", "sc6"};
public readonly int[] Depths = {1,1,1,1,1,1};
public object Create(Actor self) { return new SmudgeLayer(self, this); }
public object Create(ActorInitializer init) { return new SmudgeLayer(this); }
}
class SmudgeLayer: IRenderOverlay, ILoadWorldHook
@@ -42,7 +42,7 @@ namespace OpenRA.Traits
Sprite[][] smudgeSprites;
World world;
public SmudgeLayer(Actor self, SmudgeLayerInfo info)
public SmudgeLayer(SmudgeLayerInfo info)
{
spriteRenderer = Game.renderer.SpriteRenderer;
this.Info = info;

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Traits
class SpatialBinsInfo : ITraitInfo
{
public readonly int BinSize = 8;
public object Create(Actor self) { return new SpatialBins( self, this ); }
public object Create(ActorInitializer init) { return new SpatialBins( init.self, this ); }
}
class SpatialBins : ITick

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Traits
{
public class UnitInfluenceInfo : ITraitInfo
{
public object Create( Actor self ) { return new UnitInfluence( self ); }
public object Create( ActorInitializer init ) { return new UnitInfluence( init.world ); }
}
public class UnitInfluence : ITick
@@ -36,15 +36,15 @@ namespace OpenRA.Traits
List<Actor>[,] influence;
Map map;
public UnitInfluence( Actor self )
public UnitInfluence( World world )
{
map = self.World.Map;
influence = new List<Actor>[self.World.Map.MapSize.X, self.World.Map.MapSize.Y];
for (int i = 0; i < self.World.Map.MapSize.X; i++)
for (int j = 0; j < self.World.Map.MapSize.Y; j++)
map = world.Map;
influence = new List<Actor>[world.Map.MapSize.X, world.Map.MapSize.Y];
for (int i = 0; i < world.Map.MapSize.X; i++)
for (int j = 0; j < world.Map.MapSize.Y; j++)
influence[ i, j ] = new List<Actor>();
self.World.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IOccupySpace>() );
world.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IOccupySpace>() );
}
public void Tick( Actor self )