Drag husks to their final location. Fixes #730.

This commit is contained in:
Paul Chote
2011-07-25 23:50:11 +12:00
parent 49cbaeb1d2
commit 0a5f81c39e
3 changed files with 110 additions and 24 deletions

View File

@@ -50,7 +50,7 @@ namespace OpenRA
{
T Value( World world );
}
public class FacingInit : IActorInit<int>
{
[FieldFromYamlKey]
@@ -68,7 +68,7 @@ namespace OpenRA
return value;
}
}
public class AltitudeInit : IActorInit<int>
{
[FieldFromYamlKey]
@@ -105,6 +105,24 @@ namespace OpenRA
}
}
public class CenterLocationInit : IActorInit<int2>
{
[FieldFromYamlKey]
public readonly int2 value = int2.Zero;
public CenterLocationInit() { }
public CenterLocationInit( int2 init )
{
value = init;
}
public int2 Value( World world )
{
return value;
}
}
public class OwnerInit : IActorInit<Player>
{
[FieldFromYamlKey]

View File

@@ -9,8 +9,10 @@
#endregion
using System.Collections.Generic;
using OpenRA.Traits;
using OpenRA.FileFormats;
using OpenRA.Traits;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{
@@ -23,7 +25,10 @@ namespace OpenRA.Mods.RA
{
[Sync]
int2 location;
[Sync]
public int2 PxPosition { get; set; }
[Sync]
public int Facing { get; set; }
public int ROT { get { return 0; } }
@@ -31,13 +36,69 @@ namespace OpenRA.Mods.RA
public Husk(ActorInitializer init)
{
this.location = init.Get<LocationInit,int2>();
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit,int>() : 128;
var self = init.self;
location = init.Get<LocationInit,int2>();
PxPosition = init.Get<CenterLocationInit, int2>();
Facing = init.Contains<FacingInit>() ? init.Get<FacingInit,int>() : 128;
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));
}
}
public int2 TopLeft { get { return location; } }
public IEnumerable<Pair<int2, SubCell>> OccupiedCells() { yield return Pair.New(TopLeft, SubCell.FullCell); }
public int2 PxPosition { get { return Util.CenterOfCell( location ); } }
class DragHusk : Activity
{
int2 endLocation;
int2 startLocation;
int length;
public DragHusk(int2 start, int2 end, int length)
{
startLocation = start;
endLocation = end;
this.length = length;
}
int ticks = 0;
public override Activity Tick( Actor self )
{
var husk = self.Trait<Husk>();
husk.PxPosition = int2.Lerp(startLocation, endLocation, ticks, length - 1);
if (++ticks >= length)
return NextActivity;
return this;
}
public override IEnumerable<Target> GetTargets( Actor self ) { yield break; }
public override void Cancel( Actor self ) { }
}
}
public class HuskSpeedInit : IActorInit<int>
{
[FieldFromYamlKey]
public readonly int value = 0;
public HuskSpeedInit() { }
public HuskSpeedInit( int init )
{
value = init;
}
public int Value( World world )
{
return value;
}
}
}

View File

@@ -8,8 +8,9 @@
*/
#endregion
using OpenRA.Traits;
using OpenRA.FileFormats;
using OpenRA.Traits;
using OpenRA.Mods.RA.Move;
namespace OpenRA.Mods.RA
{
@@ -24,24 +25,30 @@ namespace OpenRA.Mods.RA
public void Killed(Actor self, AttackInfo e)
{
self.World.AddFrameEndTask(w =>
{
var info = self.Info.Traits.Get<LeavesHuskInfo>();
var td = new TypeDictionary()
{
var info = self.Info.Traits.Get<LeavesHuskInfo>();
var td = new TypeDictionary
{
new LocationInit( self.Location ),
new OwnerInit( self.Owner ),
new SkipMakeAnimsInit()
};
if (self.HasTrait<IFacing>())
td.Add(new FacingInit( self.Trait<IFacing>().Facing ));
new LocationInit( self.Location ),
new CenterLocationInit(self.CenterLocation),
new OwnerInit( self.Owner ),
new SkipMakeAnimsInit()
};
var husk = w.CreateActor(info.HuskActor, td);
var turreted = self.TraitOrDefault<Turreted>();
if (turreted != null)
foreach (var p in husk.TraitsImplementing<ThrowsParticle>())
p.InitialFacing = turreted.turretFacing;
});
if (self.HasTrait<IFacing>())
td.Add(new FacingInit( self.Trait<IFacing>().Facing ));
// Allows the husk to drag to its final position
var mobile = self.TraitOrDefault<Mobile>();
if (mobile != null)
td.Add(new HuskSpeedInit(mobile.MovementSpeedForCell(self, self.Location)));
var husk = w.CreateActor(info.HuskActor, td);
var turreted = self.TraitOrDefault<Turreted>();
if (turreted != null)
foreach (var p in husk.TraitsImplementing<ThrowsParticle>())
p.InitialFacing = turreted.turretFacing;
});
}
}
}