Convert Drag to world coords.

This commit is contained in:
Paul Chote
2013-07-06 19:55:16 +12:00
parent 7f25573cfa
commit 0fdffd7b6a
8 changed files with 40 additions and 28 deletions

View File

@@ -34,6 +34,8 @@ namespace OpenRA
public static bool operator ==(WPos me, WPos other) { return (me.X == other.X && me.Y == other.Y && me.Z == other.Z); } public static bool operator ==(WPos me, WPos other) { return (me.X == other.X && me.Y == other.Y && me.Z == other.Z); }
public static bool operator !=(WPos me, WPos other) { return !(me == other); } public static bool operator !=(WPos me, WPos other) { return !(me == other); }
public static WPos Lerp(WPos a, WPos b, int mul, int div) { return a + (b - a) * mul / div; }
public static WPos Average(params WPos[] list) public static WPos Average(params WPos[] list)
{ {
if (list == null || list.Length == 0) if (list == null || list.Length == 0)

View File

@@ -30,6 +30,11 @@ namespace OpenRA.Traits
return ( facing - rot ) & 0xFF; return ( facing - rot ) & 0xFF;
} }
public static int GetFacing(WVec d, int currentFacing)
{
return GetFacing(new int2(d.X, d.Y), currentFacing);
}
public static int GetFacing(PVecInt d, int currentFacing) public static int GetFacing(PVecInt d, int currentFacing)
{ {
return GetFacing(d.ToInt2(), currentFacing); return GetFacing(d.ToInt2(), currentFacing);

View File

@@ -27,16 +27,15 @@ namespace OpenRA.Mods.Cnc
readonly RenderUnit ru; readonly RenderUnit ru;
State state; State state;
PPos startDock; WPos startDock, endDock;
PPos endDock;
public HarvesterDockSequence(Actor self, Actor proc) public HarvesterDockSequence(Actor self, Actor proc)
{ {
this.proc = proc; this.proc = proc;
state = State.Turn; state = State.Turn;
harv = self.Trait<Harvester>(); harv = self.Trait<Harvester>();
ru = self.Trait<RenderUnit>(); ru = self.Trait<RenderUnit>();
startDock = self.Trait<IHasLocation>().PxPosition; startDock = self.Trait<IHasLocation>().PxPosition.ToWPos(0);
endDock = proc.Trait<IHasLocation>().PxPosition + new PVecInt(-15,8); endDock = (proc.Trait<IHasLocation>().PxPosition + new PVecInt(-15,8)).ToWPos(0);
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)

View File

@@ -35,9 +35,9 @@ namespace OpenRA.Mods.RA.Activities
// Move to the middle of the target, ignoring impassable tiles // Move to the middle of the target, ignoring impassable tiles
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
var to = target.CenterLocation; var to = target.CenterPosition;
var from = self.CenterLocation; var from = self.CenterPosition;
var speed = mobile.MovementSpeedForCell(self, self.Location); var speed = mobile.WorldMovementSpeedForCell(self, self.Location);
var length = speed > 0 ? (int)((to - from).Length * 3 / speed) : 0; var length = speed > 0 ? (int)((to - from).Length * 3 / speed) : 0;
return Util.SequenceActivities( return Util.SequenceActivities(

View File

@@ -79,23 +79,24 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
var actor = cargo.Unload(self); var actor = cargo.Unload(self);
var exitPx = Util.CenterOfCell(exitTile.Value); var exit = exitTile.Value.CenterPosition;
var currentPx = Util.CenterOfCell(self.Location); var current = self.Location.CenterPosition;
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
if (actor.Destroyed) return; if (actor.Destroyed)
return;
var mobile = actor.Trait<Mobile>(); var mobile = actor.Trait<Mobile>();
mobile.Facing = Util.GetFacing( (exitPx - currentPx).ToInt2(), mobile.Facing ); mobile.Facing = Util.GetFacing(exit - current, mobile.Facing );
mobile.SetPosition(actor, exitTile.Value); mobile.SetPosition(actor, exitTile.Value);
mobile.AdjustPxPosition(actor, currentPx); mobile.AdjustPxPosition(actor, PPos.FromWPos(current));
var speed = mobile.MovementSpeedForCell(actor, exitTile.Value); var speed = mobile.WorldMovementSpeedForCell(actor, exitTile.Value);
var length = speed > 0 ? ((int)(exitPx - currentPx).Length * 3 / speed) : 0; var length = speed > 0 ? ((int)(exit - current).Length * 3 / speed) : 0;
w.Add(actor); w.Add(actor);
actor.CancelActivity(); actor.CancelActivity();
actor.QueueActivity(new Drag(currentPx, exitPx, length)); actor.QueueActivity(new Drag(current, exit, length));
actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0)); actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0));
var rallyPoint = ChooseRallyPoint(actor).Value; var rallyPoint = ChooseRallyPoint(actor).Value;

View File

@@ -15,25 +15,25 @@ namespace OpenRA.Mods.RA.Move
{ {
public class Drag : Activity public class Drag : Activity
{ {
PPos endLocation; WPos start, end;
PPos startLocation;
int length; int length;
int ticks = 0; int ticks = 0;
public Drag(PPos start, PPos end, int length) public Drag(WPos start, WPos end, int length)
{ {
startLocation = start; this.start = start;
endLocation = end; this.end = end;
this.length = length; this.length = length;
} }
public override Activity Tick( Actor self ) public override Activity Tick( Actor self )
{ {
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
mobile.PxPosition = length > 1 var pos = length > 1
? PPos.Lerp(startLocation, endLocation, ticks, length - 1) ? WPos.Lerp(start, end, ticks, length - 1)
: endLocation; : end;
mobile.PxPosition = PPos.FromWPos(pos);
if (++ticks >= length) if (++ticks >= length)
{ {
mobile.IsMoving = false; mobile.IsMoving = false;
@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA.Move
public override IEnumerable<Target> GetTargets( Actor self ) public override IEnumerable<Target> GetTargets( Actor self )
{ {
yield return Target.FromPos(endLocation); yield return Target.FromPos(PPos.FromWPos(end));
} }
// Cannot be cancelled // Cannot be cancelled

View File

@@ -420,6 +420,11 @@ namespace OpenRA.Mods.RA.Move
return (int)(speed / 100); return (int)(speed / 100);
} }
public int WorldMovementSpeedForCell(Actor self, CPos cell)
{
return MovementSpeedForCell(self, cell) * 1024 / Game.CellSize;
}
public void AddInfluence() public void AddInfluence()
{ {
if (self.IsInWorld) if (self.IsInWorld)

View File

@@ -50,8 +50,8 @@ namespace OpenRA.Mods.RA
public void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo) public void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo)
{ {
var exit = self.Location + exitinfo.ExitCellVector; var exit = self.Location + exitinfo.ExitCellVector;
var spawn = self.Trait<IHasLocation>().PxPosition + exitinfo.SpawnOffsetVector; var spawn = (self.Trait<IHasLocation>().PxPosition + exitinfo.SpawnOffsetVector).ToWPos(0);
var to = Util.CenterOfCell(exit); var to = exit.CenterPosition;
var fi = producee.Traits.Get<IFacingInfo>(); var fi = producee.Traits.Get<IFacingInfo>();
var initialFacing = exitinfo.Facing < 0 ? Util.GetFacing(to - spawn, fi.GetInitialFacing()) : exitinfo.Facing; var initialFacing = exitinfo.Facing < 0 ? Util.GetFacing(to - spawn, fi.GetInitialFacing()) : exitinfo.Facing;
@@ -66,7 +66,7 @@ namespace OpenRA.Mods.RA
// TODO: Move this into an *Init // TODO: Move this into an *Init
// TODO: We should be adjusting the actual position for aircraft, not just visuals. // TODO: We should be adjusting the actual position for aircraft, not just visuals.
var teleportable = newUnit.Trait<ITeleportable>(); var teleportable = newUnit.Trait<ITeleportable>();
teleportable.AdjustPxPosition(newUnit, spawn); teleportable.AdjustPxPosition(newUnit, PPos.FromWPos(spawn));
// TODO: Generalize this for non-mobile (e.g. aircraft) too // TODO: Generalize this for non-mobile (e.g. aircraft) too
// Remember to update the Enter activity too // Remember to update the Enter activity too
@@ -74,7 +74,7 @@ namespace OpenRA.Mods.RA
if (mobile != null) if (mobile != null)
{ {
// Animate the spawn -> exit transition // Animate the spawn -> exit transition
var speed = mobile.MovementSpeedForCell(newUnit, exit); var speed = mobile.WorldMovementSpeedForCell(newUnit, exit);
var length = speed > 0 ? (int)((to - spawn).Length * 3 / speed) : 0; var length = speed > 0 ? (int)((to - spawn).Length * 3 / speed) : 0;
newUnit.QueueActivity(new Drag(spawn, to, length)); newUnit.QueueActivity(new Drag(spawn, to, length));
} }