-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.gd
54 lines (48 loc) · 1.79 KB
/
Camera.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class_name ZoomingCamera2D
extends Camera2D
# Lower cap for the `_zoom_level`.
export var min_zoom := 0.1
# Upper cap for the `_zoom_level`.
export var max_zoom := 5.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
const MOVE_SPEED = 100
# The camera's target zoom level.
var _zoom_level := 2.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)
func _process(delta):
if Input.is_action_pressed('ui_left'):
global_position += Vector2.LEFT * delta * MOVE_SPEED
elif Input.is_action_pressed('ui_right'):
global_position += Vector2.RIGHT * delta * MOVE_SPEED
if Input.is_action_pressed("ui_up"):
global_position += Vector2.UP * delta * MOVE_SPEED
elif Input.is_action_pressed('ui_down'):
global_position += Vector2.DOWN * delta * MOVE_SPEED
print(_zoom_level)