summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-07-15 09:26:14 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-07-15 09:26:14 +0200
commit29112c7cb534bddb7853fa9a34735bfde43839a1 (patch)
tree9529d224c6ac11f8a43640fb86a88b87180c0cd0
parent29b17954e10c68c082c2ccc8639476d176e498eb (diff)
downloadgodot-hexgrid-29112c7cb534bddb7853fa9a34735bfde43839a1.zip
godot-hexgrid-29112c7cb534bddb7853fa9a34735bfde43839a1.tar.gz
logic clean up
-rw-r--r--Camera.gd4
-rw-r--r--Main.gd14
-rw-r--r--Map.gd27
3 files changed, 26 insertions, 19 deletions
diff --git a/Camera.gd b/Camera.gd
index bb99330..694706e 100644
--- a/Camera.gd
+++ b/Camera.gd
@@ -8,9 +8,9 @@ var zoom_boundaries : Vector2
func _ready():
margin = Vector2(0, 0)
- #window = set within Main
-func on_configure(c : Vector2, ts : Vector2) -> void:
+func configure(w : Vector2, c : Vector2, ts : Vector2) -> void:
+ window = w
map_center = c
texture_size = ts
zoom_boundaries = Vector2(1.0, max((texture_size.x + margin.x) / window.x, (texture_size.y + margin.y) / window.y))
diff --git a/Main.gd b/Main.gd
index 3db3072..4b141ee 100644
--- a/Main.gd
+++ b/Main.gd
@@ -8,18 +8,22 @@ onready var Map : Sprite = $ViewportContainer/Viewport/Map
onready var Camera : Camera2D = $ViewportContainer/Viewport/Camera
func _ready():
- UI.get_node("rotate").connect("pressed", Map, "on_rotate")
+ UI.get_node("rotate").connect("pressed", self, "on_rotate")
UI.get_node("LOS").connect("pressed", self, "on_toggle")
UI.get_node("Move").connect("pressed", self, "on_toggle")
- Map.connect("configure", Camera, "on_configure")
Map.connect("hex_touched", self, "on_hex_touched")
- Camera.window = $ViewportContainer/Viewport.size
+ $ViewportContainer.connect("resized", self, "on_viewport_resized")
on_toggle()
- Map.on_rotate()
+ on_viewport_resized()
+
+func on_viewport_resized() -> void:
+ Camera.configure($ViewportContainer/Viewport.size, Map.center(), Map.texture_size())
+
+func on_rotate() -> void:
+ Map.rotate_map()
func on_toggle() -> void:
Map.set_mode(UI.get_node("LOS").pressed, UI.get_node("Move").pressed)
- Map.update()
func on_hex_touched(pos : Vector2, hex : Hex, key : int) -> void:
var s : String = ("offmap" if key == -1 else hex.inspect())
diff --git a/Map.gd b/Map.gd
index 46890e2..5a4da33 100644
--- a/Map.gd
+++ b/Map.gd
@@ -1,6 +1,5 @@
extends Sprite
-signal configure(center, texture_size)
signal hex_touched(pos, hex, key)
const MAPH : String = "res://assets/map-h.png"
@@ -30,11 +29,12 @@ var show_los : bool
var show_move : bool
func _ready():
+ drag = null
+ unit = Unit.new()
board = HexBoard.new()
board.tile_factory_fct = funcref(self, "get_tile")
board.v = false
- unit = Unit.new()
- drag = null
+ rotate_map()
func reset() -> void:
los.clear()
@@ -49,6 +49,12 @@ func reset() -> void:
for hex in $Hexes.get_children():
$Hexes.remove_child(hex)
hex.queue_free()
+ compute()
+
+func rotate_map() -> void:
+ texture = load(MAPH if board.v else MAPV)
+ configure()
+ reset()
func set_mode(l : bool, m : bool) -> void:
show_los = l
@@ -56,29 +62,26 @@ func set_mode(l : bool, m : bool) -> void:
compute()
func configure() -> void:
- var ts : Vector2 = texture.get_size()
var v0 : Vector2 = Vector2(50, 100)
- var c = ts / 2
if centered:
+ var ts : Vector2 = texture.get_size()
if board.v:
v0.x -= ts.y / 2
v0.y -= ts.x / 2
else:
v0 -= ts / 2
- c = Vector2(0, 0)
if board.v:
hex_rotation = 30
board.configure(10, 4, 100, v0, false)
else:
hex_rotation = 0
board.configure(10, 7, 100, v0, true)
- emit_signal("configure", c, ts)
- reset()
- compute()
-func on_rotate() -> void:
- texture = load(MAPH if board.v else MAPV)
- configure()
+func texture_size() -> Vector2:
+ return texture.get_size()
+
+func center() -> Vector2:
+ return Vector2(0, 0) if centered else texture.get_size() / 2
func on_mouse_move() -> void:
if drag != null: