summaryrefslogtreecommitdiffstats
path: root/demo/Camera.gd
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-07-20 12:26:48 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-07-20 12:26:48 +0200
commit89723ca94bd21f1ae14592a682e9c0e5c5f04a74 (patch)
tree7b5c67f9d05f253abc961c901336780755543d6d /demo/Camera.gd
parentf3c455fbfb8b28b095c161eb7769ed92f1acb862 (diff)
downloadgodot-hexgrid-89723ca94bd21f1ae14592a682e9c0e5c5f04a74.zip
godot-hexgrid-89723ca94bd21f1ae14592a682e9c0e5c5f04a74.tar.gz
move demo files into subdir demo
Diffstat (limited to 'demo/Camera.gd')
-rw-r--r--demo/Camera.gd36
1 files changed, 36 insertions, 0 deletions
diff --git a/demo/Camera.gd b/demo/Camera.gd
new file mode 100644
index 0000000..0dc6074
--- /dev/null
+++ b/demo/Camera.gd
@@ -0,0 +1,36 @@
+extends Camera2D
+
+var margin :Vector2
+var window : Vector2
+var map_center : Vector2
+var texture_size : Vector2
+var zoom_boundaries : Vector2
+
+func _ready():
+ margin = Vector2(0, 0)
+
+func configure(w : Vector2, c : Vector2, ts : Vector2) -> void:
+ window = w
+ map_center = c
+ texture_size = ts
+ var zout : float = max((texture_size.x + margin.x) / window.x, (texture_size.y + margin.y) / window.y)
+ zoom_boundaries = Vector2(zout - 0.5, zout)
+ update_camera(0, 0, zoom_boundaries.y)
+
+func update_camera(x : float, y : float, z : float) -> void:
+ if z != 0:
+ zoom.x = clamp(zoom.x + z, zoom_boundaries.x, zoom_boundaries.y)
+ zoom.y = zoom.x
+ position.x += x
+ position.y += y
+ var delta = texture_size + margin - (window * zoom.x)
+ if (delta.x <= 0):
+ position.x = map_center.x
+ else:
+ var dx = int(delta.x / 2)
+ position.x = clamp(position.x, map_center.x - dx, map_center.x + dx)
+ if (delta.y <= 0):
+ position.y = map_center.y
+ else:
+ var dy = int(delta.y / 2)
+ position.y = clamp(position.y, map_center.y - dy, map_center.y + dy)