Add IBlocksProjectiles interface to avoid using BlocksProjectiles directly

This commit is contained in:
teees
2015-11-28 21:34:12 +01:00
parent 7f2feb29cd
commit 3d597c7880
2 changed files with 27 additions and 10 deletions

View File

@@ -9,9 +9,16 @@
#endregion #endregion
using System.Linq; using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[RequireExplicitImplementation]
public interface IBlocksProjectiles
{
WDist BlockingHeight { get; }
}
[Desc("This actor blocks bullets and missiles with 'Blockable' property.")] [Desc("This actor blocks bullets and missiles with 'Blockable' property.")]
public class BlocksProjectilesInfo : UpgradableTraitInfo public class BlocksProjectilesInfo : UpgradableTraitInfo
{ {
@@ -20,17 +27,20 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new BlocksProjectiles(init.Self, this); } public override object Create(ActorInitializer init) { return new BlocksProjectiles(init.Self, this); }
} }
public class BlocksProjectiles : UpgradableTrait<BlocksProjectilesInfo> public class BlocksProjectiles : UpgradableTrait<BlocksProjectilesInfo>, IBlocksProjectiles
{ {
public BlocksProjectiles(Actor self, BlocksProjectilesInfo info) public BlocksProjectiles(Actor self, BlocksProjectilesInfo info)
: base(info) { } : base(info) { }
WDist IBlocksProjectiles.BlockingHeight { get { return Info.Height; } }
public static bool AnyBlockingActorAt(World world, WPos pos) public static bool AnyBlockingActorAt(World world, WPos pos)
{ {
var dat = world.Map.DistanceAboveTerrain(pos); var dat = world.Map.DistanceAboveTerrain(pos);
return world.ActorMap.GetActorsAt(world.Map.CellContaining(pos)) return world.ActorMap.GetActorsAt(world.Map.CellContaining(pos))
.Any(a => a.TraitsImplementing<BlocksProjectiles>() .Any(a => a.TraitsImplementing<IBlocksProjectiles>()
.Where(t => t.Info.Height.Length >= dat.Length) .Where(t => t.BlockingHeight >= dat)
.Any(Exts.IsTraitEnabled)); .Any(Exts.IsTraitEnabled));
} }
@@ -41,7 +51,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var a in actors) foreach (var a in actors)
{ {
var blockers = a.TraitsImplementing<BlocksProjectiles>() var blockers = a.TraitsImplementing<IBlocksProjectiles>()
.Where(Exts.IsTraitEnabled).ToList(); .Where(Exts.IsTraitEnabled).ToList();
if (!blockers.Any()) if (!blockers.Any())
@@ -49,7 +59,7 @@ namespace OpenRA.Mods.Common.Traits
var hitPos = WorldExtensions.MinimumPointLineProjection(start, end, a.CenterPosition); var hitPos = WorldExtensions.MinimumPointLineProjection(start, end, a.CenterPosition);
var dat = world.Map.DistanceAboveTerrain(hitPos); var dat = world.Map.DistanceAboveTerrain(hitPos);
if ((hitPos - start).Length < length && blockers.Any(t => t.Info.Height.Length >= dat.Length)) if ((hitPos - start).Length < length && blockers.Any(t => t.BlockingHeight >= dat))
{ {
hit = hitPos; hit = hitPos;
return true; return true;

View File

@@ -9,7 +9,9 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Effects; using OpenRA.Mods.Common.Effects;
using OpenRA.Traits; using OpenRA.Traits;
@@ -22,19 +24,18 @@ namespace OpenRA.Mods.Common.Traits
public object Create(ActorInitializer init) { return new CombatDebugOverlay(init.Self); } public object Create(ActorInitializer init) { return new CombatDebugOverlay(init.Self); }
} }
public class CombatDebugOverlay : IPostRender, INotifyDamage public class CombatDebugOverlay : IPostRender, INotifyDamage, INotifyCreated
{ {
readonly DeveloperMode devMode; readonly DeveloperMode devMode;
readonly HealthInfo healthInfo; readonly HealthInfo healthInfo;
readonly BlocksProjectilesInfo blockInfo; IBlocksProjectiles[] allBlockers;
Lazy<AttackBase> attack; Lazy<AttackBase> attack;
Lazy<BodyOrientation> coords; Lazy<BodyOrientation> coords;
public CombatDebugOverlay(Actor self) public CombatDebugOverlay(Actor self)
{ {
healthInfo = self.Info.TraitInfoOrDefault<HealthInfo>(); healthInfo = self.Info.TraitInfoOrDefault<HealthInfo>();
blockInfo = self.Info.TraitInfoOrDefault<BlocksProjectilesInfo>();
attack = Exts.Lazy(() => self.TraitOrDefault<AttackBase>()); attack = Exts.Lazy(() => self.TraitOrDefault<AttackBase>());
coords = Exts.Lazy(() => self.Trait<BodyOrientation>()); coords = Exts.Lazy(() => self.Trait<BodyOrientation>());
@@ -42,6 +43,11 @@ namespace OpenRA.Mods.Common.Traits
devMode = localPlayer != null ? localPlayer.PlayerActor.Trait<DeveloperMode>() : null; devMode = localPlayer != null ? localPlayer.PlayerActor.Trait<DeveloperMode>() : null;
} }
public void Created(Actor self)
{
allBlockers = self.TraitsImplementing<IBlocksProjectiles>().ToArray();
}
public void RenderAfterWorld(WorldRenderer wr, Actor self) public void RenderAfterWorld(WorldRenderer wr, Actor self)
{ {
if (devMode == null || !devMode.ShowCombatGeometry) if (devMode == null || !devMode.ShowCombatGeometry)
@@ -53,10 +59,11 @@ namespace OpenRA.Mods.Common.Traits
if (healthInfo != null) if (healthInfo != null)
healthInfo.Shape.DrawCombatOverlay(wr, wcr, self); healthInfo.Shape.DrawCombatOverlay(wr, wcr, self);
if (blockInfo != null) var blockers = allBlockers.Where(Exts.IsTraitEnabled).ToList();
if (blockers.Count > 0)
{ {
var hc = Color.Orange; var hc = Color.Orange;
var height = new WVec(0, 0, blockInfo.Height.Length); var height = new WVec(0, 0, blockers.Max(b => b.BlockingHeight.Length));
var ha = wr.ScreenPosition(self.CenterPosition); var ha = wr.ScreenPosition(self.CenterPosition);
var hb = wr.ScreenPosition(self.CenterPosition + height); var hb = wr.ScreenPosition(self.CenterPosition + height);
wcr.DrawLine(ha, hb, iz, hc); wcr.DrawLine(ha, hb, iz, hc);