fix zooming

This commit is contained in:
interfisch 2024-11-06 18:36:13 +01:00
parent 36296d9b39
commit 22e9c1bcc1
3 changed files with 42 additions and 22 deletions

View file

@ -8,6 +8,7 @@ script = ExtResource("1_7syh4")
[node name="hud" type="CanvasLayer" parent="."]
[node name="debuglabel" type="Label" parent="hud"]
visible = false
z_index = 8
offset_left = 14.0
offset_top = 535.0

View file

@ -9,11 +9,12 @@ extends Node2D
const caroffset= 32+4 #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.05,0.05)
var zoomspeed=1.0
var zoomspeed_backup=0.2
var zoom_normal=2
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
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
var zoom_normal=2.0
const CAMERA_POSITION_SPEED=0.98 #0.0 - 1.0, higher=faster
var running=false
@ -28,21 +29,18 @@ func _ready() -> void:
countdown_label.visible=true
#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")
var i=0
#for playerkey in Gamestate.getPlayerkeys(): #create all players
for player in Gamestate.getPlayers():
for player in Gamestate.getPlayers(): #create all players
var newcarscene=load("res://scenes/car.tscn")
var newcarinstance=newcarscene.instantiate()
cars.add_child(newcarinstance)
@ -52,8 +50,6 @@ func _ready() -> void:
newcarinstance.setCheckpoints(checkpoints,rounds)
newcarinstance.getCharacterBody().car_finished.connect(_on_car_finished)
#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
@ -99,8 +95,8 @@ func _process(delta: float) -> void:
meanCarPosition/=displayedCarCount
#camera.position=car.getPosition()
camera.position=lerp(camera.position,meanCarPosition,CAMERA_POSITION_SPEED*delta)
camera.position=meanCarPosition
#camera.position=lerp(camera.position,meanCarPosition,CAMERA_POSITION_SPEED*delta)
#rint("zoom = "+str(camera.zoom))
#camera.zoom=Vector2(1.5,1.5)
@ -111,21 +107,32 @@ func _process(delta: float) -> void:
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 viewsize = camera.get_viewport_rect().size/camera.zoom
var viewsize = camera.get_viewport_rect().size
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 calculatedViewCarMargin=Vector2((viewsize.x-carSpread.x)/2,(viewsize.y-carSpread.y)/2)
#var calculatedViewCarMargin=Vector2(carSpread.x/viewsize.x,carSpread.y/viewsize.y)
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)
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
if calculatedViewCarMargin.x>viewCarMargin_zoomstart_pixels.x or calculatedViewCarMargin.y>viewCarMargin_zoomstart_pixels.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
$hud/debuglabel.set("theme_override_colors/font_color",Color(0,0,0.7))
elif calculatedViewCarMargin.x<viewCarMargin_zoombackup_pixels.x and calculatedViewCarMargin.y<viewCarMargin_zoombackup_pixels.y: #cars in view again
if camera.zoom.x<zoom_normal:
camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta)
$hud/debuglabel.text=""
#$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)

12
scripts/time_display.gd Normal file
View file

@ -0,0 +1,12 @@
extends Node2D
@onready var label: Label = $Label
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass