impl camera

This commit is contained in:
Candygoblen123 2024-02-24 23:26:54 -05:00
parent 162fc09baa
commit 9cf1956770
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,42 @@
extends Camera2D
class_name PanningCamera2D
@export var MIN_ZOOM: float = 0.5
@export var MAX_ZOOM: float = 3.0
@export var ZOOM_RATE: float = 8.0
@export var ZOOM_INCREMENT: float = 0.1
var target_zoom: float = 1.0
var reset_pos: Vector2 = Vector2(0.0, 0.0)
func _physics_process(delta: float) -> void:
zoom = lerp(zoom, target_zoom * Vector2.ONE, ZOOM_RATE * delta)
set_physics_process(not is_equal_approx(zoom.x, target_zoom))
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.is_pressed():
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
zoom_in()
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
zoom_out()
if event is InputEventMouseMotion:
if event.button_mask == MOUSE_BUTTON_MASK_LEFT:
position -= event.relative / zoom
if event is InputEventKey:
if event.keycode == KEY_SPACE:
target_zoom = 1.0
zoom = Vector2(1.0, 1.0)
position = reset_pos
func zoom_in() -> void:
target_zoom = max(target_zoom - ZOOM_INCREMENT, MIN_ZOOM)
set_physics_process(true)
func zoom_out() -> void:
target_zoom = min(target_zoom + ZOOM_INCREMENT, MAX_ZOOM)
set_physics_process(true)

16
main.tscn Normal file
View File

@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=3 uid="uid://djirenq1psstj"]
[ext_resource type="Script" path="res://PanningCamera/PanningCamera2D.gd" id="1_vtapg"]
[ext_resource type="PackedScene" uid="uid://qipfodwbk1gf" path="res://Field/field.tscn" id="1_y0qkv"]
[node name="Main" type="Node2D"]
[node name="Camera2D" type="Camera2D" parent="."]
script = ExtResource("1_vtapg")
MIN_ZOOM = null
MAX_ZOOM = null
ZOOM_RATE = 5.0
[node name="Field" parent="." instance=ExtResource("1_y0qkv")]
rotation = 0.523599
skew = 0.523599