Browse Source

:tada: Add the ability to restart a game

Press space or enter after game over to restart a game.
DricomDragon 2 years ago
parent
commit
7eea536261

+ 4 - 0
godot/component/entity/ball/Ball.gd

@@ -20,6 +20,10 @@ func go_down_unstopped() -> void:
 	set_collision_mask_bit(BRICK_LAYER, false)
 
 
+func destroy() -> void:
+	queue_free()
+
+
 func _on_Launcher_interrupt_wave() -> void:
 	interrupt_ball()
 

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

@@ -36,8 +36,8 @@ func damage() -> void:
 func blow_up() -> void:
 	emit_signal("kace_broken")
 	var explosion = Explosion.instance()
-	explosion.position = position + EXPLOSION_OFFSET
-	get_parent().add_child(explosion)
+	explosion.position = to_global(EXPLOSION_OFFSET)
+	get_parent().get_parent().add_child(explosion)
 	queue_free()
 
 

+ 3 - 1
godot/component/entity/brick/Brick.tscn

@@ -10,7 +10,9 @@
 [sub_resource type="RectangleShape2D" id=1]
 extents = Vector2( 21, 11 )
 
-[node name="Brick" type="StaticBody2D"]
+[node name="Brick" type="StaticBody2D" groups=[
+"brick",
+]]
 collision_layer = 4
 collision_mask = 0
 script = ExtResource( 2 )

+ 18 - 0
godot/component/entity/brick/Grid.gd

@@ -16,6 +16,7 @@ enum State {
 const Brick = preload("res://component/entity/brick/Brick.tscn")
 
 const BRICK_SIZE = 20
+const START_POSITION = Vector2(0, 0)
 
 
 export (float) var moving_time = 1.0
@@ -66,6 +67,18 @@ func finish_moving() -> void:
 	emit_signal("has_moved")
 
 
+func reset() -> void:
+	destroy_every_brick()
+	position = START_POSITION
+	current_state = State.DONE
+	current_row = 1
+
+
+func destroy_every_brick() -> void:
+	for brick in get_tree().get_nodes_in_group("brick"):
+		brick.blow_up()
+
+
 func _ready() -> void:
 	randomize()
 	generate_and_move()
@@ -85,3 +98,8 @@ func _on_Brick_kace_broken() -> void:
 
 func _on_Brick_kace_damaged() -> void:
 	emit_signal("kace_damaged")
+
+
+func _on_Game_start_new_game():
+	reset()
+	generate_and_move()

+ 23 - 6
godot/component/entity/launcher/Launcher.gd

@@ -10,6 +10,7 @@ signal move_visor
 
 
 enum State {
+	BOOTING,
 	READY,
 	FIRING,
 	WAITING,
@@ -29,7 +30,7 @@ export (float) var visor_ratio = 1.0
 
 
 var nbAmmo := 0
-var nbStorage := 1
+var nbStorage := 0
 var current_state = State.WAITING
 
 
@@ -39,10 +40,6 @@ onready var source = $Source
 onready var shootDelay = $ShootDelay
 
 
-func _ready() -> void:
-	get_ready()
-
-
 func _input(event: InputEvent) -> void:
 	if event is InputEventMouseButton and event.is_pressed():
 		trigger(event.position)
@@ -91,6 +88,9 @@ func end_of_round() -> void:
 
 
 func get_ready() -> void:
+	if nbStorage == 0:
+		gain_new_bullet()
+
 	current_state = State.READY
 	target.set_visor_pos(source.position + visor_start_shift)
 	nbAmmo = nbStorage
@@ -101,6 +101,18 @@ func die() -> void:
 	current_state = State.DEAD
 
 
+func reset() -> void:
+	nbStorage = 0
+	nbAmmo = 0
+	current_state = State.BOOTING
+	destroy_every_ball()
+
+
+func destroy_every_ball() -> void:
+	for ball in get_tree().get_nodes_in_group("ball"):
+		ball.destroy()
+
+
 func _on_ShootDelay_timeout() -> void:
 	shoot()
 	nbAmmo -= 1
@@ -128,8 +140,13 @@ func _on_Grid_has_moved() -> void:
 
 
 func _on_Grid_kace_broken() -> void:
-	gain_new_bullet()
+	if current_state in [State.FIRING, State.WAITING, State.INTERRUPTING]:
+		gain_new_bullet()
 
 
 func _on_DeadLine_game_lost() -> void:
 	die()
+
+
+func _on_Game_start_new_game():
+	reset()

+ 9 - 0
godot/component/ui/number_panel/NumberPanel.gd

@@ -19,4 +19,13 @@ func _ready() -> void:
 
 func increase_counter() -> void:
 	counter += 1
+	_refresh_label()
+
+
+func reset_counter() -> void:
+	counter = 0
+	_refresh_label()
+
+
+func _refresh_label() -> void:
 	counter_label.text = str(counter)

+ 1 - 0
godot/effect/particle/explosion/Explosion.tscn

@@ -9,6 +9,7 @@ emitting = false
 amount = 14
 one_shot = true
 explosiveness = 1.0
+local_coords = false
 process_material = ExtResource( 3 )
 texture = ExtResource( 1 )
 script = ExtResource( 2 )

+ 0 - 2
godot/effect/particle/trail/Trail.tscn

@@ -2,8 +2,6 @@
 
 [ext_resource path="res://effect/particle/trail/trail_particle.png" type="Texture" id=1]
 
-
-
 [sub_resource type="Curve" id=1]
 _data = [ Vector2( 0, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0.290909 ), 0.0, 0.0, 0, 0 ]
 

+ 30 - 0
godot/main.gd

@@ -0,0 +1,30 @@
+class_name Game
+extends Node
+
+
+signal start_new_game
+
+
+enum State {
+	BOOTING,
+	SET_UP,
+	IN_PROGRESS,
+	LOST
+}
+
+var _current_state = State.BOOTING
+
+
+func _ready():
+	_current_state = State.IN_PROGRESS
+
+
+func _unhandled_input(event: InputEvent) -> void:
+	if event.is_action_released("ui_accept"):
+		if _current_state == State.LOST:
+			_current_state = State.IN_PROGRESS
+			emit_signal("start_new_game")
+
+
+func _on_game_lost():
+	_current_state = State.LOST

+ 9 - 2
godot/main.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=9 format=2]
+[gd_scene load_steps=10 format=2]
 
 [ext_resource path="res://component/ui/message_panel/MessagePanel.tscn" type="PackedScene" id=1]
 [ext_resource path="res://component/entity/launcher/Launcher.tscn" type="PackedScene" id=2]
@@ -7,9 +7,11 @@
 [ext_resource path="res://component/ui/UI.gd" type="Script" id=5]
 [ext_resource path="res://component/ui/number_panel/NumberPanel.tscn" type="PackedScene" id=6]
 [ext_resource path="res://component/entity/death/Death.tscn" type="PackedScene" id=7]
+[ext_resource path="res://main.gd" type="Script" id=8]
 [ext_resource path="res://component/entity/border/Border.tscn" type="PackedScene" id=13]
 
 [node name="Game" type="Node"]
+script = ExtResource( 8 )
 
 [node name="Background" type="Panel" parent="."]
 margin_right = 400.0
@@ -56,7 +58,11 @@ margin_top = 360.0
 margin_right = 50.0
 margin_bottom = 390.0
 title = "Balls"
-[connection signal="ready" from="." to="UI/BallCounter" method="increase_counter"]
+[connection signal="start_new_game" from="." to="Grid" method="_on_Game_start_new_game"]
+[connection signal="start_new_game" from="." to="Launcher" method="_on_Game_start_new_game"]
+[connection signal="start_new_game" from="." to="UI/GameOverPanel" method="hide"]
+[connection signal="start_new_game" from="." to="UI/ScoreCounter" method="reset_counter"]
+[connection signal="start_new_game" from="." to="UI/BallCounter" method="reset_counter"]
 [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"]
@@ -65,4 +71,5 @@ title = "Balls"
 [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"]
+[connection signal="game_lost" from="DeadLine" to="." method="_on_game_lost"]
 [connection signal="game_lost" from="DeadLine" to="UI" method="_on_DeadLine_game_lost"]

+ 6 - 0
godot/project.godot

@@ -49,6 +49,11 @@ _global_script_classes=[ {
 "language": "GDScript",
 "path": "res://effect/particle/explosion/Explosion.gd"
 }, {
+"base": "Node",
+"class": "Game",
+"language": "GDScript",
+"path": "res://main.gd"
+}, {
 "base": "Node2D",
 "class": "Grid",
 "language": "GDScript",
@@ -93,6 +98,7 @@ _global_script_class_icons={
 "ColorUtil": "",
 "DeadLine": "",
 "Explosion": "",
+"Game": "",
 "Grid": "",
 "ImpactSplash": "",
 "Launcher": "",