127 lines
4.3 KiB
GDScript
127 lines
4.3 KiB
GDScript
extends Node2D
|
|
|
|
@onready var camera: Camera2D = $Camera2D
|
|
#@onready var car: Node2D = $car_0
|
|
|
|
@onready var cars: Node = $cars
|
|
|
|
const caroffset= 32+4 #space cars on start line
|
|
|
|
var viewCarMargin=Vector2(0.05,0.05) #proportions of viewsize. 0,0 = adjust when cars are outside view, 1,1=infinite zoom out
|
|
var viewCarMargin_zoomstart=viewCarMargin+Vector2(0.2,0.2)
|
|
var zoomspeed=0.5
|
|
var zoomspeed_backup=0.1
|
|
var zoom_normal=2
|
|
|
|
|
|
# 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,ceil(i/2.0)*(fmod(i,2)-0.5)*2.0*caroffset))
|
|
|
|
|
|
#print("Position car "+str(i)+" = "+str(ceil(i/2.0)*(fmod(i,2)-0.5)*2.0*caroffset))
|
|
print("carpos is ="+str(newcarinstance.getPosition())+" instancepos="+str(newcarinstance.position))
|
|
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<Vector2(1.0,1.0):
|
|
camera.zoom+=Vector2(0.1*delta,0.1*delta)
|
|
'''
|
|
var viewCarMargin_zoombackup=viewCarMargin_zoomstart+Vector2(0.1,0.1)
|
|
var viewsize = camera.get_viewport_rect().size/camera.zoom
|
|
var carSpread=Vector2(maxPos.x-minPos.x,maxPos.y-minPos.y)
|
|
var calculatedViewCarMargin=Vector2((viewsize.x-carSpread.x)/2,(viewsize.y-carSpread.y)/2)
|
|
var mapped_zoomspeed_x=constrain(remap(calculatedViewCarMargin.x , viewCarMargin_zoomstart.x*viewsize.x,viewCarMargin.x*viewsize.x,0,zoomspeed),0,zoomspeed)
|
|
var mapped_zoomspeed_y=constrain(remap(calculatedViewCarMargin.y , viewCarMargin_zoomstart.y*viewsize.y,viewCarMargin.y*viewsize.y,0,zoomspeed),0,zoomspeed)
|
|
var mapped_zoomspeed=max(mapped_zoomspeed_x,mapped_zoomspeed_y)
|
|
if calculatedViewCarMargin.x<(viewCarMargin_zoomstart.x*viewsize.x) or calculatedViewCarMargin.y<(viewCarMargin_zoomstart.y*viewsize.y): #cars not in view
|
|
camera.zoom-=Vector2(mapped_zoomspeed*delta,mapped_zoomspeed*delta)
|
|
#camera.zoom-=Vector2(0.1,0.1)
|
|
elif calculatedViewCarMargin.x>(viewCarMargin_zoombackup.x*viewsize.x) and calculatedViewCarMargin.y>(viewCarMargin_zoombackup.y*viewsize.y): #cars in view again
|
|
if camera.zoom.x<zoom_normal:
|
|
camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta)
|
|
|
|
$hud/speedlabel.text=str(camera.zoom.x)
|
|
$hud/speedlabel.text=str(calculatedViewCarMargin)
|
|
#$hud/speedlabel2.text=str(viewsize.x)
|
|
|
|
|
|
|
|
#$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))
|