Files
CarRacer/TileMap2.gd

54 lines
1.7 KiB
GDScript

extends TileMap
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 !!
const TRACK_TILES = [
Vector2(7,1), Vector2(8,1), Vector2(9,1), Vector2(9,2), Vector2(9,3),
Vector2(8,3), Vector2(7,3), Vector2(7,4), Vector2(7,5), Vector2(7,6),
Vector2(6,6), Vector2(6,5), Vector2(6,4), Vector2(6,3), Vector2(5,3),
Vector2(4,3), Vector2(4,2), Vector2(4,1), Vector2(5,1), Vector2(6,1),
]
var laps = 0
var current_cell = TRACK_TILES[0]
func get_track_offset(coords):
var offset = float(TRACK_TILES.find(coords)) / float(len(TRACK_TILES))
return offset
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _input(event):
# Check if the mouse if following the tiles track
if event is InputEventMouseMotion:
var hover_cell = 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_TILES.find(hover_cell) != -1:
# Check if a lap is finished
if hover_cell == TRACK_TILES[0] and current_cell == TRACK_TILES[-1]:
laps += 1
emit_signal("lap_completed", laps)
# Check we are following the path
if TRACK_TILES[TRACK_TILES.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_to_world(hover_cell))
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass