Skip to content

Commit 42a20a3

Browse files
committed
player: Add teleport/blink action
If the player enters this action while also holding left or right, the player-character teleports by a constant distance horizontally. On keyboard, this is bound to "Enter" for player 1 (who moves with the arrow keys), and "Shift" for player 2 (who moves with WASD). On gamepad, this is bound to Xbox button B. The function call for this ability is in the correct place in _physics_process() but is commented out. A learner must find this commented-out line and uncomment it to enable this ability. The sound effect is https://freesound.org/people/D.S.G./sounds/244654/ (CC0-1.0).
1 parent 7fb6989 commit 42a20a3

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed
4.35 KB
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[remap]
2+
3+
importer="oggvorbisstr"
4+
type="AudioStreamOggVorbis"
5+
uid="uid://dmx2rtf2vy1"
6+
path="res://.godot/imported/244654__dsg__pop-2.ogg-27b8aa37b813c338a8b122e748e26d18.oggvorbisstr"
7+
8+
[deps]
9+
10+
source_file="res://assets/sounds/244654__dsg__pop-2.ogg"
11+
dest_files=["res://.godot/imported/244654__dsg__pop-2.ogg-27b8aa37b813c338a8b122e748e26d18.oggvorbisstr"]
12+
13+
[params]
14+
15+
loop=false
16+
loop_offset=0
17+
bpm=0
18+
beat_count=0
19+
bar_beats=4

components/player/player.tscn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[ext_resource type="Script" uid="uid://umb21eb2y1oj" path="res://scripts/player.gd" id="1_w3ms2"]
44
[ext_resource type="SpriteFrames" uid="uid://bo581k1esb50n" path="res://components/player/spriteframes-red.tres" id="2_msaml"]
5+
[ext_resource type="AudioStream" uid="uid://dmx2rtf2vy1" path="res://assets/sounds/244654__dsg__pop-2.ogg" id="3_ph1f6"]
56
[ext_resource type="AudioStream" uid="uid://bx1joarpc14j5" path="res://assets/sounds/538066__stevielematt__boing.ogg" id="3_wa6cj"]
67
[ext_resource type="AudioStream" uid="uid://p0b3e0t7h8j3" path="res://assets/sounds/596541__tothrec2__large-wings-flapping-foley.ogg" id="4_mpawu"]
78

@@ -14,6 +15,11 @@ random_pitch = 1.1
1415
streams_count = 1
1516
stream_0/stream = ExtResource("3_wa6cj")
1617

18+
[sub_resource type="AudioStreamRandomizer" id="AudioStreamRandomizer_ph1f6"]
19+
random_pitch = 1.2
20+
streams_count = 1
21+
stream_0/stream = ExtResource("3_ph1f6")
22+
1723
[node name="Player" type="CharacterBody2D" groups=["players"]]
1824
collision_layer = 3
1925
collision_mask = 5
@@ -56,3 +62,8 @@ stream = SubResource("AudioStreamRandomizer_mpawu")
5662
[node name="GlideSFX" type="AudioStreamPlayer" parent="SoundEffects"]
5763
unique_name_in_owner = true
5864
stream = ExtResource("4_mpawu")
65+
66+
[node name="TeleportSFX" type="AudioStreamPlayer" parent="SoundEffects"]
67+
unique_name_in_owner = true
68+
stream = SubResource("AudioStreamRandomizer_ph1f6")
69+
volume_db = -10.0

project.godot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ player_2_right={
5959
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":0,"axis_value":1.0,"script":null)
6060
]
6161
}
62+
player_2_teleport={
63+
"deadzone": 0.5,
64+
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":1,"echo":false,"script":null)
65+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":1,"pressure":0.0,"pressed":true,"script":null)
66+
]
67+
}
6268
player_1_jump={
6369
"deadzone": 0.5,
6470
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194320,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
@@ -80,6 +86,12 @@ player_1_right={
8086
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
8187
]
8288
}
89+
player_1_teleport={
90+
"deadzone": 0.5,
91+
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194309,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
92+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":1,"pressure":0.0,"pressed":true,"script":null)
93+
]
94+
}
8395

8496
[layer_names]
8597

scripts/actions.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ACTIONS = [
1212
&"jump",
1313
&"left",
1414
&"right",
15+
&"teleport",
1516
]
1617

1718
# Dictionary[Global.Player, Dictionary[StringName, StringName]]

scripts/player.gd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ extends CharacterBody2D
99
## Used by [method _glide].
1010
const GLIDE_TERMINAL_VELOCITY = 100
1111

12+
## How many pixels the player-character should teleport horizontally when the
13+
## teleport special ability is used.
14+
## [br][br]
15+
## Used by [method _teleport].
16+
const TELEPORT_DISTANCE = 512
17+
1218
## Which player controls this character?
1319
@export var player: Global.Player = Global.Player.ONE
1420

@@ -64,6 +70,7 @@ var original_position: Vector2
6470

6571
@onready var _jump_sfx: AudioStreamPlayer = %JumpSFX
6672
@onready var _glide_sfx: AudioStreamPlayer = %GlideSFX
73+
@onready var _teleport_sfx: AudioStreamPlayer = %TeleportSFX
6774

6875

6976
func _set_sprite_frames(new_sprite_frames):
@@ -128,6 +135,19 @@ func _glide() -> void:
128135
_glide_sfx.stop()
129136

130137

138+
## If the "teleport" action is pressed, and the player is moving the character horizontally,
139+
## teleport the character in that horizontal direction.
140+
func _teleport(input_direction: float) -> void:
141+
if (
142+
Input.is_action_just_pressed(Actions.lookup(player, "teleport"))
143+
and not is_zero_approx(input_direction)
144+
):
145+
# TODO: Check if we are teleporting into a wall (in which case the player should lose a
146+
# life) or an enemy (in which case maybe the enemy should be telefragged/defeated?)
147+
global_position.x += TELEPORT_DISTANCE * input_direction
148+
_teleport_sfx.play()
149+
150+
131151
func _physics_process(delta):
132152
# Don't move if there are no lives left.
133153
if Global.lives <= 0:
@@ -180,6 +200,8 @@ func _physics_process(delta):
180200

181201
move_and_slide()
182202

203+
# _teleport(direction)
204+
183205
coyote_timer -= delta
184206
jump_buffer_timer -= delta
185207

0 commit comments

Comments
 (0)