Introduce INotifyDiscovered interface and use it to move notifications from EnemyWatcher to AnnounceOnSeen

This commit is contained in:
penev92
2015-02-22 22:33:51 +02:00
parent eb0ea8d543
commit 1b616123ef
3 changed files with 51 additions and 29 deletions

View File

@@ -8,9 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -25,13 +23,13 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Minimal ticks in-between notifications.")]
public readonly int NotificationInterval = 750;
public object Create(ActorInitializer init) { return new EnemyWatcher(init.Self, this); }
public object Create(ActorInitializer init) { return new EnemyWatcher(this); }
}
class EnemyWatcher : ITick
{
readonly EnemyWatcherInfo info;
readonly Lazy<RadarPings> radarPings;
readonly HashSet<Player> discoveredPlayers;
bool announcedAny;
int rescanInterval;
@@ -40,15 +38,16 @@ namespace OpenRA.Mods.Common.Traits
HashSet<uint> visibleActorIds;
HashSet<string> playedNotifications;
public EnemyWatcher(Actor self, EnemyWatcherInfo info)
public EnemyWatcher(EnemyWatcherInfo info)
{
lastKnownActorIds = new HashSet<uint>();
discoveredPlayers = new HashSet<Player>();
this.info = info;
rescanInterval = info.ScanInterval;
ticksBeforeNextNotification = info.NotificationInterval;
radarPings = Exts.Lazy(() => self.World.WorldActor.Trait<RadarPings>());
}
// Here self is the player actor
public void Tick(Actor self)
{
// TODO: Make the AI handle such notifications and remove Owner.IsBot from this check
@@ -70,9 +69,9 @@ namespace OpenRA.Mods.Common.Traits
foreach (var actor in self.World.ActorsWithTrait<AnnounceOnSeen>())
{
// We only care about enemy actors (creeps should be enemies)
if ((actor.Actor.EffectiveOwner != null && self.Owner.Stances[actor.Actor.EffectiveOwner.Owner] != Stance.Enemy)
|| self.Owner.Stances[actor.Actor.Owner] != Stance.Enemy)
// We don't want notifications for allied actors
if ((actor.Actor.EffectiveOwner != null && self.Owner.Stances[actor.Actor.EffectiveOwner.Owner] == Stance.Ally)
|| self.Owner.Stances[actor.Actor.Owner] == Stance.Ally)
continue;
if (actor.Actor.IsDead || !actor.Actor.IsInWorld)
@@ -88,12 +87,28 @@ namespace OpenRA.Mods.Common.Traits
if (lastKnownActorIds.Contains(actor.Actor.ActorID))
continue;
var notificationPlayed = playedNotifications.Contains(actor.Trait.Info.Notification);
// Notify the actor that he has been discovered
foreach (var trait in actor.Actor.TraitsImplementing<INotifyDiscovered>())
trait.OnDiscovered(actor.Actor, self.Owner, !notificationPlayed);
var discoveredPlayer = actor.Actor.Owner;
if (!discoveredPlayers.Contains(discoveredPlayer))
{
// Notify the actor's owner that he has been discovered
foreach (var trait in discoveredPlayer.PlayerActor.TraitsImplementing<INotifyDiscovered>())
trait.OnDiscovered(actor.Actor, self.Owner, false);
discoveredPlayers.Add(discoveredPlayer);
}
// We have already played this type of notification
if (playedNotifications.Contains(actor.Trait.Info.Notification))
if (notificationPlayed)
continue;
if (self.Owner == self.World.RenderPlayer)
Announce(self, actor);
playedNotifications.Add(actor.Trait.Info.Notification);
announcedAny = true;
}
if (announcedAny)
@@ -101,18 +116,5 @@ namespace OpenRA.Mods.Common.Traits
lastKnownActorIds = visibleActorIds;
}
void Announce(Actor self, TraitPair<AnnounceOnSeen> announce)
{
// Audio notification
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", announce.Trait.Info.Notification, self.Owner.Country.Race);
// Radar notificaion
if (announce.Trait.Info.PingRadar && radarPings.Value != null)
radarPings.Value.Add(() => true, announce.Actor.CenterPosition, Color.Red, 50);
playedNotifications.Add(announce.Trait.Info.Notification);
announcedAny = true;
}
}
}