cleans code. adds sign for wrong way

This commit is contained in:
2020-11-06 21:26:56 +01:00
parent a8280df733
commit d439d67813
7 changed files with 251 additions and 89 deletions

View File

@@ -1,58 +1,72 @@
extends Node
# This is where the racing logic is handled (player car input and movement,
# ensuring valid path, ...)
# We keep informations on player progress here.
signal player_moved(track_offset)
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 laps # This is duplicated here becayse of the track offset logic
var current_cell
var next_cell
var track = null
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():
TrackSelection.connect("track_changed", self, "_reset_state")
_reset_state()
func _input(event):
match Global.settings.input_mode:
Global.InputMode.MOUSE:
_handle_mouse_input(event)
Global.InputMode.KEYBOARD:
_handle_keyboard_input(event)
func _handle_keyboard_input(event):
if event is InputEventKey:
print_debug("Input Ignored: KEYBOARD MODE")
func _handle_mouse_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
var cell_idx = track.find(hover_cell)
# Check the tile is on path
if cell_idx != -1:
# Check we are following the path
if hover_cell == next_cell:
# Check if lap is completed. The is required
# because the input can be way ahead player car sprite
if next_cell == track[0]:
laps += 1
# We use the loop property of PathFollow2D to embed
# laps count inside the offset.
emit_signal("player_moved", laps + get_track_offset(hover_cell))
emit_signal("wrong_way", Vector2(-1, -1))
current_cell = next_cell
var next_idx = cell_idx + 1 if cell_idx < track.size() - 1 else 0
next_cell = track[next_idx]
else:
emit_signal("wrong_way", map.map_to_world(hover_cell))
func get_track_offset(coords):
# Returns the offset in Path2D of a track cell.
var offset = float(track.find(coords)) / float(len(track))
return offset
func _reset_state():
track = TrackSelection.get_current_track()
laps = 0
current_cell = track[0]
func _input(event):
if not Global.race_started:
return -1
# 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
# 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))
func _on_Menu_track_changed():
print("Options clicked")
TrackSelection.set_next_track()
_reset_state()
$TileMap._reset()
$TrackPath._reset()
next_cell = track[1]