2024-10-13 17:38:43 -05:00
|
|
|
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
|
2024-10-15 15:11:06 -05:00
|
|
|
with a "target" randomly apperaing in one of them and a "seeker" "catching" it */
|
2024-10-13 17:38:43 -05:00
|
|
|
|
|
|
|
example := fn(): void {
|
2024-10-14 19:24:29 -05:00
|
|
|
screen := render.init(true)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
2024-10-14 19:24:29 -05:00
|
|
|
width := screen.width
|
|
|
|
height := screen.height
|
2024-10-13 17:38:43 -05:00
|
|
|
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 {
|
2024-10-14 19:24:29 -05:00
|
|
|
render.clear(screen, render.black)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
|
|
|
target_pixel_coord := target * .(cell_size, cell_size) + .(scroll, scroll)
|
2024-10-14 19:24:29 -05:00
|
|
|
render.put_trirect(screen, target_pixel_coord, .(cell_size, cell_size), render.red, render.light_red)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
2024-10-14 19:24:29 -05:00
|
|
|
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)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
|
|
|
x := scroll
|
|
|
|
loop if x > width break else {
|
2024-10-14 19:24:29 -05:00
|
|
|
render.put_vline(screen, x, 0, height, .(0, 127, 0, 127))
|
2024-10-13 17:38:43 -05:00
|
|
|
x += cell_size
|
|
|
|
}
|
|
|
|
|
|
|
|
y := scroll
|
|
|
|
loop if y > height break else {
|
2024-10-14 19:24:29 -05:00
|
|
|
render.put_hline(screen, y, 0, width, .(0, 127, 0, 127))
|
2024-10-13 17:38:43 -05:00
|
|
|
y += cell_size
|
|
|
|
}
|
|
|
|
|
2024-10-14 19:24:29 -05:00
|
|
|
render.put_hline(screen, seeker.y * cell_size + halfcell + scroll, 0, width, render.light_green)
|
|
|
|
render.put_vline(screen, seeker.x * cell_size + halfcell + scroll, 0, height, render.light_green)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
2024-10-14 19:24:29 -05:00
|
|
|
render.sync(screen)
|
2024-10-13 17:38:43 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|