Rework airstrike flare and camera spawning.

This commit is contained in:
Paul Chote
2014-03-12 21:52:56 +13:00
parent 91ea2d978f
commit d7d12ef799
6 changed files with 105 additions and 61 deletions

View File

@@ -23,48 +23,34 @@ namespace OpenRA.Mods.RA
[Desc("Armament name")]
public readonly string Guns = "secondary";
public readonly int FacingTolerance = 2;
public readonly WRange VisionRange = WRange.FromCells(10);
public override object Create(ActorInitializer init) { return new AttackBomber(init.self, this); }
}
class AttackBomber : AttackBase, ISync, INotifyKilled
class AttackBomber : AttackBase, ISync, INotifyRemovedFromWorld
{
AttackBomberInfo info;
Actor camera;
[Sync] Target target;
[Sync] bool inAttackRange;
public event Action<Actor> OnRemovedFromWorld = self => { };
public event Action<Actor> OnEnteredAttackRange = self => { };
public event Action<Actor> OnExitedAttackRange = self => { };
public AttackBomber(Actor self, AttackBomberInfo info)
: base(self, info)
{
this.info = info;
this.camera = null;
}
public override void Tick(Actor self)
{
base.Tick(self);
var facing = self.TraitOrDefault<IFacing>();
var cp = self.CenterPosition;
var bombTarget = Target.FromPos(cp - new WVec(0, 0, cp.Z));
// Provide vision
if (this.camera == null &&
target.IsInRange(self.CenterPosition, this.info.VisionRange))
{
this.camera = self.World.CreateActor("camera", new TypeDictionary
{
new LocationInit(target.CenterPosition.ToCPos()),
new OwnerInit(self.Owner),
});
}
else if (this.camera != null &&
!target.IsInRange(self.CenterPosition, this.info.VisionRange))
{
self.World.Remove(this.camera);
this.camera = null;
}
var wasInAttackRange = inAttackRange;
inAttackRange = false;
// Bombs drop anywhere in range
foreach (var a in Armaments.Where(a => a.Info.Name == info.Bombs))
@@ -72,39 +58,43 @@ namespace OpenRA.Mods.RA
if (!target.IsInRange(self.CenterPosition, a.Weapon.Range))
continue;
a.CheckFire(self, this, facing, bombTarget);
inAttackRange = true;
a.CheckFire(self, this, facing.Value, bombTarget);
}
// Guns only fire when approaching the target
var facingToTarget = Util.GetFacing(target.CenterPosition - self.CenterPosition, facing.Facing);
if (Math.Abs(facingToTarget - facing.Facing) % 256 > info.FacingTolerance)
return;
foreach (var a in Armaments.Where(a => a.Info.Name == info.Guns))
var f = facing.Value.Facing;
var facingToTarget = Util.GetFacing(target.CenterPosition - self.CenterPosition, f);
if (Math.Abs(facingToTarget - f) % 256 <= info.FacingTolerance)
{
if (!target.IsInRange(self.CenterPosition, a.Weapon.Range))
continue;
foreach (var a in Armaments.Where(a => a.Info.Name == info.Guns))
{
if (!target.IsInRange(self.CenterPosition, a.Weapon.Range))
continue;
var t = Target.FromPos(cp - new WVec(0, a.Weapon.Range.Range / 2, cp.Z).Rotate(WRot.FromFacing(facing.Facing)));
a.CheckFire(self, this, facing, t);
var t = Target.FromPos(cp - new WVec(0, a.Weapon.Range.Range / 2, cp.Z).Rotate(WRot.FromFacing(f)));
inAttackRange = true;
a.CheckFire(self, this, facing.Value, t);
}
}
if (inAttackRange && !wasInAttackRange)
OnEnteredAttackRange(self);
if (!inAttackRange && wasInAttackRange)
OnExitedAttackRange(self);
}
public void SetTarget(WPos pos) { target = Target.FromPos(pos); }
public void Killed(Actor self, AttackInfo e)
public void RemovedFromWorld(Actor self)
{
if (this.camera != null)
{
self.World.Remove(this.camera);
this.camera = null;
}
OnRemovedFromWorld(self);
}
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove)
{
// TODO: Player controlled units want this too!
throw new NotImplementedException("CarpetBomb requires a scripted target");
throw new NotImplementedException("AttackBomber requires a scripted target");
}
}
}