extends TileMapLayer @onready var camera: Camera2D = $"../main_game_camera" @export var render_radius: int = 20 # How many tiles out from the center to draw # Keep track of which grid coordinates we've already loaded var loaded_tiles: Dictionary = {} # Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: if camera: generate_tiles_around_camera() func generate_tiles_around_camera() -> void: # 1. Convert the camera's pixel position into grid coordinates var camera_grid_pos = self.local_to_map(camera.global_position) # 2. Loop through a square grid area around the camera for x in range(camera_grid_pos.x - render_radius / camera.zoom[0], camera_grid_pos.x + render_radius / camera.zoom[0]): for y in range(camera_grid_pos.y - render_radius / camera.zoom[1], camera_grid_pos.y + render_radius / camera.zoom[1]): var tile_coords = Vector2i(x, y) # 3. Only place a tile if we haven't already placed one there if not loaded_tiles.has(tile_coords): # set_cell(coords, source_id, atlas_coords) # Assumes your source_id is 0 and your square tile is at (0,0) in the atlas self.set_cell(tile_coords, 0, Vector2i(0, 0)) loaded_tiles[tile_coords] = true