Line of sight

Put the key directly across from the courier. Remove the cabinet, then put it back. Does the key light up even though the courier keeps facing it?
Interactive demo#TopDownDarkwood
An observer can see a target when nothing blocks the straight line between them.
The courier is always facing the key. When the segment hits the cabinet, the solid line stops at the near face and the key goes grey. Move the key past a corner to open a clear path.
Code
namespace line_of_sight_lesson;
import stage;
import std;
import math;
var observer = CourierTurn {};
// Intersect the segment with both slabs of an axis-aligned cabinet.
fn cabinet_entry(eye_x: f64, eye_y: f64, target_x: f64, target_y: f64) -> f64 {
let dx = target_x - eye_x;
let dy = target_y - eye_y;
var enter = 0.0;
var leave = 1.0;
if dx == 0.0 {
if eye_x < 73.0 || eye_x > 86.0 {
return 1.0;
}
} else {
let a = (73.0 - eye_x) / dx;
let b = (86.0 - eye_x) / dx;
enter = max(enter, min(a, b));
leave = min(leave, max(a, b));
}
if dy == 0.0 {
if eye_y < 42.0 || eye_y > 58.0 {
return 1.0;
}
} else {
let a = (42.0 - eye_y) / dy;
let b = (58.0 - eye_y) / dy;
enter = max(enter, min(a, b));
leave = min(leave, max(a, b));
}
if enter <= leave {
return enter;
}
return 1.0;
}
export "line-of-sight" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
let y = if hover { clamp(py, 6.0, 94.0) } else { 50 + 42 * sin(time) };
let wall = toggle("wall");
let portion = if wall { cabinet_entry(22, 50, 140, y) } else { 1.0 };
let x_hit = 22 + 118 * portion;
let y_hit = 50 + (y - 50) * portion;
tiles();
if wall {
cabinet(73, 42, 13, 16);
}
line(22, 50, x_hit, y_hit, if portion < 1 { PINK } else { MINT });
if portion < 1 {
line(x_hit, y_hit, 140, y, "#8995a3", 1, 3);
}
steer_courier(observer, time, 22, 50, 0, 0, atan2(y - 50, 118.0));
draw_steering_courier(observer);
key(140, y, portion == 1);
}

Comments
No comments yet.