Property definitions

gamnit $ TileSet :: defaultinit
# Efficiently retrieve tiles in a big texture
class TileSet
	# Texture containing the tileset
	var texture: Texture

	# Width of a tile
	var width: Numeric

	# Height of a tile
	var height: Numeric

	# Number of columns of tiles in the texture
	var nb_cols: Int = (texture.width / width.to_f).to_i is lazy

	# Number of rows of tiles in the texture
	var nb_rows: Int = (texture.height / height.to_f).to_i is lazy

	# Cache of the subtextures of tiles
	var subtextures: Array[Texture] is lazy do
		var subtextures = new Array[Texture]
		for j in [0..nb_rows[ do
			for i in [0..nb_cols[ do
				subtextures.add texture.subtexture(
					i.to_f*width.to_f, j.to_f*height.to_f, width.to_f, height.to_f)
			end
		end
		return subtextures
	end

	# Subtexture for the tile at `x, y`
	#
	# Require: `x < nb_cols and y < nb_rows`
	fun [](x,y: Int): Texture
	do
		assert x >= 0 and x < nb_cols and y >= 0 and y <= nb_rows else print "{x}x{y}<?{nb_cols}x{nb_rows}"
		var idx = x + y * nb_cols
		return subtextures[idx]
	end
end
lib/gamnit/tileset.nit:20,1--58,3