putting together in Main scene

This commit is contained in:
2020-10-30 15:23:13 +01:00
parent e528d2ec7f
commit edc4b5630d
16 changed files with 433 additions and 188 deletions

View File

@@ -1,86 +1,44 @@
extends Path2D
extends Node
enum { UP, RIGHT, DOWN, LEFT }
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),
]
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 = Global.TRACKS[0]
onready var map = $TileMap
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
var tile_size : int = 128
var half_size : int = tile_size / 2
onready var map = get_node("/root/Track2/TileMap")
# Called when the node enters the scene tree for the first time.
func _ready():
_build_track(TRACK_TILES)
pass # Replace with function body.
func _draw():
draw_polyline(curve.get_baked_points(), Color.red, 4.0)
for idx in range(0,curve.get_point_count()):
draw_circle(curve.get_point_in(idx), 2.0, Color.green)
draw_circle(curve.get_point_out(idx), 2.0, Color.blue)
func _build_track(tiles):
""" Build the track curve given a tile path """
var _curve = get_curve()
_curve.clear_points()
# Set start point
var start_point = map.map_to_world(tiles[0])
start_point.y += half_size
start_point.x += half_size
_point_at(_curve, start_point, RIGHT)
for idx in range(0, tiles.size()):
var next_idx = idx + 1 if idx < tiles.size() - 1 else 0
var dir = _direction_to(tiles[idx], tiles[next_idx])
# Find position of next point
var pos = map.map_to_world(tiles[idx])
match dir:
# We start from upper left corner
UP: pos = Vector2(pos.x + half_size, pos.y)
RIGHT: pos = Vector2(pos.x + tile_size, pos.y + half_size)
DOWN: pos = Vector2(pos.x + half_size, pos.y + tile_size)
LEFT: pos = Vector2(pos.x, pos.y + half_size)
_point_at(_curve, pos, dir)
_point_at(_curve, start_point, RIGHT)
set_curve(_curve)
print_debug("Built track !", _curve)
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _point_at(_curve, pos, dir):
# Use only control_in since it gives smoother turns
var half_size = tile_size / 2
var control_in
match dir:
DOWN:
control_in = Vector2(0, - half_size)
#control_out = Vector2(0, half_size)
UP:
control_in = Vector2(0, half_size)
#control_out = Vector2(0, -half_size)
RIGHT:
control_in = Vector2( - half_size, 0)
#control_out = Vector2(half_size, 0)
LEFT:
control_in = Vector2(half_size, 0)
#control_out = Vector2(-half_size, 0)
#print_debug("Add point: ", pos, ", in=", control_in), " out=", control_out, " | ", dir)
_curve.add_point(pos, control_in) #, control_out)
func _direction_to(from_tile, to_tile):
var x_dir = to_tile.x - from_tile.x
var y_dir = to_tile.y - from_tile.y
if abs(x_dir + y_dir) == 1:
if x_dir == 1:
return RIGHT
elif x_dir == -1:
return LEFT
elif y_dir == 1:
return DOWN
elif y_dir == -1:
return UP
else:
print_debug("Panic!! Path is not contiguous between", from_tile, " and ", to_tile)
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_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.map_to_world(hover_cell))