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-09 10:52:05 +00:00
|
|
|
@onready var game_hud_animations: AnimationPlayer = $hud/game_hud_animations
|
|
|
|
|
|
|
|
@onready var times_container: HFlowContainer = $hud/times_container
|
|
|
|
@onready var timer_close: Timer = $timer_close
|
2024-11-09 09:32:12 +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-10 00:06:25 +00:00
|
|
|
var viewCarMargin=Vector2(0.7,0.7) #1=zoom out full speed when car is at border. 0.9 zoom out full speed when car is 10% away from border
|
2024-11-06 17:36:13 +00:00
|
|
|
var viewCarMargin_zoomstart=viewCarMargin-Vector2(0.2,0.2) #start zooming
|
|
|
|
var viewCarMargin_zoombackup=viewCarMargin_zoomstart-Vector2(0.1,0.1) #start zooming back in
|
|
|
|
var zoomspeed=0.5
|
|
|
|
var zoomspeed_backup=0.1 #relative to screen size
|
2024-11-09 09:32:12 +00:00
|
|
|
var zoom_normal=1.5
|
2024-11-09 23:45:22 +00:00
|
|
|
const CAMERA_POSITION_SPEED=0.02 #0.0 - 1.0, higher=faster
|
2024-11-10 00:06:25 +00:00
|
|
|
var camera_zoom_out_lookahead_time=1 #predicted position of car in t seconds for camera coverage. greater value=earlier zoom out and movement of camera
|
2024-11-06 06:38:44 +00:00
|
|
|
|
|
|
|
var running=false
|
|
|
|
|
2024-11-09 10:52:05 +00:00
|
|
|
|
2024-11-09 23:45:22 +00:00
|
|
|
var time_close_keypressed=1 #seconds to wait after key pressed at endscreen
|
2024-11-06 06:38:44 +00:00
|
|
|
|
2024-11-09 09:32:12 +00:00
|
|
|
var game_ended=false
|
2024-11-03 18:16:30 +00:00
|
|
|
|
2024-11-09 10:52:05 +00:00
|
|
|
|
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-09 10:54:56 +00:00
|
|
|
Gamestate.resetTimeElapsed()
|
2024-11-09 12:58:22 +00:00
|
|
|
|
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 checkpoints :Array[String]=[]
|
2024-11-09 23:45:22 +00:00
|
|
|
var mapscene=load(Gamestate.getSelectedMapSceneName())
|
2024-11-05 07:18:14 +00:00
|
|
|
var mapsceneinstance=mapscene.instantiate()
|
|
|
|
add_child(mapsceneinstance)
|
|
|
|
for mapc in mapsceneinstance.get_children():
|
|
|
|
if mapc.name.begins_with("area_cp"):
|
|
|
|
checkpoints.append(mapc.name)
|
|
|
|
|
|
|
|
print(str(checkpoints.size())+" Checkpoints found")
|
|
|
|
|
2024-11-03 18:16:30 +00:00
|
|
|
var i=0
|
2024-11-06 17:36:13 +00:00
|
|
|
for player in Gamestate.getPlayers(): #create all players
|
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-09 12:58:22 +00:00
|
|
|
newcarinstance.setCheckpoints(checkpoints,Gamestate.getRounds())
|
2024-11-06 06:38:44 +00:00
|
|
|
newcarinstance.getCharacterBody().car_finished.connect(_on_car_finished)
|
2024-11-04 20:56:07 +00:00
|
|
|
|
|
|
|
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-10 00:06:25 +00:00
|
|
|
updateCameraMovement(delta)
|
|
|
|
|
|
|
|
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewCarMargin_zoomstart)+" zoomspeed="+str(mapped_zoomspeed)
|
|
|
|
#$hud/debuglabel.text=""+str(calculatedViewCarMargin)+" / "+str(viewsize)+" zoomspeed="+str(mapped_zoomspeed)
|
|
|
|
$hud/timer.text=str(round(Gamestate.getTimeElapsed()*1000)/1000.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if game_ended:
|
|
|
|
var anyplayerkeypressed=false
|
|
|
|
var id=0
|
|
|
|
for p in Gamestate.getPlayers():
|
|
|
|
if Input.is_action_pressed(Gamestate.userinput_prefix+str(id)):
|
|
|
|
anyplayerkeypressed=true
|
|
|
|
id+=1
|
|
|
|
if anyplayerkeypressed and timer_close.time_left<time_close_keypressed:
|
|
|
|
timer_close.wait_time=time_close_keypressed #set time back
|
|
|
|
timer_close.stop() #prepare for restarting at this time
|
|
|
|
if !anyplayerkeypressed and timer_close.is_stopped():
|
|
|
|
timer_close.start() #start timer when no key is pressed
|
|
|
|
|
|
|
|
func updateCameraMovement(delta: float):
|
|
|
|
|
2024-11-03 18:16:30 +00:00
|
|
|
var cars=cars.get_children()
|
|
|
|
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()
|
2024-11-10 00:06:25 +00:00
|
|
|
var carposLookahead= c.getPositionLookahead(camera_zoom_out_lookahead_time)
|
|
|
|
$hud/debuglabel.text=""+str(carpos)+"\n"+str(carposLookahead)
|
2024-11-06 06:38:44 +00:00
|
|
|
|
|
|
|
maxCarSpeed=max(maxCarSpeed,c.getSpeed())
|
|
|
|
if displayedCarCount==0:
|
2024-11-10 00:06:25 +00:00
|
|
|
minPos=carpos.min(carposLookahead)
|
|
|
|
maxPos=carpos.max(carposLookahead)
|
2024-11-06 06:38:44 +00:00
|
|
|
else:
|
2024-11-10 00:06:25 +00:00
|
|
|
minPos=minPos.min(carpos).min(carposLookahead)
|
|
|
|
maxPos=maxPos.max(carpos).max(carposLookahead)
|
2024-11-06 06:38:44 +00:00
|
|
|
|
|
|
|
displayedCarCount+=1
|
2024-11-03 18:16:30 +00:00
|
|
|
|
|
|
|
|
2024-11-09 23:45:22 +00:00
|
|
|
camera.position=lerp(camera.position,(minPos+maxPos)/2.0,CAMERA_POSITION_SPEED)
|
|
|
|
|
2024-11-10 00:06:25 +00:00
|
|
|
|
2024-11-09 10:52:05 +00:00
|
|
|
|
2024-11-06 17:36:13 +00:00
|
|
|
var viewsize = camera.get_viewport_rect().size
|
2024-11-03 18:16:30 +00:00
|
|
|
var carSpread=Vector2(maxPos.x-minPos.x,maxPos.y-minPos.y)
|
2024-11-06 17:36:13 +00:00
|
|
|
var calculatedViewCarMargin=carSpread*camera.zoom #when cars are screen width apart this number matches viewsize=camera.get_viewport_rect().size
|
|
|
|
|
|
|
|
|
|
|
|
var viewCarMargin_zoomstart_pixels=viewCarMargin_zoomstart*viewsize/camera.zoom
|
|
|
|
var viewCarMargin_pixels=viewCarMargin*viewsize/camera.zoom
|
|
|
|
var viewCarMargin_zoombackup_pixels=viewCarMargin_zoombackup*viewsize/camera.zoom
|
|
|
|
|
|
|
|
var mapped_zoomspeed_x=constrain(remap(calculatedViewCarMargin.x , viewCarMargin_zoomstart_pixels.x,viewCarMargin_pixels.x,0,zoomspeed),0,zoomspeed)
|
|
|
|
var mapped_zoomspeed_y=constrain(remap(calculatedViewCarMargin.y , viewCarMargin_zoomstart_pixels.y,viewCarMargin_pixels.y,0,zoomspeed),0,zoomspeed)
|
2024-11-03 18:16:30 +00:00
|
|
|
var mapped_zoomspeed=max(mapped_zoomspeed_x,mapped_zoomspeed_y)
|
2024-11-06 17:36:13 +00:00
|
|
|
if calculatedViewCarMargin.x>viewCarMargin_zoomstart_pixels.x or calculatedViewCarMargin.y>viewCarMargin_zoomstart_pixels.y: #cars not in view
|
2024-11-03 18:16:30 +00:00
|
|
|
camera.zoom-=Vector2(mapped_zoomspeed*delta,mapped_zoomspeed*delta)
|
2024-11-06 17:36:13 +00:00
|
|
|
elif calculatedViewCarMargin.x<viewCarMargin_zoombackup_pixels.x and calculatedViewCarMargin.y<viewCarMargin_zoombackup_pixels.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)
|
|
|
|
|
|
|
|
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-09 09:32:12 +00:00
|
|
|
|
|
|
|
func constrain2(val:Vector2,a:Vector2,b:Vector2):
|
|
|
|
var vmin=a.min(b)
|
|
|
|
var vmax=a.max(b)
|
|
|
|
return vmax.min(vmin.max(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
|
|
|
|
|
2024-11-09 09:32:12 +00:00
|
|
|
|
2024-11-06 06:38:44 +00:00
|
|
|
func _on_car_finished(playerid,finalTime) -> void:
|
2024-11-09 10:52:05 +00:00
|
|
|
times_container.addFinishedPlayer(playerid,finalTime)
|
|
|
|
|
|
|
|
if times_container.getPlayersFinished() == Gamestate.getPlayers().size(): #all players have finish times
|
2024-11-09 09:32:12 +00:00
|
|
|
finishGame()# Game finished
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func finishGame():
|
|
|
|
print("Game Finished")
|
|
|
|
running=false
|
|
|
|
game_ended=true
|
2024-11-09 10:52:05 +00:00
|
|
|
game_hud_animations.play("game_end")
|
|
|
|
timer_close.start()
|
|
|
|
#overlaycolor.visible=true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_timer_close_timeout() -> void:
|
|
|
|
print("Close Map")
|
|
|
|
get_tree().change_scene_to_file("res://scenes/menu.tscn")
|
|
|
|
|
2024-11-09 09:32:12 +00:00
|
|
|
|