Use fewer ternary operators and decrease volume of Platformer 2D

This commit is contained in:
Aaron Franke
2021-03-28 20:18:42 -04:00
parent 021070215c
commit d989bf6209
9 changed files with 54 additions and 16 deletions

View File

@@ -19,7 +19,10 @@ var height = 0.0
func initialize(speed, velocity):
horizontal_speed = speed
max_horizontal_speed = speed if speed > 0.0 else base_max_horizontal_speed
if speed > 0.0:
max_horizontal_speed = speed
else:
max_horizontal_speed = base_max_horizontal_speed
enter_velocity = velocity
@@ -27,7 +30,10 @@ func enter():
var input_direction = get_input_direction()
update_look_direction(input_direction)
horizontal_velocity = enter_velocity if input_direction else Vector2()
if input_direction:
horizontal_velocity = enter_velocity
else:
horizontal_velocity = Vector2()
vertical_speed = 600.0
owner.get_node("AnimationPlayer").play("idle")

View File

@@ -22,7 +22,11 @@ func update(_delta):
emit_signal("finished", "idle")
update_look_direction(input_direction)
speed = max_run_speed if Input.is_action_pressed("run") else max_walk_speed
if Input.is_action_pressed("run"):
speed = max_run_speed
else:
speed = max_walk_speed
var collision_info = move(speed, input_direction)
if not collision_info:
return

View File

@@ -49,7 +49,10 @@ func _physics_process(_delta):
_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y
# We flip the Sprite depending on which way the enemy is moving.
sprite.scale.x = 1 if _velocity.x > 0 else -1
if _velocity.x > 0:
sprite.scale.x = 1
else:
sprite.scale.x = -1
var animation = get_new_animation()
if animation != animation_player.current_animation:
@@ -64,7 +67,10 @@ func destroy():
func get_new_animation():
var animation_new = ""
if _state == State.WALKING:
animation_new = "walk" if abs(_velocity.x) > 0 else "idle"
if _velocity.x == 0:
animation_new = "idle"
else:
animation_new = "walk"
else:
animation_new = "destroy"
return animation_new

View File

@@ -51,7 +51,9 @@ func _physics_process(_delta):
var is_jump_interrupted = Input.is_action_just_released("jump" + action_suffix) and _velocity.y < 0.0
_velocity = calculate_move_velocity(_velocity, direction, speed, is_jump_interrupted)
var snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE if direction.y == 0.0 else Vector2.ZERO
var snap_vector = Vector2.ZERO
if direction.y == 0.0:
snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE
var is_on_platform = platform_detector.is_colliding()
_velocity = move_and_slide_with_snap(
_velocity, snap_vector, FLOOR_NORMAL, not is_on_platform, 4, 0.9, false
@@ -60,7 +62,10 @@ func _physics_process(_delta):
# When the characters direction changes, we want to to scale the Sprite accordingly to flip it.
# This will make Robi face left or right depending on the direction you move.
if direction.x != 0:
sprite.scale.x = 1 if direction.x > 0 else -1
if direction.x > 0:
sprite.scale.x = 1
else:
sprite.scale.x = -1
# We use the sprite's scale to store Robis look direction which allows us to shoot
# bullets forward.
@@ -106,9 +111,15 @@ func calculate_move_velocity(
func get_new_animation(is_shooting = false):
var animation_new = ""
if is_on_floor():
animation_new = "run" if abs(_velocity.x) > 0.1 else "idle"
if abs(_velocity.x) > 0.1:
animation_new = "run"
else:
animation_new = "idle"
else:
animation_new = "falling" if _velocity.y > 0 else "jumping"
if _velocity.y > 0:
animation_new = "falling"
else:
animation_new = "jumping"
if is_shooting:
animation_new += "_weapon"
return animation_new

View File

@@ -2,7 +2,7 @@ extends AudioStreamPlayer
const DOUBLE_VOLUME_DB = 6 # Do not change. Represents doubling of sound pressure.
export(int) var base_volume_db = -4
export(int) var base_volume_db = -14
func _ready():
# To avoid AudioStreamPlayer2D sounds playing on top of each other and

View File

@@ -12,7 +12,10 @@ func _ready():
var n = name.to_lower()
_up = n + "_move_up"
_down = n + "_move_down"
_ball_dir = 1 if n == "left" else -1
if n == "left":
_ball_dir = 1
else:
_ball_dir = -1
func _process(delta):

View File

@@ -13,7 +13,10 @@ func _process(_delta):
pause.visible = crosshair.visible
crosshair.visible = !crosshair.visible
options.visible = false
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if crosshair.visible else Input.MOUSE_MODE_VISIBLE)
if crosshair.visible:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func _on_Resume_pressed():

View File

@@ -30,9 +30,10 @@ func _input(event):
map.inverted = joy_mapping_axis_invert.pressed
if joy_mapping_full_axis.pressed:
map.axis = JoyMapping.AXIS.FULL
elif motion.axis_value > 0:
map.axis = JoyMapping.AXIS.HALF_PLUS
else:
var plus = motion.axis_value > 0
map.axis = JoyMapping.AXIS.HALF_PLUS if plus else JoyMapping.AXIS.HALF_MINUS
map.axis = JoyMapping.AXIS.HALF_MINUS
joy_mapping_text.text = map.to_human_string()
cur_mapping[steps[cur_step]] = map
elif event is InputEventJoypadButton and event.pressed:

View File

@@ -1,11 +1,15 @@
extends Node
func encode_data(data, mode):
return data.to_utf8() if mode == WebSocketPeer.WRITE_MODE_TEXT else var2bytes(data)
if mode == WebSocketPeer.WRITE_MODE_TEXT:
return data.to_utf8()
return var2bytes(data)
func decode_data(data, is_string):
return data.get_string_from_utf8() if is_string else bytes2var(data)
if is_string:
return data.get_string_from_utf8()
return bytes2var(data)
func _log(node, msg):