Skip to content

Commit

Permalink
Add a script trigger overlay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mailaender committed Oct 12, 2021
1 parent 7785c0a commit 4d5c57c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions OpenRA.Game/Traits/TraitsInterfaces.cs
Expand Up @@ -226,6 +226,7 @@ public interface IActorMap
void AddInfluence(Actor self, IOccupySpace ios);
void RemoveInfluence(Actor self, IOccupySpace ios);
int AddCellTrigger(CPos[] cells, Action<Actor> onEntry, Action<Actor> onExit);
List<CPos> TriggerPositions();
void RemoveCellTrigger(int id);
int AddProximityTrigger(WPos pos, WDist range, WDist vRange, Action<Actor> onEntry, Action<Actor> onExit);
void RemoveProximityTrigger(int id);
Expand Down
5 changes: 5 additions & 0 deletions OpenRA.Mods.Common/Traits/World/ActorMap.cs
Expand Up @@ -510,6 +510,11 @@ public int AddCellTrigger(CPos[] cells, Action<Actor> onEntry, Action<Actor> onE
return id;
}

public List<CPos> TriggerPositions()
{
return cellTriggerInfluence.Keys.ToList();
}

public void RemoveCellTrigger(int id)
{
if (!cellTriggers.TryGetValue(id, out var trigger))
Expand Down
89 changes: 89 additions & 0 deletions OpenRA.Mods.Common/Traits/World/TriggerOverlay.cs
@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 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 OpenRA.Graphics;
using OpenRA.Mods.Common.Commands;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World)]
[Desc("Renders a debug overlay showing the script triggers. Attach this to the world actor.")]
public class TriggerOverlayInfo : TraitInfo
{
public readonly string Font = "BigBold";

public readonly Color Color = Color.Red;

public override object Create(ActorInitializer init) { return new TriggerOverlay(this); }
}

public class TriggerOverlay : IRenderAnnotations, IWorldLoaded, IChatCommand
{
const string CommandName = "triggers";
const string CommandDesc = "toggles the script triggers overlay.";

public bool Enabled;

readonly SpriteFont font;
readonly Color color;

public TriggerOverlay(TriggerOverlayInfo info)
{
font = Game.Renderer.Fonts[info.Font];
color = info.Color;
}

void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr)
{
var console = w.WorldActor.TraitOrDefault<ChatCommands>();
var help = w.WorldActor.TraitOrDefault<HelpCommand>();

if (console == null || help == null)
return;

console.RegisterCommand(CommandName, this);
help.RegisterHelp(CommandName, CommandDesc);
}

void IChatCommand.InvokeCommand(string name, string arg)
{
if (name == CommandName)
Enabled ^= true;
}

IEnumerable<IRenderable> IRenderAnnotations.RenderAnnotations(Actor self, WorldRenderer wr)
{
if (!Enabled)
yield break;

var triggerPositions = wr.World.ActorMap.TriggerPositions();

foreach (var uv in wr.Viewport.VisibleCellsInsideBounds.CandidateMapCoords)
{
if (self.World.ShroudObscures(uv))
continue;

var cell = uv.ToCPos(wr.World.Map);
if (!triggerPositions.Contains(cell))
continue;

var center = wr.World.Map.CenterOfCell(cell);
yield return new TextAnnotationRenderable(font, center, 1024, color, "T");
}
}

bool IRenderAnnotations.SpatiallyPartitionable => false;
}
}
1 change: 1 addition & 0 deletions mods/cnc/rules/world.yaml
Expand Up @@ -252,6 +252,7 @@ World:
LoadWidgetAtGameStart:
ShellmapRoot: MENU_BACKGROUND
ScriptTriggers:
TriggerOverlay:
TimeLimitManager:
ColorPickerManager:
PreviewActor: fact.colorpicker
Expand Down
1 change: 1 addition & 0 deletions mods/d2k/rules/world.yaml
Expand Up @@ -233,6 +233,7 @@ World:
PanelName: SKIRMISH_STATS
LoadWidgetAtGameStart:
ScriptTriggers:
TriggerOverlay:
StartGameNotification:
TimeLimitManager:
ColorPickerManager:
Expand Down
1 change: 1 addition & 0 deletions mods/ra/rules/world.yaml
Expand Up @@ -274,6 +274,7 @@ World:
PanelName: SKIRMISH_STATS
LoadWidgetAtGameStart:
ScriptTriggers:
TriggerOverlay:
TimeLimitManager:
TimeLimitWarnings:
40: FourtyMinutesRemaining
Expand Down
1 change: 1 addition & 0 deletions mods/ts/rules/world.yaml
Expand Up @@ -379,6 +379,7 @@ World:
LoadWidgetAtGameStart:
ShellmapRoot: MAINMENU_PRERELEASE_NOTIFICATION
ScriptTriggers:
TriggerOverlay:
TimeLimitManager:
ColorPickerManager:
PreviewActor: mmch.colorpicker
Expand Down

0 comments on commit 4d5c57c

Please sign in to comment.