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-07-07 22:22:07 +02:00
|
|
|
var offset = Vector2.ZERO
|
|
|
|
var center_coord = Vector2.ZERO
|
|
|
|
var center_world_coord = Vector2.ZERO
|
|
|
|
var tile_local_coords = []
|
|
|
|
var rect_world = Rect2()
|
|
|
|
var radius_world = 0.0
|
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.
|
|
|
|
|
|
|
|
|
2021-06-28 22:34:48 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
func set_offset(value: Vector2):
|
|
|
|
offset = value
|
|
|
|
|
|
|
|
|
2021-06-28 22:34:48 +02:00
|
|
|
func clear_island():
|
|
|
|
tiles = {}
|
|
|
|
update()
|
|
|
|
|
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
func calc_bbox():
|
|
|
|
var coord_min = Vector2.ONE * 10000
|
|
|
|
var coord_max = Vector2.ONE * -10000
|
|
|
|
|
|
|
|
var coords = tiles.keys()
|
|
|
|
for coord in coords:
|
|
|
|
var local_coords = (coord)
|
|
|
|
tile_local_coords.append(local_coords)
|
|
|
|
if local_coords.x < coord_min.x:
|
|
|
|
coord_min.x = local_coords.x
|
|
|
|
if local_coords.y < coord_min.y:
|
|
|
|
coord_min.y = local_coords.y
|
|
|
|
|
|
|
|
if local_coords.x > coord_max.x:
|
|
|
|
coord_max.x = local_coords.x
|
|
|
|
if local_coords.y > coord_max.y:
|
|
|
|
coord_max.y = local_coords.y
|
|
|
|
|
|
|
|
var hex_half_size = Vector2.ONE * Globals.hex_size * 0.5
|
|
|
|
rect_world = Rect2(coord_min - hex_half_size, coord_max - coord_min + hex_half_size * 2)
|
|
|
|
center_world_coord = Globals.HexToWorld(Globals.WorldToHex(coord_min + 0.5 * (coord_max - coord_min)))
|
|
|
|
radius_world = max(rect_world.size.x, rect_world.size.y) * 0.5
|
|
|
|
|
|
|
|
|
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()
|
2021-06-28 22:34:48 +02:00
|
|
|
|
|
|
|
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 22:34:48 +02:00
|
|
|
|
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:
|
2021-06-28 22:34:48 +02:00
|
|
|
# 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]
|
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
name = path
|
|
|
|
calc_bbox()
|
2021-06-28 22:34:48 +02:00
|
|
|
update()
|
|
|
|
|
2021-06-28 21:51:09 +02:00
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
func check_overlap(other):
|
|
|
|
var tile_offset = Globals.HexToWorld(offset)
|
|
|
|
var other_offset = Globals.HexToWorld(other.offset)
|
|
|
|
|
|
|
|
var rel_offset = other_offset - tile_offset
|
|
|
|
var dist = rel_offset.length() - radius_world - other.radius_world
|
|
|
|
|
|
|
|
return dist < 0
|
|
|
|
|
|
|
|
|
2021-06-28 21:51:09 +02:00
|
|
|
func _draw():
|
2021-07-07 22:22:07 +02:00
|
|
|
var tile_offset = Globals.HexToWorld(offset)
|
|
|
|
print (name, tile_offset)
|
2021-06-28 21:51:09 +02:00
|
|
|
for coord in tiles.keys():
|
2021-07-07 22:22:07 +02:00
|
|
|
draw_set_transform (coord + tile_offset, 0, Vector2.ONE)
|
2021-06-28 21:51:09 +02:00
|
|
|
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))
|
2021-07-07 22:22:07 +02:00
|
|
|
|
|
|
|
var transform = get_transform()
|
|
|
|
draw_set_transform(transform.origin + tile_offset, transform.get_rotation(), transform.get_scale())
|
|
|
|
|
|
|
|
draw_rect(rect_world, Color.red, false)
|
|
|
|
draw_circle(center_world_coord, radius_world, Color.brown)
|
|
|
|
draw_circle(center_world_coord, 10, Color.red)
|
|
|
|
|
|
|
|
var default_font = Control.new().get_font("font")
|
|
|
|
draw_string(default_font, Vector2(0, 0), name)
|