1
0
Fork 0
gai-ca2/project/scripts/global/Camera.gd

89 lines
3.3 KiB
GDScript

class_name Camera
extends Camera2D
@export var border_acceleration: float = 800.0
@export var max_speed: float = 500.0
@export var inner_border_threshold: float = 60.0
@export var outer_border_threshold: float = 20.0
@export var min_position: Vector2 = Vector2(-1000, -1000)
@export var max_position: Vector2 = Vector2(4000, 4000)
var target_position: Vector2
var velocity: Vector2 = Vector2.ZERO
#
var drag_active: bool = false
var drag_start: Vector2
func _ready():
target_position = position
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
zoom = zoom * 1.1
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
zoom = zoom / 1.1
func _process(delta):
if Input.is_action_just_pressed("camera_drag"):
drag_active = true
drag_start = get_viewport().get_mouse_position()
if Input.is_action_just_released("camera_drag"):
drag_active = false
if drag_active:
var drag_end: Vector2 = get_viewport().get_mouse_position()
var drag_offset: Vector2 = drag_end - drag_start
drag_start = drag_end
position -= drag_offset / zoom
var is_zoom_in: bool = Input.is_action_pressed("camera_zoom_in")
var is_zoom_out: bool = Input.is_action_pressed("camera_zoom_out")
if is_zoom_in:
zoom = zoom * 1.1
elif is_zoom_out:
zoom = zoom / 1.1
var mouse_pos: Vector2 = get_viewport().get_mouse_position()
var screen_size: Vector2 = get_viewport().get_visible_rect().size
var acceleration: Vector2 = Vector2.ZERO
var is_up: bool = Input.is_action_pressed("camera_up") or mouse_pos.y < inner_border_threshold and mouse_pos.y > -outer_border_threshold
var is_down: bool = Input.is_action_pressed("camera_down") or mouse_pos.y > screen_size.y - inner_border_threshold and mouse_pos.y < screen_size.y + outer_border_threshold
var is_left: bool = Input.is_action_pressed("camera_left") or mouse_pos.x < inner_border_threshold and mouse_pos.x > -outer_border_threshold
var is_right: bool = Input.is_action_pressed("camera_right") or mouse_pos.x > screen_size.x - inner_border_threshold and mouse_pos.x < screen_size.x + outer_border_threshold
if is_left:
acceleration.x = -border_acceleration
elif is_right:
acceleration.x = border_acceleration
if is_up:
acceleration.y = -border_acceleration
elif is_down:
acceleration.y = border_acceleration
acceleration *= Vector2.ONE / zoom
if acceleration.length() > 0:
# if the acceleration is the opposite direction of the velocity, double the acceleration
if acceleration.dot(velocity) < 0:
acceleration = acceleration * 2
velocity = velocity + acceleration * delta
else:
velocity = velocity.move_toward(Vector2.ZERO, border_acceleration * delta)
if velocity.length() > max_speed:
velocity = velocity.normalized() * max_speed
var target_offset = velocity * delta
target_position = position + target_offset
target_position.x = clamp(target_position.x, min_position.x, max_position.x)
target_position.y = clamp(target_position.y, min_position.y, max_position.y)
var offset: Vector2 = target_position - position
position += offset