Move ScaredyCat, TakeCover, RenderInfantry, WithBuildingExplosion and SpawnMPUnits as well as Hunt activity to Mods.Common.

This commit is contained in:
reaperrr
2015-01-04 16:41:37 +01:00
parent d07db9c6a9
commit b7a3b9fdbf
9 changed files with 13 additions and 20 deletions

View File

@@ -0,0 +1,91 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Makes the unit automatically run around when taking damage.")]
class ScaredyCatInfo : ITraitInfo
{
[Desc("How long (in ticks) the actor should panic for.")]
public readonly int PanicLength = 25 * 10;
[Desc("Panic movement speed as a precentage of the normal speed.")]
public readonly int PanicSpeedModifier = 200;
[Desc("Chance (out of 100) the unit has to enter panic mode when attacked.")]
public readonly int AttackPanicChance = 20;
public object Create(ActorInitializer init) { return new ScaredyCat(init.Self, this); }
}
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
{
readonly ScaredyCatInfo info;
[Sync] readonly Actor self;
[Sync] int panicStartedTick;
bool Panicking { get { return panicStartedTick > 0; } }
public bool IsModifyingSequence { get { return Panicking; } }
public string SequencePrefix { get { return "panic-"; } }
public ScaredyCat(Actor self, ScaredyCatInfo info)
{
this.self = self;
this.info = info;
}
public void Panic()
{
if (!Panicking)
self.CancelActivity();
panicStartedTick = self.World.WorldTick;
}
public void Tick(Actor self)
{
if (!Panicking)
return;
if (self.World.WorldTick >= panicStartedTick + info.PanicLength)
{
self.CancelActivity();
panicStartedTick = 0;
}
}
public void TickIdle(Actor self)
{
if (!Panicking)
return;
self.Trait<Mobile>().Nudge(self, self, true);
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.Damage > 0)
Panic();
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (self.World.SharedRandom.Next(100 / info.AttackPanicChance) == 0)
Panic();
}
public int GetSpeedModifier()
{
return Panicking ? info.PanicSpeedModifier : 100;
}
}
}

View File

@@ -0,0 +1,77 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Make the unit go prone when under attack, in an attempt to reduce damage.")]
public class TakeCoverInfo : TurretedInfo
{
[Desc("How long (in ticks) the actor remains prone.")]
public readonly int ProneTime = 100;
[Desc("Prone movement speed as a percentage of the normal speed.")]
public readonly int SpeedModifier = 50;
public readonly WVec ProneOffset = new WVec(85, 0, -171);
public readonly string ProneSequencePrefix = "prone-";
public override object Create(ActorInitializer init) { return new TakeCover(init, this); }
}
public class TakeCover : Turreted, INotifyDamage, IDamageModifier, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
{
readonly TakeCoverInfo info;
[Sync] int remainingProneTime = 0;
bool IsProne { get { return remainingProneTime > 0; } }
public bool IsModifyingSequence { get { return IsProne; } }
public string SequencePrefix { get { return info.ProneSequencePrefix; } }
public TakeCover(ActorInitializer init, TakeCoverInfo info)
: base(init, info)
{
this.info = info;
}
public void Damaged(Actor self, AttackInfo e)
{
/* Don't go prone when healed */
if (e.Damage > 0 && (e.Warhead == null || !e.Warhead.PreventProne))
{
if (!IsProne)
localOffset = info.ProneOffset;
remainingProneTime = info.ProneTime;
}
}
public override void Tick(Actor self)
{
base.Tick(self);
if (IsProne && --remainingProneTime == 0)
localOffset = WVec.Zero;
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
{
return IsProne && warhead != null ? warhead.ProneModifier : 100;
}
public int GetSpeedModifier()
{
return IsProne ? info.SpeedModifier : 100;
}
}
}

View File

@@ -0,0 +1,168 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class RenderInfantryInfo : RenderSimpleInfo, Requires<IMoveInfo>
{
public readonly int MinIdleWaitTicks = 30;
public readonly int MaxIdleWaitTicks = 110;
public readonly string MoveAnimation = "run";
public readonly string AttackAnimation = "shoot";
public readonly string[] IdleAnimations = { };
public readonly string[] StandAnimations = { "stand" };
public override object Create(ActorInitializer init) { return new RenderInfantry(init.Self, this); }
public override IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
{
var facing = 0;
var ifacing = init.Actor.Traits.GetOrDefault<IFacingInfo>();
if (ifacing != null)
facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : ifacing.GetInitialFacing();
var anim = new Animation(init.World, image, () => facing);
anim.PlayRepeating(StandAnimations.First());
yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
}
public override int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai)
{
return sequenceProvider.GetSequence(RenderSprites.GetImage(ai), StandAnimations.First()).Facings;
}
}
public class RenderInfantry : RenderSimple, INotifyAttack, INotifyIdle
{
readonly RenderInfantryInfo info;
readonly IMove move;
bool dirty = false;
string idleSequence;
int idleDelay;
AnimationState state;
IRenderInfantrySequenceModifier rsm;
bool IsModifyingSequence { get { return rsm != null && rsm.IsModifyingSequence; } }
bool wasModifying;
public RenderInfantry(Actor self, RenderInfantryInfo info)
: base(self, MakeFacingFunc(self))
{
this.info = info;
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
state = AnimationState.Waiting;
move = self.Trait<IMove>();
rsm = self.TraitOrDefault<IRenderInfantrySequenceModifier>();
}
protected virtual string NormalizeInfantrySequence(Actor self, string baseSequence)
{
var prefix = IsModifyingSequence ? rsm.SequencePrefix : "";
if (DefaultAnimation.HasSequence(prefix + baseSequence))
return prefix + baseSequence;
else
return baseSequence;
}
protected virtual bool AllowIdleAnimation(Actor self)
{
return !IsModifyingSequence;
}
public void Attacking(Actor self, Target target)
{
state = AnimationState.Attacking;
if (DefaultAnimation.HasSequence(NormalizeInfantrySequence(self, info.AttackAnimation)))
DefaultAnimation.PlayThen(NormalizeInfantrySequence(self, info.AttackAnimation), () => state = AnimationState.Idle);
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
Attacking(self, target);
}
public override void Tick(Actor self)
{
base.Tick(self);
if (rsm != null)
{
if (wasModifying != rsm.IsModifyingSequence)
dirty = true;
wasModifying = rsm.IsModifyingSequence;
}
if ((state == AnimationState.Moving || dirty) && !move.IsMoving)
{
state = AnimationState.Waiting;
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
}
else if ((state != AnimationState.Moving || dirty) && move.IsMoving)
{
state = AnimationState.Moving;
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.MoveAnimation));
}
dirty = false;
}
public void TickIdle(Actor self)
{
if (state != AnimationState.Idle && state != AnimationState.IdleAnimating)
{
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
state = AnimationState.Idle;
if (info.IdleAnimations.Length > 0)
{
idleSequence = info.IdleAnimations.Random(self.World.SharedRandom);
idleDelay = self.World.SharedRandom.Next(info.MinIdleWaitTicks, info.MaxIdleWaitTicks);
}
}
else if (AllowIdleAnimation(self))
{
if (idleSequence != null && DefaultAnimation.HasSequence(idleSequence))
{
if (idleDelay > 0 && --idleDelay == 0)
{
state = AnimationState.IdleAnimating;
DefaultAnimation.PlayThen(idleSequence, () =>
{
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)));
state = AnimationState.Waiting;
});
}
}
else
{
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)));
state = AnimationState.Waiting;
}
}
}
enum AnimationState
{
Idle,
Attacking,
Moving,
Waiting,
IdleAnimating
}
}
}

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Display explosions over the building footprint when it is destroyed.")]
class WithBuildingExplosionInfo : ITraitInfo, Requires<BuildingInfo>
{
[Desc("Explosion sequence name to use")]
public readonly string Sequence = "building";
[Desc("Custom palette name")]
public readonly string Palette = "effect";
public object Create(ActorInitializer init) { return new WithBuildingExplosion(this); }
}
class WithBuildingExplosion : INotifyKilled
{
WithBuildingExplosionInfo info;
public WithBuildingExplosion(WithBuildingExplosionInfo info)
{
this.info = info;
}
public void Killed(Actor self, AttackInfo e)
{
var buildingInfo = self.Info.Traits.Get<BuildingInfo>();
FootprintUtils.UnpathableTiles(self.Info.Name, buildingInfo, self.Location).Do(
t => self.World.AddFrameEndTask(w => w.Add(new Explosion(w, w.Map.CenterOfCell(t), info.Sequence, info.Palette))));
}
}
}

View File

@@ -0,0 +1,75 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawn base actor at the spawnpoint and support units in an annulus around the base actor. Both are defined at MPStartUnits. Attach this to the world actor.")]
public class SpawnMPUnitsInfo : TraitInfo<SpawnMPUnits>, Requires<MPStartLocationsInfo>, Requires<MPStartUnitsInfo> { }
public class SpawnMPUnits : IWorldLoaded
{
public void WorldLoaded(World world, WorldRenderer wr)
{
foreach (var s in world.WorldActor.Trait<MPStartLocations>().Start)
SpawnUnitsForPlayer(world, s.Key, s.Value);
}
static void SpawnUnitsForPlayer(World w, Player p, CPos sp)
{
var spawnClass = p.PlayerReference.StartingUnitsClass ?? w.LobbyInfo.GlobalSettings.StartingUnitsClass;
var unitGroup = w.Map.Rules.Actors["world"].Traits.WithInterface<MPStartUnitsInfo>()
.Where(g => g.Class == spawnClass && g.Races != null && g.Races.Contains(p.Country.Race))
.RandomOrDefault(w.SharedRandom);
if (unitGroup == null)
throw new InvalidOperationException("No starting units defined for country {0} with class {1}".F(p.Country.Race, spawnClass));
if (unitGroup.BaseActor != null)
{
w.CreateActor(unitGroup.BaseActor.ToLowerInvariant(), new TypeDictionary
{
new LocationInit(sp),
new OwnerInit(p),
new SkipMakeAnimsInit(),
});
}
if (!unitGroup.SupportActors.Any())
return;
var supportSpawnCells = w.Map.FindTilesInAnnulus(sp, unitGroup.InnerSupportRadius + 1, unitGroup.OuterSupportRadius);
foreach (var s in unitGroup.SupportActors)
{
var mi = w.Map.Rules.Actors[s.ToLowerInvariant()].Traits.Get<MobileInfo>();
var validCells = supportSpawnCells.Where(c => mi.CanEnterCell(w, null, c));
if (!validCells.Any())
throw new InvalidOperationException("No cells available to spawn starting unit {0}".F(s));
var cell = validCells.Random(w.SharedRandom);
var subCell = mi.SharesCell ? w.ActorMap.FreeSubCell(cell) : 0;
w.CreateActor(s.ToLowerInvariant(), new TypeDictionary
{
new OwnerInit(p),
new LocationInit(cell),
new SubCellInit(subCell),
new FacingInit(w.SharedRandom.Next(256))
});
}
}
}
}