Refactor AutoCarryall trait and rename to Carryall

Fixup Carryable
This commit is contained in:
penev92
2015-03-14 01:35:38 +02:00
parent f9a2378be8
commit 4eb67b328f
4 changed files with 69 additions and 57 deletions

View File

@@ -72,7 +72,7 @@
<Compile Include="Activities\SwallowActor.cs" />
<Compile Include="SpriteLoaders\R8Loader.cs" />
<Compile Include="Traits\AttackSwallow.cs" />
<Compile Include="Traits\AutoCarryall.cs" />
<Compile Include="Traits\Carryall.cs" />
<Compile Include="Traits\Buildings\ProductionFromMapEdge.cs" />
<Compile Include="Traits\Buildings\DamagedWithoutFoundation.cs" />
<Compile Include="Traits\Buildings\LaysTerrain.cs" />

View File

@@ -7,16 +7,15 @@
* see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Can be carried by units with the trait `AutoCarryall`.")]
[Desc("Can be carried by units with the trait `Carryall`.")]
public class CarryableInfo : ITraitInfo
{
[Desc("Required distance away from destination before requesting a pickup.")]
@@ -32,7 +31,7 @@ namespace OpenRA.Mods.D2k.Traits
public bool Reserved { get; private set; }
// If we're locked there isnt much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
// If we're locked there isn't much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
bool locked;
public bool WantsTransport { get; private set; }
@@ -62,15 +61,14 @@ namespace OpenRA.Mods.D2k.Traits
Destination = destination;
this.afterLandActivity = afterLandActivity;
WantsTransport = true;
if (locked || Reserved)
return;
WantsTransport = true;
// Inform all idle carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(c => !c.Trait.Busy && !c.Actor.IsDead && c.Actor.Owner == self.Owner)
var carriers = self.World.ActorsWithTrait<Carryall>()
.Where(c => !c.Trait.IsBusy && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var carrier in carriers)
@@ -90,7 +88,7 @@ namespace OpenRA.Mods.D2k.Traits
WantsTransport = false;
afterLandActivity = null;
// TODO: We could implement something like a carrier.Trait<AutoCarryAll>().CancelTransportNotify(self) and call it here
// TODO: We could implement something like a carrier.Trait<Carryall>().CancelTransportNotify(self) and call it here
}
// We do not handle Harvested notification
@@ -99,8 +97,8 @@ namespace OpenRA.Mods.D2k.Traits
public Actor GetClosestIdleCarrier()
{
// Find carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(p => p.Actor.Owner == self.Owner && !p.Trait.Busy)
var carriers = self.World.ActorsWithTrait<Carryall>()
.Where(p => p.Actor.Owner == self.Owner && !p.Trait.IsBusy && p.Actor.IsInWorld)
.Select(h => h.Actor);
return WorldUtils.ClosestTo(carriers, self);

View File

@@ -7,57 +7,63 @@
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Activities;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries")]
public class AutoCarryallInfo : ITraitInfo, Requires<IBodyOrientationInfo>
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries.")]
public class CarryallInfo : ITraitInfo, Requires<IBodyOrientationInfo>
{
public object Create(ActorInitializer init) { return new AutoCarryall(init.Self, this); }
[Desc("Set to false when the carryall should not automatically get new jobs.")]
public readonly bool Automatic = true;
public object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
}
public class AutoCarryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender
public class Carryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender
{
readonly Actor self;
readonly WRange carryHeight;
readonly CarryallInfo info;
// The actor we are currently carrying.
[Sync] Actor carrying;
bool isCarrying;
[Sync] public Actor Carrying { get; internal set; }
public bool IsCarrying { get; internal set; }
// TODO: Use ActorPreviews so that this can support actors with multiple sprites
Animation anim;
public bool Busy { get; internal set; }
public bool IsBusy { get; internal set; }
public AutoCarryall(Actor self, AutoCarryallInfo info)
public Carryall(Actor self, CarryallInfo info)
{
this.self = self;
this.info = info;
IsBusy = false;
IsCarrying = false;
carryHeight = self.Trait<Helicopter>().Info.LandAltitude;
}
public void OnBecomingIdle(Actor self)
{
if (info.Automatic)
FindCarryableForTransport();
if (!Busy)
if (!IsBusy)
self.QueueActivity(new HeliFlyCircle(self));
}
// A carryable notifying us that he'd like to be carried
public bool RequestTransportNotify(Actor carryable)
{
if (Busy)
if (IsBusy || !info.Automatic)
return false;
if (ReserveCarryable(carryable))
@@ -71,7 +77,10 @@ namespace OpenRA.Mods.D2k.Traits
void FindCarryableForTransport()
{
// get all carryables who want transport
if (!self.IsInWorld)
return;
// Get all carryables who want transport
var carryables = self.World.ActorsWithTrait<Carryable>()
.Where(p =>
{
@@ -112,8 +121,8 @@ namespace OpenRA.Mods.D2k.Traits
{
if (carryable.Trait<Carryable>().Reserve(self))
{
carrying = carryable;
Busy = true;
Carrying = carryable;
IsBusy = true;
return true;
}
@@ -123,24 +132,26 @@ namespace OpenRA.Mods.D2k.Traits
// Unreserve the carryable
public void UnreserveCarryable()
{
if (carrying != null)
if (Carrying != null)
{
if (carrying.IsInWorld && !carrying.IsDead)
carrying.Trait<Carryable>().UnReserve(self);
if (Carrying.IsInWorld && !Carrying.IsDead)
Carrying.Trait<Carryable>().UnReserve(self);
carrying = null;
Carrying = null;
}
Busy = false;
IsBusy = false;
}
// INotifyKilled
public void Killed(Actor self, AttackInfo e)
{
if (carrying != null)
if (Carrying != null)
{
if (isCarrying && carrying.IsInWorld && !carrying.IsDead)
carrying.Kill(e.Attacker);
if (IsCarrying && Carrying.IsInWorld && !Carrying.IsDead)
Carrying.Kill(e.Attacker);
Carrying = null;
}
UnreserveCarryable();
@@ -149,7 +160,9 @@ namespace OpenRA.Mods.D2k.Traits
// Called when carryable is inside.
public void AttachCarryable(Actor carryable)
{
isCarrying = true;
IsBusy = true;
IsCarrying = true;
Carrying = carryable;
// Create a new animation for our carryable unit
var rs = carryable.Trait<RenderSprites>();
@@ -161,7 +174,7 @@ namespace OpenRA.Mods.D2k.Traits
// Called when released
public void CarryableReleased()
{
isCarrying = false;
IsCarrying = false;
anim = null;
}
@@ -171,7 +184,8 @@ namespace OpenRA.Mods.D2k.Traits
if (anim != null && !self.World.FogObscures(self))
{
anim.Tick();
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Range), wr.Palette("player" + carrying.Owner.InternalName));
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Range),
wr.Palette("player" + Carrying.Owner.InternalName));
foreach (var rr in renderables)
yield return rr;

View File

@@ -29,7 +29,7 @@ carryall.scripted:
carryall:
Inherits: carryall.scripted
AutoCarryall:
Carryall:
Buildable:
Queue: Aircraft
BuildPaletteOrder: 120