PirateTreasureHunt/scenes/Island.gd

70 lines
1.6 KiB
GDScript
Raw Normal View History

2021-06-28 21:51:09 +02:00
extends Node2D
2021-06-27 12:26:44 +02:00
2021-06-28 21:51:09 +02:00
var tiles = {}
2021-06-27 12:26:44 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func set_tile(coord: Vector2, value: String):
if HexTileDrawer.TileTypeFromString[value] == HexTileDrawer.TileType.None:
if coord in tiles.keys():
tiles.erase(coord)
else:
tiles[coord] = value
update()
func clear_island():
tiles = {}
update()
2021-06-28 21:51:09 +02:00
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()
if !island_save_data.file_exists(path):
clear_island()
return
2021-06-28 21:51:09 +02:00
island_save_data.open(path, File.READ)
2021-06-28 21:51:09 +02:00
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"))
2021-06-28 21:51:09 +02:00
coords = Vector2(float(regresult.get_string("tile_x")), float(regresult.get_string("tile_y")))
tiles[coords] = data[k]
update()
2021-06-28 21:51:09 +02:00
func _draw():
for coord in tiles.keys():
draw_set_transform (coord, 0, Vector2.ONE)
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))