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

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -9,6 +9,8 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Mods.RA.Activities; using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Air; using OpenRA.Mods.RA.Air;
@@ -27,8 +29,18 @@ namespace OpenRA.Mods.RA
public readonly WRange Cordon = new WRange(5120); public readonly WRange Cordon = new WRange(5120);
[ActorReference] [ActorReference]
public readonly string FlareType = null; [Desc("Actor to spawn when the aircraft first enter the map")]
public readonly int FlareTime = 3000; // 2 minutes public readonly string FlareActor = null;
[Desc("Amount of time to keep the flare alive after the aircraft have finished attacking")]
public readonly int FlareRemoveDelay = 25;
[ActorReference]
[Desc("Actor to spawn when the aircraft start attacking")]
public readonly string CameraActor = null;
[Desc("Amount of time to keep the camera alive after the aircraft have finished attacking")]
public readonly int CameraRemoveDelay = 25;
public override object Create(ActorInitializer init) { return new AirstrikePower(init.self, this); } public override object Create(ActorInitializer init) { return new AirstrikePower(init.self, this); }
} }
@@ -51,24 +63,65 @@ namespace OpenRA.Mods.RA
var startEdge = target - (self.World.DistanceToMapEdge(target, -delta) + info.Cordon).Range * delta / 1024; var startEdge = target - (self.World.DistanceToMapEdge(target, -delta) + info.Cordon).Range * delta / 1024;
var finishEdge = target + (self.World.DistanceToMapEdge(target, delta) + info.Cordon).Range * delta / 1024; var finishEdge = target + (self.World.DistanceToMapEdge(target, delta) + info.Cordon).Range * delta / 1024;
Actor flare = null;
Actor camera = null;
Dictionary<Actor, bool> aircraftInRange = new Dictionary<Actor, bool>();
Action<Actor> onEnterRange = a =>
{
// Spawn a camera when the first plane enters the target area
if (info.CameraActor != null && !aircraftInRange.Any(kv => kv.Value))
{
self.World.AddFrameEndTask(w =>
{
camera = w.CreateActor(info.CameraActor, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner),
});
});
}
aircraftInRange[a] = true;
};
Action<Actor> onExitRange = a =>
{
aircraftInRange[a] = false;
// Remove the camera when the final plane leaves the target area
if (!aircraftInRange.Any(kv => kv.Value))
{
if (camera != null)
{
camera.QueueActivity(new Wait(info.CameraRemoveDelay));
camera.QueueActivity(new RemoveSelf());
}
if (flare != null)
{
flare.QueueActivity(new Wait(info.FlareRemoveDelay));
flare.QueueActivity(new RemoveSelf());
}
camera = flare = null;
}
};
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
var notification = self.Owner.IsAlliedWith(self.World.RenderPlayer) ? Info.LaunchSound : Info.IncomingSound; if (info.FlareActor != null)
Sound.Play(notification);
Actor flare = null;
if (info.FlareType != null)
{ {
flare = w.CreateActor(info.FlareType, new TypeDictionary flare = w.CreateActor(info.FlareActor, new TypeDictionary
{ {
new LocationInit(order.TargetLocation), new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner), new OwnerInit(self.Owner),
}); });
flare.QueueActivity(new Wait(info.FlareTime));
flare.QueueActivity(new RemoveSelf());
} }
var notification = self.Owner.IsAlliedWith(self.World.RenderPlayer) ? Info.LaunchSound : Info.IncomingSound;
Sound.Play(notification);
for (var i = -info.SquadSize / 2; i <= info.SquadSize / 2; i++) for (var i = -info.SquadSize / 2; i <= info.SquadSize / 2; i++)
{ {
// Even-sized squads skip the lead plane // Even-sized squads skip the lead plane
@@ -77,8 +130,8 @@ namespace OpenRA.Mods.RA
// Includes the 90 degree rotation between body and world coordinates // Includes the 90 degree rotation between body and world coordinates
var so = info.SquadOffset; var so = info.SquadOffset;
var spawnOffset = new WVec(i*so.Y, -Math.Abs(i)*so.X, 0).Rotate(attackRotation); var spawnOffset = new WVec(i * so.Y, -Math.Abs(i) * so.X, 0).Rotate(attackRotation);
var targetOffset = new WVec(i*so.Y, 0, 0).Rotate(attackRotation); var targetOffset = new WVec(i * so.Y, 0, 0).Rotate(attackRotation);
var a = w.CreateActor(info.UnitType, new TypeDictionary var a = w.CreateActor(info.UnitType, new TypeDictionary
{ {
@@ -87,13 +140,15 @@ namespace OpenRA.Mods.RA
new FacingInit(attackFacing), new FacingInit(attackFacing),
}); });
a.Trait<AttackBomber>().SetTarget(target + targetOffset); var attack = a.Trait<AttackBomber>();
attack.SetTarget(target + targetOffset);
if (flare != null) attack.OnEnteredAttackRange += onEnterRange;
a.QueueActivity(new CallFunc(() => flare.Destroy())); attack.OnExitedAttackRange += onExitRange;
attack.OnRemovedFromWorld += onExitRange;
a.QueueActivity(new Fly(a, Target.FromPos(finishEdge + spawnOffset))); a.QueueActivity(new Fly(a, Target.FromPos(finishEdge + spawnOffset)));
a.QueueActivity(new RemoveSelf()); a.QueueActivity(new RemoveSelf());
aircraftInRange.Add(a, false);
} }
}); });
} }

View File

@@ -385,6 +385,7 @@ HQ:
UnitType: a10 UnitType: a10
DisplayBeacon: True DisplayBeacon: True
DisplayRadarPing: True DisplayRadarPing: True
CameraActor: camera
SupportPowerChargeBar: SupportPowerChargeBar:
FIX: FIX:

View File

@@ -59,7 +59,6 @@ PALACEA:
LongDesc: Ornithopter drops a load of parachuted\nbombs on your target LongDesc: Ornithopter drops a load of parachuted\nbombs on your target
UnitType: orni.bomber UnitType: orni.bomber
SelectTargetSound: SelectTargetSound:
FlareType:
CanPowerDown: CanPowerDown:
DisabledOverlay: DisabledOverlay:
RequiresPower: RequiresPower:

View File

@@ -88,7 +88,6 @@ PALACEO:
LongDesc: Ornithopter drops a load of parachuted\nbombs on your target LongDesc: Ornithopter drops a load of parachuted\nbombs on your target
UnitType: orni.bomber UnitType: orni.bomber
SelectTargetSound: SelectTargetSound:
FlareType:
CanPowerDown: CanPowerDown:
DisabledOverlay: DisabledOverlay:
RequiresPower: RequiresPower:

View File

@@ -152,7 +152,7 @@ powerproxy.parabombs:
AllowMultiple: yes AllowMultiple: yes
UnitType: badr.bomber UnitType: badr.bomber
SelectTargetSound: slcttgt1.aud SelectTargetSound: slcttgt1.aud
FlareType: flare FlareActor: flare
powerproxy.sonarpulse: powerproxy.sonarpulse:
SonarPulsePower: SonarPulsePower: