Files
CarRacer/RaceTrack.gd

48 lines
1.4 KiB
GDScript

extends Node
signal player_moved(track_offset)
signal lap_completed(laps)
signal wrong_way(coords)
# TODO:
# - Find a mean to import tracks (arrays of cell coordinates).
# ( - Autopopulate the map according to track data )
# - Build the track Curve2D from track and tiles data
# - Keyboard controls !!
var laps
var current_cell
onready var track = TrackSelection.get_current_track()
onready var map = $TileMap
func get_track_offset(coords):
var offset = float(track.find(coords)) / float(len(track))
return offset
# Called when the node enters the scene tree for the first time.
func _ready():
_reset_progress()
func _reset_progress():
laps = 0
current_cell = track[0]
func _input(event):
# Check if the mouse if following the tiles track
if event is InputEventMouseMotion:
var hover_cell = map.world_to_map(event.position)
if hover_cell != current_cell: # The mouse moved to a new cell
# Check the tile is on path
if track.find(hover_cell) != -1:
# Check if a lap is finished
if hover_cell == track[0] and current_cell == track[-1]:
laps += 1
emit_signal("lap_completed", laps)
# Check we are following the path
if track[track.find(hover_cell) - 1] == current_cell:
emit_signal("player_moved", laps + get_track_offset(hover_cell))
emit_signal("wrong_way", Vector2(-1, -1))
current_cell = hover_cell
else:
emit_signal("wrong_way", map.map_to_world(hover_cell))