@tool
extends Control
const MAP_WIDTH = 16
const MAP_HEIGHT = 16
const MAP_LAYER = 0
const FILE_PATH = "res://savedata.txt"
const TILE_MAP_NODE_POS = 0
@onready var save_button = $SaveButton
@onready var load_button = $LoadButton
var root
var tile_map_layer
var atlas_cords : Array
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func _get_general_nodes():
root = get_tree().edited_scene_root
tile_map_layer = root.get_child(0)
func _on_save_button_pressed():
_get_general_nodes()
atlas_cords = _get_atlas_cords()
_save()
func _on_load_button_pressed():
_get_general_nodes()
_load()
_set_atlas_cords(atlas_cords)
print("マップを読み込みました")
func _get_atlas_cords() -> Array:
var _atlas_cords : Array = []
_atlas_cords.clear()
for y in range(0, MAP_HEIGHT, 1):
var line_data : Array =[]
for x in range(0, MAP_WIDTH, 1):
line_data.append(tile_map_layer.get_cell_atlas_coords(Vector2i(x, y)))
_atlas_cords.append(line_data)
return _atlas_cords
func _set_atlas_cords(_atlas_cords : Array):
for y in range(0, MAP_HEIGHT, 1):
var line_data : Array = _atlas_cords[y]
for x in range(0, MAP_WIDTH, 1):
tile_map_layer.set_cell(Vector2i(x, y), 0, line_data[x])
# データを書き出す
func _save():
var savedata = {}
savedata["Atlas"] = atlas_cords
var f = FileAccess.open(FILE_PATH, FileAccess.WRITE)
var s = var_to_str(savedata)
f.store_string(s)
f.close()
print("ファイルを保存しました: %s" %FILE_PATH)
func _load():
if FileAccess.file_exists(FILE_PATH) == false:
print("セーブデータが存在しません: %s" %FILE_PATH)
load_button.modulate = Color(1.0, 0, 0)
return
var f = FileAccess.open(FILE_PATH, FileAccess.READ)
var s = f.get_as_text()
f.close()
print("ファイルを読み込みました: %s" %FILE_PATH)
var savedata = str_to_var(s)
if savedata == null:
print("セーブデータがエラーです: %s" %FILE_PATH)
return
atlas_cords = savedata["Atlas"]