PirateTreasureHunt/scenes/Island.gd

47 lines
1.3 KiB
GDScript

extends Node2D
var tiles = {}
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func save_island(path: String):
var island_save_data = File.new()
island_save_data.open(path, File.WRITE)
island_save_data.store_line (to_json(tiles))
island_save_data.close()
func load_island(path: String):
var island_save_data = File.new()
island_save_data.open(path, File.READ)
var data = parse_json(island_save_data.get_line())
island_save_data.close()
var data_keys = data.keys()
tiles = {}
var coord_regex = RegEx.new()
coord_regex.compile("\\((?<tile_x>-?\\d+(.?\\d+)?), (?<tile_y>-?\\d+(.?\\d+)?)\\)")
var reg_test_result = coord_regex.search("(0, 0)")
assert(reg_test_result)
reg_test_result = coord_regex.search("(123.124, 552.0)")
assert(reg_test_result)
for k in data_keys:
var coords = Vector2.ZERO
var regresult = coord_regex.search(k)
if regresult:
print(regresult.get_string("tile_x"), " - ", regresult.get_string("tile_y"))
coords = Vector2(float(regresult.get_string("tile_x")), float(regresult.get_string("tile_y")))
tiles[coords] = data[k]
func _draw():
for coord in tiles.keys():
draw_set_transform (coord, 0, Vector2.ONE)
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))