Merge pull request #10587 from Mailaender/gps-refactor

Cleaned up the GPS code a bit
This commit is contained in:
RoosterDragon
2016-02-04 18:09:25 +00:00
6 changed files with 156 additions and 109 deletions

View File

@@ -19,10 +19,16 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Effects namespace OpenRA.Mods.RA.Effects
{ {
[Desc("Attach this to actors to render pictograms while hidden.")]
class GpsDotInfo : ITraitInfo class GpsDotInfo : ITraitInfo
{ {
public readonly string String = "Infantry"; [Desc("Sprite collection for symbols.")]
public readonly string IndicatorPalettePrefix = "player"; 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) public object Create(ActorInitializer init)
{ {
@@ -58,7 +64,7 @@ namespace OpenRA.Mods.RA.Effects
{ {
this.self = self; this.self = self;
this.info = info; this.info = info;
anim = new Animation(self.World, "gpsdot"); anim = new Animation(self.World, info.Image);
anim.PlayRepeating(info.String); anim.PlayRepeating(info.String);
self.World.AddFrameEndTask(w => w.Add(this)); self.World.AddFrameEndTask(w => w.Add(this));

View File

@@ -11,20 +11,23 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Effects; using OpenRA.Effects;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.RA.Traits;
namespace OpenRA.Mods.RA.Effects namespace OpenRA.Mods.RA.Effects
{ {
class GpsSatellite : IEffect class GpsSatellite : IEffect
{ {
readonly GpsPowerInfo info;
readonly Animation anim; readonly Animation anim;
WPos pos; WPos pos;
public GpsSatellite(World world, WPos pos) public GpsSatellite(World world, WPos pos, GpsPowerInfo info)
{ {
this.info = info;
this.pos = pos; this.pos = pos;
anim = new Animation(world, "sputnik"); anim = new Animation(world, info.SatelliteImage);
anim.PlayRepeating("idle"); anim.PlayRepeating(info.SatelliteSequence);
} }
public void Tick(World world) public void Tick(World world)
@@ -38,7 +41,7 @@ namespace OpenRA.Mods.RA.Effects
public IEnumerable<IRenderable> Render(WorldRenderer wr) public IEnumerable<IRenderable> Render(WorldRenderer wr)
{ {
return anim.Render(pos, wr.Palette("effect")); return anim.Render(pos, wr.Palette(info.SatellitePalette));
} }
} }
} }

View File

@@ -11,20 +11,23 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Effects; using OpenRA.Effects;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.RA.Traits;
namespace OpenRA.Mods.RA.Effects namespace OpenRA.Mods.RA.Effects
{ {
class SatelliteLaunch : IEffect class SatelliteLaunch : IEffect
{ {
readonly GpsPowerInfo info;
readonly Animation doors; readonly Animation doors;
readonly WPos pos; readonly WPos pos;
int frame = 0; int frame = 0;
public SatelliteLaunch(Actor a) public SatelliteLaunch(Actor a, GpsPowerInfo info)
{ {
doors = new Animation(a.World, "atek"); this.info = info;
doors.PlayThen("active", doors = new Animation(a.World, info.DoorImage);
doors.PlayThen(info.DoorSequence,
() => a.World.AddFrameEndTask(w => w.Remove(this))); () => a.World.AddFrameEndTask(w => w.Remove(this)));
pos = a.CenterPosition; pos = a.CenterPosition;
@@ -35,12 +38,12 @@ namespace OpenRA.Mods.RA.Effects
doors.Tick(); doors.Tick();
if (++frame == 19) if (++frame == 19)
world.AddFrameEndTask(w => w.Add(new GpsSatellite(world, pos))); world.AddFrameEndTask(w => w.Add(new GpsSatellite(world, pos, info)));
} }
public IEnumerable<IRenderable> Render(WorldRenderer wr) public IEnumerable<IRenderable> Render(WorldRenderer wr)
{ {
return doors.Render(pos, wr.Palette("effect")); return doors.Render(pos, wr.Palette(info.DoorPalette));
} }
} }
} }

View File

@@ -108,6 +108,7 @@
<Compile Include="Traits\Render\RenderShroudCircle.cs" /> <Compile Include="Traits\Render\RenderShroudCircle.cs" />
<Compile Include="Traits\SupportPowers\ChronoshiftPower.cs" /> <Compile Include="Traits\SupportPowers\ChronoshiftPower.cs" />
<Compile Include="Traits\SupportPowers\GpsPower.cs" /> <Compile Include="Traits\SupportPowers\GpsPower.cs" />
<Compile Include="Traits\GpsWatcher.cs" />
<Compile Include="Traits\SupportPowers\ParatroopersPower.cs" /> <Compile Include="Traits\SupportPowers\ParatroopersPower.cs" />
<Compile Include="Scripting\Properties\ChronosphereProperties.cs" /> <Compile Include="Scripting\Properties\ChronosphereProperties.cs" />
<Compile Include="Scripting\Properties\ParadropProperties.cs" /> <Compile Include="Scripting\Properties\ParadropProperties.cs" />

View File

@@ -0,0 +1,117 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Required for `GpsPower`. Attach this to the player actor.")]
class GpsWatcherInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new GpsWatcher(init.Self.Owner); }
}
interface IOnGpsRefreshed { void OnGpsRefresh(Actor self, Player player); }
class GpsWatcher : ISync, IFogVisibilityModifier
{
[Sync] public bool Launched { get; private set; }
[Sync] public bool GrantedAllies { get; private set; }
[Sync] public bool Granted { get; private set; }
readonly Player owner;
readonly List<Actor> actors = new List<Actor>();
readonly HashSet<TraitPair<IOnGpsRefreshed>> notifyOnRefresh = new HashSet<TraitPair<IOnGpsRefreshed>>();
public GpsWatcher(Player owner)
{
this.owner = owner;
}
public void GpsRemove(Actor atek)
{
actors.Remove(atek);
RefreshGps(atek);
}
public void GpsAdd(Actor atek)
{
actors.Add(atek);
RefreshGps(atek);
}
public void Launch(Actor atek, GpsPowerInfo info)
{
atek.World.Add(new DelayedAction(info.RevealDelay * 25,
() =>
{
Launched = true;
RefreshGps(atek);
}));
}
public void RefreshGps(Actor atek)
{
RefreshGranted();
foreach (var i in atek.World.ActorsWithTrait<GpsWatcher>())
i.Trait.RefreshGranted();
if ((Granted || GrantedAllies) && atek.Owner.IsAlliedWith(owner))
atek.Owner.Shroud.ExploreAll(atek.World);
}
void RefreshGranted()
{
var wasGranted = Granted;
var wasGrantedAllies = GrantedAllies;
Granted = actors.Count > 0 && Launched;
GrantedAllies = owner.World.ActorsHavingTrait<GpsWatcher>(g => g.Granted).Any(p => p.Owner.IsAlliedWith(owner));
if (Granted || GrantedAllies)
owner.Shroud.ExploreAll(owner.World);
if (wasGranted != Granted || wasGrantedAllies != GrantedAllies)
foreach (var tp in notifyOnRefresh.ToList())
tp.Trait.OnGpsRefresh(tp.Actor, owner);
}
public bool HasFogVisibility()
{
return Granted || GrantedAllies;
}
public bool IsVisible(Actor actor)
{
var gpsDot = actor.TraitOrDefault<GpsDot>();
if (gpsDot == null)
return false;
return gpsDot.IsDotVisible(owner);
}
public void RegisterForOnGpsRefreshed(Actor actor, IOnGpsRefreshed toBeNotified)
{
notifyOnRefresh.Add(new TraitPair<IOnGpsRefreshed>(actor, toBeNotified));
}
public void UnregisterForOnGpsRefreshed(Actor actor, IOnGpsRefreshed toBeNotified)
{
notifyOnRefresh.Remove(new TraitPair<IOnGpsRefreshed>(actor, toBeNotified));
}
}
}

View File

@@ -17,114 +17,31 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits namespace OpenRA.Mods.RA.Traits
{ {
[Desc("Required for GpsPower. Attach this to the player actor.")] [Desc("Requires `GpsWatcher` on the player actor.")]
class GpsWatcherInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new GpsWatcher(init.Self.Owner); }
}
class GpsWatcher : ISync, IFogVisibilityModifier
{
[Sync] public bool Launched { get; private set; }
[Sync] public bool GrantedAllies { get; private set; }
[Sync] public bool Granted { get; private set; }
public readonly Player Owner;
readonly List<Actor> actors = new List<Actor>();
readonly HashSet<TraitPair<IOnGpsRefreshed>> notifyOnRefresh = new HashSet<TraitPair<IOnGpsRefreshed>>();
public GpsWatcher(Player owner) { Owner = owner; }
public void GpsRem(Actor atek)
{
actors.Remove(atek);
RefreshGps(atek);
}
public void GpsAdd(Actor atek)
{
actors.Add(atek);
RefreshGps(atek);
}
public void Launch(Actor atek, SupportPowerInfo info)
{
atek.World.Add(new DelayedAction(((GpsPowerInfo)info).RevealDelay * 25,
() =>
{
Launched = true;
RefreshGps(atek);
}));
}
public void RefreshGps(Actor atek)
{
RefreshGranted();
foreach (var i in atek.World.ActorsWithTrait<GpsWatcher>())
i.Trait.RefreshGranted();
if ((Granted || GrantedAllies) && atek.Owner.IsAlliedWith(Owner))
atek.Owner.Shroud.ExploreAll(atek.World);
}
void RefreshGranted()
{
var wasGranted = Granted;
var wasGrantedAllies = GrantedAllies;
Granted = actors.Count > 0 && Launched;
GrantedAllies = Owner.World.ActorsHavingTrait<GpsWatcher>(g => g.Granted).Any(p => p.Owner.IsAlliedWith(Owner));
if (Granted || GrantedAllies)
Owner.Shroud.ExploreAll(Owner.World);
if (wasGranted != Granted || wasGrantedAllies != GrantedAllies)
foreach (var tp in notifyOnRefresh.ToList())
tp.Trait.OnGpsRefresh(tp.Actor, Owner);
}
public bool HasFogVisibility()
{
return Granted || GrantedAllies;
}
public bool IsVisible(Actor actor)
{
var gpsDot = actor.TraitOrDefault<GpsDot>();
if (gpsDot == null)
return false;
return gpsDot.IsDotVisible(Owner);
}
public void RegisterForOnGpsRefreshed(Actor actor, IOnGpsRefreshed toBeNotified)
{
notifyOnRefresh.Add(new TraitPair<IOnGpsRefreshed>(actor, toBeNotified));
}
public void UnregisterForOnGpsRefreshed(Actor actor, IOnGpsRefreshed toBeNotified)
{
notifyOnRefresh.Remove(new TraitPair<IOnGpsRefreshed>(actor, toBeNotified));
}
}
interface IOnGpsRefreshed { void OnGpsRefresh(Actor self, Player player); }
class GpsPowerInfo : SupportPowerInfo class GpsPowerInfo : SupportPowerInfo
{ {
public readonly int RevealDelay = 0; public readonly int RevealDelay = 0;
public readonly string DoorImage = "atek";
[SequenceReference("DoorImage")] public readonly string DoorSequence = "active";
[PaletteReference] public readonly string DoorPalette = "effect";
public readonly string SatelliteImage = "sputnik";
[SequenceReference("SatelliteImage")] public readonly string SatelliteSequence = "idle";
[PaletteReference] public readonly string SatellitePalette = "effect";
public override object Create(ActorInitializer init) { return new GpsPower(init.Self, this); } public override object Create(ActorInitializer init) { return new GpsPower(init.Self, this); }
} }
class GpsPower : SupportPower, INotifyKilled, INotifyStanceChanged, INotifySold, INotifyOwnerChanged class GpsPower : SupportPower, INotifyKilled, INotifyStanceChanged, INotifySold, INotifyOwnerChanged
{ {
readonly GpsPowerInfo info;
GpsWatcher owner; GpsWatcher owner;
public GpsPower(Actor self, GpsPowerInfo info) public GpsPower(Actor self, GpsPowerInfo info)
: base(self, info) : base(self, info)
{ {
this.info = info;
owner = self.Owner.PlayerActor.Trait<GpsWatcher>(); owner = self.Owner.PlayerActor.Trait<GpsWatcher>();
owner.GpsAdd(self); owner.GpsAdd(self);
} }
@@ -142,9 +59,9 @@ namespace OpenRA.Mods.RA.Traits
{ {
Game.Sound.PlayToPlayer(self.Owner, Info.LaunchSound); Game.Sound.PlayToPlayer(self.Owner, Info.LaunchSound);
w.Add(new SatelliteLaunch(self)); w.Add(new SatelliteLaunch(self, info));
owner.Launch(self, Info); owner.Launch(self, info);
}); });
} }
@@ -156,7 +73,7 @@ namespace OpenRA.Mods.RA.Traits
void RemoveGps(Actor self) void RemoveGps(Actor self)
{ {
// Extra function just in case something needs to be added later // Extra function just in case something needs to be added later
owner.GpsRem(self); owner.GpsRemove(self);
} }
public void StanceChanged(Actor self, Player a, Player b, Stance oldStance, Stance newStance) public void StanceChanged(Actor self, Player a, Player b, Stance oldStance, Stance newStance)