28 lines
773 B
GDScript
28 lines
773 B
GDScript
class_name BoundedRangeIterator extends Iterator
|
|
|
|
var range: IntRange
|
|
var bounds: IntRange
|
|
var step: int
|
|
var limit: int
|
|
var index: int
|
|
|
|
func _init(range: IntRange, bounds: IntRange, step: int = 1, limit: int = 1, start_index: int = range.min) -> void:
|
|
self.range = range
|
|
self.bounds = bounds
|
|
self.step = step
|
|
self.limit = limit
|
|
self.index = start_index
|
|
|
|
func clone() -> BoundedRangeIterator:
|
|
return BoundedRangeIterator.new(range, bounds, step, limit, index)
|
|
|
|
func next() -> Option:
|
|
if bounds.contains(index):
|
|
var current_value = Option.some(index)
|
|
index += step
|
|
return current_value
|
|
elif limit > 0:
|
|
limit -= 1
|
|
index = bounds.wrap(index)
|
|
return Option.some(index)
|
|
else: return Option.none
|