2021-06-15 21:58:31 +02:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
onready var HexTile = get_node("HexTile")
|
|
|
|
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-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-15 21:58:31 +02:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
HexGrid.hex_scale = Vector2(hex_size, hex_size)
|
|
|
|
HexTile.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-15 21:58:31 +02:00
|
|
|
|
|
|
|
func _draw():
|
|
|
|
for r in range(hex_grid_bbox[0][0], hex_grid_bbox[1][0]):
|
|
|
|
for c in range(hex_grid_bbox[0][1], hex_grid_bbox[1][1]):
|
|
|
|
var coords = HexGrid.get_hex_center(Vector2(r,c))
|
|
|
|
draw_set_transform(coords, 0, Vector2.ONE)
|
|
|
|
draw_polygon (HexTile.HexPoints, HexTile.HexColors[HexTile.TileType.None])
|
|
|
|
draw_polyline(HexTile.HexPoints, "#888", 1 * WorldCamera.zoom.x)
|
|
|
|
|
|
|
|
var coords = HexGrid.get_hex_center (hex_hover)
|
|
|
|
draw_set_transform(coords, 0, Vector2.ONE)
|
|
|
|
draw_polyline(HexTile.HexPoints, "#f00", 2 * WorldCamera.zoom.x)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
func _unhandled_input(event):
|
|
|
|
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))
|
|
|
|
|
|
|
|
# Move camera
|
2021-06-15 21:58:31 +02:00
|
|
|
if event.pressed and event.button_index == 3:
|
|
|
|
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-15 21:58:31 +02:00
|
|
|
if event.pressed and event.button_index == BUTTON_WHEEL_DOWN and event.pressed:
|
|
|
|
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:
|
|
|
|
if is_dragging:
|
|
|
|
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
|
|
|
|
else:
|
|
|
|
hex_hover = screen_to_hex(event.position)
|
|
|
|
HexCoordValueLabel.text = str(hex_hover)
|
|
|
|
update()
|
|
|
|
|
|
|
|
OffsetValueLabel.text = str(WorldCamera.offset)
|
|
|
|
ZoomValueLabel.text = str(WorldCamera.zoom)
|