Overhaul sand worm rendering and attacking.

This commit is contained in:
Paul Chote
2016-01-02 19:26:15 +00:00
parent 8ce274ed79
commit 47c97735ed
7 changed files with 171 additions and 122 deletions

View File

@@ -19,22 +19,22 @@ using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Activities namespace OpenRA.Mods.D2k.Activities
{ {
enum AttackState { Burrowed, EmergingAboveGround, ReturningUnderground } enum AttackState { Uninitialized, Burrowed, Attacking }
class SwallowActor : Activity class SwallowActor : Activity
{ {
const int NearEnough = 1; const int NearEnough = 1;
readonly CPos location;
readonly Target target; readonly Target target;
readonly Sandworm sandworm; readonly Sandworm sandworm;
readonly UpgradeManager manager;
readonly WeaponInfo weapon; readonly WeaponInfo weapon;
readonly WithSpriteBody withSpriteBody;
readonly RadarPings radarPings; readonly RadarPings radarPings;
readonly AttackSwallow swallow; readonly AttackSwallow swallow;
readonly IPositionable positionable; readonly IPositionable positionable;
int countdown; int countdown;
CPos burrowLocation;
AttackState stance; AttackState stance;
public SwallowActor(Actor self, Target target, WeaponInfo weapon) public SwallowActor(Actor self, Target target, WeaponInfo weapon)
@@ -44,128 +44,133 @@ namespace OpenRA.Mods.D2k.Activities
sandworm = self.Trait<Sandworm>(); sandworm = self.Trait<Sandworm>();
positionable = self.Trait<Mobile>(); positionable = self.Trait<Mobile>();
swallow = self.Trait<AttackSwallow>(); swallow = self.Trait<AttackSwallow>();
withSpriteBody = self.Trait<WithSpriteBody>(); manager = self.Trait<UpgradeManager>();
radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>(); radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>();
countdown = swallow.Info.AttackTime;
withSpriteBody.DefaultAnimation.ReplaceAnim(sandworm.Info.BurrowedSequence);
stance = AttackState.Burrowed;
location = target.Actor.Location;
} }
bool WormAttack(Actor worm) bool AttackTargets(Actor self, IEnumerable<Actor> targets)
{ {
var targetLocation = target.Actor.Location; var targetLocation = target.Actor.Location;
foreach (var t in targets)
// The target has moved too far away
if ((location - targetLocation).Length > NearEnough)
return false;
var lunch = worm.World.ActorMap.GetActorsAt(targetLocation)
.Where(t => !t.Equals(worm) && weapon.IsValidAgainst(t, worm));
if (!lunch.Any())
return false;
stance = AttackState.EmergingAboveGround;
sandworm.IsAttacking = true;
foreach (var actor in lunch)
{ {
var actor1 = actor; // loop variable in closure hazard var target = t; // loop variable in closure hazard
actor.World.AddFrameEndTask(_ => self.World.AddFrameEndTask(_ =>
{
// Don't use Kill() because we don't want any of its side-effects (husks, etc)
target.Dispose();
// Harvester insurance
if (target.Info.HasTraitInfo<HarvesterInfo>())
{ {
actor1.Dispose(); var insurance = target.Owner.PlayerActor.TraitOrDefault<HarvesterInsurance>();
// Harvester insurance
if (!actor1.Info.HasTraitInfo<HarvesterInfo>())
return;
var insurance = actor1.Owner.PlayerActor.TraitOrDefault<HarvesterInsurance>();
if (insurance != null) if (insurance != null)
actor1.World.AddFrameEndTask(__ => insurance.TryActivate()); self.World.AddFrameEndTask(__ => insurance.TryActivate());
}); }
});
} }
positionable.SetPosition(worm, targetLocation); positionable.SetPosition(self, targetLocation);
var attackPosition = worm.CenterPosition; var attackPosition = self.CenterPosition;
var affectedPlayers = lunch.Select(x => x.Owner).Distinct().ToList(); var affectedPlayers = targets.Select(x => x.Owner).Distinct().ToList();
PlayAttack(worm, attackPosition, affectedPlayers);
foreach (var notify in worm.TraitsImplementing<INotifyAttack>())
notify.Attacking(worm, target, null, null);
return true;
}
// List because IEnumerable gets evaluated too late.
void PlayAttack(Actor self, WPos attackPosition, List<Player> affectedPlayers)
{
withSpriteBody.PlayCustomAnimation(self, sandworm.Info.MouthSequence);
Game.Sound.Play(swallow.Info.WormAttackSound, self.CenterPosition); Game.Sound.Play(swallow.Info.WormAttackSound, self.CenterPosition);
Game.RunAfterDelay(1000, () => Game.RunAfterDelay(1000, () =>
{ {
if (Game.IsCurrentWorld(self.World)) if (!Game.IsCurrentWorld(self.World))
foreach (var affectedPlayer in affectedPlayers) return;
NotifyPlayer(affectedPlayer, attackPosition);
foreach (var player in affectedPlayers)
{
Game.Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallow.Info.WormAttackNotification, player.Faction.InternalName);
if (player == player.World.RenderPlayer)
radarPings.Add(() => true, attackPosition, Color.Red, 50);
}
}); });
}
void NotifyPlayer(Player player, WPos location) foreach (var notify in self.TraitsImplementing<INotifyAttack>())
{ notify.Attacking(self, target, null, null);
Game.Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallow.Info.WormAttackNotification, player.Faction.InternalName);
if (player == player.World.RenderPlayer) return true;
radarPings.Add(() => true, location, Color.Red, 50);
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (countdown > 0) switch (stance)
{ {
countdown--; case AttackState.Uninitialized:
return this; GrantUpgrades(self);
} stance = AttackState.Burrowed;
countdown = swallow.Info.AttackDelay;
burrowLocation = self.Location;
break;
case AttackState.Burrowed:
if (--countdown > 0)
return this;
// Wait for the worm to get back underground var targetLocation = target.Actor.Location;
if (stance == AttackState.ReturningUnderground)
{
sandworm.IsAttacking = false;
// There is a chance that the worm would just go away after attacking // The target has moved too far away
if (self.World.SharedRandom.Next() % 100 <= sandworm.Info.ChanceToDisappear) if ((burrowLocation - targetLocation).Length > NearEnough)
{ {
self.CancelActivity(); RevokeUpgrades(self);
self.World.AddFrameEndTask(w => self.Kill(self)); return NextActivity;
} }
else
withSpriteBody.DefaultAnimation.ReplaceAnim(sandworm.Info.IdleSequence);
return NextActivity; // The target reached solid ground
} if (!positionable.CanEnterCell(targetLocation, null, false))
{
RevokeUpgrades(self);
return NextActivity;
}
// Wait for the worm to get in position var targets = self.World.ActorMap.GetActorsAt(targetLocation)
if (stance == AttackState.Burrowed) .Where(t => !t.Equals(self) && weapon.IsValidAgainst(t, self));
{
// This is so that the worm cancels an attack against a target that has reached solid rock if (!targets.Any())
if (!positionable.CanEnterCell(target.Actor.Location, null, false)) {
RevokeUpgrades(self);
return NextActivity;
}
stance = AttackState.Attacking;
countdown = swallow.Info.ReturnDelay;
sandworm.IsAttacking = true;
AttackTargets(self, targets);
break;
case AttackState.Attacking:
if (--countdown > 0)
return this;
sandworm.IsAttacking = false;
// There is a chance that the worm would just go away after attacking
if (self.World.SharedRandom.Next(100) <= sandworm.Info.ChanceToDisappear)
{
self.CancelActivity();
self.World.AddFrameEndTask(w => self.Dispose());
}
RevokeUpgrades(self);
return NextActivity; return NextActivity;
if (!WormAttack(self))
{
withSpriteBody.DefaultAnimation.ReplaceAnim(sandworm.Info.IdleSequence);
return NextActivity;
}
countdown = swallow.Info.ReturnTime;
stance = AttackState.ReturningUnderground;
} }
return this; return this;
} }
void GrantUpgrades(Actor self)
{
foreach (var up in swallow.Info.AttackingUpgrades)
manager.GrantUpgrade(self, up, this);
}
void RevokeUpgrades(Actor self)
{
foreach (var up in swallow.Info.AttackingUpgrades)
manager.RevokeUpgrade(self, up, this);
}
} }
} }

View File

@@ -20,10 +20,14 @@ namespace OpenRA.Mods.D2k.Traits
class AttackSwallowInfo : AttackFrontalInfo class AttackSwallowInfo : AttackFrontalInfo
{ {
[Desc("The number of ticks it takes to return underground.")] [Desc("The number of ticks it takes to return underground.")]
public readonly int ReturnTime = 60; public readonly int ReturnDelay = 60;
[Desc("The number of ticks it takes to get in place under the target to attack.")] [Desc("The number of ticks it takes to get in place under the target to attack.")]
public readonly int AttackTime = 30; public readonly int AttackDelay = 30;
[UpgradeGrantedReference]
[Desc("The upgrades to grant while attacking.")]
public readonly string[] AttackingUpgrades = { "attacking" };
public readonly string WormAttackSound = "Worm.wav"; public readonly string WormAttackSound = "Worm.wav";

View File

@@ -10,12 +10,13 @@
using System; using System;
using System.Linq; using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits namespace OpenRA.Mods.D2k.Traits
{ {
class SandwormInfo : WandersInfo, Requires<MobileInfo>, Requires<WithSpriteBodyInfo>, Requires<AttackBaseInfo> class SandwormInfo : WandersInfo, Requires<MobileInfo>, Requires<AttackBaseInfo>
{ {
[Desc("Time between rescanning for targets (in ticks).")] [Desc("Time between rescanning for targets (in ticks).")]
public readonly int TargetRescanInterval = 125; public readonly int TargetRescanInterval = 125;
@@ -29,15 +30,6 @@ namespace OpenRA.Mods.D2k.Traits
[Desc("The chance this actor has of disappearing after it attacks (in %).")] [Desc("The chance this actor has of disappearing after it attacks (in %).")]
public readonly int ChanceToDisappear = 100; public readonly int ChanceToDisappear = 100;
[Desc("Name of the sequence that is used when the actor is idle or moving (not attacking).")]
[SequenceReference] public readonly string IdleSequence = "idle";
[Desc("Name of the sequence that is used when the actor is attacking.")]
[SequenceReference] public readonly string MouthSequence = "mouth";
[Desc("Name of the sequence that is used when the actor is burrowed.")]
[SequenceReference] public readonly string BurrowedSequence = "burrowed";
public override object Create(ActorInitializer init) { return new Sandworm(init.Self, this); } public override object Create(ActorInitializer init) { return new Sandworm(init.Self, this); }
} }
@@ -47,7 +39,6 @@ namespace OpenRA.Mods.D2k.Traits
readonly WormManager manager; readonly WormManager manager;
readonly Mobile mobile; readonly Mobile mobile;
readonly WithSpriteBody withSpriteBody;
readonly AttackBase attackTrait; readonly AttackBase attackTrait;
public bool IsMovingTowardTarget { get; private set; } public bool IsMovingTowardTarget { get; private set; }
@@ -61,19 +52,10 @@ namespace OpenRA.Mods.D2k.Traits
{ {
Info = info; Info = info;
mobile = self.Trait<Mobile>(); mobile = self.Trait<Mobile>();
withSpriteBody = self.Trait<WithSpriteBody>();
attackTrait = self.Trait<AttackBase>(); attackTrait = self.Trait<AttackBase>();
manager = self.World.WorldActor.Trait<WormManager>(); manager = self.World.WorldActor.Trait<WormManager>();
} }
public override void OnBecomingIdle(Actor self)
{
if (withSpriteBody.DefaultAnimation.CurrentSequence.Name != Info.IdleSequence)
withSpriteBody.DefaultAnimation.PlayRepeating(Info.IdleSequence);
base.OnBecomingIdle(self);
}
public override void DoAction(Actor self, CPos targetCell) public override void DoAction(Actor self, CPos targetCell)
{ {
IsMovingTowardTarget = false; IsMovingTowardTarget = false;

BIN
mods/d2k/bits/sandtrail.shp Normal file

Binary file not shown.

View File

@@ -69,8 +69,20 @@ sandworm:
Spice: 100 Spice: 100
Targetable: Targetable:
TargetTypes: Ground TargetTypes: Ground
WithFacingSpriteBody: WithSpriteBody:
WithAttackOverlay: WithIdleAnimation:
Interval: 160
Sequences: lightninga, lightningb, lightningc, lightningd, lightninge, lightningf
UpgradeTypes: attacking
UpgradeMaxEnabledLevel: 0
AmbientSound:
SoundFile: WRMSIGN1.WAV
Interval: 160
UpgradeTypes: attacking
UpgradeMaxEnabledLevel: 0
WithAttackOverlay@mouth:
Sequence: mouth
WithAttackOverlay@sand:
Sequence: sand Sequence: sand
HiddenUnderFog: HiddenUnderFog:
AppearsOnRadar: AppearsOnRadar:
@@ -88,6 +100,16 @@ sandworm:
PingRadar: True PingRadar: True
RevealsShroud: RevealsShroud:
Range: 5c0 Range: 5c0
LeavesTrails:
Image: sandtrail
Sequences: traila, trailb, trailc
Palette: effect
Type: CenterPosition
TerrainTypes: Sand, Dune, SpiceSand, Spice
MovingInterval: 3
UpgradeTypes: attacking
UpgradeMaxEnabledLevel: 0
UpgradeManager:
sietch: sietch:
Inherits: ^Building Inherits: ^Building

View File

@@ -481,10 +481,35 @@ sandworm:
Length: 20 Length: 20
Tick: 100 Tick: 100
idle: DATA.R8 idle: DATA.R8
Start: 3586
Length: 35
Tick: 180
BlendMode: Additive
burrowed: DATA.R8
Start: 39 Start: 39
lightninga: DATA.R8
Start: 3591
Length: 5
Tick: 80
BlendMode: Additive
lightningb: DATA.R8
Start: 3596
Length: 5
Tick: 80
BlendMode: Additive
lightningc: DATA.R8
Start: 3601
Length: 5
Tick: 80
BlendMode: Additive
lightningd: DATA.R8
Start: 3606
Length: 5
Tick: 80
BlendMode: Additive
lightninge: DATA.R8
Start: 3611
Length: 5
Tick: 80
BlendMode: Additive
lightningf: DATA.R8
Start: 3616
Length: 5
Tick: 80
BlendMode: Additive
icon: wormicon.shp icon: wormicon.shp

View File

@@ -109,6 +109,17 @@ laserfire:
BlendMode: Additive BlendMode: Additive
ZOffset: 511 ZOffset: 511
sandtrail:
Defaults:
Length: 8
Tick: 200
ZOffset: -512
traila: sandtrail.shp
trailb: sandtrail.shp
Frames: 2, 6, 4, 5, 0, 1, 3, 7
trailc: sandtrail.shp
Frames: 7, 4, 6, 5, 2, 0, 3, 1
pips: pips:
groups: DATA.R8 groups: DATA.R8
Start: 17 Start: 17