Browse Source

Move player creation to hub

DricomDragon 4 năm trước cách đây
mục cha
commit
0e939b2e2e
5 tập tin đã thay đổi với 88 bổ sung22 xóa
  1. 28 19
      Game.gd
  2. 4 1
      Game.tscn
  3. 40 0
      Hub.gd
  4. 9 0
      Hub.tscn
  5. 7 2
      project.godot

+ 28 - 19
Game.gd

@@ -16,50 +16,51 @@ var levelScenes = []
 var levelOldNode
 var levelCurrentNode
 var levelIndex = -1
-onready var levelSwap = $LevelSwap
 
-var playerScene = preload("res://Player.tscn")
-var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
-var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
-export (Array, Color) var playerColors = [Color.red, Color.blue, Color.green, Color.yellow, Color.white, Color.cyan, Color.magenta, Color.orange]
+var _players_connected = false
+
+onready var levelSwap = $LevelSwap
+onready var hub = $Hub
 
 func _ready():
 	# Prepare levels
 	for _k in range(levelNames.size()):
 		levelScenes.append(null)
 
-	levelSwap.connect("tween_completed", self, "_on_swap_completed")
-
 	if levelToLoop:
 		levelSceneToLoop = load_level(levelToLoop)
 		if !levelSceneToLoop:
 			push_error("Cant't load level " + levelToLoop)
 
-	# Create players
-	var player
-	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()
-		player.modulate = playerColors[k]
-		add_child(player)
+	# Connections
+	levelSwap.connect("tween_completed", self, "_on_swap_completed")
+	hub.connect("hub_finished", self, "_on_hub_finished")
+
+func connect_players():
+	if _players_connected:
+		push_error("Players are already connected!")
+		return
+
+	for player in get_tree().get_nodes_in_group("players"):
 
 		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")
 
+		player.connect("crash", self, "_on_player_crash")
+
+		_players_connected = true
+
 func load_level(name):
 	var fullName = "res://Levels/" + name + ".tscn"
 	return load(fullName)
 
 func _unhandled_input(event):
 	if event.is_action("ui_accept") and event.is_pressed():
-		if is_round_won == null or is_round_won:
+		if is_round_won:
 			is_round_won = false
 			create_game()
+			get_tree().set_input_as_handled()
 
 func create_game():
 	# Clear if required
@@ -123,3 +124,11 @@ func _on_swap_completed(object, _k):
 
 	elif object == levelCurrentNode:
 		start_timer.start()
+
+func _on_hub_finished():
+	connect_players()
+
+#	hub.remove_and_skip()
+	hub = null # TODO : add an argument in connect and remove temporary attribute
+
+	create_game()

+ 4 - 1
Game.tscn

@@ -1,8 +1,9 @@
-[gd_scene load_steps=4 format=2]
+[gd_scene load_steps=5 format=2]
 
 [ext_resource path="res://MainCamera.gd" type="Script" id=1]
 [ext_resource path="res://LevelSwap.gd" type="Script" id=2]
 [ext_resource path="res://Game.gd" type="Script" id=3]
+[ext_resource path="res://Hub.tscn" type="PackedScene" id=4]
 
 [node name="Game" type="Node2D"]
 script = ExtResource( 3 )
@@ -28,6 +29,8 @@ __meta__ = {
 "_edit_use_anchors_": false
 }
 
+[node name="Hub" parent="." instance=ExtResource( 4 )]
+
 [node name="LevelSwap" type="Tween" parent="."]
 script = ExtResource( 2 )
 [connection signal="timeout" from="StartTimer" to="." method="_on_start_game"]

+ 40 - 0
Hub.gd

@@ -0,0 +1,40 @@
+class_name Hub
+extends Node2D
+# User interface to create liners
+
+signal hub_finished
+
+export (Array, Color) var playerColors = [
+	Color.red,
+	Color.blue,
+	Color.green,
+	Color.yellow,
+	Color.white,
+	Color.cyan,
+	Color.magenta,
+	Color.orange,
+]
+
+var _playerScene = preload("res://Player.tscn")
+var _playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
+var _playerActionsRight = ["p1_right", "p2_right", "p3_right"]
+
+var _hub_enabled = true
+
+func _unhandled_input(event):
+	if event.is_action("ui_accept") and event.is_pressed() and _hub_enabled:
+		get_tree().set_input_as_handled()
+
+		# Create players
+		var player
+		for k in 2:
+			player = _playerScene.instance()
+			player.turn_left_action = _playerActionsLeft[k]
+			player.turn_right_action = _playerActionsRight[k]
+			player.position = Vector2()
+			player.modulate = playerColors[k]
+			add_child(player)
+	
+		# TODO : make hub interactive
+		_hub_enabled = false
+		emit_signal("hub_finished")

+ 9 - 0
Hub.tscn

@@ -0,0 +1,9 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://Hub.gd" type="Script" id=1]
+
+[node name="Hub" type="Node2D"]
+script = ExtResource( 1 )
+__meta__ = {
+"_editor_description_": "Create players"
+}

+ 7 - 2
project.godot

@@ -8,9 +8,14 @@
 
 config_version=4
 
-_global_script_classes=[  ]
+_global_script_classes=[ {
+"base": "Node2D",
+"class": "Hub",
+"language": "GDScript",
+"path": "res://Hub.gd"
+} ]
 _global_script_class_icons={
-
+"Hub": ""
 }
 
 [application]