forked from AbleOS/ableos
83 lines
2.5 KiB
Plaintext
83 lines
2.5 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 {
|
||
|
render.init()
|
||
|
|
||
|
width := render.width()
|
||
|
height := render.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(render.black)
|
||
|
|
||
|
target_pixel_coord := target * .(cell_size, cell_size) + .(scroll, scroll)
|
||
|
render.put_trirect(target_pixel_coord, .(cell_size, cell_size), render.red, render.light_red)
|
||
|
|
||
|
render.put_hline(target_pixel_coord.y + halfcell, target_pixel_coord.x - octcell, target_pixel_coord.x - sevenoctcell, render.light_red)
|
||
|
render.put_hline(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(target_pixel_coord.x + halfcell, target_pixel_coord.y - octcell, target_pixel_coord.y - sevenoctcell, render.light_red)
|
||
|
render.put_vline(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(x, 0, height, .(0, 127, 0, 127))
|
||
|
x += cell_size
|
||
|
}
|
||
|
|
||
|
y := scroll
|
||
|
loop if y > height break else {
|
||
|
render.put_hline(y, 0, width, .(0, 127, 0, 127))
|
||
|
y += cell_size
|
||
|
}
|
||
|
|
||
|
render.put_hline(seeker.y * cell_size + halfcell + scroll, 0, width, render.light_green)
|
||
|
render.put_vline(seeker.x * cell_size + halfcell + scroll, 0, height, render.light_green)
|
||
|
|
||
|
render.sync()
|
||
|
|
||
|
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
|
||
|
}
|