bugged-racing/scenes/tracks/track.gd

86 lines
2.4 KiB
GDScript
Raw Normal View History

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
export(Vector2) var checkpoint_dim = Vector2(20, 15)
export(Material) var debug_material = null
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-18 23:36:05 +00:00
onready var checkpoints = $Checkpoints
onready var path: Path = get_node(track_path)
2021-11-18 23:08:21 +00:00
func get_last_checkpoint() -> Node:
return checkpoints.get_child(max(0, last_checkpoint))
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()
var section_size = 1.0 * length / checkpoint_count
var section = 0.0
for _checkpoint_number in range(checkpoint_count):
var new_checkpoint: Area = Area.new()
new_checkpoint.add_child(_build_checkpoint_collision())
new_checkpoint.transform.origin = path.curve.interpolate_baked(section, true)
section += section_size
checkpoints.add_child(new_checkpoint)
2021-11-17 22:23:12 +00:00
if GlobalSettings.debug:
2021-11-17 19:10:51 +00:00
var mesh = CylinderMesh.new()
mesh.top_radius = checkpoint_dim.y
mesh.bottom_radius = checkpoint_dim.y
mesh.height = checkpoint_dim.x
if debug_material != null:
mesh.material = debug_material
var meshinst = MeshInstance.new()
meshinst.mesh = mesh
new_checkpoint.add_child(meshinst)
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-18 19:44:53 +00:00
if furthest_checkpoint == checkpoints.get_child_count() - 1:
2021-11-17 19:10:51 +00:00
emit_signal("lap_complete", current_time)
start_time = OS.get_ticks_msec()
_update_time()
2021-11-18 19:44:53 +00:00
furthest_checkpoint = -1
2021-11-17 19:10:51 +00:00
func _build_checkpoint_collision():
var collision = CollisionShape.new()
var shape = CylinderShape.new()
shape.radius = checkpoint_dim.x
shape.height = checkpoint_dim.y
collision.shape = shape
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)