Skip to content

Commit 4643c11

Browse files
committed
player: Add "shrink" special ability
As with the other special abilities, the function call in _physics_process() that implements this ability is commented out.
1 parent e4a66af commit 4643c11

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

project.godot

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ player_2_phase={
7171
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":2,"pressure":0.0,"pressed":true,"script":null)
7272
]
7373
}
74+
player_2_shrink={
75+
"deadzone": 0.5,
76+
"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":83,"key_label":0,"unicode":111,"location":0,"echo":false,"script":null)
77+
, 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":83,"physical_keycode":0,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
78+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
79+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":7,"pressure":0.0,"pressed":true,"script":null)
80+
]
81+
}
7482
player_1_jump={
7583
"deadzone": 0.5,
7684
"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)
@@ -104,6 +112,13 @@ player_1_phase={
104112
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":true,"script":null)
105113
]
106114
}
115+
player_1_shrink={
116+
"deadzone": 0.5,
117+
"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":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
118+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
119+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":7,"pressure":0.0,"pressed":true,"script":null)
120+
]
121+
}
107122

108123
[layer_names]
109124

scripts/actions.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ACTIONS = [
1414
&"right",
1515
&"teleport",
1616
&"phase",
17+
&"shrink",
1718
]
1819

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

scripts/player.gd

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ const GLIDE_TERMINAL_VELOCITY = 100
1515
## Used by [method _teleport].
1616
const TELEPORT_DISTANCE = 512
1717

18+
## How much to scale [member jump_velocity] when the player-character is shrunk. Setting this close
19+
## to or below [code]0[/code] prevents jumping; setting this to [code]1[/code] or greater causes
20+
## jumps while shrunk to be the same as normal size.
21+
## [br][br]
22+
## Used by [method _shrink].
23+
const JUMP_VELOCITY_SCALE_WHEN_SMALL = 0.85
24+
1825
## Which player controls this character?
1926
@export var player: Global.Player = Global.Player.ONE
2027

@@ -64,6 +71,9 @@ var double_jump_armed: bool = false
6471
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
6572
var original_position: Vector2
6673

74+
# Whether the player-character is currently shrunk. See _shrink().
75+
var _is_shrunk := false
76+
6777
@onready var _sprite: AnimatedSprite2D = %AnimatedSprite2D
6878
@onready var _initial_sprite_frames: SpriteFrames = %AnimatedSprite2D.sprite_frames
6979
@onready var _double_jump_particles: CPUParticles2D = %DoubleJumpParticles
@@ -171,6 +181,29 @@ func _phase() -> void:
171181
_sprite.modulate.a = 1
172182

173183

184+
## When the "shrink" action is pressed, toggle the player between normal size and half-size. While
185+
## shrunk, the player can pass through narrower passages, but cannot jump so high.
186+
func _shrink() -> void:
187+
if Input.is_action_just_pressed(Actions.lookup(player, "shrink")):
188+
_is_shrunk = not _is_shrunk
189+
190+
if _is_shrunk:
191+
# Shrink the player-character's sprite and collision shape
192+
scale = Vector2(0.5, 0.5)
193+
else:
194+
scale = Vector2(1, 1)
195+
196+
if _is_shrunk:
197+
# Reduce the jump height while shrunk. _jump() sets velocity.y to -jump_velocity, so
198+
# clamping this to a smaller value cuts the initial upwards velocity, and hence the jump
199+
# height.
200+
if velocity.y < -jump_velocity * JUMP_VELOCITY_SCALE_WHEN_SMALL:
201+
velocity.y = -jump_velocity * JUMP_VELOCITY_SCALE_WHEN_SMALL
202+
203+
# TODO: should there be other consequences to being small? Could we make the player somehow more
204+
# vulnerable to enemies?
205+
206+
174207
func _physics_process(delta):
175208
# Don't move if there are no lives left.
176209
if Global.lives <= 0:
@@ -198,6 +231,8 @@ func _physics_process(delta):
198231
if coyote_timer <= 0:
199232
velocity.y += gravity * delta
200233

234+
# _shrink()
235+
201236
# Get the input direction and handle the movement/deceleration.
202237
var direction = Input.get_axis(Actions.lookup(player, "left"), Actions.lookup(player, "right"))
203238
if direction:

0 commit comments

Comments
 (0)