Extended HexGrid to allow getting hex center from offset coordinates.

master
Martin Felis 2022-01-23 08:29:01 +01:00
parent f0c3035a5f
commit 1a820aa26a
4 changed files with 128 additions and 0 deletions

View File

@ -207,6 +207,12 @@ func get_hex_center(hex):
hex = HexCell.new(hex) hex = HexCell.new(hex)
return hex_transform * hex.axial_coords return hex_transform * hex.axial_coords
func get_hex_center_from_offset(offset):
# Returns hex's centre position at the given offset coordinates
var hex = HexCell.new()
hex.offset_coords = offset
return hex_transform * hex.axial_coords
func get_hex_at(coords): func get_hex_at(coords):
# Returns a HexCell at the given Vector2/3 on the projection plane # Returns a HexCell at the given Vector2/3 on the projection plane
# If the given value is a Vector3, its x,z coords will be used # If the given value is a Vector3, its x,z coords will be used

View File

@ -0,0 +1,19 @@
tool
extends Polygon2D
export(Color) var OutLine = Color(0,0,0) setget set_color
export(float) var Width = 2.0 setget set_width
func _draw():
var poly = get_polygon()
for i in range(1 , poly.size()):
draw_line(poly[i-1] , poly[i], OutLine , Width)
draw_line(poly[poly.size() - 1] , poly[0], OutLine , Width)
func set_color(color):
OutLine = color
update()
func set_width(new_width):
Width = new_width
update()

View File

@ -0,0 +1,51 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://HexShape.gd" type="Script" id=1]
[node name="Highlight" type="Polygon2D"]
color = Color( 1, 1, 1, 0 )
polygon = PoolVector2Array( -12.5, 21.6506, 12.5, 21.6506, 25, 0, 12.5, -21.6506, -12.5, -21.6506, -25, 0 )
script = ExtResource( 1 )
OutLine = Color( 1, 1, 1, 0.133333 )
[node name="Label" type="Label" parent="."]
visible = false
margin_left = 5.0
margin_top = -39.0
margin_right = 52.0
margin_bottom = -25.0
text = "SCREEN"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AreaCoords" type="Label" parent="."]
visible = false
margin_left = 55.0
margin_top = -39.0
margin_right = 105.0
margin_bottom = -25.0
text = "SCREEN"
[node name="Label2" type="Label" parent="."]
visible = false
margin_left = 25.0
margin_top = -19.0
margin_right = 56.0
margin_bottom = -5.0
text = "HEX"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HexCoords" type="Label" parent="."]
margin_left = -26.0
margin_top = -8.0
margin_right = 24.0
margin_bottom = 6.0
text = "HEX"
align = 1
valign = 1
__meta__ = {
"_edit_use_anchors_": false
}

View File

@ -2,15 +2,55 @@
extends Node2D extends Node2D
var HexGrid = preload("./HexGrid.gd").new() var HexGrid = preload("./HexGrid.gd").new()
var HexShape = preload("./HexShape.tscn")
onready var highlight = get_node("Highlight") onready var highlight = get_node("Highlight")
onready var area_coords = get_node("Highlight/AreaCoords") onready var area_coords = get_node("Highlight/AreaCoords")
onready var hex_coords = get_node("Highlight/HexCoords") onready var hex_coords = get_node("Highlight/HexCoords")
var rect_min = Vector2(-200, 50)
var rect_max = Vector2(400, 200)
var drag_start = null
func _ready(): func _ready():
HexGrid.hex_scale = Vector2(50, 50) HexGrid.hex_scale = Vector2(50, 50)
func _draw():
var hex_cell_bb_min = HexGrid.get_hex_at(rect_min).offset_coords
var hex_cell_bb_max = HexGrid.get_hex_at(rect_max).offset_coords
var hex_cell_offset_min = Vector2(
min(hex_cell_bb_min.x, hex_cell_bb_max.x),
min(hex_cell_bb_min.y, hex_cell_bb_max.y)
)
var hex_cell_offset_max = Vector2(
max(hex_cell_bb_min.x, hex_cell_bb_max.x),
max(hex_cell_bb_min.y, hex_cell_bb_max.y)
)
var rect_highlights = get_node ("rect_highlights")
if rect_highlights:
remove_child(rect_highlights)
rect_highlights.queue_free()
rect_highlights = Node2D.new()
rect_highlights.name = "rect_highlights"
add_child(rect_highlights)
for xi in range (hex_cell_offset_min.x, hex_cell_offset_max.x + 1):
for yi in range (hex_cell_offset_min.y, hex_cell_offset_max.y + 1):
var offset_coords = Vector2(xi, yi)
var hex_shape = HexShape.instance()
hex_shape.position = HexGrid.get_hex_center_from_offset(offset_coords)
var hex_shape_coords = hex_shape.get_node("HexCoords")
hex_shape_coords.text = str(offset_coords)
rect_highlights.add_child (hex_shape)
var rect = Rect2(rect_min, rect_max - rect_min)
draw_rect (rect, Color("55FFFFFF"))
func _unhandled_input(event): func _unhandled_input(event):
if 'position' in event: if 'position' in event:
@ -24,3 +64,15 @@ func _unhandled_input(event):
# Snap the highlight to the nearest grid cell # Snap the highlight to the nearest grid cell
if highlight != null: if highlight != null:
highlight.position = HexGrid.get_hex_center(HexGrid.get_hex_at(relative_pos)) highlight.position = HexGrid.get_hex_center(HexGrid.get_hex_at(relative_pos))
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.pressed:
drag_start = relative_pos
else:
drag_start = null
if drag_start != null:
rect_min = Vector2(min(drag_start.x, relative_pos.x), min(drag_start.y, relative_pos.y))
rect_max = Vector2(max(drag_start.x, relative_pos.x), max(drag_start.y, relative_pos.y))
update()