Simplify and fix code quality in GpsDot.
This commit is contained in:
@@ -1,149 +0,0 @@
|
|||||||
#region Copyright & License Information
|
|
||||||
/*
|
|
||||||
* Copyright 2007-2017 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, either version 3 of
|
|
||||||
* the License, or (at your option) any later version. For more
|
|
||||||
* information, see COPYING.
|
|
||||||
*/
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using OpenRA.Effects;
|
|
||||||
using OpenRA.Graphics;
|
|
||||||
using OpenRA.Mods.Cnc.Traits;
|
|
||||||
using OpenRA.Mods.Common.Traits;
|
|
||||||
using OpenRA.Primitives;
|
|
||||||
using OpenRA.Traits;
|
|
||||||
|
|
||||||
namespace OpenRA.Mods.Cnc.Effects
|
|
||||||
{
|
|
||||||
[Desc("Attach this to actors to render pictograms while hidden.")]
|
|
||||||
class GpsDotInfo : ITraitInfo
|
|
||||||
{
|
|
||||||
[Desc("Sprite collection for symbols.")]
|
|
||||||
public readonly string Image = "gpsdot";
|
|
||||||
|
|
||||||
[Desc("Sprite used for this actor.")]
|
|
||||||
[SequenceReference("Image")] public readonly string String = "Infantry";
|
|
||||||
|
|
||||||
[PaletteReference(true)] public readonly string IndicatorPalettePrefix = "player";
|
|
||||||
|
|
||||||
public object Create(ActorInitializer init)
|
|
||||||
{
|
|
||||||
return new GpsDot(init.Self, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GpsDot : IEffect, IEffectAboveShroud
|
|
||||||
{
|
|
||||||
readonly Actor self;
|
|
||||||
readonly GpsDotInfo info;
|
|
||||||
readonly Animation anim;
|
|
||||||
|
|
||||||
readonly PlayerDictionary<DotState> dotStates;
|
|
||||||
readonly Lazy<HiddenUnderFog> huf;
|
|
||||||
readonly Lazy<FrozenUnderFog> fuf;
|
|
||||||
readonly Lazy<Disguise> disguise;
|
|
||||||
readonly Lazy<Cloak> cloak;
|
|
||||||
readonly Cache<Player, FrozenActorLayer> frozen;
|
|
||||||
|
|
||||||
class DotState
|
|
||||||
{
|
|
||||||
public readonly GpsWatcher Gps;
|
|
||||||
public bool IsTargetable;
|
|
||||||
public bool ShouldRender;
|
|
||||||
public DotState(GpsWatcher gps)
|
|
||||||
{
|
|
||||||
Gps = gps;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public GpsDot(Actor self, GpsDotInfo info)
|
|
||||||
{
|
|
||||||
this.self = self;
|
|
||||||
this.info = info;
|
|
||||||
anim = new Animation(self.World, info.Image);
|
|
||||||
anim.PlayRepeating(info.String);
|
|
||||||
|
|
||||||
self.World.AddFrameEndTask(w => w.Add(this));
|
|
||||||
|
|
||||||
huf = Exts.Lazy(() => self.TraitOrDefault<HiddenUnderFog>());
|
|
||||||
fuf = Exts.Lazy(() => self.TraitOrDefault<FrozenUnderFog>());
|
|
||||||
disguise = Exts.Lazy(() => self.TraitOrDefault<Disguise>());
|
|
||||||
cloak = Exts.Lazy(() => self.TraitOrDefault<Cloak>());
|
|
||||||
|
|
||||||
frozen = new Cache<Player, FrozenActorLayer>(p => p.PlayerActor.Trait<FrozenActorLayer>());
|
|
||||||
dotStates = new PlayerDictionary<DotState>(self.World, player => new DotState(player.PlayerActor.Trait<GpsWatcher>()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsDotVisible(Player toPlayer)
|
|
||||||
{
|
|
||||||
return dotStates[toPlayer].IsTargetable;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsTargetableBy(Player toPlayer, out bool shouldRenderIndicator)
|
|
||||||
{
|
|
||||||
shouldRenderIndicator = false;
|
|
||||||
|
|
||||||
if (cloak.Value != null && cloak.Value.Cloaked)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (disguise.Value != null && disguise.Value.Disguised)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (huf.Value != null && !huf.Value.IsVisible(self, toPlayer)
|
|
||||||
&& toPlayer.Shroud.IsExplored(self.CenterPosition))
|
|
||||||
{
|
|
||||||
var f1 = FrozenActorForPlayer(toPlayer);
|
|
||||||
shouldRenderIndicator = f1 == null || !f1.HasRenderables;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fuf.Value == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var f2 = FrozenActorForPlayer(toPlayer);
|
|
||||||
if (f2 == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
shouldRenderIndicator = !f2.HasRenderables;
|
|
||||||
|
|
||||||
return f2.Visible && !f2.Shrouded && toPlayer.Shroud.IsExplored(self.CenterPosition);
|
|
||||||
}
|
|
||||||
|
|
||||||
FrozenActor FrozenActorForPlayer(Player player)
|
|
||||||
{
|
|
||||||
return frozen[player].FromID(self.ActorID);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IEffect.Tick(World world)
|
|
||||||
{
|
|
||||||
if (self.Disposed)
|
|
||||||
world.AddFrameEndTask(w => w.Remove(this));
|
|
||||||
|
|
||||||
for (var playerIndex = 0; playerIndex < dotStates.Count; playerIndex++)
|
|
||||||
{
|
|
||||||
var state = dotStates[playerIndex];
|
|
||||||
var shouldRender = false;
|
|
||||||
if (self.IsInWorld && !self.IsDead)
|
|
||||||
state.IsTargetable = (state.Gps.Granted || state.Gps.GrantedAllies) && IsTargetableBy(world.Players[playerIndex], out shouldRender);
|
|
||||||
|
|
||||||
state.ShouldRender = state.IsTargetable && shouldRender;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerable<IRenderable> IEffect.Render(WorldRenderer wr) { return SpriteRenderable.None; }
|
|
||||||
|
|
||||||
IEnumerable<IRenderable> IEffectAboveShroud.RenderAboveShroud(WorldRenderer wr)
|
|
||||||
{
|
|
||||||
if (self.World.RenderPlayer == null || !dotStates[self.World.RenderPlayer].ShouldRender || self.Disposed)
|
|
||||||
return SpriteRenderable.None;
|
|
||||||
|
|
||||||
var palette = wr.Palette(info.IndicatorPalettePrefix + self.Owner.InternalName);
|
|
||||||
return anim.Render(self.CenterPosition, palette);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
112
OpenRA.Mods.Cnc/Effects/GpsDotEffect.cs
Normal file
112
OpenRA.Mods.Cnc/Effects/GpsDotEffect.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2017 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, either version 3 of
|
||||||
|
* the License, or (at your option) any later version. For more
|
||||||
|
* information, see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using OpenRA.Effects;
|
||||||
|
using OpenRA.Graphics;
|
||||||
|
using OpenRA.Mods.Cnc.Traits;
|
||||||
|
using OpenRA.Primitives;
|
||||||
|
using OpenRA.Traits;
|
||||||
|
|
||||||
|
namespace OpenRA.Mods.Cnc.Effects
|
||||||
|
{
|
||||||
|
class GpsDotEffect : IEffect, IEffectAboveShroud
|
||||||
|
{
|
||||||
|
readonly Actor actor;
|
||||||
|
readonly GpsDotInfo info;
|
||||||
|
readonly Animation anim;
|
||||||
|
|
||||||
|
readonly PlayerDictionary<DotState> dotStates;
|
||||||
|
readonly IDefaultVisibility visibility;
|
||||||
|
readonly IVisibilityModifier[] visibilityModifiers;
|
||||||
|
|
||||||
|
class DotState
|
||||||
|
{
|
||||||
|
public readonly GpsWatcher Watcher;
|
||||||
|
public readonly FrozenActor FrozenActor;
|
||||||
|
public bool Visible;
|
||||||
|
public DotState(Actor a, GpsWatcher watcher, FrozenActorLayer frozenLayer)
|
||||||
|
{
|
||||||
|
Watcher = watcher;
|
||||||
|
if (frozenLayer != null)
|
||||||
|
FrozenActor = frozenLayer.FromID(a.ActorID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GpsDotEffect(Actor actor, GpsDotInfo info)
|
||||||
|
{
|
||||||
|
this.actor = actor;
|
||||||
|
this.info = info;
|
||||||
|
anim = new Animation(actor.World, info.Image);
|
||||||
|
anim.PlayRepeating(info.String);
|
||||||
|
|
||||||
|
visibility = actor.Trait<IDefaultVisibility>();
|
||||||
|
visibilityModifiers = actor.TraitsImplementing<IVisibilityModifier>().ToArray();
|
||||||
|
|
||||||
|
dotStates = new PlayerDictionary<DotState>(actor.World,
|
||||||
|
p => new DotState(actor, p.PlayerActor.Trait<GpsWatcher>(), p.PlayerActor.TraitOrDefault<FrozenActorLayer>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShouldRender(DotState state, Player toPlayer)
|
||||||
|
{
|
||||||
|
// Hide the indicator if no watchers are available
|
||||||
|
if (!state.Watcher.Granted && !state.Watcher.GrantedAllies)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Hide the indicator if a frozen actor portrait is visible
|
||||||
|
if (state.FrozenActor != null && state.FrozenActor.HasRenderables)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Hide the indicator if the unit appears to be owned by an allied player
|
||||||
|
if (actor.EffectiveOwner != null && actor.EffectiveOwner.Owner != null &&
|
||||||
|
toPlayer.IsAlliedWith(actor.EffectiveOwner.Owner))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Hide indicator if the actor wouldn't otherwise be visible if there wasn't fog
|
||||||
|
foreach (var visibilityModifier in visibilityModifiers)
|
||||||
|
if (!visibilityModifier.IsVisible(actor, toPlayer))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Hide the indicator behind shroud
|
||||||
|
if (!toPlayer.Shroud.IsExplored(actor.CenterPosition))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return !visibility.IsVisible(actor, toPlayer) && toPlayer.Shroud.IsExplored(actor.CenterPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IEffect.Tick(World world)
|
||||||
|
{
|
||||||
|
for (var playerIndex = 0; playerIndex < dotStates.Count; playerIndex++)
|
||||||
|
{
|
||||||
|
var state = dotStates[playerIndex];
|
||||||
|
state.Visible = ShouldRender(state, world.Players[playerIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<IRenderable> IEffect.Render(WorldRenderer wr)
|
||||||
|
{
|
||||||
|
return SpriteRenderable.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<IRenderable> IEffectAboveShroud.RenderAboveShroud(WorldRenderer wr)
|
||||||
|
{
|
||||||
|
if (actor.World.RenderPlayer == null || !dotStates[actor.World.RenderPlayer].Visible)
|
||||||
|
return SpriteRenderable.None;
|
||||||
|
|
||||||
|
var effectiveOwner = actor.EffectiveOwner != null && actor.EffectiveOwner.Owner != null ?
|
||||||
|
actor.EffectiveOwner.Owner : actor.Owner;
|
||||||
|
|
||||||
|
var palette = wr.Palette(info.IndicatorPalettePrefix + effectiveOwner.InternalName);
|
||||||
|
return anim.Render(actor.CenterPosition, palette);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -100,7 +100,7 @@
|
|||||||
<Compile Include="Activities\LayMines.cs" />
|
<Compile Include="Activities\LayMines.cs" />
|
||||||
<Compile Include="Activities\Leap.cs" />
|
<Compile Include="Activities\Leap.cs" />
|
||||||
<Compile Include="Activities\Teleport.cs" />
|
<Compile Include="Activities\Teleport.cs" />
|
||||||
<Compile Include="Effects\GpsDot.cs" />
|
<Compile Include="Effects\GpsDotEffect.cs" />
|
||||||
<Compile Include="Effects\GpsSatellite.cs" />
|
<Compile Include="Effects\GpsSatellite.cs" />
|
||||||
<Compile Include="Effects\SatelliteLaunch.cs" />
|
<Compile Include="Effects\SatelliteLaunch.cs" />
|
||||||
<Compile Include="Projectiles\TeslaZap.cs" />
|
<Compile Include="Projectiles\TeslaZap.cs" />
|
||||||
@@ -158,6 +158,7 @@
|
|||||||
<Compile Include="Traits\World\VoxelNormalsPalette.cs" />
|
<Compile Include="Traits\World\VoxelNormalsPalette.cs" />
|
||||||
<Compile Include="Traits\TDGunboat.cs" />
|
<Compile Include="Traits\TDGunboat.cs" />
|
||||||
<Compile Include="Traits\Attack\AttackTDGunboatTurreted.cs" />
|
<Compile Include="Traits\Attack\AttackTDGunboatTurreted.cs" />
|
||||||
|
<Compile Include="Traits\GpsDot.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
|
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
|
||||||
|
|||||||
53
OpenRA.Mods.Cnc/Traits/GpsDot.cs
Normal file
53
OpenRA.Mods.Cnc/Traits/GpsDot.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2017 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, either version 3 of
|
||||||
|
* the License, or (at your option) any later version. For more
|
||||||
|
* information, see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using OpenRA.Graphics;
|
||||||
|
using OpenRA.Mods.Cnc.Effects;
|
||||||
|
using OpenRA.Traits;
|
||||||
|
|
||||||
|
namespace OpenRA.Mods.Cnc.Traits
|
||||||
|
{
|
||||||
|
[Desc("Show an indicator revealing the actor underneath the fog when a GPSWatcher is activated.")]
|
||||||
|
class GpsDotInfo : ITraitInfo
|
||||||
|
{
|
||||||
|
[Desc("Sprite collection for symbols.")]
|
||||||
|
public readonly string Image = "gpsdot";
|
||||||
|
|
||||||
|
[Desc("Sprite used for this actor.")]
|
||||||
|
[SequenceReference("Image")] public readonly string String = "Infantry";
|
||||||
|
|
||||||
|
[PaletteReference(true)] public readonly string IndicatorPalettePrefix = "player";
|
||||||
|
|
||||||
|
public object Create(ActorInitializer init) { return new GpsDot(this); }
|
||||||
|
}
|
||||||
|
|
||||||
|
class GpsDot : INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||||
|
{
|
||||||
|
readonly GpsDotInfo info;
|
||||||
|
GpsDotEffect effect;
|
||||||
|
|
||||||
|
public GpsDot(GpsDotInfo info)
|
||||||
|
{
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
|
||||||
|
void INotifyAddedToWorld.AddedToWorld(Actor self)
|
||||||
|
{
|
||||||
|
effect = new GpsDotEffect(self, info);
|
||||||
|
self.World.AddFrameEndTask(w => w.Add(effect));
|
||||||
|
}
|
||||||
|
|
||||||
|
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||||
|
{
|
||||||
|
self.World.AddFrameEndTask(w => w.Remove(effect));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user