137 lines
3.7 KiB
GDScript
137 lines
3.7 KiB
GDScript
extends Node2D
|
|
|
|
var tiles = {}
|
|
var offset_world = Vector2.ZERO
|
|
var center_coord = Vector2.ZERO
|
|
var center_world_coord = Vector2.ZERO
|
|
var tile_local_coords = []
|
|
var rect_local = Rect2()
|
|
var radius_world = 0.0
|
|
|
|
# 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 set_offset_world(value: Vector2):
|
|
offset_world = value
|
|
|
|
|
|
func clear_island():
|
|
tiles = {}
|
|
update()
|
|
|
|
|
|
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 (Globals.hex_size * 0.5, Globals.hex_size * sqrt(3) * 0.25)
|
|
rect_local = 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)))
|
|
center_coord = Globals.WorldToHex(center_world_coord)
|
|
print ("center coord: " + str(center_coord))
|
|
radius_world = max(rect_local.size.x, rect_local.size.y) * 0.5
|
|
|
|
|
|
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
|
|
|
|
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]
|
|
|
|
name = path
|
|
calc_bbox()
|
|
update()
|
|
|
|
|
|
func check_overlap(other):
|
|
var rect_world = calc_rect_world()
|
|
var rect_world_other = other.calc_rect_world()
|
|
|
|
return rect_world.intersects(rect_world_other)
|
|
|
|
|
|
func calc_rect_world():
|
|
var rect_world = Rect2(rect_local)
|
|
rect_world.position = transform.origin + rect_world.position + offset_world
|
|
return rect_world
|
|
|
|
|
|
func draw_bsphere():
|
|
var transform = get_transform()
|
|
draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale())
|
|
draw_circle(center_world_coord, radius_world, Color("#80000033"))
|
|
draw_circle(center_world_coord, 10, Color.red)
|
|
|
|
func _draw():
|
|
print (name, offset_world)
|
|
for coord in tiles.keys():
|
|
draw_set_transform (coord + offset_world, 0, Vector2.ONE)
|
|
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))
|
|
|
|
var transform = get_transform()
|
|
draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale())
|
|
|
|
# draw_rect(rect_local, Color.red, false)
|
|
# draw_bsphere()
|
|
|
|
var default_font = Control.new().get_font("font")
|
|
draw_string(default_font, Vector2(0, 0), name + str(" ") + str(offset_world))
|