extends Camera2D
 
var screenWidth = ProjectSettings.get_setting("display/window/size/viewport_width")
var screenHeight = ProjectSettings.get_setting("display/window/size/viewport_height")
 
@export var scrollArea : int
@export var scrollSpeed : float
@export var scrollDivisions : int
@export var officeSprite : Sprite2D
 
var officeWidth : int
var officeHeight : int
var distance : float
var divisionSize : float
var speedMultiplier : float
 
func _ready() -> void:
	make_current()
	print("Camera Running")
	if scrollDivisions == 0:
		scrollDivisions = 1
	divisionSize = scrollArea / scrollDivisions
	if officeSprite:
		officeWidth = officeSprite.texture.get_width() * officeSprite.scale.x
		position.x = officeSprite.position.x + (officeWidth - screenWidth) / 2
		officeHeight = officeSprite.texture.get_height() * officeSprite.scale.y
		position.y = officeSprite.position.y + (officeHeight - screenHeight) / 2
 
func _process(delta : float) -> void:
	if get_local_mouse_position().x < scrollArea:
		distance = scrollArea - get_local_mouse_position().x
		getSpeedMultiplier()
		position.x -= (scrollSpeed * speedMultiplier) * delta
	if get_local_mouse_position().x > screenWidth - scrollArea:
		distance = get_local_mouse_position().x - (screenWidth - scrollArea)
		getSpeedMultiplier()
		position.x += (scrollSpeed * speedMultiplier) * delta
	
	if officeSprite:
		if position.x < officeSprite.position.x:
			position.x = officeSprite.position.x
		if (position.x - officeSprite.position.x) + screenWidth > officeWidth:
			position.x = officeWidth - screenWidth + officeSprite.position.x
 
func getSpeedMultiplier() -> void:
	speedMultiplier = clamp(floor(distance / divisionSize) + 1, 1, scrollDivisions)
	speedMultiplier /= scrollDivisions
