# Duane's Dungeon - a rogue-like game # Copyright (C) 2023 Duane Robertson # state_maps.gd - state for saving map images # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . extends State class_name StateMaps const MAP_BD = 8 const WIN_SIZE = Vector2(1280, 720) var MAP_SZ = Vector2.ONE * (WIN_SIZE.y - MAP_BD * 2) var SSHOT_DIR = 'screenshots' var SSHOT_FN = 'user://%s/dungeon_shot_%%02d.png' % [SSHOT_DIR] var map:Dungeon var map_pos:Vector2 var msz:Vector2i var save_sprite:Sprite2D var view:ColorRect var vp:SubViewport func _ready(): super() name = 'StateMaps' map_pos = get_parent().map_pos var dir = DirAccess.open('user://') if not dir.dir_exists(SSHOT_DIR): dir.make_dir(SSHOT_DIR) setup_gui() func make_map(map_seed = null): if map_seed: seed(map_seed) if map: map.queue_free() # the game map map = Dungeon.new() vp.add_child(map) # Change the size to this and it won't draw # without the ColorRect. msz = map.bounds.size vp.size = msz * map.cell_quadrant_size save_sprite.scale = view.size / Vector2(vp.size) # # Change the size to this and it draws anyway. # vp.size = msz * (map.cell_quadrant_size + 2) func save_map(num:int): # Show the map and save it. var tex = vp.get_texture() save_sprite.texture = tex var img = tex.get_image() img.save_png(SSHOT_FN % [num + 1]) func setup_gui(): var back:TextureRect = game.background var lab:Label = Label.new() lab.text = 'Generating Dungeon Maps' lab.position = Vector2(30, 30) lab.label_settings = LabelSettings.new() lab.label_settings.font_size = 40 back.add_child(lab) # the map background view = ColorRect.new() view.size = MAP_SZ view.position = map_pos view.color = Color.BLACK view.mouse_filter = Control.MOUSE_FILTER_IGNORE view.clip_children = CanvasItem.CLIP_CHILDREN_AND_DRAW back.add_child(view) await RenderingServer.frame_post_draw # This viewport will create the image. vp = SubViewport.new() vp.render_target_update_mode = SubViewport.UPDATE_ALWAYS add_child(vp) ############################################## # Same stupid issue with viewports not updating. # I thought UPDATE_ALWAYS fixed this, but no. # Add any useless control, and it will update. ############################################## var crec:ColorRect = ColorRect.new() vp.add_child(crec) ############################################## # Add a sprite to preview the map, because of # godotengine scaling bug #80986. save_sprite = Sprite2D.new() save_sprite.scale = view.size / Vector2(vp.size) save_sprite.centered = false view.add_child(save_sprite) for i in range(int(game.switches.png)): ############################################## await RenderingServer.frame_post_draw ############################################## make_map() ############################################## await RenderingServer.frame_post_draw ############################################## save_map(i) get_tree().quit(0)