Browse Source

:recycle: Enforce Godot practices

DricomDragon 3 years ago
parent
commit
cd8634785d

+ 5 - 8
godot/component/entity/ball/Ball.gd

@@ -2,31 +2,28 @@ class_name Ball
 extends RigidBody2D
 
 
-export (Color) var interrupt_color = Color.white
-
-
 const BRICK_LAYER = 2
 const INTERRUPT_SPEED = 700
 
 
-var last_normal:Vector2 = Vector2(0.0, 0.0)
+export (Color) var interrupt_color = Color.white
 
 
-func interrupt_ball():
+func interrupt_ball() -> void:
 	modulate = interrupt_color
 	go_down_unstopped()
 
 
-func go_down_unstopped():
+func go_down_unstopped() -> void:
 	linear_velocity.y = INTERRUPT_SPEED
 	linear_velocity = linear_velocity.normalized() * INTERRUPT_SPEED
 	set_collision_mask_bit(BRICK_LAYER, false)
 
 
-func _on_Launcher_interrupt_wave():
+func _on_Launcher_interrupt_wave() -> void:
 	interrupt_ball()
 
 
-func _on_Ball_body_exited(body: Node):
+func _on_Ball_body_exited(body: Node) -> void:
 	if body.has_method("impact"):
 		body.impact(position)

+ 4 - 4
godot/component/entity/brick/Brick.gd

@@ -13,7 +13,7 @@ const MAX_LIFE := 1999
 var life: int
 
 
-func setup(x: int, y: int, max_life: int):
+func setup(x: int, y: int, max_life: int) -> void:
 	position.x = x * 40
 	position.y = y * 20
 	life = randi() % max_life + 1
@@ -21,7 +21,7 @@ func setup(x: int, y: int, max_life: int):
 	emit_signal("life_changed", life)
 
 
-func damage():
+func damage() -> void:
 	life = life - 1
 	emit_signal("kace_damaged")
 	if life < 1:
@@ -30,10 +30,10 @@ func damage():
 		emit_signal("life_changed", life)
 
 
-func blow_up():
+func blow_up() -> void:
 	emit_signal("kace_broken")
 	queue_free()
 
 
-func impact(pos: Vector2):
+func impact(pos: Vector2) -> void:
 	damage()

+ 4 - 4
godot/component/entity/brick/BrickLifeDisplay.gd

@@ -9,19 +9,19 @@ var default_font
 var life := 0
 
 
-func _ready():
+func _ready() -> void:
 	create_font()
 
 
-func _draw():
+func _draw() -> void:
 	draw_string(default_font, position + LABEL_OFFSET, str(life))
 
 
-func create_font():
+func create_font() -> void:
 	var label = Label.new()
 	default_font = label.get_font("font")
 
 
-func _on_Brick_life_changed(new_life: int):
+func _on_Brick_life_changed(new_life: int) -> void:
 	life = new_life
 	update()

+ 2 - 1
godot/component/entity/brick/BrickMainSprite.gd

@@ -1,3 +1,4 @@
+class_name BrickMainSprite
 extends Sprite
 
 
@@ -8,5 +9,5 @@ func _refresh_look(life: int) -> void:
 	modulate = ColorUtil.color_from_life(life)
 
 
-func _on_Brick_life_changed(life: int):
+func _on_Brick_life_changed(life: int) -> void:
 	_refresh_look(life)

+ 16 - 14
godot/component/entity/brick/Grid.gd

@@ -1,5 +1,7 @@
+class_name Grid
 extends Node2D
 
+
 signal kace_broken
 signal kace_damaged
 signal has_moved
@@ -19,31 +21,31 @@ const BRICK_SIZE = 20
 export (float) var moving_time = 1.0
 
 
-onready var tween = $MoveDown
-
-
 var current_state = State.DONE
 
 var current_row = 1
 
 
-func generate_and_move():
+onready var tween = $MoveDown
+
+
+func generate_and_move() -> void:
 	generate_row()
 	current_state = State.MOVING
 	start_moving()
 
 
-func generate_row():
+func generate_row() -> void:
 	for x in range(10):
 		generate_cell(x, -current_row)
 
 
-func generate_cell(x, y):
+func generate_cell(x: int, y: int) -> void:
 	if randi() % 2 : # 50% of generation
 		build_brick(x, y)
 
 
-func build_brick(x, y):
+func build_brick(x: int, y: int) -> void:
 	var brick = Brick.instance()
 	brick.setup(x, y, current_row * current_row)
 	brick.connect("kace_broken", self, "_on_Brick_kace_broken")
@@ -51,7 +53,7 @@ func build_brick(x, y):
 	add_child(brick)
 
 
-func start_moving():
+func start_moving() -> void:
 	current_row += 1
 	tween.interpolate_property(self, "position", position,
 		position + Vector2(0, BRICK_SIZE), moving_time,
@@ -59,27 +61,27 @@ func start_moving():
 	tween.start()
 
 
-func finish_moving():
+func finish_moving() -> void:
 	current_state = State.DONE
 	emit_signal("has_moved")
 
 
-func _ready():
+func _ready() -> void:
 	randomize()
 	generate_and_move()
 
 
-func _on_Launcher_is_full():
+func _on_Launcher_is_full() -> void:
 	generate_and_move()
 
 
-func _on_MoveDown_tween_all_completed():
+func _on_MoveDown_tween_all_completed() -> void:
 	finish_moving()
 
 
-func _on_Brick_kace_broken():
+func _on_Brick_kace_broken() -> void:
 	emit_signal("kace_broken")
 
 
-func _on_Brick_kace_damaged():
+func _on_Brick_kace_damaged() -> void:
 	emit_signal("kace_damaged")

+ 3 - 1
godot/component/entity/catcher/Catcher.gd

@@ -1,10 +1,12 @@
 class_name Catcher
 extends Area2D
 
+
 signal kace_catch
 signal game_over
 
-func _on_Catcher_body_entered(body):
+
+func _on_Catcher_body_entered(body: Node) -> void:
 	# TODO Debug game over signal not detected
 	if body.is_in_group("ball"):
 		body.queue_free()

+ 4 - 3
godot/component/entity/death/DeadLine.gd

@@ -1,15 +1,16 @@
+class_name DeadLine
 extends RayCast2D
 
 
 signal game_lost
 
 
-func check_death():
+func check_death() -> void:
 	force_raycast_update()
 	if is_colliding():
 		emit_signal("game_lost")
 
 
-func _on_Grid_has_moved():
+func _on_Grid_has_moved() -> void:
 	check_death()
-	
+

+ 31 - 26
godot/component/entity/launcher/Launcher.gd

@@ -1,6 +1,6 @@
+class_name Launcher
 extends Node2D
 
-export (float) var visor_ratio = 1.0
 
 signal get_firing
 signal is_full
@@ -8,6 +8,7 @@ signal gain_new_bullet
 signal interrupt_wave
 signal move_visor
 
+
 enum State {
 	READY,
 	FIRING,
@@ -16,43 +17,47 @@ enum State {
 	DEAD
 }
 
-export var BULLET_SPEED = 500.0
 
 const visor_min_heigth = 10
 const visor_start_shift = Vector2(0, -visor_min_heigth)
 
-var _ballScene = preload("res://component/entity/ball/Ball.tscn")
+const Ball = preload("res://component/entity/ball/Ball.tscn")
+
+
+export (float) var bullet_speed = 500.0
+export (float) var visor_ratio = 1.0
+
+
+var nbAmmo := 0
+var nbStorage := 1
+var current_state = State.WAITING
 
-var nbAmmo = 0
-var nbStorage = 1
 
 onready var target = $TargetLine
 onready var visor = $TargetLine/TargetVisor
 onready var source = $Source
 onready var shootDelay = $ShootDelay
 
-onready var current_state = State.WAITING
-
 
-func _ready():
+func _ready() -> void:
 	get_ready()
 
 
-func _input(event):
-	if event is InputEventMouseButton and event.is_pressed() :
+func _input(event: InputEvent) -> void:
+	if event is InputEventMouseButton and event.is_pressed():
 		trigger(event.position)
 	elif event is InputEventMouseMotion and current_state == State.READY:
 		aim_at(event.position)
 
 
-func aim_at(cursor:Vector2):
+func aim_at(cursor: Vector2) -> void:
 	var visor_pos = (cursor - source.position) * visor_ratio
 	if abs(visor_pos.y) > visor_min_heigth:
 		visor.position = visor_pos
 		emit_signal("move_visor")
 
 
-func trigger(aim:Vector2):
+func trigger(aim: Vector2) -> void:
 	if (current_state == State.READY):
 		current_state = State.FIRING
 		emit_signal("get_firing")
@@ -61,42 +66,42 @@ func trigger(aim:Vector2):
 		emit_signal("interrupt_wave")
 
 
-func shoot():
-		var bullet = _ballScene.instance()
+func shoot() -> void:
+		var bullet = Ball.instance()
 		bullet.position = source.position
 		bullet.linear_velocity = (target.get_visor_pos() - source.position).normalized()
-		bullet.linear_velocity *= BULLET_SPEED
+		bullet.linear_velocity *= bullet_speed
 		add_child(bullet)
 		bullet.connect("tree_exited", self, "_on_Ball_tree_exited")
 		self.connect("interrupt_wave", bullet, "_on_Launcher_interrupt_wave")
 
 
-func gain_new_bullet():
+func gain_new_bullet() -> void:
 	load_bullet(1)
 	emit_signal("gain_new_bullet")
 
 
-func load_bullet(n:int):
+func load_bullet(n: int) -> void:
 	nbStorage += n
 
 
-func end_of_round():
+func end_of_round() -> void:
 	if (current_state != State.DEAD):
 		get_ready()
 
 
-func get_ready():
+func get_ready() -> void:
 	current_state = State.READY
 	target.set_visor_pos(source.position + visor_start_shift)
 	nbAmmo = nbStorage
 	nbStorage = 0
 
 
-func die():
+func die() -> void:
 	current_state = State.DEAD
 
 
-func _on_ShootDelay_timeout():
+func _on_ShootDelay_timeout() -> void:
 	shoot()
 	nbAmmo -= 1
 	if (nbAmmo <= 0):
@@ -105,11 +110,11 @@ func _on_ShootDelay_timeout():
 		emit_signal("get_firing")
 
 
-func _on_Launcher_get_firing():
+func _on_Launcher_get_firing() -> void:
 	shootDelay.start()
 
 
-func _on_Ball_tree_exited():
+func _on_Ball_tree_exited() -> void:
 	if get_tree() == null:
 		return # Game is exiting
 
@@ -118,13 +123,13 @@ func _on_Ball_tree_exited():
 		emit_signal("is_full")
 
 
-func _on_Grid_has_moved():
+func _on_Grid_has_moved() -> void:
 	end_of_round()
 
 
-func _on_Grid_kace_broken():
+func _on_Grid_kace_broken() -> void:
 	gain_new_bullet()
 
 
-func _on_DeadLine_game_lost():
+func _on_DeadLine_game_lost() -> void:
 	die()

+ 5 - 4
godot/component/entity/launcher/TargetLine.gd

@@ -1,23 +1,24 @@
+class_name TargetLine
 extends RayCast2D
 
 
 onready var visor = $TargetVisor
 
 
-func get_visor_pos():
+func get_visor_pos() -> Vector2:
 	return visor.position + position
 
 
-func set_visor_pos(new_pos):
+func set_visor_pos(new_pos: Vector2) -> void:
 	visor.position = new_pos - position
 
 
-func focus_visor():
+func focus_visor() -> void:
 	cast_to = visor.position
 	force_raycast_update()
 	if is_colliding():
 		visor.position = get_collision_point() - position
 
 
-func _on_Launcher_move_visor():
+func _on_Launcher_move_visor() -> void:
 	focus_visor()

+ 6 - 5
godot/component/ui/UI.gd

@@ -1,3 +1,4 @@
+class_name UI
 extends Control
 
 
@@ -5,23 +6,23 @@ onready var game_over_panel = $GameOverPanel
 onready var tutorial_panel = $TutorialPanel
 
 
-func hide_tutorial():
+func hide_tutorial() -> void:
 	if tutorial_panel:
 		tutorial_panel.queue_free()
 		tutorial_panel = null
 
 
-func show_end_of_game():
+func show_end_of_game() -> void:
 	game_over_panel.show()
 
 
-func _on_Catcher_game_over():
+func _on_Catcher_game_over() -> void:
 	show_end_of_game()
 
 
-func _on_DeadLine_game_lost():
+func _on_DeadLine_game_lost() -> void:
 	show_end_of_game()
 
 
-func _on_Launcher_get_firing():
+func _on_Launcher_get_firing() -> void:
 	hide_tutorial()

+ 2 - 1
godot/component/ui/message_panel/MessagePanel.gd

@@ -5,8 +5,9 @@ extends Panel
 
 export(String) var message = "Edit your message"
 
+
 onready var label = $Label
 
 
-func _ready():
+func _ready() -> void:
 	label.text = message

+ 4 - 2
godot/component/ui/number_panel/NumberPanel.gd

@@ -5,16 +5,18 @@ extends Panel
 
 export(String) var title = "Counter"
 
+
 var counter = 0
 
+
 onready var label = $Label
 onready var counter_label = $CounterLabel
 
 
-func _ready():
+func _ready() -> void:
 	label.text = title
 
 
-func increase_counter():
+func increase_counter() -> void:
 	counter += 1
 	counter_label.text = str(counter)

+ 1 - 1
godot/main.tscn

@@ -60,8 +60,8 @@ title = "Balls"
 [connection signal="gain_new_bullet" from="Launcher" to="UI/BallCounter" method="increase_counter"]
 [connection signal="get_firing" from="Launcher" to="UI" method="_on_Launcher_get_firing"]
 [connection signal="is_full" from="Launcher" to="Grid" method="_on_Launcher_is_full"]
-[connection signal="has_moved" from="Grid" to="Launcher" method="_on_Grid_has_moved"]
 [connection signal="has_moved" from="Grid" to="DeadLine" method="_on_Grid_has_moved"]
+[connection signal="has_moved" from="Grid" to="Launcher" method="_on_Grid_has_moved"]
 [connection signal="kace_broken" from="Grid" to="Launcher" method="_on_Grid_kace_broken"]
 [connection signal="kace_damaged" from="Grid" to="UI/ScoreCounter" method="increase_counter"]
 [connection signal="game_lost" from="DeadLine" to="Launcher" method="_on_DeadLine_game_lost"]

+ 37 - 1
godot/project.godot

@@ -24,6 +24,11 @@ _global_script_classes=[ {
 "language": "GDScript",
 "path": "res://component/entity/brick/BrickLifeDisplay.gd"
 }, {
+"base": "Sprite",
+"class": "BrickMainSprite",
+"language": "GDScript",
+"path": "res://component/entity/brick/BrickMainSprite.gd"
+}, {
 "base": "Area2D",
 "class": "Catcher",
 "language": "GDScript",
@@ -34,6 +39,21 @@ _global_script_classes=[ {
 "language": "GDScript",
 "path": "res://utility/ColorUtil.gd"
 }, {
+"base": "RayCast2D",
+"class": "DeadLine",
+"language": "GDScript",
+"path": "res://component/entity/death/DeadLine.gd"
+}, {
+"base": "Node2D",
+"class": "Grid",
+"language": "GDScript",
+"path": "res://component/entity/brick/Grid.gd"
+}, {
+"base": "Node2D",
+"class": "Launcher",
+"language": "GDScript",
+"path": "res://component/entity/launcher/Launcher.gd"
+}, {
 "base": "Panel",
 "class": "MessagePanel",
 "language": "GDScript",
@@ -43,15 +63,31 @@ _global_script_classes=[ {
 "class": "NumberPanel",
 "language": "GDScript",
 "path": "res://component/ui/number_panel/NumberPanel.gd"
+}, {
+"base": "RayCast2D",
+"class": "TargetLine",
+"language": "GDScript",
+"path": "res://component/entity/launcher/TargetLine.gd"
+}, {
+"base": "Control",
+"class": "UI",
+"language": "GDScript",
+"path": "res://component/ui/UI.gd"
 } ]
 _global_script_class_icons={
 "Ball": "",
 "Brick": "",
 "BrickLifeDisplay": "",
+"BrickMainSprite": "",
 "Catcher": "",
 "ColorUtil": "",
+"DeadLine": "",
+"Grid": "",
+"Launcher": "",
 "MessagePanel": "",
-"NumberPanel": ""
+"NumberPanel": "",
+"TargetLine": "",
+"UI": ""
 }
 
 [application]