adds keyboard input
This commit is contained in:
@@ -153,15 +153,15 @@ animations = [ {
|
|||||||
"name": "bus",
|
"name": "bus",
|
||||||
"speed": 5.0
|
"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 ) ],
|
"frames": [ ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ), ExtResource( 28 ), ExtResource( 29 ) ],
|
||||||
"loop": true,
|
"loop": true,
|
||||||
"name": "car",
|
"name": "car",
|
||||||
"speed": 5.0
|
"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]
|
[sub_resource type="SpriteFrames" id=6]
|
||||||
@@ -171,15 +171,15 @@ animations = [ {
|
|||||||
"name": "bus",
|
"name": "bus",
|
||||||
"speed": 5.0
|
"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 ) ],
|
"frames": [ ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ), ExtResource( 28 ), ExtResource( 29 ) ],
|
||||||
"loop": true,
|
"loop": true,
|
||||||
"name": "car",
|
"name": "car",
|
||||||
"speed": 5.0
|
"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"]
|
[node name="RaceTrack" type="Node"]
|
||||||
@@ -211,6 +211,7 @@ rotation = 0.112563
|
|||||||
script = ExtResource( 5 )
|
script = ExtResource( 5 )
|
||||||
|
|
||||||
[node name="CarSprite" type="AnimatedSprite" parent="TrackPath/Opponent"]
|
[node name="CarSprite" type="AnimatedSprite" parent="TrackPath/Opponent"]
|
||||||
|
visible = false
|
||||||
position = Vector2( -0.801025, 22.7594 )
|
position = Vector2( -0.801025, 22.7594 )
|
||||||
scale = Vector2( 0.2, 0.2 )
|
scale = Vector2( 0.2, 0.2 )
|
||||||
frames = SubResource( 5 )
|
frames = SubResource( 5 )
|
||||||
|
|||||||
55
TileMap.gd
55
TileMap.gd
@@ -5,11 +5,11 @@ enum { LEFT, RIGHT, UP, DOWN }
|
|||||||
enum { DOWN_LEFT, DOWN_RIGHT, LEFT_RIGHT, START, UP_DOWN, UP_LEFT, UP_RIGHT }
|
enum { DOWN_LEFT, DOWN_RIGHT, LEFT_RIGHT, START, UP_DOWN, UP_LEFT, UP_RIGHT }
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
TrackSelection.connect("track_changed", self, "_reload_track")
|
TrackSelection.connect("track_changed", self, "reload_track")
|
||||||
_reload_track()
|
reload_track()
|
||||||
|
|
||||||
func _reload_track():
|
func reload_track():
|
||||||
_clear_tiles()
|
clear_tiles()
|
||||||
var track = TrackSelection.get_current_track()
|
var track = TrackSelection.get_current_track()
|
||||||
for idx in range(track.size()):
|
for idx in range(track.size()):
|
||||||
var cell = track[idx]
|
var cell = track[idx]
|
||||||
@@ -19,37 +19,28 @@ func _reload_track():
|
|||||||
else:
|
else:
|
||||||
var prev = track[idx - 1]
|
var prev = track[idx - 1]
|
||||||
var next = track[wrapi(idx + 1, 0, track.size())]
|
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)
|
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 """
|
""" Find the right tile given prev and next directions """
|
||||||
prev = _as_dir(prev)
|
prev = as_direction(prev)
|
||||||
next = _as_dir(next)
|
next = as_direction(next)
|
||||||
# Could be smarter
|
match [prev, next]:
|
||||||
if prev == LEFT:
|
[LEFT, DOWN]: return DOWN_LEFT
|
||||||
match next:
|
[LEFT, UP]: return UP_LEFT
|
||||||
DOWN: return DOWN_LEFT
|
[LEFT, RIGHT]: return LEFT_RIGHT
|
||||||
UP: return UP_LEFT
|
[RIGHT, DOWN]: return DOWN_RIGHT
|
||||||
RIGHT: return LEFT_RIGHT
|
[RIGHT, UP]: return UP_RIGHT
|
||||||
elif prev == RIGHT:
|
[RIGHT, LEFT]: return LEFT_RIGHT
|
||||||
match next:
|
[UP, DOWN]: return UP_DOWN
|
||||||
DOWN: return DOWN_RIGHT
|
[UP, LEFT]: return UP_LEFT
|
||||||
UP: return UP_RIGHT
|
[UP, RIGHT]: return UP_RIGHT
|
||||||
LEFT: return LEFT_RIGHT
|
[DOWN, UP]: return UP_DOWN
|
||||||
elif prev == UP:
|
[DOWN, LEFT]: return DOWN_LEFT
|
||||||
match next:
|
[DOWN, RIGHT]: return DOWN_RIGHT
|
||||||
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_direction(vector):
|
||||||
func _as_dir(vector):
|
|
||||||
""" Returns the vector direction as a LEFT, RIGHT, UP or DOWN variant """
|
""" Returns the vector direction as a LEFT, RIGHT, UP or DOWN variant """
|
||||||
if abs(vector.x) + abs(vector.y) != 1:
|
if abs(vector.x) + abs(vector.y) != 1:
|
||||||
return -1 # This is not a valid variant
|
return -1 # This is not a valid variant
|
||||||
@@ -67,6 +58,6 @@ func _as_dir(vector):
|
|||||||
#func _process(delta):
|
#func _process(delta):
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
func _clear_tiles():
|
func clear_tiles():
|
||||||
for cell in get_used_cells():
|
for cell in get_used_cells():
|
||||||
self.set_cell(cell.x, cell.y, -1)
|
self.set_cell(cell.x, cell.y, -1)
|
||||||
8
assets/tracks/Circuit_de_bernede.txt
Normal file
8
assets/tracks/Circuit_de_bernede.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
...............
|
||||||
|
.xxsxxxxxxxx...
|
||||||
|
.x.........xxx.
|
||||||
|
.x...xxxxx...x.
|
||||||
|
.x...x...x...x.
|
||||||
|
.xxxxx...xxxxx.
|
||||||
|
...............
|
||||||
|
...............
|
||||||
8
assets/tracks/track2.txt
Normal file
8
assets/tracks/track2.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
...............
|
||||||
|
.xxxsxxxxxxxxx.
|
||||||
|
.x...........x.
|
||||||
|
.x...........x.
|
||||||
|
.xxxxxxxx....x.
|
||||||
|
........x....x.
|
||||||
|
........xxxxxx.
|
||||||
|
...............
|
||||||
@@ -29,9 +29,42 @@ func _input(event):
|
|||||||
_handle_keyboard_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):
|
func _handle_keyboard_input(event):
|
||||||
if event is InputEventKey:
|
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):
|
func _handle_mouse_input(event):
|
||||||
@@ -50,11 +83,7 @@ func _handle_mouse_input(event):
|
|||||||
laps += 1
|
laps += 1
|
||||||
# We use the loop property of PathFollow2D to embed
|
# We use the loop property of PathFollow2D to embed
|
||||||
# laps count inside the offset.
|
# laps count inside the offset.
|
||||||
emit_signal("player_moved", laps + get_track_offset(hover_cell))
|
step_next()
|
||||||
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:
|
else:
|
||||||
emit_signal("wrong_way", map.map_to_world(hover_cell))
|
emit_signal("wrong_way", map.map_to_world(hover_cell))
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func _build_track(tiles):
|
|||||||
# pass
|
# pass
|
||||||
|
|
||||||
func _point_at(_curve, pos, dir):
|
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 half_size = tile_size / 2
|
||||||
var control_in
|
var control_in
|
||||||
match dir:
|
match dir:
|
||||||
|
|||||||
Reference in New Issue
Block a user