extends Node2D @onready var camera: Camera2D = $Camera2D #@onready var car: Node2D = $car_0 @onready var cars: Node = $cars const caroffset=32+12 #space cars on start line var viewCarMargin=Vector2(0.1,0.1) #proportions of viewsize. 0,0 = adjust when cars are outside view, 1,1=infinite zoom out var viewCarMargin_zoomstart=viewCarMargin+Vector2(0.1,0.1) var zoomspeed=0.3 var zoomspeed_backup=0.05 # Called when the node enters the scene tree for the first time. func _ready() -> void: var i=0 #for playerkey in Gamestate.getPlayerkeys(): #create all players for player in Gamestate.getPlayers(): var newcarscene=load("res://scenes/car.tscn") var newcarinstance=newcarscene.instantiate() cars.add_child(newcarinstance) newcarinstance.setPlayerinformation(i,player.color) newcarinstance.setPosition(Vector2(0,i*caroffset)) i+=1 #Load Map var mapscene=load("res://scenes/map_01.tscn") var mapsceneinstance=mapscene.instantiate() add_child(mapsceneinstance) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: var cars=cars.get_children() var meanCarPosition=Vector2.ZERO var displayedCarCount=0 var maxCarSpeed=0 var minPos=Vector2.ZERO #min/max x and y position of all cars var maxPos=Vector2.ZERO for c in cars: var carpos = c.getPosition() meanCarPosition+=carpos maxCarSpeed=max(maxCarSpeed,c.getSpeed()) if displayedCarCount==0: minPos.x=carpos.x minPos.y=carpos.y maxPos.x=carpos.x maxPos.y=carpos.y else: minPos.x=min(minPos.x,carpos.x) minPos.y=min(minPos.y,carpos.y) maxPos.x=max(maxPos.x,carpos.x) maxPos.y=max(maxPos.y,carpos.y) displayedCarCount+=1 meanCarPosition/=displayedCarCount #camera.position=car.getPosition() camera.position=meanCarPosition #rint("zoom = "+str(camera.zoom)) #camera.zoom=Vector2(1.5,1.5) '''var allCarsInView=checkPositionInsideView(minPos) and checkPositionInsideView(maxPos) if not allCarsInView: camera.zoom-=Vector2(0.1*delta,0.1*delta) else: if camera.zoom(viewCarMargin_zoombackup.x*viewsize.x) and calculatedViewCarMargin.y>(viewCarMargin_zoombackup.y*viewsize.y): #cars in view again if camera.zoom.x<1.0: camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta) #$Camera2D/speedlabel.text=str(carSpread) #$Camera2D/speedlabel.text=str(round(maxCarSpeed)) func _input(ev): #if ev is InputEventKey and if Input.is_action_just_pressed("ui_cancel"): get_tree().change_scene_to_file("res://scenes/menu.tscn") func checkPositionInsideView(checkpos: Vector2) -> bool: var viewsize = camera.get_viewport_rect().size var canvas_pos = camera.get_viewport().get_canvas_transform().affine_inverse() * -checkpos print("cp="+str(canvas_pos) + " viewsize="+str(viewsize)) if canvas_pos.x < 0 and canvas_pos.x > -viewsize.x: if canvas_pos.y < 0 and canvas_pos.y > -viewsize.y: return true return false func constrain(val,a,b): var vmin=min(a,b) var vmax=max(a,b) return min(vmax,max(vmin,val))