From 5e102c16403c8e821c6042dc3e8e1ef46d4806b7 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 15 Jun 2021 21:58:31 +0200 Subject: [PATCH] Starting from scratch, added Game scene --- project.godot | 5 +++ scenes/Game.tscn | 77 +++++++++++++++++++++++++++++++++++++++++++++++ scenes/HexTile.gd | 38 +++++++++++++++++++++++ scenes/World.gd | 67 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 scenes/Game.tscn create mode 100644 scenes/HexTile.gd create mode 100644 scenes/World.gd diff --git a/project.godot b/project.godot index 6c562d8..90acb5f 100644 --- a/project.godot +++ b/project.godot @@ -8,6 +8,11 @@ config_version=4 +_global_script_classes=[ ] +_global_script_class_icons={ + +} + [application] config/name="LilPirateTreasureHunt" diff --git a/scenes/Game.tscn b/scenes/Game.tscn new file mode 100644 index 0000000..27e9edc --- /dev/null +++ b/scenes/Game.tscn @@ -0,0 +1,77 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scenes/World.gd" type="Script" id=1] +[ext_resource path="res://scenes/HexTile.gd" type="Script" id=2] + +[node name="GameRoot" type="Node"] + +[node name="World" type="Node2D" parent="."] +script = ExtResource( 1 ) + +[node name="Camera" type="Camera2D" parent="World"] +current = true + +[node name="HexTile" type="Node" parent="World"] +script = ExtResource( 2 ) + +[node name="UI" type="CanvasLayer" parent="World"] + +[node name="TopContainer" type="HBoxContainer" parent="World/UI"] +margin_right = 40.0 +margin_bottom = 40.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Button" type="Button" parent="World/UI/TopContainer"] +margin_right = 55.0 +margin_bottom = 40.0 +text = "Reload" + +[node name="Offset" type="Label" parent="World/UI/TopContainer"] +margin_left = 59.0 +margin_top = 13.0 +margin_right = 106.0 +margin_bottom = 27.0 +text = "Offset: " + +[node name="OffsetValue" type="Label" parent="World/UI/TopContainer"] +margin_left = 110.0 +margin_top = 13.0 +margin_right = 130.0 +margin_bottom = 27.0 +text = "0,0" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Zoom" type="Label" parent="World/UI/TopContainer"] +margin_left = 134.0 +margin_top = 13.0 +margin_right = 183.0 +margin_bottom = 27.0 +text = " Zoom: " + +[node name="ZoomValue" type="Label" parent="World/UI/TopContainer"] +margin_left = 187.0 +margin_top = 13.0 +margin_right = 207.0 +margin_bottom = 27.0 +text = "0.0" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="HexCoord" type="Label" parent="World/UI/TopContainer"] +margin_left = 211.0 +margin_top = 13.0 +margin_right = 285.0 +margin_bottom = 27.0 +text = " HexCoord: " + +[node name="HexCoordValue" type="Label" parent="World/UI/TopContainer"] +margin_left = 289.0 +margin_top = 13.0 +margin_right = 317.0 +margin_bottom = 27.0 +text = "(0,0)" diff --git a/scenes/HexTile.gd b/scenes/HexTile.gd new file mode 100644 index 0000000..60ceabc --- /dev/null +++ b/scenes/HexTile.gd @@ -0,0 +1,38 @@ +extends Node + +enum TileType { + None, + Sand, + Grass +} + +var HexPoints = null +var HexColors = [] + +export(Vector2) var hex_scale = Vector2(1, 1) setget set_hex_scale + +# Called when the node enters the scene tree for the first time. +func _ready(): + set_hex_scale(Vector2(32,32)) + +func set_hex_scale(scale): + hex_scale = scale + + HexPoints = PoolVector2Array() + + var NoneColors = PoolColorArray() + var SandColors = PoolColorArray() + var GrassColors = PoolColorArray() + + for i in range (7): + var angle = (60 * i) * PI / 180 + HexPoints.append(Vector2(cos(angle), sin(angle)) * hex_scale.x / 2) + NoneColors.append("#555555") + SandColors.append("#ffa106") + GrassColors.append("#4b9635") + + HexColors = { + TileType.None: NoneColors, + TileType.Sand: SandColors, + TileType.Grass: GrassColors + } diff --git a/scenes/World.gd b/scenes/World.gd new file mode 100644 index 0000000..d744c00 --- /dev/null +++ b/scenes/World.gd @@ -0,0 +1,67 @@ +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") + +var HexGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new() + +var hex_size = 128 +var hex_grid_bbox = [[0,0], [100,100]] + +var hex_hover = Vector2.ZERO +var is_dragging = false +var drag_start = null + +# 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) + +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: + if event.pressed and event.button_index == 3: + is_dragging = true + drag_start = (WorldCamera.offset / WorldCamera.zoom.x + event.position) + else: + is_dragging = false + + 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)