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
@onready var tile_map = $"../TileMap"
var atlas_cords : Array
func _ready():
pass
func _on_save_button_pressed():
atlas_cords = _get_atlas_cords()
_save()
func _on_load_button_pressed():
_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.get_cell_atlas_coords(MAP_LAYER, 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.set_cell(MAP_LAYER, Vector2i(x, y), MAP_LAYER, 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"]