Rewrite husks using world coords.

This commit is contained in:
Paul Chote
2013-07-21 13:01:36 +12:00
parent 6a435752fd
commit e23c1ad33e
6 changed files with 57 additions and 57 deletions

View File

@@ -9,88 +9,79 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class HuskInfo : ITraitInfo, IFacingInfo
{
public object Create( ActorInitializer init ) { return new Husk( init ); }
public readonly string[] AllowedTerrain = { };
public object Create(ActorInitializer init) { return new Husk(init, this); }
public int GetInitialFacing() { return 128; }
}
class Husk : IOccupySpace, IFacing, ISync
class Husk : IPositionable, IFacing, ISync
{
[Sync] CPos location;
[Sync] PPos PxPosition;
readonly HuskInfo info;
readonly Actor self;
public WPos CenterPosition { get { return PxPosition.ToWPos(0); } }
[Sync] public CPos TopLeft { get; private set; }
[Sync] public WPos CenterPosition { get; private set; }
[Sync] public int Facing { get; set; }
public int ROT { get { return 0; } }
public int Altitude { get { return 0; } set { } }
[Sync] public int Facing { get; set; }
public int ROT { get { return 0; } }
public Husk(ActorInitializer init)
public Husk(ActorInitializer init, HuskInfo info)
{
var self = init.self;
location = init.Get<LocationInit, CPos>();
PxPosition = init.Contains<CenterLocationInit>() ? init.Get<CenterLocationInit, PPos>() : Util.CenterOfCell(location);
Facing = init.Contains<FacingInit>() ? init.Get<FacingInit,int>() : 128;
this.info = info;
this.self = init.self;
var speed = init.Contains<HuskSpeedInit>() ? init.Get<HuskSpeedInit,int>() : 0;
if (speed > 0)
{
var to = Util.CenterOfCell(location);
var length = (int)((to - PxPosition).Length * 3 / speed);
self.QueueActivity(new DragHusk(PxPosition, to, length, this));
}
TopLeft = init.Get<LocationInit, CPos>();
var ppos = init.Contains<CenterLocationInit>() ? init.Get<CenterLocationInit, PPos>() : Util.CenterOfCell(TopLeft);
CenterPosition = ppos.ToWPos(0);
Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : 128;
var speed = init.Contains<HuskSpeedInit>() ? init.Get<HuskSpeedInit, int>() : 0;
var distance = (TopLeft.CenterPosition - CenterPosition).Length;
if (speed > 0 && distance > 0)
self.QueueActivity(new Drag(CenterPosition, TopLeft.CenterPosition, distance / speed));
}
public CPos TopLeft { get { return location; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); }
class DragHusk : Activity
public bool CanEnterCell(CPos cell)
{
Husk husk;
PPos endLocation;
PPos startLocation;
int length;
if (!self.World.Map.IsInMap(cell.X, cell.Y))
return false;
public DragHusk(PPos start, PPos end, int length, Husk husk)
{
startLocation = start;
endLocation = end;
this.length = length;
this.husk = husk;
}
if (!info.AllowedTerrain.Contains(self.World.GetTerrainType(cell)))
return false;
int ticks = 0;
public override Activity Tick( Actor self )
{
if (ticks >= length || length <= 1)
{
husk.PxPosition = endLocation;
return NextActivity;
}
return !self.World.ActorMap.AnyUnitsAt(cell);
}
husk.PxPosition = PPos.Lerp(startLocation, endLocation, ticks++, length - 1);
return this;
}
public void SetPosition(Actor self, CPos cell) { SetPosition(self, cell.CenterPosition); }
public void SetVisualPosition(Actor self, WPos pos) { CenterPosition = pos; }
public override IEnumerable<Target> GetTargets( Actor self ) { yield break; }
public override void Cancel( Actor self ) { }
public void SetPosition(Actor self, WPos pos)
{
self.World.ActorMap.Remove(self, this);
CenterPosition = pos;
TopLeft = pos.ToCPos();
self.World.ActorMap.Add(self, this);
}
}
public class HuskSpeedInit : IActorInit<int>
{
[FieldFromYamlKey] public readonly int value = 0;
[FieldFromYamlKey] readonly int value = 0;
public HuskSpeedInit() { }
public HuskSpeedInit( int init ) { value = init; }
public int Value( World world ) { return value; }
public HuskSpeedInit(int init) { value = init; }
public int Value(World world) { return value; }
}
}

View File

@@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA
if (mobile != null)
{
if (!mobile.CanEnterCell(self.Location, self, false)) return;
td.Add(new HuskSpeedInit(mobile.MovementSpeedForCell(self, self.Location) * 3 * Game.CellSize / 1024));
td.Add(new HuskSpeedInit(mobile.MovementSpeedForCell(self, self.Location)));
}
var aircraft = self.TraitOrDefault<Aircraft>();

View File

@@ -28,19 +28,25 @@ namespace OpenRA.Mods.RA.Move
public override Activity Tick(Actor self)
{
var mobile = self.Trait<Mobile>();
var positionable = self.Trait<IPositionable>();
var mobile = positionable as Mobile;
var pos = length > 1
? WPos.Lerp(start, end, ticks, length - 1)
: end;
mobile.SetVisualPosition(self, pos);
positionable.SetVisualPosition(self, pos);
if (++ticks >= length)
{
mobile.IsMoving = false;
if (mobile != null)
mobile.IsMoving = false;
return NextActivity;
}
mobile.IsMoving = true;
if (mobile != null)
mobile.IsMoving = true;
return this;
}

View File

@@ -449,6 +449,7 @@
Armor:
Type: Light
Husk:
AllowedTerrain: Clear, Rough, Road, Tiberium, BlueTiberium, Beach
HiddenUnderFog:
AppearsOnRadar:
Burns:

View File

@@ -78,6 +78,7 @@
Armor:
Type: Light
Husk:
AllowedTerrain: Sand, Rock, Transition, Concrete, Spice, SpiceBlobs, Dune
HiddenUnderFog:
AppearsOnRadar:
Burns:

View File

@@ -373,6 +373,7 @@
^Husk:
Husk:
AllowedTerrain: Clear, Rough, Road, Ore, Gems, Beach
RenderUnit:
Health:
HP: 140