2021-06-15 21:58:31 +02:00
|
|
|
extends Node2D
|
|
|
|
|
2021-06-28 21:51:09 +02:00
|
|
|
|
|
|
|
onready var Grid = get_node("Grid")
|
|
|
|
onready var GridHighlight = get_node("GridHighlight")
|
|
|
|
|
|
|
|
onready var Islands = get_node("Islands")
|
2021-06-19 23:14:01 +02:00
|
|
|
onready var EditIslandButton = get_node("UI/TopContainer/EditIslandButton")
|
2021-06-15 21:58:31 +02:00
|
|
|
onready var WorldCamera = get_node("Camera")
|
|
|
|
onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue")
|
|
|
|
onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue")
|
|
|
|
onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue")
|
2021-07-07 22:22:07 +02:00
|
|
|
onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue")
|
2021-06-17 23:03:45 +02:00
|
|
|
onready var PlayerChar = get_node("PlayerChar")
|
2021-07-09 14:17:25 +02:00
|
|
|
onready var PlayerBoat = get_node("PlayerBoat")
|
2021-06-17 23:07:57 +02:00
|
|
|
onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue")
|
2021-06-15 21:58:31 +02:00
|
|
|
|
2021-06-27 12:26:44 +02:00
|
|
|
var Island = preload("Island.gd")
|
2021-06-15 21:58:31 +02:00
|
|
|
|
2021-06-17 23:03:45 +02:00
|
|
|
var hex_grid_bbox = [[0,0], [10,10]]
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
var hex_hover = Vector2.ZERO
|
|
|
|
var is_dragging = false
|
|
|
|
var drag_start = null
|
2021-06-17 23:03:45 +02:00
|
|
|
var target = Vector2()
|
2021-06-19 23:14:01 +02:00
|
|
|
var tile_data = {}
|
2021-06-28 22:34:48 +02:00
|
|
|
var current_island = null
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
|
2021-06-28 21:51:09 +02:00
|
|
|
func _ready():
|
|
|
|
Grid.view_camera = WorldCamera
|
|
|
|
GridHighlight.view_camera = WorldCamera
|
2021-06-17 23:07:57 +02:00
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
generate()
|
2021-06-15 21:58:31 +02:00
|
|
|
|
2021-06-28 21:51:09 +02:00
|
|
|
# Set player starting position
|
|
|
|
PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0))
|
2021-06-17 23:07:57 +02:00
|
|
|
|
|
|
|
|
2021-07-09 14:17:25 +02:00
|
|
|
func clear_islands():
|
|
|
|
for island in Islands.get_children():
|
|
|
|
Islands.remove_child(island)
|
|
|
|
island.queue_free()
|
|
|
|
|
|
|
|
|
|
|
|
func _process(_delta):
|
|
|
|
WorldCamera.offset = PlayerChar.position
|
|
|
|
PlayerBoat.transform.origin = PlayerChar.transform.origin
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
func check_island_overlap(new_island):
|
|
|
|
var islands = Islands.get_children()
|
|
|
|
|
|
|
|
for island in islands:
|
|
|
|
if island.check_overlap(new_island):
|
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
func add_island_at(file_name, offset_world: Vector2):
|
|
|
|
var island = Island.new()
|
|
|
|
island.load_island(file_name)
|
|
|
|
island.offset_world = offset_world
|
|
|
|
Islands.add_child(island)
|
|
|
|
|
|
|
|
|
|
|
|
func populate_ocean_grid():
|
|
|
|
Globals.OceanGrid.remove_obstacles(Globals.OceanGrid.get_obstacles())
|
|
|
|
|
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
func generate():
|
|
|
|
var rng = RandomNumberGenerator.new()
|
|
|
|
rng.randomize()
|
|
|
|
|
2021-07-09 14:17:25 +02:00
|
|
|
clear_islands()
|
|
|
|
|
|
|
|
var radius = 1300
|
|
|
|
var num_islands = 10
|
2021-07-07 22:22:07 +02:00
|
|
|
|
|
|
|
for i in range (num_islands):
|
|
|
|
var island = Island.new()
|
2021-07-09 14:17:25 +02:00
|
|
|
var island_index = i % 4
|
|
|
|
var file_name = "user://pirate_game_island_" + str(island_index) + ".island"
|
2021-07-07 22:22:07 +02:00
|
|
|
island.load_island(file_name)
|
|
|
|
|
2021-07-09 14:17:25 +02:00
|
|
|
var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
|
|
|
|
island.offset_world = Globals.WorldToHexCenter(rand_coord)
|
|
|
|
|
|
|
|
var overlapping = check_island_overlap(island)
|
|
|
|
var overlap_retry_num = 0
|
|
|
|
var overlap_retry_max = 10
|
|
|
|
while overlapping and overlap_retry_num < overlap_retry_max:
|
|
|
|
if overlap_retry_num % 4 == 0:
|
|
|
|
radius = radius + 500
|
|
|
|
overlap_retry_num = overlap_retry_num + 1
|
|
|
|
|
|
|
|
rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
|
|
|
|
island.offset_world = Globals.WorldToHexCenter(rand_coord)
|
|
|
|
overlapping = check_island_overlap(island)
|
2021-07-07 22:22:07 +02:00
|
|
|
|
|
|
|
if overlapping:
|
2021-07-09 14:17:25 +02:00
|
|
|
print ("Could not place island!")
|
2021-07-07 22:22:07 +02:00
|
|
|
else:
|
2021-07-09 14:17:25 +02:00
|
|
|
print ("Placed after " + str(overlap_retry_num) + " retries.")
|
2021-07-07 22:22:07 +02:00
|
|
|
Islands.add_child(island)
|
2021-07-09 14:17:25 +02:00
|
|
|
|
|
|
|
populate_ocean_grid()
|
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
|
|
|
|
|
2021-06-19 23:14:01 +02:00
|
|
|
func handle_game_event(event):
|
2021-06-15 21:58:31 +02:00
|
|
|
if event is InputEventMouseButton:
|
2021-06-17 23:03:45 +02:00
|
|
|
# Move main character
|
|
|
|
if event.pressed and event.button_index == BUTTON_LEFT:
|
2021-06-28 21:51:09 +02:00
|
|
|
PlayerChar.target = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera))
|
2021-06-19 23:14:01 +02:00
|
|
|
return true
|
2021-06-22 22:47:01 +02:00
|
|
|
|
|
|
|
return false
|
2021-06-28 21:51:09 +02:00
|
|
|
|
|
|
|
|
2021-06-19 23:14:01 +02:00
|
|
|
func _unhandled_input(event):
|
|
|
|
if event is InputEventMouseButton:
|
2021-06-27 12:26:44 +02:00
|
|
|
if handle_game_event(event):
|
2021-06-19 23:14:01 +02:00
|
|
|
return
|
|
|
|
|
2021-06-17 23:03:45 +02:00
|
|
|
# Move camera
|
2021-06-17 23:07:57 +02:00
|
|
|
if event.pressed and event.button_index == 2:
|
2021-06-15 21:58:31 +02:00
|
|
|
is_dragging = true
|
|
|
|
drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position)
|
|
|
|
else:
|
|
|
|
is_dragging = false
|
|
|
|
|
2021-06-17 23:03:45 +02:00
|
|
|
# Zoom Camera
|
2021-06-28 21:51:09 +02:00
|
|
|
if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed and WorldCamera.zoom.y < 5.5:
|
2021-06-15 21:58:31 +02:00
|
|
|
WorldCamera.zoom = WorldCamera.zoom * 1.0 / 0.8
|
|
|
|
elif event.button_index == BUTTON_WHEEL_UP and event.pressed:
|
|
|
|
WorldCamera.zoom = WorldCamera.zoom * 0.8
|
|
|
|
|
|
|
|
if 'position' in event:
|
2021-06-28 21:51:09 +02:00
|
|
|
hex_hover = Globals.ScreenToHex(event.position, WorldCamera)
|
|
|
|
GridHighlight.pos = hex_hover
|
2021-06-22 22:47:01 +02:00
|
|
|
HexCoordValueLabel.text = str(hex_hover)
|
2021-07-09 14:17:25 +02:00
|
|
|
|
|
|
|
# Islands.get_children()[1].offset_world = Globals.HexToWorld(hex_hover)
|
|
|
|
# Islands.get_children()[1].update()
|
2021-06-22 22:47:01 +02:00
|
|
|
|
2021-07-07 22:22:07 +02:00
|
|
|
WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera))
|
|
|
|
|
2021-06-15 21:58:31 +02:00
|
|
|
if is_dragging:
|
|
|
|
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
|
2021-06-28 21:51:09 +02:00
|
|
|
|
|
|
|
|
2021-06-15 21:58:31 +02:00
|
|
|
OffsetValueLabel.text = str(WorldCamera.offset)
|
|
|
|
ZoomValueLabel.text = str(WorldCamera.zoom)
|
2021-06-22 22:47:01 +02:00
|
|
|
|
|
|
|
|
2021-06-27 12:26:44 +02:00
|
|
|
func save_world(path: String):
|
2021-06-22 22:47:01 +02:00
|
|
|
var world_save_data = File.new()
|
2021-06-27 12:26:44 +02:00
|
|
|
world_save_data.open(path, File.WRITE)
|
2021-06-22 22:47:01 +02:00
|
|
|
world_save_data.store_line (to_json(tile_data))
|
|
|
|
world_save_data.close()
|
|
|
|
|
|
|
|
|
2021-06-27 12:26:44 +02:00
|
|
|
func load_world(path: String):
|
2021-06-22 22:47:01 +02:00
|
|
|
var world_save_data = File.new()
|
2021-06-27 12:26:44 +02:00
|
|
|
world_save_data.open(path, File.READ)
|
2021-06-22 22:47:01 +02:00
|
|
|
var data = parse_json(world_save_data.get_line())
|
|
|
|
world_save_data.close()
|
|
|
|
|
|
|
|
var data_keys = data.keys()
|
|
|
|
tile_data = {}
|
|
|
|
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")))
|
|
|
|
tile_data[coords] = data[k]
|
2021-06-27 12:26:44 +02:00
|
|
|
|
2021-06-22 22:47:01 +02:00
|
|
|
update()
|
2021-07-07 22:22:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_generate_button_pressed():
|
|
|
|
generate()
|