Skip to content

Commit d989bf6

Browse files
committed
Use fewer ternary operators and decrease volume of Platformer 2D
1 parent 0210702 commit d989bf6

File tree

9 files changed

+54
-16
lines changed

9 files changed

+54
-16
lines changed

2d/finite_state_machine/player/states/motion/in_air/jump.gd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ var height = 0.0
1919

2020
func initialize(speed, velocity):
2121
horizontal_speed = speed
22-
max_horizontal_speed = speed if speed > 0.0 else base_max_horizontal_speed
22+
if speed > 0.0:
23+
max_horizontal_speed = speed
24+
else:
25+
max_horizontal_speed = base_max_horizontal_speed
2326
enter_velocity = velocity
2427

2528

2629
func enter():
2730
var input_direction = get_input_direction()
2831
update_look_direction(input_direction)
2932

30-
horizontal_velocity = enter_velocity if input_direction else Vector2()
33+
if input_direction:
34+
horizontal_velocity = enter_velocity
35+
else:
36+
horizontal_velocity = Vector2()
3137
vertical_speed = 600.0
3238

3339
owner.get_node("AnimationPlayer").play("idle")

2d/finite_state_machine/player/states/motion/on_ground/move.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func update(_delta):
2222
emit_signal("finished", "idle")
2323
update_look_direction(input_direction)
2424

25-
speed = max_run_speed if Input.is_action_pressed("run") else max_walk_speed
25+
if Input.is_action_pressed("run"):
26+
speed = max_run_speed
27+
else:
28+
speed = max_walk_speed
29+
2630
var collision_info = move(speed, input_direction)
2731
if not collision_info:
2832
return

2d/platformer/src/Actors/Enemy.gd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ func _physics_process(_delta):
4949
_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y
5050

5151
# We flip the Sprite depending on which way the enemy is moving.
52-
sprite.scale.x = 1 if _velocity.x > 0 else -1
52+
if _velocity.x > 0:
53+
sprite.scale.x = 1
54+
else:
55+
sprite.scale.x = -1
5356

5457
var animation = get_new_animation()
5558
if animation != animation_player.current_animation:
@@ -64,7 +67,10 @@ func destroy():
6467
func get_new_animation():
6568
var animation_new = ""
6669
if _state == State.WALKING:
67-
animation_new = "walk" if abs(_velocity.x) > 0 else "idle"
70+
if _velocity.x == 0:
71+
animation_new = "idle"
72+
else:
73+
animation_new = "walk"
6874
else:
6975
animation_new = "destroy"
7076
return animation_new

2d/platformer/src/Actors/Player.gd

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ func _physics_process(_delta):
5151
var is_jump_interrupted = Input.is_action_just_released("jump" + action_suffix) and _velocity.y < 0.0
5252
_velocity = calculate_move_velocity(_velocity, direction, speed, is_jump_interrupted)
5353

54-
var snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE if direction.y == 0.0 else Vector2.ZERO
54+
var snap_vector = Vector2.ZERO
55+
if direction.y == 0.0:
56+
snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE
5557
var is_on_platform = platform_detector.is_colliding()
5658
_velocity = move_and_slide_with_snap(
5759
_velocity, snap_vector, FLOOR_NORMAL, not is_on_platform, 4, 0.9, false
@@ -60,7 +62,10 @@ func _physics_process(_delta):
6062
# When the character’s direction changes, we want to to scale the Sprite accordingly to flip it.
6163
# This will make Robi face left or right depending on the direction you move.
6264
if direction.x != 0:
63-
sprite.scale.x = 1 if direction.x > 0 else -1
65+
if direction.x > 0:
66+
sprite.scale.x = 1
67+
else:
68+
sprite.scale.x = -1
6469

6570
# We use the sprite's scale to store Robi’s look direction which allows us to shoot
6671
# bullets forward.
@@ -106,9 +111,15 @@ func calculate_move_velocity(
106111
func get_new_animation(is_shooting = false):
107112
var animation_new = ""
108113
if is_on_floor():
109-
animation_new = "run" if abs(_velocity.x) > 0.1 else "idle"
114+
if abs(_velocity.x) > 0.1:
115+
animation_new = "run"
116+
else:
117+
animation_new = "idle"
110118
else:
111-
animation_new = "falling" if _velocity.y > 0 else "jumping"
119+
if _velocity.y > 0:
120+
animation_new = "falling"
121+
else:
122+
animation_new = "jumping"
112123
if is_shooting:
113124
animation_new += "_weapon"
114125
return animation_new

2d/platformer/src/Level/Music.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extends AudioStreamPlayer
22

33
const DOUBLE_VOLUME_DB = 6 # Do not change. Represents doubling of sound pressure.
44

5-
export(int) var base_volume_db = -4
5+
export(int) var base_volume_db = -14
66

77
func _ready():
88
# To avoid AudioStreamPlayer2D sounds playing on top of each other and

2d/pong/logic/paddle.gd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ func _ready():
1212
var n = name.to_lower()
1313
_up = n + "_move_up"
1414
_down = n + "_move_down"
15-
_ball_dir = 1 if n == "left" else -1
15+
if n == "left":
16+
_ball_dir = 1
17+
else:
18+
_ball_dir = -1
1619

1720

1821
func _process(delta):

3d/voxel/menu/ingame/pause_menu.gd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ func _process(_delta):
1313
pause.visible = crosshair.visible
1414
crosshair.visible = !crosshair.visible
1515
options.visible = false
16-
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if crosshair.visible else Input.MOUSE_MODE_VISIBLE)
16+
if crosshair.visible:
17+
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
18+
else:
19+
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
1720

1821

1922
func _on_Resume_pressed():

misc/joypads/remap/remap_wizard.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func _input(event):
3030
map.inverted = joy_mapping_axis_invert.pressed
3131
if joy_mapping_full_axis.pressed:
3232
map.axis = JoyMapping.AXIS.FULL
33+
elif motion.axis_value > 0:
34+
map.axis = JoyMapping.AXIS.HALF_PLUS
3335
else:
34-
var plus = motion.axis_value > 0
35-
map.axis = JoyMapping.AXIS.HALF_PLUS if plus else JoyMapping.AXIS.HALF_MINUS
36+
map.axis = JoyMapping.AXIS.HALF_MINUS
3637
joy_mapping_text.text = map.to_human_string()
3738
cur_mapping[steps[cur_step]] = map
3839
elif event is InputEventJoypadButton and event.pressed:

networking/websocket_chat/utils.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
extends Node
22

33
func encode_data(data, mode):
4-
return data.to_utf8() if mode == WebSocketPeer.WRITE_MODE_TEXT else var2bytes(data)
4+
if mode == WebSocketPeer.WRITE_MODE_TEXT:
5+
return data.to_utf8()
6+
return var2bytes(data)
57

68

79
func decode_data(data, is_string):
8-
return data.get_string_from_utf8() if is_string else bytes2var(data)
10+
if is_string:
11+
return data.get_string_from_utf8()
12+
return bytes2var(data)
913

1014

1115
func _log(node, msg):

0 commit comments

Comments
 (0)