From 08136054826f17901e6adce780cc37373a2b6366 Mon Sep 17 00:00:00 2001 From: fram3d Date: Tue, 2 Jul 2024 14:13:19 +0200 Subject: [PATCH] added zoom --- src/World.tscn | 10 +++++++++- src/camera.gd | 41 +++++++++++++++++++++++++++++++++++++++++ src/project.godot | 13 +++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/camera.gd diff --git a/src/World.tscn b/src/World.tscn index 6103684..8ac92b3 100644 --- a/src/World.tscn +++ b/src/World.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=15 format=2] +[gd_scene load_steps=16 format=2] [ext_resource path="res://Tilesets/stone.png" type="Texture" id=1] [ext_resource path="res://Tilesets/dirt.png" type="Texture" id=2] @@ -7,6 +7,7 @@ [ext_resource path="res://NPCs/ooze.tscn" type="PackedScene" id=5] [ext_resource path="res://NPCs/moss.tscn" type="PackedScene" id=6] [ext_resource path="res://Audio/Background/cave.ogg" type="AudioStream" id=7] +[ext_resource path="res://camera.gd" type="Script" id=8] [sub_resource type="ConvexPolygonShape2D" id=1] points = PoolVector2Array( 0, 0, 8, 0, 8, 8, 0, 8 ) @@ -136,6 +137,13 @@ points = PoolVector2Array( 0, 0, 8, 0, 8, 8, 0, 8 ) [node name="World" type="Node2D"] +[node name="Camera2D" type="Camera2D" parent="."] +position = Vector2( 40, 32 ) +current = true +script = ExtResource( 8 ) + +[node name="Tween" type="Tween" parent="Camera2D"] + [node name="TileMap" type="TileMap" parent="."] tile_set = SubResource( 7 ) cell_size = Vector2( 8, 8 ) diff --git a/src/camera.gd b/src/camera.gd new file mode 100644 index 0000000..f6d8dfd --- /dev/null +++ b/src/camera.gd @@ -0,0 +1,41 @@ +extends Camera2D + +# Lower cap for the `_zoom_level`. +export var min_zoom := 0.5 +# Upper cap for the `_zoom_level`. +export var max_zoom := 2.0 +# Controls how much we increase or decrease the `_zoom_level` on every turn of the scroll wheel. +export var zoom_factor := 0.1 +# Duration of the zoom's tween animation. +export var zoom_duration := 0.2 + +# The camera's target zoom level. +var _zoom_level := 1.0 setget _set_zoom_level + +# We store a reference to the scene's tween node. +onready var tween: Tween = $Tween + +func _set_zoom_level(value: float) -> void: + # We limit the value between `min_zoom` and `max_zoom` + _zoom_level = clamp(value, min_zoom, max_zoom) + # Then, we ask the tween node to animate the camera's `zoom` property from its current value + # to the target zoom level. + tween.interpolate_property( + self, + "zoom", + zoom, + Vector2(_zoom_level, _zoom_level), + zoom_duration, + tween.TRANS_SINE, + # Easing out means we start fast and slow down as we reach the target value. + tween.EASE_OUT + ) + tween.start() + +func _unhandled_input(event): + if event.is_action_pressed("zoom_in"): + # Inside a given class, we need to either write `self._zoom_level = ...` or explicitly + # call the setter function to use it. + _set_zoom_level(_zoom_level - zoom_factor) + if event.is_action_pressed("zoom_out"): + _set_zoom_level(_zoom_level + zoom_factor) diff --git a/src/project.godot b/src/project.godot index a880779..7b6dc5e 100644 --- a/src/project.godot +++ b/src/project.godot @@ -37,6 +37,19 @@ window/stretch/mode="2d" singletons=[ ] +[input] + +zoom_in={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":4,"pressed":false,"doubleclick":false,"script":null) + ] +} +zoom_out={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":5,"pressed":false,"doubleclick":false,"script":null) + ] +} + [physics] common/physics_fps=5