0:01 / 0:30
Playback speed

Fog of war

Wait for the second corner, then switch memory off. Which part of the map disappears?

Interactive demo#TopDown

A map can distinguish places visible now, explored earlier, and still unknown.

The courier reveals nearby floor tiles. As the courier moves on, memory keeps explored floor dimly visible while distant unexplored tiles stay black. The route restarts with a fresh map each loop.

Code

src/demos/fog_of_war.goose
namespace fog_of_war_lesson;
import stage;
import std;
import math;

var scout = CourierTurn {};

// A tile was explored if a traversed segment passed within sight radius.
fn explored_tile(x: f64, y: f64, ax: f64, ay: f64, bx: f64, by: f64, radius: f64) -> bool {
    let dx = bx - ax;
    let dy = by - ay;
    let length_sq = dx * dx + dy * dy;
    var along = 0.0;
    if length_sq > 0.0 {
        along = clamp(((x - ax) * dx + (y - ay) * dy) / length_sq, 0.0, 1.0);
    }
    return hypot(x - ax - along * dx, y - ay - along * dy) <= radius;
}

export "fog-of-war" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    let route = [[17.0, 75.0], [50.0, 75.0], [50.0, 28.0], [103.0, 28.0], [103.0, 66.0], [142.0, 66.0]];
    let progress = min((time + 2.0) % 6.0, 5.0);
    let leg = min(progress as i64, 4);
    let a = route[leg];
    let b = route[leg + 1];
    let x = mix_value(a[0], b[0], progress - leg);
    let y = mix_value(a[1], b[1], progress - leg);
    tiles();
    cabinet(64, 47, 28, 14);
    cabinet(18, 17, 14, 14);
    let memory = toggle("memory");
    for row in 13 {
        for col in 20 {
            let cx = col * 8 + 4;
            let cy = row * 8 + 4;
            if hypot(cx - x, cy - y) <= 21 {
                continue;
            }
            var explored = false;
            if memory {
                for i in 0..leg + 1 {
                    let end = if i == leg { [x, y] } else { route[i + 1] };
                    if explored_tile(cx, cy, route[i][0], route[i][1], end[0], end[1], 21) {
                        explored = true;
                        break;
                    }
                }
            }
            rect(col * 8, row * 8, 8, 8, if explored { "rgba(17,22,42,0.58)" } else { "#10131f" });
        }
    }
    let face = atan2(b[1] - a[1], b[0] - a[0]);
    steer_courier(scout, time, x, y, face, face, face);
    draw_steering_courier(scout);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.