MORE Camera
This commit is contained in:
84
scenes/main/camera_2d.gd
Normal file
84
scenes/main/camera_2d.gd
Normal file
@@ -0,0 +1,84 @@
|
||||
extends Camera2D
|
||||
|
||||
@export_group("Zoom")
|
||||
@export var _target_zoom: float = 1.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
position = Vector2(500, 500)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseMotion:
|
||||
if event.button_mask == MOUSE_BUTTON_MASK_MIDDLE:
|
||||
position -= event.relative * zoom
|
||||
|
||||
if event is InputEventMouseButton:
|
||||
if event.is_pressed():
|
||||
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
||||
zoom_in()
|
||||
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
||||
zoom_out()
|
||||
|
||||
if event is InputEventAction:
|
||||
print("Action pressed", event.action)
|
||||
if event.is_action_pressed("camera_up"):
|
||||
move_up()
|
||||
elif event.is_action_pressed("camera_down"):
|
||||
move_down()
|
||||
elif event.is_action_pressed("camera_left"):
|
||||
move_left()
|
||||
elif event.is_action_pressed("camera_right"):
|
||||
move_right()
|
||||
|
||||
|
||||
@export_group("Zoom")
|
||||
@export var MIN_ZOOM: float = 0.1
|
||||
@export var MAX_ZOOM: float = 1.0
|
||||
@export var ZOOM_INCREMENT: float = 0.1
|
||||
|
||||
func zoom_in():
|
||||
_target_zoom = max(_target_zoom - ZOOM_INCREMENT, MIN_ZOOM)
|
||||
set_physics_process(true)
|
||||
|
||||
|
||||
func zoom_out():
|
||||
_target_zoom = min(_target_zoom + ZOOM_INCREMENT, MAX_ZOOM)
|
||||
set_physics_process(true)
|
||||
|
||||
const ZOOM_RATE: float = 8.0
|
||||
|
||||
@export var camera_speed: float = 10
|
||||
var target_position: Vector2
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
zoom = lerp(
|
||||
zoom,
|
||||
_target_zoom * Vector2.ONE,
|
||||
ZOOM_RATE * delta
|
||||
)
|
||||
|
||||
position = lerp(
|
||||
position,
|
||||
target_position,
|
||||
camera_speed * delta
|
||||
)
|
||||
|
||||
# set_physics_process(not is_equal_approx(zoom.x, _target_zoom))
|
||||
|
||||
|
||||
func move_up():
|
||||
target_position.y -= camera_speed
|
||||
|
||||
|
||||
func move_down():
|
||||
target_position.y += camera_speed
|
||||
|
||||
|
||||
func move_right():
|
||||
target_position.x += camera_speed
|
||||
|
||||
|
||||
func move_left():
|
||||
target_position.x -= camera_speed
|
||||
Reference in New Issue
Block a user