Prechádzať zdrojové kódy

Add split-screen modes (horizontal/vertical)

William Tumeo 9 rokov pred
rodič
commit
2ea9e52dd7

+ 45 - 6
2d/split_screen_platformer/split_stage.gd

@@ -1,8 +1,17 @@
 extends Control
 
-# class member variables go here, for example:
-# var a = 2
-# var b = "textvar"
+#export split screen modes
+export(int, "None", "Horizontal", "Vertical") var split = 1
+var split_mode = -1
+
+#split modes constants
+const SPLIT_NONE = 0
+const SPLIT_HORIZONTAL = 1
+const SPLIT_VERTICAL = 2
+
+#top and bottom controls
+onready var top = get_node("top")
+onready var bottom = get_node("bottom")
 
 func _ready():
 	#make bottom viewport have the same world2D as the top viewport, so both show the same
@@ -17,6 +26,36 @@ func _ready():
 	#simple and alternatively, copy them to the other viewport, but they must be erased when level is unloaded
 	#get_node("bottom/viewport").add_child( get_node("top/viewport/stage/parallax_bg").duplicate() )
 	
-	# Called every time the node is added to the scene.
-	# Initialization here
-	pass
+	#set split screen mode for the first time
+	set_split_mode(split)
+	#if used inside _process/_fixed_process, the mode can be made chageable by inspector/animation key
+
+
+func set_split_mode(mode):
+	#change mode only if "split_mode" is different
+	if mode != split_mode and mode in [0, 1, 2]:
+		#get game screen size
+		var screen = get_viewport().get_rect().size
+		
+		#changing sizes and positions
+		if mode == SPLIT_HORIZONTAL:
+			top.set_size(Vector2(screen.x, screen.y/2))
+			top.set_pos(Vector2(0, 0))
+			bottom.set_size(Vector2(screen.x, screen.y/2))
+			bottom.set_pos(Vector2(0, screen.y/2))
+		
+		elif mode == SPLIT_VERTICAL:
+			top.set_size(Vector2(screen.x/2, screen.y))
+			top.set_pos(Vector2(0, 0))
+			bottom.set_size(Vector2(screen.x/2, screen.y))
+			bottom.set_pos(Vector2(screen.x/2, 0))
+		
+		elif mode == SPLIT_NONE:
+			top.set_size(Vector2(screen.x, screen.y))
+			top.set_pos(Vector2(0, 0))
+			bottom.set_hidden(true)
+		
+		if split_mode == SPLIT_NONE:
+			bottom.set_hidden(false)
+		#apply mode
+		split_mode = mode

+ 1 - 0
2d/split_screen_platformer/split_stage.tscn

@@ -16,6 +16,7 @@ margin/top = 0.0
 margin/right = 0.0
 margin/bottom = 0.0
 script/script = ExtResource( 1 )
+split = 1
 
 [node name="top" type="Control" parent="."]