moving Actor.Location onto the appropriate traits (bob)

This commit is contained in:
Chris Forbes
2010-06-19 14:37:06 +12:00
parent db465e1fdd
commit 66adbee2a6
25 changed files with 218 additions and 103 deletions

View File

@@ -38,8 +38,7 @@ namespace OpenRA
public readonly World World;
public readonly uint ActorID;
[Sync]
public int2 Location;
public int2 Location { get { return traits.Get<IOccupySpace>().TopLeft; } }
[Sync]
public Player Owner;
[Sync]
@@ -52,11 +51,9 @@ namespace OpenRA
{
World = world;
ActorID = world.NextAID();
Location = location;
CenterLocation = Traits.Util.CenterOfCell(Location);
Owner = owner;
var init = new ActorInitializer( this );
var init = new ActorInitializer( this, location );
if (name != null)
{
@@ -70,6 +67,9 @@ namespace OpenRA
traits.Add(trait.Create(init));
}
if( CenterLocation == float2.Zero && traits.Contains<IOccupySpace>() )
CenterLocation = Traits.Util.CenterOfCell(Location);
Size = Lazy.New(() =>
{
var si = Info.Traits.GetOrDefault<SelectableInfo>();
@@ -262,10 +262,12 @@ namespace OpenRA
{
public readonly Actor self;
public World world { get { return self.World; } }
public readonly int2 location;
public ActorInitializer( Actor actor )
public ActorInitializer( Actor actor, int2 location )
{
this.self = actor;
this.location = location;
}
}
}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

View File

@@ -54,14 +54,16 @@ namespace OpenRA.Traits
public readonly string DamagedSound = "kaboom1.aud";
public readonly string DestroyedSound = "kaboom22.aud";
public object Create(ActorInitializer init) { return new Building(init.self); }
public object Create(ActorInitializer init) { return new Building(init); }
}
public class Building : INotifyDamage, IResolveOrder, ITick, IRenderModifier
public class Building : INotifyDamage, IResolveOrder, ITick, IRenderModifier, IOccupySpace
{
readonly Actor self;
public readonly BuildingInfo Info;
[Sync]
readonly int2 topLeft;
[Sync]
bool isRepairing = false;
public bool Disabled
@@ -69,12 +71,13 @@ namespace OpenRA.Traits
get { return self.traits.WithInterface<IDisable>().Any(t => t.Disabled); }
}
public Building(Actor self)
public Building(ActorInitializer init)
{
this.self = self;
this.self = init.self;
this.topLeft = init.location;
Info = self.Info.Traits.Get<BuildingInfo>();
self.CenterLocation = Game.CellSize
* ((float2)self.Location + .5f * (float2)Info.Dimensions);
* ((float2)topLeft + .5f * (float2)Info.Dimensions);
}
public int GetPowerUsage()
@@ -157,5 +160,15 @@ namespace OpenRA.Traits
yield return a.WithPalette("disabled");
}
}
public int2 TopLeft
{
get { return topLeft; }
}
public IEnumerable<int2> OccupiedCells()
{
return Footprint.UnpathableTiles( self.Info.Name, Info, TopLeft );
}
}
}

View File

@@ -24,13 +24,13 @@ using OpenRA.GameRules;
namespace OpenRA.Traits
{
public class MobileInfo : ITraitInfo, ITraitPrerequisite<Unit>
public class MobileInfo : ITraitInfo, ITraitPrerequisite<UnitInfo>
{
public readonly UnitMovementType MovementType = UnitMovementType.Wheel;
public readonly int WaitAverage = 60;
public readonly int WaitSpread = 20;
public object Create(ActorInitializer init) { return new Mobile(init.self); }
public object Create(ActorInitializer init) { return new Mobile(init); }
}
public class Mobile : IIssueOrder, IResolveOrder, IOccupySpace, IMovement
@@ -38,36 +38,37 @@ namespace OpenRA.Traits
readonly Actor self;
[Sync]
int2 __fromCell;
int2 __fromCell, __toCell;
public int2 fromCell
{
get { return __fromCell; }
set { self.World.WorldActor.traits.Get<UnitInfluence>().Remove(self, this); __fromCell = value; self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this); }
set { SetLocation( value, __toCell ); }
}
[Sync]
public int2 toCell
{
get { return self.Location; }
set
{
if (self.Location != value)
{
self.World.WorldActor.traits.Get<UnitInfluence>().Remove(self, this);
self.Location = value;
}
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
}
get { return __toCell; }
set { SetLocation( __fromCell, value ); }
}
void SetLocation( int2 from, int2 to )
{
if( fromCell == from && toCell == to ) return;
self.World.WorldActor.traits.Get<UnitInfluence>().Remove(self, this);
__fromCell = from;
__toCell = to;
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
}
public Mobile(Actor self)
public Mobile(ActorInitializer init)
{
this.self = self;
__fromCell = toCell;
this.self = init.self;
this.__fromCell = this.__toCell = init.location;
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
}
public void TeleportTo(Actor self, int2 xy)
{
fromCell = toCell = xy;
SetLocation( xy, xy );
self.CenterLocation = Util.CenterOfCell(fromCell);
}
@@ -102,6 +103,8 @@ namespace OpenRA.Traits
}
}
public int2 TopLeft { get { return toCell; } }
public IEnumerable<int2> OccupiedCells()
{
return (fromCell == toCell)

View File

@@ -61,8 +61,32 @@ namespace OpenRA.Traits
}
public interface IDisable { bool Disabled { get; } }
public interface IOccupySpace { IEnumerable<int2> OccupiedCells(); }
public interface IOccupySpace
{
int2 TopLeft { get; }
IEnumerable<int2> OccupiedCells();
}
public static class IOccupySpaceExts
{
public static int2 NearestCellTo( this IOccupySpace ios, int2 other )
{
var nearest = ios.TopLeft;
var nearestDistance = int.MaxValue;
foreach( var cell in ios.OccupiedCells() )
{
var dist = ( other - cell ).LengthSquared;
if( dist < nearestDistance )
{
nearest = cell;
nearestDistance = dist;
}
}
return nearest;
}
}
public interface INotifyAttack { void Attacking(Actor self); }
public interface IRenderModifier { IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r); }
public interface IDamageModifier { float GetDamageModifier( WarheadInfo warhead ); }
@@ -112,7 +136,7 @@ namespace OpenRA.Traits
public class TraitInfo<T> : ITraitInfo where T : new() { public virtual object Create(ActorInitializer init) { return new T(); } }
public interface ITraitPrerequisite<T> { }
public interface ITraitPrerequisite<T> where T : ITraitInfo { }
public interface INotifySelection { void SelectionChanged(); }
public interface ILoadWorldHook { void WorldLoaded(World w); }