adds keyboard input

This commit is contained in:
2021-04-20 19:01:50 +02:00
parent 9726fb8fcf
commit 39451c5553
6 changed files with 88 additions and 51 deletions

View File

@@ -153,15 +153,15 @@ animations = [ {
"name": "bus",
"speed": 5.0
}, {
"frames": [ ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ), ExtResource( 19 ), ExtResource( 20 ), ExtResource( 21 ) ],
"loop": true,
"name": "police",
"speed": 5.0
}, {
"frames": [ ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ), ExtResource( 28 ), ExtResource( 29 ) ],
"loop": true,
"name": "car",
"speed": 5.0
}, {
"frames": [ ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ), ExtResource( 19 ), ExtResource( 20 ), ExtResource( 21 ) ],
"loop": true,
"name": "police",
"speed": 5.0
} ]
[sub_resource type="SpriteFrames" id=6]
@@ -171,15 +171,15 @@ animations = [ {
"name": "bus",
"speed": 5.0
}, {
"frames": [ ExtResource( 19 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 20 ), ExtResource( 21 ), ExtResource( 18 ) ],
"loop": true,
"name": "police",
"speed": 5.0
}, {
"frames": [ ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ), ExtResource( 28 ), ExtResource( 29 ) ],
"loop": true,
"name": "car",
"speed": 5.0
}, {
"frames": [ ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ), ExtResource( 19 ), ExtResource( 20 ), ExtResource( 21 ) ],
"loop": true,
"name": "police",
"speed": 5.0
} ]
[node name="RaceTrack" type="Node"]
@@ -211,6 +211,7 @@ rotation = 0.112563
script = ExtResource( 5 )
[node name="CarSprite" type="AnimatedSprite" parent="TrackPath/Opponent"]
visible = false
position = Vector2( -0.801025, 22.7594 )
scale = Vector2( 0.2, 0.2 )
frames = SubResource( 5 )

View File

@@ -5,11 +5,11 @@ enum { LEFT, RIGHT, UP, DOWN }
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()
TrackSelection.connect("track_changed", self, "reload_track")
reload_track()
func _reload_track():
_clear_tiles()
func reload_track():
clear_tiles()
var track = TrackSelection.get_current_track()
for idx in range(track.size()):
var cell = track[idx]
@@ -19,37 +19,28 @@ func _reload_track():
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))
tile = find_tile(cell.direction_to(prev), cell.direction_to(next))
set_cell(cell.x, cell.y, tile)
func _find_tile(prev, next):
func find_tile(prev, next):
""" Find the right tile given prev and next directions """
prev = _as_dir(prev)
next = _as_dir(next)
# Could be smarter
if prev == LEFT:
match next:
DOWN: return DOWN_LEFT
UP: return UP_LEFT
RIGHT: return LEFT_RIGHT
elif prev == RIGHT:
match next:
DOWN: return DOWN_RIGHT
UP: return UP_RIGHT
LEFT: return LEFT_RIGHT
elif prev == UP:
match next:
DOWN: return UP_DOWN
LEFT: return UP_LEFT
RIGHT: return UP_RIGHT
elif prev == DOWN:
match next:
UP: return UP_DOWN
LEFT: return DOWN_LEFT
RIGHT: return DOWN_RIGHT
func _as_dir(vector):
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
@@ -67,6 +58,6 @@ func _as_dir(vector):
#func _process(delta):
# pass
func _clear_tiles():
func clear_tiles():
for cell in get_used_cells():
self.set_cell(cell.x, cell.y, -1)
self.set_cell(cell.x, cell.y, -1)

View File

@@ -0,0 +1,8 @@
...............
.xxsxxxxxxxx...
.x.........xxx.
.x...xxxxx...x.
.x...x...x...x.
.xxxxx...xxxxx.
...............
...............

8
assets/tracks/track2.txt Normal file
View File

@@ -0,0 +1,8 @@
...............
.xxxsxxxxxxxxx.
.x...........x.
.x...........x.
.xxxxxxxx....x.
........x....x.
........xxxxxx.
...............

View File

@@ -29,9 +29,42 @@ func _input(event):
_handle_keyboard_input(event)
func step_next():
""" Step this module state to the next cell """
current_cell = next_cell
var current_idx = track.find(current_cell)
var next_idx = current_idx + 1 if current_idx < track.size() - 1 else 0
next_cell = track[next_idx]
emit_signal("player_moved", laps + get_track_offset(current_cell))
emit_signal("wrong_way", Vector2(-1, -1)) # Cancel any wrong way sign
func _handle_keyboard_input(event):
if event is InputEventKey:
print_debug("Input Ignored: KEYBOARD MODE")
var direction_to_next = current_cell.direction_to(next_cell)
if event.is_action_pressed("ui_up"):
print_debug("Up key")
if direction_to_next == Vector2(0, -1):
step_next()
else:
emit_signal("wrong_way", map.map_to_world(Vector2(current_cell.x, current_cell.y - 1)))
elif event.is_action_pressed("ui_right"):
print_debug("Right key")
if direction_to_next == Vector2(1, 0):
step_next()
else:
emit_signal("wrong_way", map.map_to_world(Vector2(current_cell.x + 1, current_cell.y)))
elif event.is_action_pressed("ui_left"):
print_debug("Left key")
if direction_to_next == Vector2(-1, 0):
step_next()
else:
emit_signal("wrong_way", map.map_to_world(Vector2(current_cell.x - 1, current_cell.y)))
elif event.is_action_pressed("ui_down"):
print_debug("Key down")
if direction_to_next == Vector2(0, 1):
step_next()
else:
emit_signal("wrong_way", map.map_to_world(Vector2(current_cell.x, current_cell.y + 1)))
func _handle_mouse_input(event):
@@ -50,11 +83,7 @@ func _handle_mouse_input(event):
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]
step_next()
else:
emit_signal("wrong_way", map.map_to_world(hover_cell))

View File

@@ -54,7 +54,7 @@ func _build_track(tiles):
# pass
func _point_at(_curve, pos, dir):
# Use only control_in since it gives smoother turns
# Using only control_in gives smoother turns
var half_size = tile_size / 2
var control_in
match dir: