PirateTreasureHunt/scenes/Island.gd

185 lines
5.5 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-07-09 14:17:25 +02:00
var offset_world = Vector2.ZERO
var center_coord = Vector2.ZERO
var center_world_coord = Vector2.ZERO
2021-07-09 14:17:25 +02:00
var rect_local = Rect2()
var radius_world = 0.0
2021-07-10 14:57:18 +02:00
var is_active = false
2021-07-11 16:09:04 +02:00
var landing_site_local_coord = null
var landing_site_world = null
var ship_anchor_world = null
var obstacles_local_coords = {}
2021-07-16 10:38:57 +02:00
var treasure_local_coords = null
var highlight_treasure = false
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()
2021-07-09 14:17:25 +02:00
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)
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
2021-07-09 14:17:25 +02:00
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)))
2021-07-09 14:17:25 +02:00
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
2021-07-11 16:09:04 +02:00
# brute force the bbox tiles
obstacles_local_coords = {}
var dx = Globals.hex_size * 0.5
var dy = Globals.hex_size * 0.5
var n_tiles_x = (rect_local.size.x + Globals.hex_size * 2) / dx
var n_tiles_y = (rect_local.size.y + Globals.hex_size * 2) / dy
for r in range (n_tiles_y):
for c in range (n_tiles_x):
var point_hex_local = Globals.WorldToHexCenter(Vector2(rect_local.position.x+ c * dx, rect_local.position.y + r * dy) - Vector2.ONE * Globals.hex_size * 0.5)
if not obstacles_local_coords.has(point_hex_local) and not tiles.has(point_hex_local):
obstacles_local_coords[point_hex_local] = "#bb44aa"
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]
name = path
calc_bbox()
update()
2021-06-28 21:51:09 +02:00
func check_overlap(other):
2021-07-09 14:17:25 +02:00
var rect_world = calc_rect_world()
var rect_world_other = other.calc_rect_world()
2021-07-09 14:17:25 +02:00
return rect_world.intersects(rect_world_other)
2021-07-09 18:16:21 +02:00
func get_tile_by_world_coord(world_coord: Vector2):
2021-07-11 16:09:04 +02:00
var local_coord = get_local_coord_by_world_coord(world_coord)
if local_coord in tiles.keys():
return tiles[local_coord]
2021-07-09 18:16:21 +02:00
return null
2021-07-16 12:07:11 +02:00
func is_point_on_treasure_site(world_coord: Vector2):
var local_coord = get_local_coord_by_world_coord(world_coord)
return treasure_local_coords != null and local_coord == treasure_local_coords
2021-07-11 16:09:04 +02:00
func is_point_on_landing_site(world_coord: Vector2):
return (Globals.WorldToHex(world_coord) - Globals.WorldToHex(landing_site_world)).length_squared() < 1
func get_local_coord_by_world_coord(world_coord: Vector2):
return Globals.WorldToHexCenter(world_coord - offset_world)
2021-07-09 14:17:25 +02:00
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)
2021-07-10 14:18:47 +02:00
2021-06-28 21:51:09 +02:00
func _draw():
2021-07-11 16:09:04 +02:00
var transform = get_transform()
2021-06-28 21:51:09 +02:00
for coord in tiles.keys():
2021-07-09 14:17:25 +02:00
draw_set_transform (coord + offset_world, 0, Vector2.ONE)
2021-06-28 21:51:09 +02:00
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))
2021-07-16 10:38:57 +02:00
if treasure_local_coords and highlight_treasure:
draw_set_transform (treasure_local_coords + offset_world, 0, Vector2.ONE)
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.create_color_array("#922"))
2021-07-09 14:17:25 +02:00
draw_set_transform(transform.origin + offset_world, transform.get_rotation(), transform.get_scale())
2021-07-11 16:09:04 +02:00
if Globals.debug_nav and is_active:
2021-07-10 14:57:18 +02:00
draw_rect(rect_local, Color.red, false)
2021-07-11 16:09:04 +02:00
draw_set_transform_matrix(transform)
for coord in obstacles_local_coords.keys():
draw_set_transform(coord + offset_world, 0, Vector2.ONE)
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.create_color_array("#922"))