PirateTreasureHunt/scenes/World.gd

156 lines
4.3 KiB
GDScript

extends Node2D
onready var Grid = get_node("Grid")
onready var GridHighlight = get_node("GridHighlight")
onready var Islands = get_node("Islands")
onready var EditIslandButton = get_node("UI/TopContainer/EditIslandButton")
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")
onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue")
onready var PlayerChar = get_node("PlayerChar")
onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue")
var Island = preload("Island.gd")
var hex_grid_bbox = [[0,0], [10,10]]
var hex_hover = Vector2.ZERO
var is_dragging = false
var drag_start = null
var target = Vector2()
var tile_data = {}
var islands = []
var current_island = null
func _ready():
Grid.view_camera = WorldCamera
GridHighlight.view_camera = WorldCamera
generate()
# Set player starting position
PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0))
func generate():
var rng = RandomNumberGenerator.new()
rng.randomize()
islands.clear()
for island in Islands.get_children():
Islands.remove_child(island)
island.queue_free()
var radius = 5
var num_islands = 3
for i in range (num_islands):
var overlapping = true
var find_free_retries = 10
var island = Island.new()
var file_name = "user://pirate_game_island_" + str(i) + ".island"
island.load_island(file_name)
while overlapping and find_free_retries > 0:
overlapping = false
island.offset = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
for world_island in Islands.get_children():
overlapping = world_island.check_overlap(island)
find_free_retries = find_free_retries - 1
if overlapping:
print ("Could not find free spot for island!")
else:
print ("Retries left: " + str(find_free_retries) )
Islands.add_child(island)
func handle_game_event(event):
if event is InputEventMouseButton:
# Move main character
if event.pressed and event.button_index == BUTTON_LEFT:
PlayerChar.target = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera))
return true
return false
func _unhandled_input(event):
if event is InputEventMouseButton:
if handle_game_event(event):
return
# Move camera
if event.pressed and event.button_index == 2:
is_dragging = true
drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position)
else:
is_dragging = false
# Zoom Camera
if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed and WorldCamera.zoom.y < 5.5:
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:
hex_hover = Globals.ScreenToHex(event.position, WorldCamera)
GridHighlight.pos = hex_hover
HexCoordValueLabel.text = str(hex_hover)
WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera))
if is_dragging:
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
OffsetValueLabel.text = str(WorldCamera.offset)
ZoomValueLabel.text = str(WorldCamera.zoom)
func save_world(path: String):
var world_save_data = File.new()
world_save_data.open(path, File.WRITE)
world_save_data.store_line (to_json(tile_data))
world_save_data.close()
func load_world(path: String):
var world_save_data = File.new()
world_save_data.open(path, File.READ)
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]
update()
func _on_generate_button_pressed():
generate()