64 lines
1.7 KiB
GDScript
64 lines
1.7 KiB
GDScript
extends TileMap
|
|
|
|
enum { LEFT, RIGHT, UP, DOWN }
|
|
# Enum for tiles from tile_set
|
|
enum { DOWN_LEFT, DOWN_RIGHT, LEFT_RIGHT, START, UP_DOWN, UP_LEFT, UP_RIGHT }
|
|
|
|
func _ready():
|
|
TrackSelection.connect("track_changed", self, "reload_track")
|
|
reload_track()
|
|
|
|
func reload_track():
|
|
clear_tiles()
|
|
var track = TrackSelection.get_current_track()
|
|
for idx in range(track.size()):
|
|
var cell = track[idx]
|
|
var tile
|
|
if idx == 0:
|
|
tile = START
|
|
else:
|
|
var prev = track[idx - 1]
|
|
var next = track[wrapi(idx + 1, 0, track.size())]
|
|
tile = find_tile(cell.direction_to(prev), cell.direction_to(next))
|
|
set_cell(cell.x, cell.y, tile)
|
|
|
|
func find_tile(prev, next):
|
|
""" Find the right tile given prev and next directions """
|
|
prev = as_direction(prev)
|
|
next = as_direction(next)
|
|
match [prev, next]:
|
|
[LEFT, DOWN]: return DOWN_LEFT
|
|
[LEFT, UP]: return UP_LEFT
|
|
[LEFT, RIGHT]: return LEFT_RIGHT
|
|
[RIGHT, DOWN]: return DOWN_RIGHT
|
|
[RIGHT, UP]: return UP_RIGHT
|
|
[RIGHT, LEFT]: return LEFT_RIGHT
|
|
[UP, DOWN]: return UP_DOWN
|
|
[UP, LEFT]: return UP_LEFT
|
|
[UP, RIGHT]: return UP_RIGHT
|
|
[DOWN, UP]: return UP_DOWN
|
|
[DOWN, LEFT]: return DOWN_LEFT
|
|
[DOWN, RIGHT]: return DOWN_RIGHT
|
|
|
|
func as_direction(vector):
|
|
""" Returns the vector direction as a LEFT, RIGHT, UP or DOWN variant """
|
|
if abs(vector.x) + abs(vector.y) != 1:
|
|
return -1 # This is not a valid variant
|
|
if vector.x == -1:
|
|
return LEFT
|
|
elif vector.x == 1:
|
|
return RIGHT
|
|
elif vector.y == -1:
|
|
return UP
|
|
elif vector.y == 1:
|
|
return DOWN
|
|
return -1 # Impossible case
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
#func _process(delta):
|
|
# pass
|
|
|
|
func clear_tiles():
|
|
for cell in get_used_cells():
|
|
self.set_cell(cell.x, cell.y, -1)
|