ableos/sysdata/programs/render_example/src/examples/tactical_screen.hb
2024-10-25 09:17:37 -05:00

83 lines
2.6 KiB
Plaintext

render := @use("../../../../libraries/render/src/lib.hb");
.{math, random} := @use("../../../../libraries/stn/src/lib.hb")
Vec2 := math.Vec2
/* expected result:
a grid of green lines scrolling from the left top corner to the right bottom one
with a "target" randomly apperaing in one of them and a "seeker" "catching" it */
example := fn(): void {
screen := render.init(true)
width := screen.width
height := screen.height
cell_size := 0
range := Vec2(int).(0, 0)
if width > height {
cell_size = width / 40
range = .(39, height / cell_size - 1)
} else {
cell_size = height / 40
range = .(width / cell_size - 1, 39)
}
width -= 1
height -= 1
scroll := 0
target := Vec2(int).(random.range(int, 0, range.x), random.range(int, 0, range.y))
halfcell := cell_size / 2
octcell := cell_size / 8
sevenoctcell := cell_size - octcell
seeker := Vec2(int).(random.range(int, 0, range.x), random.range(int, 0, range.y))
loop {
render.clear(screen, render.black)
target_pixel_coord := target * .(cell_size, cell_size) + .(scroll, scroll)
render.put_trirect(screen, target_pixel_coord, .(cell_size, cell_size), render.red, render.light_red)
render.put_hline(screen, target_pixel_coord.y + halfcell, target_pixel_coord.x - octcell, target_pixel_coord.x - sevenoctcell, render.light_red)
render.put_hline(screen, target_pixel_coord.y + halfcell, target_pixel_coord.x + cell_size + octcell, target_pixel_coord.x + cell_size + sevenoctcell, render.light_red)
render.put_vline(screen, target_pixel_coord.x + halfcell, target_pixel_coord.y - octcell, target_pixel_coord.y - sevenoctcell, render.light_red)
render.put_vline(screen, target_pixel_coord.x + halfcell, target_pixel_coord.y + cell_size + octcell, target_pixel_coord.y + cell_size + sevenoctcell, render.light_red)
x := scroll
loop if x > width break else {
render.put_vline(screen, x, 0, height, .(0, 127, 0, 127))
x += cell_size
}
y := scroll
loop if y > height break else {
render.put_hline(screen, y, 0, width, .(0, 127, 0, 127))
y += cell_size
}
render.put_hline(screen, seeker.y * cell_size + halfcell + scroll, 0, width, render.blue)
render.put_vline(screen, seeker.x * cell_size + halfcell + scroll, 0, height, render.blue)
render.sync(screen)
if seeker.x < target.x {
seeker.x += 1
} else if seeker.x > target.x {
seeker.x -= 1
} else if seeker.y < target.y {
seeker.y += 1
} else if seeker.y > target.y {
seeker.y -= 1
} else {
target = .(random.range(int, 0, range.x), random.range(int, 0, range.y))
}
scroll += 1
if scroll > cell_size {
scroll = 0
target += .(1, 1)
seeker += .(1, 1)
}
}
return
}