2021-06-15 21:58:31 +02:00
|
|
|
extends Node2D
|
|
|
|
|
2021-06-19 23:14:01 +02:00
|
|
|
onready var EditIslandButton = get_node("UI/TopContainer/EditIslandButton")
|
|
|
|
onready var TileTypes = get_node("UI/TileTypes")
|
2021-06-22 22:47:01 +02:00
|
|
|
onready var NoneButton = get_node("UI/Editor/NoneButton")
|
2021-06-19 23:14:01 +02:00
|
|
|
onready var GridDrawer = get_node("GridDrawer")
|
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-06-17 23:03:45 +02:00
|
|
|
onready var PlayerChar = get_node("PlayerChar")
|
2021-06-17 23:07:57 +02:00
|
|
|
onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue")
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new()
|
|
|
|
|
|
|
|
var hex_size = 128
|
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-15 21:58:31 +02:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2021-06-22 22:47:01 +02:00
|
|
|
_on_LoadWorldButton_pressed()
|
2021-06-15 21:58:31 +02:00
|
|
|
HexGrid.hex_scale = Vector2(hex_size, hex_size)
|
2021-06-17 23:03:45 +02:00
|
|
|
|
|
|
|
# Set player starting position
|
|
|
|
PlayerChar.position = HexGrid.get_hex_center(Vector2(0,0))
|
2021-06-19 23:14:01 +02:00
|
|
|
GridDrawer.hex_scale = Vector2(hex_size, hex_size)
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
func _draw():
|
2021-06-19 23:14:01 +02:00
|
|
|
draw_tiles()
|
2021-06-22 22:47:01 +02:00
|
|
|
draw_grid()
|
|
|
|
|
2021-06-19 23:14:01 +02:00
|
|
|
|
|
|
|
func draw_grid():
|
2021-06-17 23:07:57 +02:00
|
|
|
var view_grid_corners = calc_grid_view_rect()
|
|
|
|
|
|
|
|
var tl_tile_coords = HexGrid.get_hex_center(view_grid_corners[0])
|
|
|
|
var tr_tile_coords = HexGrid.get_hex_center(view_grid_corners[1])
|
|
|
|
var bl_tile_coords = HexGrid.get_hex_center(view_grid_corners[2])
|
|
|
|
var br_tile_coords = HexGrid.get_hex_center(view_grid_corners[3])
|
|
|
|
|
|
|
|
var nc = ceil ((tr_tile_coords[0] - tl_tile_coords[0]) * 1.5 / hex_size)
|
|
|
|
var nr = ceil ((bl_tile_coords[1] - tr_tile_coords[1]) / hex_size) + 2
|
|
|
|
|
|
|
|
for c in range (nc):
|
|
|
|
var dx = 0.75 * hex_size * c
|
|
|
|
var dy = (c % 2) * sqrt(3) * 0.25 * hex_size #(c % 2) * sqrt(3) * hex_size * 0.75
|
|
|
|
for r in range (nr):
|
|
|
|
draw_set_transform(tl_tile_coords + Vector2(dx, r * hex_size * sqrt(3) * 0.5 + dy), 0, Vector2.ONE)
|
2021-06-19 23:14:01 +02:00
|
|
|
draw_polyline(GridDrawer.HexPoints, "#888", 1 * WorldCamera.zoom.x)
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
var coords = HexGrid.get_hex_center (hex_hover)
|
|
|
|
draw_set_transform(coords, 0, Vector2.ONE)
|
2021-06-19 23:14:01 +02:00
|
|
|
draw_polyline(GridDrawer.HexPoints, "#f00", 2 * WorldCamera.zoom.x)
|
|
|
|
|
|
|
|
func draw_tiles():
|
|
|
|
for coord in tile_data.keys():
|
|
|
|
draw_set_transform (coord, 0, Vector2.ONE)
|
|
|
|
draw_polygon(GridDrawer.HexPoints, GridDrawer.get_tile_color(tile_data[coord]))
|
2021-06-15 21:58:31 +02:00
|
|
|
|
|
|
|
func screen_to_world(pos: Vector2):
|
|
|
|
pos = pos - OS.get_window_safe_area().size * 0.5
|
|
|
|
return WorldCamera.offset + pos * WorldCamera.zoom
|
|
|
|
|
|
|
|
func screen_to_hex(pos: Vector2):
|
|
|
|
return HexGrid.get_hex_at(screen_to_world(pos)).axial_coords
|
|
|
|
|
|
|
|
func world_to_screen(pos: Vector2):
|
|
|
|
return pos * WorldCamera.zoom + WorldCamera.offset
|
|
|
|
|
2021-06-17 23:07:57 +02:00
|
|
|
func calc_grid_view_rect():
|
|
|
|
var view_tl = Vector2(-hex_size,-hex_size)
|
|
|
|
var view_br = OS.get_window_safe_area().size - view_tl
|
|
|
|
|
|
|
|
var top_left = screen_to_hex (view_tl)
|
|
|
|
var top_right = screen_to_hex (Vector2(view_br.x, view_tl.y))
|
|
|
|
var bottom_left = screen_to_hex (Vector2(view_tl.x, view_br.y))
|
|
|
|
var bottom_right = screen_to_hex (view_br)
|
|
|
|
return [top_left, top_right, bottom_left, bottom_right]
|
|
|
|
|
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:
|
|
|
|
PlayerChar.target = HexGrid.get_hex_center(screen_to_hex(event.position))
|
2021-06-19 23:14:01 +02:00
|
|
|
return true
|
2021-06-22 22:47:01 +02:00
|
|
|
|
|
|
|
return false
|
2021-06-19 23:14:01 +02:00
|
|
|
|
|
|
|
func handle_editor_event(event):
|
2021-06-22 22:47:01 +02:00
|
|
|
if (Input.get_mouse_button_mask() & BUTTON_LEFT) == BUTTON_LEFT:
|
|
|
|
# if event.pressed and event.button_index == BUTTON_LEFT:
|
|
|
|
var pressed_button = NoneButton.group.get_pressed_button()
|
|
|
|
var tile_type = "None"
|
|
|
|
if pressed_button:
|
|
|
|
tile_type = pressed_button.text
|
|
|
|
|
|
|
|
var hex_coord = HexGrid.get_hex_center(screen_to_hex(event.position))
|
|
|
|
if tile_type != "None":
|
|
|
|
tile_data[hex_coord] = tile_type
|
|
|
|
update()
|
|
|
|
elif hex_coord in tile_data.keys():
|
|
|
|
tile_data.erase(hex_coord)
|
|
|
|
update()
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
2021-06-19 23:14:01 +02:00
|
|
|
|
|
|
|
func _unhandled_input(event):
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
if EditIslandButton.pressed:
|
|
|
|
if handle_editor_event (event):
|
|
|
|
return
|
|
|
|
elif handle_game_event(event):
|
|
|
|
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-19 23:14:01 +02:00
|
|
|
if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed and WorldCamera.zoom.y < 3.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-22 22:47:01 +02:00
|
|
|
hex_hover = screen_to_hex(event.position)
|
|
|
|
HexCoordValueLabel.text = str(hex_hover)
|
|
|
|
|
2021-06-15 21:58:31 +02:00
|
|
|
if is_dragging:
|
|
|
|
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
|
2021-06-22 22:47:01 +02:00
|
|
|
elif EditIslandButton.pressed:
|
|
|
|
if handle_editor_event (event):
|
|
|
|
return
|
2021-06-19 23:14:01 +02:00
|
|
|
|
2021-06-15 21:58:31 +02:00
|
|
|
update()
|
|
|
|
|
|
|
|
OffsetValueLabel.text = str(WorldCamera.offset)
|
|
|
|
ZoomValueLabel.text = str(WorldCamera.zoom)
|
2021-06-22 22:47:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_SaveWorldButton_pressed():
|
|
|
|
var world_save_data = File.new()
|
|
|
|
world_save_data.open("user://pirate_game_world.save", File.WRITE)
|
|
|
|
world_save_data.store_line (to_json(tile_data))
|
|
|
|
world_save_data.close()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_ClearWorldButton_pressed():
|
|
|
|
tile_data = {}
|
|
|
|
update()
|
|
|
|
|
|
|
|
func _on_LoadWorldButton_pressed():
|
|
|
|
var world_save_data = File.new()
|
|
|
|
world_save_data.open("user://pirate_game_world.save", 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()
|