fix collision at start and autozoom
This commit is contained in:
parent
e370a4698f
commit
0ff14f7384
10 changed files with 116 additions and 46 deletions
|
@ -19,6 +19,10 @@ config/icon="res://icon.svg"
|
|||
|
||||
Gamestate="*res://scripts/gamestate.gd"
|
||||
|
||||
[display]
|
||||
|
||||
window/stretch/mode="canvas_items"
|
||||
|
||||
[layer_names]
|
||||
|
||||
2d_physics/layer_1="walls"
|
||||
|
|
|
@ -12,38 +12,44 @@ height = 34.0
|
|||
[node name="Car" type="Node2D"]
|
||||
script = ExtResource("1_0tin3")
|
||||
|
||||
[node name="Car" type="CharacterBody2D" parent="."]
|
||||
[node name="CharacterBody_Car" type="CharacterBody2D" parent="."]
|
||||
motion_mode = 1
|
||||
script = ExtResource("1_i5tet")
|
||||
|
||||
[node name="sprite_features" type="Sprite2D" parent="Car"]
|
||||
[node name="sprite_features" type="Sprite2D" parent="CharacterBody_Car"]
|
||||
z_index = 10
|
||||
scale = Vector2(0.5, 0.5)
|
||||
texture = ExtResource("3_ts6mm")
|
||||
|
||||
[node name="sprite_body" type="Sprite2D" parent="Car"]
|
||||
[node name="sprite_body" type="Sprite2D" parent="CharacterBody_Car"]
|
||||
z_index = 10
|
||||
scale = Vector2(0.5, 0.5)
|
||||
texture = ExtResource("4_lps13")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Car"]
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody_Car"]
|
||||
rotation = -1.57079
|
||||
shape = SubResource("CapsuleShape2D_bj1hp")
|
||||
|
||||
[node name="RayCast_FL" type="RayCast2D" parent="Car"]
|
||||
[node name="RayCast_FL" type="RayCast2D" parent="CharacterBody_Car"]
|
||||
target_position = Vector2(256, -128)
|
||||
collision_mask = 2
|
||||
|
||||
[node name="RayCast_FR" type="RayCast2D" parent="Car"]
|
||||
[node name="RayCast_FR" type="RayCast2D" parent="CharacterBody_Car"]
|
||||
target_position = Vector2(256, 128)
|
||||
collision_mask = 2
|
||||
|
||||
[node name="resetTimer" type="Timer" parent="Car"]
|
||||
[node name="resetTimer" type="Timer" parent="CharacterBody_Car"]
|
||||
wait_time = 2.0
|
||||
one_shot = true
|
||||
|
||||
[node name="RayCast_Car" type="RayCast2D" parent="Car"]
|
||||
[node name="RayCast_Car" type="RayCast2D" parent="CharacterBody_Car"]
|
||||
position = Vector2(-15, 0)
|
||||
target_position = Vector2(31, 0)
|
||||
collision_mask = 4
|
||||
|
||||
[connection signal="timeout" from="Car/resetTimer" to="Car" method="_on_reset_timer_timeout"]
|
||||
[node name="collisionEnableTimer" type="Timer" parent="CharacterBody_Car"]
|
||||
wait_time = 0.1
|
||||
one_shot = true
|
||||
|
||||
[connection signal="timeout" from="CharacterBody_Car/resetTimer" to="CharacterBody_Car" method="_on_reset_timer_timeout"]
|
||||
[connection signal="timeout" from="CharacterBody_Car/collisionEnableTimer" to="CharacterBody_Car" method="_on_collision_enable_timer_timeout"]
|
||||
|
|
|
@ -5,15 +5,28 @@
|
|||
[node name="Game" type="Node2D"]
|
||||
script = ExtResource("1_7syh4")
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
[node name="hud" type="CanvasLayer" parent="."]
|
||||
|
||||
[node name="speedlabel" type="Label" parent="Camera2D"]
|
||||
[node name="speedlabel" type="Label" parent="hud"]
|
||||
z_index = 8
|
||||
offset_left = -484.0
|
||||
offset_top = 213.0
|
||||
offset_right = -68.0
|
||||
offset_bottom = 258.0
|
||||
offset_left = 38.0
|
||||
offset_top = 541.0
|
||||
offset_right = 454.0
|
||||
offset_bottom = 586.0
|
||||
theme_override_font_sizes/font_size = 32
|
||||
text = "adsf"
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="speedlabel2" type="Label" parent="hud"]
|
||||
z_index = 8
|
||||
offset_left = 40.0
|
||||
offset_top = 582.0
|
||||
offset_right = 456.0
|
||||
offset_bottom = 627.0
|
||||
theme_override_font_sizes/font_size = 32
|
||||
text = "adsf"
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
|
||||
[node name="cars" type="Node" parent="."]
|
||||
|
|
|
@ -85,7 +85,16 @@ layout_mode = 2
|
|||
theme_override_font_sizes/font_size = 32
|
||||
uppercase = true
|
||||
|
||||
[node name="btn_fullscreen" type="CheckButton" parent="MenuDisplay"]
|
||||
offset_left = 845.0
|
||||
offset_top = 540.0
|
||||
offset_right = 972.0
|
||||
offset_bottom = 571.0
|
||||
scale = Vector2(2, 2)
|
||||
text = "Fullscreen"
|
||||
|
||||
[node name="Timer" type="Timer" parent="."]
|
||||
wait_time = 5.0
|
||||
|
||||
[connection signal="toggled" from="MenuDisplay/btn_fullscreen" to="." method="_on_btn_fullscreen_toggled"]
|
||||
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]
|
||||
|
|
|
@ -2,6 +2,7 @@ extends CharacterBody2D
|
|||
|
||||
#Tutorial: https://www.youtube.com/watch?v=mJ1ZfGDTMCY t=15s
|
||||
|
||||
|
||||
const COLLISIONMASK_FINISH=3 #set in road_overlay
|
||||
const COLLISIONMASK_CHECKPOINT=4 #set in road_overlay
|
||||
|
||||
|
@ -48,14 +49,22 @@ var steer_direction=0
|
|||
|
||||
var autoreset=false
|
||||
|
||||
var autosteer_enabled=false
|
||||
|
||||
@onready var ray_cast_fl: RayCast2D = $RayCast_FL
|
||||
@onready var ray_cast_fr: RayCast2D = $RayCast_FR
|
||||
@onready var reset_timer: Timer = $resetTimer
|
||||
|
||||
@onready var ray_cast_car: RayCast2D = $RayCast_Car #for tracking markers
|
||||
|
||||
@onready var collision_shape: CollisionShape2D = $CollisionShape2D
|
||||
@onready var collision_enable_timer: Timer = $collisionEnableTimer
|
||||
|
||||
var playerid=0
|
||||
|
||||
func _ready() -> void:
|
||||
collision_shape.disabled=true #disable collisions on start. also to avoid collision when initially setting position
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
acceleration=Vector2.ZERO
|
||||
check_markers()
|
||||
|
@ -72,7 +81,7 @@ func _physics_process(delta: float) -> void:
|
|||
# var collision = get_slide_collision(i)
|
||||
# print("Collided with: ", collision.get_collider().name)
|
||||
|
||||
if velocity.length() < resetcar_stoppedspeed: #moving slow, possibly crash?
|
||||
if velocity.length() < resetcar_stoppedspeed and autosteer_enabled: #moving slow, possibly crash?
|
||||
if reset_timer.is_stopped():
|
||||
reset_timer.start()
|
||||
if velocity.length() > resetcar_movingspeed:
|
||||
|
@ -122,12 +131,12 @@ func get_input():
|
|||
var steering_distance_close=constrain( remap(velocity.length(),steering_speed_fast,steering_speed_slow,steering_distance_close_fast,steering_distance_close_slow),steering_distance_close_fast,steering_distance_close_slow)
|
||||
|
||||
|
||||
|
||||
if distance_min<steering_distance_far: #wall close, start steering away
|
||||
steer_direction = turndirection*deg_to_rad(constrain(remap(distance_min,steering_distance_far,steering_distance_close,0,steering_angle), 0,steering_angle))
|
||||
else:
|
||||
steer_direction=0 #drive straight
|
||||
|
||||
if autosteer_enabled:
|
||||
if distance_min<steering_distance_far: #wall close, start steering away
|
||||
steer_direction = turndirection*deg_to_rad(constrain(remap(distance_min,steering_distance_far,steering_distance_close,0,steering_angle), 0,steering_angle))
|
||||
else:
|
||||
steer_direction=0 #drive straight
|
||||
|
||||
# Manual steering here
|
||||
var turn = 0
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
|
@ -144,6 +153,10 @@ func get_input():
|
|||
#velocity = transform.x * 500
|
||||
acceleration = transform.x * engine_power
|
||||
|
||||
if not autosteer_enabled: #start autosteer when accelerate is pressed
|
||||
autosteer_enabled=true
|
||||
collision_enable_timer.start()
|
||||
|
||||
if autoreset:
|
||||
print("Cancel autoreset")
|
||||
autoreset=false
|
||||
|
@ -196,3 +209,8 @@ func constrain(val,a,b):
|
|||
var vmin=min(a,b)
|
||||
var vmax=max(a,b)
|
||||
return min(vmax,max(vmin,val))
|
||||
|
||||
|
||||
|
||||
func _on_collision_enable_timer_timeout() -> void:
|
||||
collision_shape.disabled=false
|
||||
|
|
|
@ -1,18 +1,26 @@
|
|||
extends Node2D
|
||||
@onready var car: CharacterBody2D = $Car
|
||||
@onready var carbody: Sprite2D = $Car/sprite_body
|
||||
@onready var cbcar: CharacterBody2D = $CharacterBody_Car
|
||||
@onready var carbody: Sprite2D = $CharacterBody_Car/sprite_body
|
||||
|
||||
|
||||
func getSpeed() -> float:
|
||||
return car.velocity.length()
|
||||
return cbcar.velocity.length()
|
||||
|
||||
func getPosition():
|
||||
return car.position
|
||||
return cbcar.position
|
||||
|
||||
func setPosition(p:Vector2):
|
||||
car.position=p
|
||||
cbcar.position=p
|
||||
|
||||
func setPlayerinformation(playerid, playercolor):
|
||||
car.playerid=playerid
|
||||
cbcar.playerid=playerid
|
||||
#TODO set playercolor
|
||||
carbody.modulate = playercolor#Color(0, 0, 1)
|
||||
carbody.modulate = playercolor
|
||||
|
||||
func move_and_slide():
|
||||
cbcar.move_and_slide()
|
||||
|
||||
func printDebug():
|
||||
for i in cbcar.get_slide_collision_count():
|
||||
var collision = cbcar.get_slide_collision(i)
|
||||
print(""+str(cbcar.playerid)+" Collided with: "+ str( collision.get_collider().name)+" pid="+str(collision.get_collider().playerid)+" pos="+str(collision.get_collider().position))
|
||||
|
|
|
@ -5,12 +5,13 @@ extends Node2D
|
|||
|
||||
@onready var cars: Node = $cars
|
||||
|
||||
const caroffset=32+12 #space cars on start line
|
||||
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.1,0.1)
|
||||
var zoomspeed=0.3
|
||||
var zoomspeed_backup=0.05
|
||||
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.
|
||||
|
@ -22,7 +23,12 @@ func _ready() -> void:
|
|||
var newcarinstance=newcarscene.instantiate()
|
||||
cars.add_child(newcarinstance)
|
||||
newcarinstance.setPlayerinformation(i,player.color)
|
||||
newcarinstance.setPosition(Vector2(0,i*caroffset))
|
||||
|
||||
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
|
||||
|
@ -74,17 +80,22 @@ func _process(delta: float) -> void:
|
|||
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,viewsize.y-carSpread.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<1.0:
|
||||
if camera.zoom.x<zoom_normal:
|
||||
camera.zoom+=Vector2(zoomspeed_backup*delta,zoomspeed_backup*delta)
|
||||
|
||||
#$Camera2D/speedlabel.text=str(carSpread)
|
||||
$hud/speedlabel.text=str(camera.zoom.x)
|
||||
$hud/speedlabel.text=str(calculatedViewCarMargin)
|
||||
#$hud/speedlabel2.text=str(viewsize.x)
|
||||
|
||||
|
||||
|
||||
#$Camera2D/speedlabel.text=str(round(maxCarSpeed))
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ class Player:
|
|||
|
||||
func _init(key:int, i:int=-1):
|
||||
inputkey=key
|
||||
var sat = 0.8
|
||||
var val = 0.5
|
||||
var sat = 1.0
|
||||
var val = 0.7
|
||||
|
||||
if i<0:
|
||||
color=Color.from_hsv(randf_range(0.0,1.0), sat,val,1.0)
|
||||
|
|
|
@ -54,3 +54,10 @@ func _input(ev):
|
|||
#if ev is InputEventKey and
|
||||
if Input.is_action_just_pressed("ui_cancel"):
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
func _on_btn_fullscreen_toggled(toggled_on: bool) -> void:
|
||||
if toggled_on:
|
||||
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
|
||||
else:
|
||||
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
|
||||
|
|
|
@ -28,9 +28,3 @@ func update_playerlist(players:Array[Gamestate.Player]):
|
|||
else:
|
||||
label.text=""
|
||||
i+=1
|
||||
#for p in players:
|
||||
#$Playerlist.text+=str(OS.get_keycode_string(p.inputkey))+"\n"
|
||||
|
||||
|
||||
|
||||
#todo add playercolor here
|
||||
|
|
Loading…
Reference in a new issue