2021-11-17 19:10:51 +00:00
|
|
|
class_name Track
|
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
signal time_updated(new_time)
|
|
|
|
signal lap_complete(lap_time)
|
2021-11-18 23:36:05 +00:00
|
|
|
signal wrong_way
|
2021-11-17 19:10:51 +00:00
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
export(NodePath) var track_path = null
|
|
|
|
export(int, 10, 50) var checkpoint_count = 20
|
2021-11-20 16:19:39 +00:00
|
|
|
export(float) var checkpoint_depth = 5.0
|
|
|
|
export(PoolVector2Array) var checkpoint_polygon = PoolVector2Array(
|
|
|
|
[Vector2(-10, -10), Vector2(-10, 10), Vector2(10, 10), Vector2(10, -10)]
|
|
|
|
)
|
2021-11-17 19:10:51 +00:00
|
|
|
|
2021-11-18 19:44:53 +00:00
|
|
|
var furthest_checkpoint = -1
|
2021-11-17 19:10:51 +00:00
|
|
|
var last_checkpoint = -1
|
|
|
|
|
|
|
|
var start_time = 0
|
|
|
|
var current_time = 0
|
|
|
|
|
2021-11-21 22:18:30 +00:00
|
|
|
var lap_done = false
|
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
onready var checkpoints = $Checkpoints
|
|
|
|
onready var path: Path = get_node(track_path)
|
|
|
|
|
|
|
|
|
2021-11-20 17:46:23 +00:00
|
|
|
func get_furthest_checkpoint() -> Node:
|
|
|
|
return checkpoints.get_child(max(0, furthest_checkpoint))
|
2021-11-18 23:08:21 +00:00
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
|
2021-11-17 19:10:51 +00:00
|
|
|
func _ready() -> void:
|
|
|
|
start_time = OS.get_ticks_msec()
|
|
|
|
var length = path.curve.get_baked_length()
|
2021-11-20 16:44:35 +00:00
|
|
|
var section_size = 1 * length / checkpoint_count
|
2021-11-17 19:10:51 +00:00
|
|
|
var section = 0.0
|
|
|
|
for _checkpoint_number in range(checkpoint_count):
|
|
|
|
var new_checkpoint: Area = Area.new()
|
|
|
|
new_checkpoint.add_child(_build_checkpoint_collision())
|
2021-11-20 16:04:57 +00:00
|
|
|
new_checkpoint.look_at_from_position(
|
|
|
|
checkpoints.to_global(path.curve.interpolate_baked(section)),
|
|
|
|
checkpoints.to_global(path.curve.interpolate_baked(section + 0.01)),
|
|
|
|
path.curve.interpolate_baked_up_vector(section)
|
|
|
|
)
|
2021-11-17 19:10:51 +00:00
|
|
|
section += section_size
|
|
|
|
checkpoints.add_child(new_checkpoint)
|
2021-11-18 23:36:05 +00:00
|
|
|
new_checkpoint.connect("body_entered", self, "_on_body_entered_area", [new_checkpoint])
|
2021-11-17 19:10:51 +00:00
|
|
|
checkpoints.global_transform.origin = path.global_transform.origin
|
|
|
|
|
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
func _process(_delta: float) -> void:
|
2021-11-17 19:10:51 +00:00
|
|
|
_update_time()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_body_entered_area(body: Node, area: Area) -> void:
|
|
|
|
if body.get_groups().has("car"):
|
2021-11-18 19:44:53 +00:00
|
|
|
if area.get_index() < last_checkpoint || abs(area.get_index() - last_checkpoint) > 1:
|
|
|
|
emit_signal("wrong_way")
|
|
|
|
|
|
|
|
last_checkpoint = area.get_index()
|
|
|
|
|
2021-11-17 19:10:51 +00:00
|
|
|
# We got the correct checkpoint
|
2021-11-18 19:44:53 +00:00
|
|
|
if furthest_checkpoint == area.get_index() - 1:
|
|
|
|
furthest_checkpoint += 1
|
2021-11-17 19:10:51 +00:00
|
|
|
|
2021-11-21 22:18:30 +00:00
|
|
|
if lap_done and furthest_checkpoint == 0:
|
2021-11-17 19:10:51 +00:00
|
|
|
emit_signal("lap_complete", current_time)
|
|
|
|
start_time = OS.get_ticks_msec()
|
|
|
|
_update_time()
|
2021-11-21 22:18:30 +00:00
|
|
|
lap_done = false
|
|
|
|
|
|
|
|
if furthest_checkpoint == checkpoints.get_child_count() - 1:
|
|
|
|
lap_done = true
|
2021-11-18 19:44:53 +00:00
|
|
|
furthest_checkpoint = -1
|
2021-11-21 22:18:30 +00:00
|
|
|
last_checkpoint = -1
|
2021-11-17 19:10:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _build_checkpoint_collision():
|
2021-11-20 16:19:39 +00:00
|
|
|
var collision = CollisionPolygon.new()
|
|
|
|
collision.depth = checkpoint_depth
|
|
|
|
collision.polygon = checkpoint_polygon
|
2021-11-17 19:10:51 +00:00
|
|
|
return collision
|
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
|
2021-11-17 19:10:51 +00:00
|
|
|
func _update_time():
|
|
|
|
current_time = OS.get_ticks_msec() - start_time
|
|
|
|
emit_signal("time_updated", current_time)
|