Pass ForgeDocs Product page

Scripting

Fog of War

Reveal fog around a unit, or from a tower

A Fog Revealer opens the fog around its object; add it to the unit prefab, or from code for units made at runtime. ExploreAround explores a whole region at once.

using PassForge.Effects;
using UnityEngine;

public static class FogSight
{
    // A unit spawned while the game runs opens the fog around it. Adding the revealer once to the unit prefab does
    // the same with no code.
    public static PassForgeFogRevealer AddSight(GameObject unit, float radius)
    {
        var revealer = unit.AddComponent<PassForgeFogRevealer>();
        revealer.radius = radius;
        revealer.lineOfSight = true;   // sight stops at colliders on the layers in Blocked By
        return revealer;
    }

    // A lookout tower or a map: explores every place within the radius at once.
    public static void ExploreFromTower(Vector3 tower, float radius)
    {
        var fog = PassForgeFogOfWar.current;

        if (fog != null)
            fog.ExploreAround(tower, radius);
    }
}

Can the player see this?

IsVisible is true when a revealer sees the point right now, and IsExplored when it has ever been seen. PassForgeFogOfWar.current is the fog enabled last.

using PassForge.Effects;
using UnityEngine;

// Fog hides only the look of what it covers: ask it before the player may select or target something there.
public class FogTargeting : MonoBehaviour
{
    PassForgeFogOfWar m_Fog;

    void Start() => m_Fog = PassForgeFogOfWar.current;

    // A fog switched off releases its map, which then reports every place as hidden, so count everything as seen.
    public bool CanSee(Vector3 point) => m_Fog == null || !m_Fog.isActiveAndEnabled || m_Fog.IsVisible(point);

    public bool HasExplored(Vector3 point) => m_Fog == null || !m_Fog.isActiveAndEnabled || m_Fog.IsExplored(point);
}

Save and load explored fog

SaveExplored gives the explored places as bytes for a save game, and LoadExplored puts them back.

using System;
using PassForge.Effects;

// The explored places in a save game, as text. Keep the bytes as they are when your save format holds bytes.
public static class FogSave
{
    public static string Save()
    {
        var fog = PassForgeFogOfWar.current;
        return fog != null ? Convert.ToBase64String(fog.SaveExplored()) : "";
    }

    // False, with nothing changed, for text that is not a saved map.
    public static bool Load(string text)
    {
        var fog = PassForgeFogOfWar.current;
        return fog != null && !string.IsNullOrEmpty(text) && fog.LoadExplored(Convert.FromBase64String(text));
    }
}

Reset the fog for a new round

ResetExplored covers every place again, for a new round or match on the same map.

using PassForge.Effects;

public static class FogRound
{
    // A new round or match on the same map: every place is covered again.
    public static void NewRound()
    {
        var fog = PassForgeFogOfWar.current;

        if (fog != null)
            fog.ResetExplored();
    }
}

Multiplayer: only the local team's sight

Give each Fog Revealer a team, then set the fog's visibleTeams once the local team is known, followed by ResetExplored.

using PassForge.Effects;

// Multiplayer: each player's machine draws its own fog, showing only the sight of the local side.
public static class FogTeams
{
    // When a unit spawns, with the team the game gives it, from 0 to 31.
    public static void OnUnitSpawned(PassForgeFogRevealer revealer, int team) => revealer.team = team;

    // Once the local player's team is known.
    public static void OnLocalTeamKnown(int localTeam)
    {
        var fog = PassForgeFogOfWar.current;

        if (fog == null)
            return;

        fog.visibleTeams = 1 << localTeam;   // only this side's revealers open the fog
        fog.ResetExplored();                 // the map does not keep which team explored a place: start clean
    }

    // Allies share sight: show both teams.
    public static void ShareSight(int localTeam, int allyTeam)
    {
        var fog = PassForgeFogOfWar.current;

        if (fog != null)
            fog.visibleTeams = (1 << localTeam) | (1 << allyTeam);
    }
}

Alert when an enemy comes into sight

The fog's onShown passes each object Hiding shows, and onHidden each one it hides.

using PassForge.Effects;
using UnityEngine;

// A ping on the radar and a beep when an enemy comes into sight. The enemies are on a layer in the fog's Hide While
// Unseen, so the fog hides and shows them.
public class EnemySpotted : MonoBehaviour
{
    [SerializeField] PassForgeFogOfWar m_Fog;
    [SerializeField] LayerMask m_Enemies;
    [SerializeField] AudioSource m_Beep;

    void OnEnable() => m_Fog.onShown.AddListener(OnShown);
    void OnDisable() => m_Fog.onShown.RemoveListener(OnShown);

    void OnShown(GameObject shown)
    {
        if ((m_Enemies.value & (1 << shown.layer)) == 0)
            return;

        PassForgePings.Ping(shown.transform.position, Color.red, 1.5f, 4f);

        if (m_Beep != null)
            m_Beep.Play();
    }
}