Explorar o código

Stop liner when 1 player remaining

DricomDragon %!s(int64=4) %!d(string=hai) anos
pai
achega
e12a8659a1
Modificáronse 2 ficheiros con 18 adicións e 4 borrados
  1. 8 2
      Game.gd
  2. 10 2
      Player.gd

+ 8 - 2
Game.gd

@@ -3,6 +3,8 @@ extends Node2D
 onready var cam = $MainCamera
 onready var start_timer = $StartTimer
 
+signal round_won
+
 # Used to test a level in creation
 export (String) var levelToLoop
 var levelSceneToLoop
@@ -34,7 +36,10 @@ func _ready():
 		player.turn_right_action = playerActionsRight[k]
 		player.position = Vector2()
 		add_child(player)
+
 		start_timer.connect("timeout", player, "_on_round_start")
+		player.connect("crash", self, "_on_player_crash")
+		self.connect("round_won", player, "_on_round_won")
 
 func load_level(name):
 	var fullName = "res://Levels/" + name + ".tscn"
@@ -84,5 +89,6 @@ func create_game():
 	# Delay start
 	start_timer.start()
 
-func _on_round_end():
-	get_tree().call_group("players", "_on_round_end")
+func _on_player_crash():
+	if get_tree().get_nodes_in_group("living").size() <= 1:
+		emit_signal("round_won")

+ 10 - 2
Player.gd

@@ -4,6 +4,9 @@ extends Area2D
 var grid:TileMap
 onready var tween = $Tween
 
+# Signals
+signal crash
+
 # Misc
 const cell_size = 64
 const cell_half_size = 32
@@ -74,16 +77,21 @@ func _on_round_start():
 	assert(grid)
 	spring()
 
+func _on_round_won():
+	if running:
+		stop()
+
 func _on_tween_completed(_o, key):
 	if (key == ":position"):
 		move()
 
 func _on_crash(body):
 	if running:
-		die()
+		stop()
 		generate_wall()
 		# TODO : use enum for blocks
 		grid.set_cell(posix, posiy, 2)
+		emit_signal("crash")
 
 func generate_wall():
 	# TODO : use enum for blocks
@@ -159,7 +167,7 @@ func has_block_on(left_or_right:int):
 	# TODO : use enum for blocks
 	return grid.get_cell(bposx, bposy) == 1
 
-func die():
+func stop():
 	if running:
 		remove_from_group("running")
 	running = false