added zoom

This commit is contained in:
fram3d 2024-07-02 14:13:19 +02:00
parent f87abc0631
commit 0813605482
Signed by: fram3d
GPG Key ID: 938920E709EEA32A
3 changed files with 63 additions and 1 deletions

View File

@ -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 )

41
src/camera.gd Normal file
View File

@ -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)

View File

@ -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