浏览代码

Merge branch 'new/hub'

DricomDragon 4 年之前
父节点
当前提交
0d0934e51b
共有 5 个文件被更改,包括 176 次插入27 次删除
  1. 29 19
      Game.gd
  2. 17 6
      Game.tscn
  3. 62 0
      Hub.gd
  4. 9 0
      Hub.tscn
  5. 59 2
      project.godot

+ 29 - 19
Game.gd

@@ -16,50 +16,52 @@ 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
+onready var ui = $Ui
 
 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 +125,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()

+ 17 - 6
Game.tscn

@@ -1,15 +1,15 @@
-[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 )
 levelNames = [ "Square", "Lab", "Focus", "Atom", "template" ]
 
 [node name="MainCamera" type="Camera2D" parent="."]
-position = Vector2( 500, -500 )
 current = true
 smoothing_enabled = true
 script = ExtResource( 1 )
@@ -18,16 +18,27 @@ script = ExtResource( 1 )
 wait_time = 3.0
 one_shot = true
 
-[node name="Title" type="Label" parent="."]
-margin_top = -700.0
-margin_right = 1000.0
-margin_bottom = -686.0
+[node name="Ui" type="Control" parent="."]
+margin_right = 40.0
+margin_bottom = 40.0
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="Title" type="Label" parent="Ui"]
+margin_left = -502.476
+margin_top = -284.577
+margin_right = 497.524
+margin_bottom = -270.577
 text = "Mighty Liners Reborn"
 align = 1
 __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="Ui" method="hide"]
 [connection signal="timeout" from="StartTimer" to="." method="_on_start_game"]

+ 62 - 0
Hub.gd

@@ -0,0 +1,62 @@
+class_name Hub
+extends Node2D
+# User interface to create liners
+
+signal hub_finished
+
+const ACTION_PLAYER_PREFIX = "p"
+const ACTION_LEFT_SUFFIX = "_left"
+const ACTION_RIGHT_SUFFIX = "_right"
+const MAX_PLAYERS = 8
+
+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
+var _playerActionsRight
+
+var _hub_enabled = true
+
+func _ready():
+	_fill_action_arrays()
+
+func _unhandled_input(event):
+	if event.is_pressed() and _hub_enabled:
+		if event.is_action("ui_accept"):
+			if get_tree().get_nodes_in_group("players").size() > 0:
+				get_tree().set_input_as_handled()
+	
+				_hub_enabled = false
+				emit_signal("hub_finished")
+
+		elif event.is_action_type():
+			for pal in _playerActionsLeft:
+				if event.is_action(pal):
+					# Create player
+					var id = _playerActionsLeft.find(pal)
+
+					var player
+					player = _playerScene.instance()
+					player.turn_left_action = _playerActionsLeft[id]
+					player.turn_right_action = _playerActionsRight[id]
+					player.position = Vector2(id * 100.0, 0.0)
+					player.modulate = playerColors[id]
+					add_child(player)
+
+					get_tree().set_input_as_handled()
+
+func _fill_action_arrays():
+	_playerActionsLeft = []
+	_playerActionsRight = []
+	for k in MAX_PLAYERS:
+		_playerActionsLeft.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_LEFT_SUFFIX)
+		_playerActionsRight.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_RIGHT_SUFFIX)

+ 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"
+}

+ 59 - 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]
@@ -59,6 +64,58 @@ p3_right={
 "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
  ]
 }
+p0_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p0_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p4_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777354,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p4_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777356,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p5_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
+ ]
+}
+p5_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":2,"pressed":false,"doubleclick":false,"script":null)
+ ]
+}
+p6_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":75,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p6_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":59,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":77,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p7_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":86,"unicode":0,"echo":false,"script":null)
+ ]
+}
+p7_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":78,"unicode":0,"echo":false,"script":null)
+ ]
+}
 
 [rendering]