package ru.dbotthepony.kstarbound.world import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZE import ru.dbotthepony.kstarbound.world.api.IChunk import ru.dbotthepony.kstarbound.world.api.IChunkCell import ru.dbotthepony.kstarbound.world.api.ITileAccess import ru.dbotthepony.kstarbound.world.api.ITileState import ru.dbotthepony.kvector.vector.Vector2i fun IChunk.iterate(fromX: Int = 0, fromY: Int = 0, toX: Int = fromX + CHUNK_SIZE, toY: Int = fromY + CHUNK_SIZE): Iterator> { return object : Iterator> { private var x = fromX private var y = fromY override fun hasNext(): Boolean { return x < toX && y < toY } override fun next(): Pair { if (!hasNext()) throw NoSuchElementException() val tile = getCell(x, y) val pos = Vector2i(x, y) x++ if (x >= toX) { y++ x = 0 } return pos to tile } } } fun ITileAccess.iterateTiles(fromX: Int = 0, fromY: Int = 0, toX: Int = fromX + CHUNK_SIZE, toY: Int = fromY + CHUNK_SIZE): Iterator> { return object : Iterator> { private var x = fromX private var y = fromY override fun hasNext(): Boolean { return x < toX && y < toY } override fun next(): Pair { if (!hasNext()) throw NoSuchElementException() val tile = getTile(x, y) val pos = Vector2i(x, y) x++ if (x >= toX) { y++ x = 0 } return pos to tile } } }