浏览代码

Stop round when one (or zero) remaining player

DricomDragon 4 年之前
父节点
当前提交
a6ac0dd35c
共有 3 个文件被更改,包括 39 次插入9 次删除
  1. 17 3
      Game.gd
  2. 1 0
      Game.tscn
  3. 21 6
      Player.gd

+ 17 - 3
Game.gd

@@ -3,6 +3,10 @@ extends Node2D
 onready var cam = $MainCamera
 onready var start_timer = $StartTimer
 
+var is_round_won
+signal round_won
+signal arena_removed
+
 # Used to test a level in creation
 export (String) var levelToLoop
 var levelSceneToLoop
@@ -27,14 +31,18 @@ func _ready():
 
 	# Create players
 	var player
-	for k in 2:
+	for k in 3:
 		player = playerScene.instance()
 		player.grid = levelCurrentNode
 		player.turn_left_action = playerActionsLeft[k]
 		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")
+		self.connect("arena_removed", player, "_on_arena_removed")
 
 func load_level(name):
 	var fullName = "res://Levels/" + name + ".tscn"
@@ -53,6 +61,7 @@ func create_game():
 	if levelCurrentNode:
 		remove_child(levelCurrentNode)
 		levelCurrentNode.queue_free()
+		emit_signal("arena_removed")
 
 	# Create level node
 	if levelToLoop:
@@ -84,5 +93,10 @@ 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("running").size() <= 1 and !is_round_won:
+		emit_signal("round_won")
+		is_round_won = true
+
+func _on_start_game():
+	is_round_won = false

+ 1 - 0
Game.tscn

@@ -26,3 +26,4 @@ align = 1
 __meta__ = {
 "_edit_use_anchors_": false
 }
+[connection signal="timeout" from="StartTimer" to="." method="_on_start_game"]

+ 21 - 6
Player.gd

@@ -4,10 +4,16 @@ extends Area2D
 var grid:TileMap
 onready var tween = $Tween
 
+# Signals
+signal crash
+
 # Misc
 const cell_size = 64
 const cell_half_size = 32
+
+# State
 var running = false
+var landed = false
 
 # Enum
 enum Direction {
@@ -72,18 +78,25 @@ func spawn(newGrid, newPosition, newOrientation):
 
 func _on_round_start():
 	assert(grid)
-	spring()
+	run()
+
+func _on_round_won():
+	stop()
+
+func _on_arena_removed():
+	grid = null
+	landed = false
 
 func _on_tween_completed(_o, key):
 	if (key == ":position"):
 		move()
 
 func _on_crash(body):
-	if running:
-		die()
-		generate_wall()
+	if landed and running:
+		stop()
 		# TODO : use enum for blocks
 		grid.set_cell(posix, posiy, 2)
+		emit_signal("crash")
 
 func generate_wall():
 	# TODO : use enum for blocks
@@ -159,12 +172,14 @@ 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")
+		generate_wall()
 	running = false
 
-func spring():
+func run():
+	landed = true # TODO : land at game signal `land`
 	running = true
 	add_to_group("running")
 	move()