mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-06 16:00:08 +01:00
Improve code style (#1021)
* Remove unnecessary use of `self`
* Connect to signals directly over `connect("name")`
* Use `call_deferred` on callables over `call_deferred("name"))`
* Emit signals directly over `emit_signal("name"...)`
This commit is contained in:
@@ -24,4 +24,4 @@ func set_dead(value):
|
||||
|
||||
|
||||
func set_look_direction(value):
|
||||
emit_signal("direction_changed", value)
|
||||
direction_changed.emit(value)
|
||||
|
||||
@@ -5,4 +5,4 @@ func enter():
|
||||
|
||||
|
||||
func _on_Sword_attack_finished():
|
||||
emit_signal("finished", "previous")
|
||||
finished.emit("previous")
|
||||
|
||||
@@ -9,4 +9,4 @@ func enter():
|
||||
|
||||
func _on_animation_finished(anim_name):
|
||||
assert(anim_name == "stagger")
|
||||
emit_signal("finished", "previous")
|
||||
finished.emit("previous")
|
||||
|
||||
@@ -7,4 +7,4 @@ func enter():
|
||||
|
||||
|
||||
func _on_animation_finished(_anim_name):
|
||||
emit_signal("finished", "dead")
|
||||
finished.emit("dead")
|
||||
|
||||
@@ -46,7 +46,7 @@ func update(delta):
|
||||
move_horizontally(delta, input_direction)
|
||||
animate_jump_height(delta)
|
||||
if height <= 0.0:
|
||||
emit_signal("finished", "previous")
|
||||
finished.emit("previous")
|
||||
|
||||
|
||||
func move_horizontally(delta, direction):
|
||||
|
||||
@@ -3,7 +3,7 @@ extends "res://state_machine/state.gd"
|
||||
|
||||
func handle_input(event):
|
||||
if event.is_action_pressed("simulate_damage"):
|
||||
emit_signal("finished", "stagger")
|
||||
finished.emit("stagger")
|
||||
|
||||
|
||||
func get_input_direction():
|
||||
|
||||
@@ -11,4 +11,4 @@ func handle_input(event):
|
||||
func update(_delta):
|
||||
var input_direction = get_input_direction()
|
||||
if input_direction:
|
||||
emit_signal("finished", "move")
|
||||
finished.emit("move")
|
||||
|
||||
@@ -19,7 +19,7 @@ func handle_input(event):
|
||||
func update(_delta):
|
||||
var input_direction = get_input_direction()
|
||||
if input_direction.is_zero_approx():
|
||||
emit_signal("finished", "idle")
|
||||
finished.emit("idle")
|
||||
update_look_direction(input_direction)
|
||||
|
||||
if Input.is_action_pressed("run"):
|
||||
|
||||
@@ -6,5 +6,5 @@ var velocity = Vector2()
|
||||
|
||||
func handle_input(event):
|
||||
if event.is_action_pressed("jump"):
|
||||
emit_signal("finished", "jump")
|
||||
finished.emit("jump")
|
||||
return super.handle_input(event)
|
||||
|
||||
@@ -31,8 +31,8 @@ var combo = [{
|
||||
var hit_objects = []
|
||||
|
||||
func _ready():
|
||||
$AnimationPlayer.animation_finished.connect(self._on_animation_finished)
|
||||
body_entered.connect(self._on_body_entered)
|
||||
$AnimationPlayer.animation_finished.connect(_on_animation_finished)
|
||||
body_entered.connect(_on_body_entered)
|
||||
_change_state(States.IDLE)
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ func _on_animation_finished(_name):
|
||||
attack()
|
||||
else:
|
||||
_change_state(States.IDLE)
|
||||
emit_signal("attack_finished")
|
||||
attack_finished.emit()
|
||||
|
||||
|
||||
func _on_StateMachine_state_changed(current_state):
|
||||
|
||||
@@ -3,7 +3,7 @@ extends Marker2D
|
||||
var z_index_start = 0
|
||||
|
||||
func _ready():
|
||||
owner.direction_changed.connect(self._on_Parent_direction_changed)
|
||||
owner.direction_changed.connect(_on_Parent_direction_changed)
|
||||
z_index_start = z_index
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ func _enter_tree():
|
||||
if start_state.is_empty():
|
||||
start_state = get_child(0).get_path()
|
||||
for child in get_children():
|
||||
var err = child.finished.connect(self._change_state)
|
||||
var err = child.finished.connect(_change_state)
|
||||
if err:
|
||||
printerr(err)
|
||||
initialize(start_state)
|
||||
@@ -70,7 +70,7 @@ func _change_state(state_name):
|
||||
states_stack[0] = states_map[state_name]
|
||||
|
||||
current_state = states_stack[0]
|
||||
emit_signal("state_changed", current_state)
|
||||
state_changed.emit(current_state)
|
||||
|
||||
if state_name != "previous":
|
||||
current_state.enter()
|
||||
|
||||
@@ -191,7 +191,7 @@ func _shot_bullet() -> void:
|
||||
else:
|
||||
speed_scale = 1.0
|
||||
|
||||
bullet.position = self.position + bullet_shoot.position * Vector2(speed_scale, 1.0)
|
||||
bullet.position = position + bullet_shoot.position * Vector2(speed_scale, 1.0)
|
||||
get_parent().add_child(bullet)
|
||||
|
||||
bullet.linear_velocity = Vector2(400.0 * speed_scale, -40)
|
||||
@@ -204,5 +204,5 @@ func _shot_bullet() -> void:
|
||||
|
||||
func _spawn_enemy_above() -> void:
|
||||
var enemy := Enemy.instantiate() as RigidBody2D
|
||||
enemy.position = self.position + 50 * Vector2.UP
|
||||
enemy.position = position + 50 * Vector2.UP
|
||||
get_parent().add_child(enemy)
|
||||
|
||||
@@ -32,7 +32,7 @@ func _physics_process(_delta):
|
||||
if _wait_physics_ticks_counter > 0:
|
||||
_wait_physics_ticks_counter -= 1
|
||||
if _wait_physics_ticks_counter == 0:
|
||||
emit_signal("wait_done")
|
||||
wait_done.emit()
|
||||
|
||||
|
||||
func add_line(pos_start, pos_end, color):
|
||||
@@ -111,7 +111,7 @@ func start_timer(timeout):
|
||||
_timer = Timer.new()
|
||||
_timer.one_shot = true
|
||||
add_child(_timer)
|
||||
_timer.timeout.connect(self._on_timer_done)
|
||||
_timer.timeout.connect(_on_timer_done)
|
||||
else:
|
||||
cancel_timer()
|
||||
|
||||
@@ -124,7 +124,7 @@ func start_timer(timeout):
|
||||
func cancel_timer():
|
||||
if _timer_started:
|
||||
_timer.paused = true
|
||||
_timer.emit_signal("timeout")
|
||||
_timer.timeout.emit()
|
||||
_timer.paused = false
|
||||
|
||||
|
||||
|
||||
@@ -44,8 +44,8 @@ var _moving_body: PhysicsBody2D = null
|
||||
|
||||
|
||||
func _ready():
|
||||
options.option_selected.connect(self._on_option_selected)
|
||||
options.option_changed.connect(self._on_option_changed)
|
||||
options.option_selected.connect(_on_option_selected)
|
||||
options.option_changed.connect(_on_option_changed)
|
||||
|
||||
_character_body_template = find_child("CharacterBody2D")
|
||||
if _character_body_template:
|
||||
|
||||
@@ -42,8 +42,8 @@ func _ready():
|
||||
options.add_menu_item(OPTION_SHAPE_CONCAVE_POLYGON, true, true)
|
||||
options.add_menu_item(OPTION_SHAPE_CONCAVE_SEGMENTS, true, true)
|
||||
|
||||
options.option_selected.connect(self._on_option_selected)
|
||||
options.option_changed.connect(self._on_option_changed)
|
||||
options.option_selected.connect(_on_option_selected)
|
||||
options.option_changed.connect(_on_option_changed)
|
||||
|
||||
await start_timer(0.5).timeout
|
||||
if is_timer_canceled():
|
||||
|
||||
@@ -43,8 +43,8 @@ func _ready():
|
||||
options.add_menu_item(OPTION_TEST_CASE_DESTROY_BODY, true, false)
|
||||
options.add_menu_item(OPTION_TEST_CASE_CHANGE_POSITIONS, true, false)
|
||||
|
||||
options.connect(&"option_selected", Callable(self, "_on_option_selected"))
|
||||
options.connect(&"option_changed", Callable(self, "_on_option_changed"))
|
||||
options.option_selected.connect(_on_option_selected)
|
||||
options.option_changed.connect(_on_option_changed)
|
||||
|
||||
_selected_joint = _joint_types.values()[0]
|
||||
_update_joint = true
|
||||
|
||||
@@ -81,15 +81,15 @@ func _ready():
|
||||
options.add_menu_item(OPTION_TEST_CASE_MOVING_PLATFORM_RIGID)
|
||||
options.add_menu_item(OPTION_TEST_CASE_MOVING_PLATFORM_CHARACTER)
|
||||
|
||||
options.option_selected.connect(self._on_option_selected)
|
||||
options.option_selected.connect(_on_option_selected)
|
||||
|
||||
$Controls/PlatformSize/HSlider.value = _platform_size
|
||||
$Controls/PlatformAngle/HSlider.value = _platform_angle
|
||||
$Controls/BodyAngle/HSlider.value = _body_angle
|
||||
|
||||
remove_child(_target_area)
|
||||
_target_area.body_entered.connect(self._on_target_entered)
|
||||
$Timer.timeout.connect(self._on_timeout)
|
||||
_target_area.body_entered.connect(_on_target_entered)
|
||||
$Timer.timeout.connect(_on_timeout)
|
||||
|
||||
_rigid_body_template = $RigidBody2D
|
||||
remove_child(_rigid_body_template)
|
||||
@@ -236,13 +236,13 @@ func _start_test_case(option):
|
||||
|
||||
await _on_option_selected(option)
|
||||
|
||||
await self.all_tests_done
|
||||
await all_tests_done
|
||||
|
||||
|
||||
func _wait_for_test():
|
||||
await _reset_test()
|
||||
|
||||
await self.test_done
|
||||
await test_done
|
||||
|
||||
|
||||
func _test_all_rigid_body():
|
||||
@@ -331,7 +331,7 @@ func _test_moving_platform():
|
||||
return
|
||||
|
||||
_platform_speed = 0.0
|
||||
emit_signal("all_tests_done")
|
||||
all_tests_done.emit()
|
||||
|
||||
|
||||
func _test_all():
|
||||
@@ -373,7 +373,7 @@ func _start_test():
|
||||
test_label += String(_rigid_body_template.name)
|
||||
_moving_body = _rigid_body_template.duplicate()
|
||||
_moving_body.linear_velocity = _body_velocity
|
||||
_moving_body.body_entered.connect(self._on_contact_detected)
|
||||
_moving_body.body_entered.connect(_on_contact_detected)
|
||||
add_child(_moving_body)
|
||||
|
||||
if _platform_speed != 0.0:
|
||||
@@ -411,7 +411,7 @@ func _reset_test(cancel_test = true):
|
||||
if cancel_test:
|
||||
Log.print_log("*** Stop around the clock tests")
|
||||
_test_all_angles = false
|
||||
emit_signal("all_tests_done")
|
||||
all_tests_done.emit()
|
||||
else:
|
||||
Log.print_log("*** Start around the clock tests")
|
||||
_platform_body.rotation = deg_to_rad(_platform_angle)
|
||||
@@ -487,8 +487,8 @@ func _on_timeout():
|
||||
$Timer.stop()
|
||||
|
||||
if _test_canceled:
|
||||
emit_signal("test_done")
|
||||
emit_signal("all_tests_done")
|
||||
test_done.emit()
|
||||
all_tests_done.emit()
|
||||
return
|
||||
|
||||
if not _contact_detected and not _target_entered:
|
||||
@@ -497,18 +497,18 @@ func _on_timeout():
|
||||
|
||||
await start_timer(0.5).timeout
|
||||
if _test_canceled:
|
||||
emit_signal("test_done")
|
||||
emit_signal("all_tests_done")
|
||||
test_done.emit()
|
||||
all_tests_done.emit()
|
||||
return
|
||||
|
||||
var was_all_angles = _test_all_angles
|
||||
|
||||
_next_test()
|
||||
|
||||
emit_signal("test_done")
|
||||
test_done.emit()
|
||||
|
||||
if was_all_angles and not _test_all_angles:
|
||||
emit_signal("all_tests_done")
|
||||
all_tests_done.emit()
|
||||
|
||||
|
||||
func _set_result():
|
||||
|
||||
@@ -12,7 +12,7 @@ func _ready():
|
||||
|
||||
options.add_menu_item(OPTION_TEST_CASE_HIT_FROM_INSIDE, true, false)
|
||||
|
||||
options.option_changed.connect(self._on_option_changed)
|
||||
options.option_changed.connect(_on_option_changed)
|
||||
|
||||
await start_timer(0.5).timeout
|
||||
if is_timer_canceled():
|
||||
|
||||
@@ -39,7 +39,7 @@ func _ready():
|
||||
options.add_menu_item(OPTION_TYPE_CAPSULE)
|
||||
options.add_menu_item(OPTION_TYPE_CONVEX_POLYGON)
|
||||
options.add_menu_item(OPTION_TYPE_CONCAVE_POLYGON)
|
||||
options.option_selected.connect(self._on_option_selected)
|
||||
options.option_selected.connect(_on_option_selected)
|
||||
|
||||
await _start_all_types()
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ var _current_test_scene: Node = null
|
||||
|
||||
|
||||
func _ready():
|
||||
option_selected.connect(self._on_option_selected)
|
||||
option_selected.connect(_on_option_selected)
|
||||
|
||||
|
||||
func _process(_delta):
|
||||
|
||||
@@ -7,7 +7,7 @@ var _entry_template
|
||||
|
||||
|
||||
func _enter_tree():
|
||||
Log.entry_logged.connect(self._on_log_entry)
|
||||
Log.entry_logged.connect(_on_log_entry)
|
||||
|
||||
_entry_template = get_child(0) as Label
|
||||
remove_child(_entry_template)
|
||||
|
||||
@@ -62,10 +62,10 @@ func _on_item_pressed(item_index, popup_menu, path):
|
||||
for other_index in range(popup_menu.get_item_count()):
|
||||
if other_index != item_index:
|
||||
popup_menu.set_item_checked(other_index, false)
|
||||
emit_signal("option_selected", item_path)
|
||||
option_selected.emit(item_path)
|
||||
elif popup_menu.is_item_checkable(item_index):
|
||||
var checked = not popup_menu.is_item_checked(item_index)
|
||||
popup_menu.set_item_checked(item_index, checked)
|
||||
emit_signal("option_changed", item_path, checked)
|
||||
option_changed.emit(item_path, checked)
|
||||
else:
|
||||
emit_signal("option_selected", item_path)
|
||||
option_selected.emit(item_path)
|
||||
|
||||
@@ -6,7 +6,7 @@ extends ScrollContainer
|
||||
|
||||
func _ready():
|
||||
var scrollbar = get_v_scroll_bar()
|
||||
scrollbar.scrolling.connect(self._on_scrolling)
|
||||
scrollbar.scrolling.connect(_on_scrolling)
|
||||
|
||||
|
||||
func _process(_delta):
|
||||
|
||||
@@ -11,10 +11,10 @@ signal entry_logged(message, type)
|
||||
|
||||
func print_log(message):
|
||||
print(message)
|
||||
emit_signal("entry_logged", message, LogType.LOG)
|
||||
entry_logged.emit(message, LogType.LOG)
|
||||
|
||||
|
||||
func print_error(message):
|
||||
push_error(message)
|
||||
printerr(message)
|
||||
emit_signal("entry_logged", message, LogType.ERROR)
|
||||
entry_logged.emit(message, LogType.ERROR)
|
||||
|
||||
@@ -9,7 +9,7 @@ func initialize(combat_combatants):
|
||||
combatant = combatant.instantiate()
|
||||
if combatant is Combatant:
|
||||
$Combatants.add_combatant(combatant)
|
||||
combatant.get_node("Health").connect("dead", Callable(self, "_on_combatant_death").bind(combatant))
|
||||
combatant.get_node("Health").dead.connect(_on_combatant_death.bind(combatant))
|
||||
else:
|
||||
combatant.queue_free()
|
||||
$UI.initialize()
|
||||
@@ -24,7 +24,7 @@ func clear_combat():
|
||||
|
||||
|
||||
func finish_combat(winner, loser):
|
||||
emit_signal("combat_finished", winner, loser)
|
||||
combat_finished.emit(winner, loser)
|
||||
|
||||
|
||||
func _on_combatant_death(combatant):
|
||||
|
||||
@@ -24,21 +24,21 @@ func set_active(value):
|
||||
|
||||
func attack(target):
|
||||
target.take_damage(damage)
|
||||
emit_signal("turn_finished")
|
||||
turn_finished.emit()
|
||||
|
||||
|
||||
func consume(item):
|
||||
item.use(self)
|
||||
emit_signal("turn_finished")
|
||||
turn_finished.emit()
|
||||
|
||||
|
||||
func defend():
|
||||
$Health.armor += defense
|
||||
emit_signal("turn_finished")
|
||||
turn_finished.emit()
|
||||
|
||||
|
||||
func flee():
|
||||
emit_signal("turn_finished")
|
||||
turn_finished.emit()
|
||||
|
||||
|
||||
func take_damage(damage_to_take):
|
||||
|
||||
@@ -17,15 +17,15 @@ func _ready():
|
||||
func take_damage(damage):
|
||||
life = life - damage + armor
|
||||
if life <= 0:
|
||||
emit_signal("dead")
|
||||
dead.emit()
|
||||
else:
|
||||
emit_signal("health_changed", life)
|
||||
health_changed.emit(life)
|
||||
|
||||
|
||||
func heal(amount):
|
||||
life += amount
|
||||
life = clamp(life, life, max_life)
|
||||
emit_signal("health_changed", life)
|
||||
health_changed.emit(life)
|
||||
|
||||
|
||||
func get_health_ratio():
|
||||
|
||||
@@ -13,7 +13,7 @@ func initialize():
|
||||
health_info.value = health.life
|
||||
health_info.max_value = health.max_life
|
||||
info.get_node("VBoxContainer/NameContainer/Name").text = combatant.name
|
||||
health.connect("health_changed", Callable(health_info, "set_value"))
|
||||
health.health_changed.connect(health_info.set_value)
|
||||
$Combatants.add_child(info)
|
||||
$Buttons/GridContainer/Attack.grab_focus()
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ func get_next_in_queue():
|
||||
var current_combatant = queue.pop_front()
|
||||
current_combatant.active = false
|
||||
queue.append(current_combatant)
|
||||
self.active_combatant = queue[0]
|
||||
active_combatant = queue[0]
|
||||
return active_combatant
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ func remove(combatant):
|
||||
new_queue.append(n)
|
||||
new_queue.remove(new_queue.find(combatant))
|
||||
combatant.queue_free()
|
||||
self.queue = new_queue
|
||||
queue = new_queue
|
||||
|
||||
|
||||
func set_queue(new_queue):
|
||||
@@ -44,10 +44,10 @@ func set_queue(new_queue):
|
||||
queue.append(node)
|
||||
node.active = false
|
||||
if queue.size() > 0:
|
||||
self.active_combatant = queue[0]
|
||||
active_combatant = queue[0]
|
||||
|
||||
|
||||
func _set_active_combatant(new_combatant):
|
||||
active_combatant = new_combatant
|
||||
active_combatant.active = true
|
||||
emit_signal("active_combatant_changed", active_combatant)
|
||||
active_combatant_changed.emit(active_combatant)
|
||||
|
||||
@@ -12,7 +12,7 @@ var dialogue_text = ""
|
||||
|
||||
|
||||
func start_dialogue():
|
||||
emit_signal("dialogue_started")
|
||||
dialogue_started.emit()
|
||||
current = 0
|
||||
index_dialogue()
|
||||
dialogue_text = dialogue_keys[current].text
|
||||
@@ -22,7 +22,7 @@ func start_dialogue():
|
||||
func next_dialogue():
|
||||
current += 1
|
||||
if current == dialogue_keys.size():
|
||||
emit_signal("dialogue_finished")
|
||||
dialogue_finished.emit()
|
||||
return
|
||||
dialogue_text = dialogue_keys[current].text
|
||||
dialogue_name = dialogue_keys[current].name
|
||||
|
||||
@@ -18,10 +18,10 @@ func show_dialogue(player, dialogue):
|
||||
$Name.text = "[center]" + dialogue_node.dialogue_name + "[/center]"
|
||||
$Text.text = dialogue_node.dialogue_text
|
||||
return
|
||||
dialogue_node.connect("dialogue_started", Callable(player, "set_active").bind(false))
|
||||
dialogue_node.connect("dialogue_finished", Callable(player, "set_active").bind(true))
|
||||
dialogue_node.connect("dialogue_finished", Callable(self, "hide"))
|
||||
dialogue_node.connect("dialogue_finished", Callable(self, "_on_dialogue_finished").bind(player))
|
||||
dialogue_node.dialogue_started.connect(player.set_active.bind(false))
|
||||
dialogue_node.dialogue_finished.connect(player.set_active.bind(true))
|
||||
dialogue_node.dialogue_finished.connect(hide)
|
||||
dialogue_node.dialogue_finished.connect(_on_dialogue_finished.bind(player))
|
||||
dialogue_node.start_dialogue()
|
||||
$Name.text = "[center]" + dialogue_node.dialogue_name + "[/center]"
|
||||
$Text.text = dialogue_node.dialogue_text
|
||||
@@ -34,7 +34,7 @@ func _on_Button_button_up():
|
||||
|
||||
|
||||
func _on_dialogue_finished(player):
|
||||
dialogue_node.disconnect("dialogue_started", Callable(player, "set_active"))
|
||||
dialogue_node.disconnect("dialogue_finished", Callable(player, "set_active"))
|
||||
dialogue_node.disconnect("dialogue_finished", Callable(self, "hide"))
|
||||
dialogue_node.disconnect("dialogue_finished", Callable(self, "_on_dialogue_finished"))
|
||||
dialogue_node.dialogue_started.disconnect(player.set_active)
|
||||
dialogue_node.dialogue_finished.disconnect(player.set_active)
|
||||
dialogue_node.dialogue_finished.disconnect(hide)
|
||||
dialogue_node.dialogue_finished.disconnect(_on_dialogue_finished)
|
||||
|
||||
Reference in New Issue
Block a user