Hitbox

Pause during the lunge. Find the first moment the red attack box reaches the green target box.
An invisible shape that deals a hit when it touches something.
Games don’t check the artwork pixel by pixel. They check simple invisible boxes. The red box appears only while the attack is active; when it overlaps the other monkey’s green box, the hit lands.
Code
namespace hitbox_lesson;
import stage;
import std;
import math;
export "hitbox" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
artwork("hall", 0, 0, W, 112);
let t = time % 1.5;
let attack = t > 0.55 && t < 0.85;
let lunge = if attack { 6 } else { 0 };
let hit_x = 56 + lunge;
let struck = attack && hit_x < 132 && hit_x + 50 > 102;
ground(70);
actor(37 + lunge, 82, if attack { REACH } else { STAND }, 3, "base", "knight");
actor(117 + (if struck { 3 } else { 0 }), 82, if struck { LAND } else { STAND }, 3, if struck { "white" } else { "rose" }, "knight", true);
if attack {
quad(58 + lunge, 50, 100 + lunge, 60, 58 + lunge, 72, 72 + lunge, 60, WHITE);
opacity(0.4);
rect(hit_x, 54, 50, 14, RED);
opacity(1);
box(hit_x, 54, 50, 14, RED);
}
box(22 + lunge, 46, 30, 34, MOSS);
box(102 + (if struck { 3 } else { 0 }), 46, 30, 34, MOSS);
}

Comments
No comments yet.