2024-11-03 18:16:30 +00:00
extends Node2D
@ onready var camera : Camera2D = $ Camera2D
2024-11-06 06:38:44 +00:00
@ onready var countdown : Timer = $ countdown
@ onready var countdown_label : Label = $ hud / countdown_label
2024-11-03 18:16:30 +00:00
@ onready var cars : Node = $ cars
2024-11-06 06:38:44 +00:00
@ onready var times_container : HFlowContainer = $ hud / times_container
2024-11-03 18:16:30 +00:00
2024-11-04 20:56:07 +00:00
const caroffset = 32 + 4 #space cars on start line
2024-11-03 18:16:30 +00:00
2024-11-06 06:38:44 +00:00
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.05 , 0.05 )
var zoomspeed = 1.0
var zoomspeed_backup = 0.2
2024-11-04 20:56:07 +00:00
var zoom_normal = 2
2024-11-06 06:38:44 +00:00
const CAMERA_POSITION_SPEED = 0.98 #0.0 - 1.0, higher=faster
var running = false
var rounds = 3
2024-11-03 18:16:30 +00:00
# Called when the node enters the scene tree for the first time.
func _ready ( ) - > void :
2024-11-06 06:38:44 +00:00
countdown . start ( )
countdown_label . visible = true
2024-11-05 07:18:14 +00:00
#Load Map
#var num_checkpoints=0
var checkpoints : Array [ String ] = [ ]
var mapscene = load ( " res://scenes/map_01.tscn " )
var mapsceneinstance = mapscene . instantiate ( )
add_child ( mapsceneinstance )
for mapc in mapsceneinstance . get_children ( ) :
if mapc . name . begins_with ( " area_cp " ) :
#num_checkpoints+=1
checkpoints . append ( mapc . name )
print ( str ( checkpoints . size ( ) ) + " Checkpoints found " )
2024-11-03 18:16:30 +00:00
var i = 0
2024-11-04 18:55:13 +00:00
#for playerkey in Gamestate.getPlayerkeys(): #create all players
for player in Gamestate . getPlayers ( ) :
2024-11-03 18:16:30 +00:00
var newcarscene = load ( " res://scenes/car.tscn " )
var newcarinstance = newcarscene . instantiate ( )
cars . add_child ( newcarinstance )
2024-11-04 18:55:13 +00:00
newcarinstance . setPlayerinformation ( i , player . color )
2024-11-04 20:56:07 +00:00
newcarinstance . setPosition ( Vector2 ( 0 , ceil ( i / 2.0 ) * ( fmod ( i , 2 ) - 0.5 ) * 2.0 * caroffset ) )
2024-11-06 06:38:44 +00:00
newcarinstance . setCheckpoints ( checkpoints , rounds )
newcarinstance . getCharacterBody ( ) . car_finished . connect ( _on_car_finished )
2024-11-04 20:56:07 +00:00
#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 ) )
2024-11-03 18:16:30 +00:00
i += 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process ( delta : float ) - > void :
2024-11-06 06:38:44 +00:00
if running :
Gamestate . addTimeElapsed ( delta )
if ! countdown . is_stopped ( ) :
countdown_label . text = str ( round ( countdown . time_left ) )
2024-11-03 18:16:30 +00:00
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
2024-11-06 06:38:44 +00:00
var oneDriving = false
for c in cars : #check if any one car is still driving
if ! c . hasFinished ( ) :
oneDriving = true
2024-11-03 18:16:30 +00:00
for c in cars :
2024-11-06 06:38:44 +00:00
if ! c . hasFinished ( ) or ! oneDriving :
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
2024-11-03 18:16:30 +00:00
meanCarPosition /= displayedCarCount
#camera.position=car.getPosition()
2024-11-06 06:38:44 +00:00
camera . position = lerp ( camera . position , meanCarPosition , CAMERA_POSITION_SPEED * delta )
2024-11-03 18:16:30 +00:00
#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 )
2024-11-04 20:56:07 +00:00
var calculatedViewCarMargin = Vector2 ( ( viewsize . x - carSpread . x ) / 2 , ( viewsize . y - carSpread . y ) / 2 )
2024-11-03 18:16:30 +00:00
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 )
2024-11-04 20:56:07 +00:00
#camera.zoom-=Vector2(0.1,0.1)
2024-11-03 18:16:30 +00:00
elif calculatedViewCarMargin . x > ( viewCarMargin_zoombackup . x * viewsize . x ) and calculatedViewCarMargin . y > ( viewCarMargin_zoombackup . y * viewsize . y ) : #cars in view again
2024-11-04 20:56:07 +00:00
if camera . zoom . x < zoom_normal :
2024-11-03 18:16:30 +00:00
camera . zoom += Vector2 ( zoomspeed_backup * delta , zoomspeed_backup * delta )
2024-11-06 06:38:44 +00:00
$ hud / debuglabel . text = " "
$ hud / timer . text = str ( round ( Gamestate . getTimeElapsed ( ) * 1000 ) / 1000.0 )
2024-11-03 18:16:30 +00:00
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 ) )
2024-11-06 06:38:44 +00:00
func _on_countdown_timeout ( ) - > void :
running = true
var cars = cars . get_children ( )
for c in cars :
c . setRunning ( true )
countdown_label . visible = false
func _on_car_finished ( playerid , finalTime ) - > void :
print ( " Finished " + str ( playerid ) + " final time= " + str ( finalTime ) )
var place = times_container . get_child_count ( ) + 1
print ( " New Label. Place " + str ( place ) )
var newlabel = Label . new ( )
times_container . add_child ( newlabel )
newlabel . set ( " theme_override_font_sizes/font_size " , 40 )
newlabel . text = str ( place ) + " : " + str ( round ( finalTime * 1000 ) / 1000.0 ) + " s "
newlabel . set ( " theme_override_colors/font_color " , Gamestate . getPlayers ( ) [ playerid ] . color )