extends Camera3D
class_name CameraManager

var target:int
var offset:Vector3 = Vector3(0,1,1) * 5
var level_size:Vector2
var camera_limit = Vector2(6.4,3.6)

func _ready():
	Global.camera_mgr = self
	SignalManager.character_create.connect(on_character_create)
	SignalManager.character_pos_changed.connect(on_character_pos_changed)
	SignalManager.character_destroy.connect(on_character_destroy)
	SignalManager.level_size_change.connect(on_level_size_change)
	
func _process(delta):
	pass
	
func on_character_create(id:int,team:Enum.ETeam,pos:Vector3):
	if team == Enum.ETeam.Player:
		target = id
	
func on_character_pos_changed(id:int,pos:Vector3):
	if id == target:
		pos.x = clamp(pos.x,camera_limit.x,level_size.x-camera_limit.x)
		pos.z = clamp(pos.z,camera_limit.y,level_size.y-camera_limit.y)
		pos.y = clamp(pos.y,0,0.5)
		position = pos + offset

func on_character_destroy(id:int):
	target = 0
	
func on_level_size_change(size:Vector2):
	level_size = size
