From 00f5f054ad217ffa274d26f3ff0e10f28f117244 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 21 Aug 2016 10:05:52 -0300 Subject: [PATCH] Added a pong multiplayer example --- networking/pong_multiplayer/ball.gd | 74 +++++++++++ networking/pong_multiplayer/ball.png | Bin 0 -> 203 bytes networking/pong_multiplayer/ball.tscn | 33 +++++ networking/pong_multiplayer/engine.cfg | 20 +++ networking/pong_multiplayer/icon.png | Bin 0 -> 956 bytes networking/pong_multiplayer/lobby.gd | 104 ++++++++++++++++ networking/pong_multiplayer/lobby.tscn | 144 ++++++++++++++++++++++ networking/pong_multiplayer/paddle.gd | 69 +++++++++++ networking/pong_multiplayer/paddle.png | Bin 0 -> 184 bytes networking/pong_multiplayer/paddle.tscn | 53 ++++++++ networking/pong_multiplayer/pong.gd | 52 ++++++++ networking/pong_multiplayer/pong.tscn | 122 ++++++++++++++++++ networking/pong_multiplayer/separator.png | Bin 0 -> 203 bytes 13 files changed, 671 insertions(+) create mode 100644 networking/pong_multiplayer/ball.gd create mode 100644 networking/pong_multiplayer/ball.png create mode 100644 networking/pong_multiplayer/ball.tscn create mode 100644 networking/pong_multiplayer/engine.cfg create mode 100644 networking/pong_multiplayer/icon.png create mode 100644 networking/pong_multiplayer/lobby.gd create mode 100644 networking/pong_multiplayer/lobby.tscn create mode 100644 networking/pong_multiplayer/paddle.gd create mode 100644 networking/pong_multiplayer/paddle.png create mode 100644 networking/pong_multiplayer/paddle.tscn create mode 100644 networking/pong_multiplayer/pong.gd create mode 100644 networking/pong_multiplayer/pong.tscn create mode 100644 networking/pong_multiplayer/separator.png diff --git a/networking/pong_multiplayer/ball.gd b/networking/pong_multiplayer/ball.gd new file mode 100644 index 00000000..20a548ab --- /dev/null +++ b/networking/pong_multiplayer/ball.gd @@ -0,0 +1,74 @@ + +extends Area2D + +const DEFAULT_SPEED=80 + +var direction = Vector2(1,0) +var ball_speed = DEFAULT_SPEED +var stopped=false + + + +onready var screen_size = get_viewport_rect().size + +sync func _reset_ball(for_left): + + set_pos( screen_size /2 ) + if (for_left): + direction = Vector2(-1,0) + else: + direction = Vector2( 1,0) + + ball_speed = DEFAULT_SPEED + +sync func stop(): + stopped=true + +func _process(delta): + + # ball will move normally for both players + # even if it's sightly out of sync between them + # so each player sees the motion as smooth and not jerky + + if (not stopped): + translate( direction * ball_speed * delta ) + + # check screen bounds to make ball bounce + + var ball_pos = get_pos() + if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)): + direction.y = -direction.y + + if (is_network_master()): + # only master will decide when the ball is out in the left side (it's own side) + # this makes the game playable even if latency is high and ball is going fast + # otherwise ball might be out in the other player's screen but not this one + + if (ball_pos.x < 0 ): + get_parent().rpc("update_score",false) + rpc("_reset_ball",false) + else: + # only the slave will decide when the ball is out in the right side (it's own side) + # this makes the game playable even if latency is high and ball is going fast + # otherwise ball might be out in the other player's screen but not this one + + if (ball_pos.x > screen_size.x): + get_parent().rpc("update_score",true) + rpc("_reset_ball",true) + + +sync func bounce(left,random): + + #using sync because both players can make it bounce + if (left): + direction.x = abs(direction.x) + else: + direction.x = -abs(direction.x) + + ball_speed *= 1.1 + direction.y = random*2.0 - 1 + direction = direction.normalized() + +func _ready(): + set_process(true) + diff --git a/networking/pong_multiplayer/ball.png b/networking/pong_multiplayer/ball.png new file mode 100644 index 0000000000000000000000000000000000000000..c0f6da4f6f7981032ab7a16fba252f62e0f89c47 GIT binary patch literal 203 zcmeAS@N?(olHy`uVBq!ia0vp^93afW1|*O0@9PFqY)RhkE({+S>=^9g9@_~6MK}vQ zB8wRqxPfeLVMc~ob0mO*>?NMQuIzVt1jLkB{9ihm0EHw=Tq8=H^K)}k^GX<;i&7Iy zQd1PlGfOfQ+&z5*!W;R-fr^YhT^vIsE-&qO svx137YRy@%>WB6T5&AM4W$%UgeA~^#(ZMt8IZ!);r>mdKI;Vst07SPq5dZ)H literal 0 HcmV?d00001 diff --git a/networking/pong_multiplayer/ball.tscn b/networking/pong_multiplayer/ball.tscn new file mode 100644 index 00000000..cbfed496 --- /dev/null +++ b/networking/pong_multiplayer/ball.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=4 format=1] + +[ext_resource path="res://ball.gd" type="Script" id=1] +[ext_resource path="res://ball.png" type="Texture" id=2] + +[sub_resource type="CircleShape2D" id=1] + +custom_solver_bias = 0.0 +radius = 5.11969 + +[node name="ball" type="Area2D"] + +input/pickable = true +shapes/0/shape = SubResource( 1 ) +shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 ) +shapes/0/trigger = false +gravity_vec = Vector2( 0, 1 ) +gravity = 98.0 +linear_damp = 0.1 +angular_damp = 1.0 +script/script = ExtResource( 1 ) + +[node name="sprite" type="Sprite" parent="."] + +texture = ExtResource( 2 ) + +[node name="shape" type="CollisionShape2D" parent="."] + +shape = SubResource( 1 ) +trigger = false +_update_shape_index = 0 + + diff --git a/networking/pong_multiplayer/engine.cfg b/networking/pong_multiplayer/engine.cfg new file mode 100644 index 00000000..30924cba --- /dev/null +++ b/networking/pong_multiplayer/engine.cfg @@ -0,0 +1,20 @@ +[application] + +name="Pong Multiplayer" +main_scene="res://lobby.tscn" +icon="res://icon.png" + +[display] + +width=640 +height=400 +stretch_2d=true + +[input] + +move_up=[key(Up)] +move_down=[key(Down)] + +[render] + +default_clear_color=#ff000000 diff --git a/networking/pong_multiplayer/icon.png b/networking/pong_multiplayer/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..eab34de53d631d62c6806a2061a33060a316d836 GIT binary patch literal 956 zcmV;t14I0YP)e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00SgRL_t(|+T~i$PZ~iK{&=^gf*1b{Py7x32VOkrp$Fs5lh~lCkqe2^8j&>CX48YI zh+?%)53E&bw?v%HjJ!u)LMFpb_T!sx-h1;D01N`m)HDEqT`ohUHHljI*VPqdFhvvt z_P4eG06jxPG#QZ3WFU(%SGOz%#%_FOhW&T`J>2i}F~85p?RoY36H%yl1%v1c1|`;j zxj7&j1tunL#tH>sb+z(+dip^Ne_C4u0K6F=mlOb2LmL(4K?{GFp9cWEo0_5tfGj3V z1BML)!-gTT26)VXoTCAKR5gG(&sPL*C1HWkQ=gBv2E;83am%8s0r|~M06_1^2u%Qf zo}E>;g5OV51EP_L6dE85fWD3n0KoTy1DadF-rEBJn6Vg529%3MDYk$x2D}Ia003V~ zC7KNA42PuvKo|oq&d&h=YI8G924q)OXlp<$6aoP32^K?P40z^TjDRx$tm^+%pwLQ( z?+bu123*$!y?XKLaW^1@F`&mOiSM7BcnUx*CNxY@v|a#&6-1708^^XyHyix8v_v}& zc1A;*y9=|&_J6jp&aH+YI$zVrUO3idxPFKHfwZJLI*>wJJB13stI0D#wt1kF?6+wLxH4Im#159&Gw zbzKthIJsdM?6xJ7N!Pi#REp(NDQ;`uZD;%2-G0YS12j#8QPuIG>pCC06uu_s4I zjimvsM(*HsBgCWAfP`fsA$5O%#|+qAUk3oZ8XIejI3W8zi;x*Y_uKA2W5vTF3LAxg z&VM;ND$xMa@lomL{(WtRfV4d!ZcvOG%;YBbR)s9+uHzuXn#M=T%bu+ zY3Bl+EiC|mgVR%*4Croc screen_size.y): + set_pos( Vector2( pos.x, screen_size.y) ) + + + +func _ready(): + set_process(true) + +func _on_paddle_area_enter( area ): + + if (is_network_master()): + area.rpc("bounce",left,randf()) #random for new direction generated on each peer + + + + diff --git a/networking/pong_multiplayer/paddle.png b/networking/pong_multiplayer/paddle.png new file mode 100644 index 0000000000000000000000000000000000000000..e23491ec96465ad605e1f41620d5b2dea89eff07 GIT binary patch literal 184 zcmeAS@N?(olHy`uVBq!ia0vp^96+qV!3HGtKUiJ>Qk(@Ik;M!Q+`=Ht$S`Y;1W=H@ z#M9T6{Q-w46RRBOZ3mEY$r9Iy66gHf+|;}h2Ir#G#FEq$h4Rdj3SpkC^>bP0l+XkKA}})t literal 0 HcmV?d00001 diff --git a/networking/pong_multiplayer/paddle.tscn b/networking/pong_multiplayer/paddle.tscn new file mode 100644 index 00000000..f9e0ebe0 --- /dev/null +++ b/networking/pong_multiplayer/paddle.tscn @@ -0,0 +1,53 @@ +[gd_scene load_steps=4 format=1] + +[ext_resource path="res://paddle.gd" type="Script" id=1] +[ext_resource path="res://paddle.png" type="Texture" id=2] + +[sub_resource type="CapsuleShape2D" id=1] + +custom_solver_bias = 0.0 +radius = 4.78568 +height = 23.6064 + +[node name="paddle" type="Area2D"] + +input/pickable = true +shapes/0/shape = SubResource( 1 ) +shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 ) +shapes/0/trigger = false +gravity_vec = Vector2( 0, 1 ) +gravity = 98.0 +linear_damp = 0.1 +angular_damp = 1.0 +script/script = ExtResource( 1 ) +left = false + +[node name="sprite" type="Sprite" parent="."] + +texture = ExtResource( 2 ) + +[node name="shape" type="CollisionShape2D" parent="."] + +shape = SubResource( 1 ) +trigger = false +_update_shape_index = 0 + +[node name="you" type="Label" parent="."] + +focus/ignore_mouse = true +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 0 +margin/left = -26.0 +margin/top = -33.0 +margin/right = 27.0 +margin/bottom = -19.0 +text = "You" +align = 1 +percent_visible = 1.0 +lines_skipped = 0 +max_lines_visible = -1 + +[connection signal="area_enter" from="." to="." method="_on_paddle_area_enter"] + + diff --git a/networking/pong_multiplayer/pong.gd b/networking/pong_multiplayer/pong.gd new file mode 100644 index 00000000..df2eb16a --- /dev/null +++ b/networking/pong_multiplayer/pong.gd @@ -0,0 +1,52 @@ + +extends Node2D + +const SCORE_TO_WIN=10 + +var score_left = 0 +var score_right = 0 + +signal game_finished() + +sync func update_score(add_to_left): + if (add_to_left): + + score_left+=1 + get_node("score_left").set_text( str(score_left) ) + else: + + score_right+=1 + get_node("score_right").set_text( str(score_right) ) + + var game_ended = false + + if (score_left==SCORE_TO_WIN): + get_node("winner_left").show() + game_ended=true + elif (score_right==SCORE_TO_WIN): + get_node("winner_right").show() + game_ended=true + + if (game_ended): + get_node("exit_game").show() + get_node("ball").rpc("stop") + +func _on_exit_game_pressed(): + emit_signal("game_finished") + +func _ready(): + + # by default, all nodes in server inherit from master + # while all nodes in clients inherit from slave + + if (get_tree().is_network_server()): + #set to not control player 2. since it's master as everything else + get_node("player2").set_network_mode(NETWORK_MODE_SLAVE) + else: + #set to control player 2, as it's slave as everything else + get_node("player2").set_network_mode(NETWORK_MODE_MASTER) + + #let each paddle know which one is left, too + get_node("player1").left=true + get_node("player2").left=false + diff --git a/networking/pong_multiplayer/pong.tscn b/networking/pong_multiplayer/pong.tscn new file mode 100644 index 00000000..ce1065f8 --- /dev/null +++ b/networking/pong_multiplayer/pong.tscn @@ -0,0 +1,122 @@ +[gd_scene load_steps=5 format=1] + +[ext_resource path="res://pong.gd" type="Script" id=1] +[ext_resource path="res://separator.png" type="Texture" id=2] +[ext_resource path="res://paddle.tscn" type="PackedScene" id=3] +[ext_resource path="res://ball.tscn" type="PackedScene" id=4] + +[node name="pong" type="Node2D"] + +script/script = ExtResource( 1 ) + +[node name="separator" type="Sprite" parent="."] + +transform/pos = Vector2( 320, 200 ) +texture = ExtResource( 2 ) + +[node name="player1" parent="." instance=ExtResource( 3 )] + +transform/pos = Vector2( 32.49, 188.622 ) + +[node name="sprite" parent="player1"] + +modulate = Color( 1, 0, 0.960938, 1 ) + +[node name="player2" parent="." instance=ExtResource( 3 )] + +transform/pos = Vector2( 608.88, 188.622 ) + +[node name="sprite" parent="player2"] + +modulate = Color( 0, 0.929688, 1, 1 ) + +[node name="ball" parent="." instance=ExtResource( 4 )] + +transform/pos = Vector2( 320.387, 189.525 ) + +[node name="score_left" type="Label" parent="."] + +focus/ignore_mouse = true +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 0 +margin/left = 240.0 +margin/top = 10.0 +margin/right = 280.0 +margin/bottom = 30.0 +text = "0" +align = 1 +percent_visible = 1.0 +lines_skipped = 0 +max_lines_visible = -1 + +[node name="score_right" type="Label" parent="."] + +focus/ignore_mouse = true +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 0 +margin/left = 360.0 +margin/top = 10.0 +margin/right = 400.0 +margin/bottom = 30.0 +text = "0" +align = 1 +percent_visible = 1.0 +lines_skipped = 0 +max_lines_visible = -1 + +[node name="winner_left" type="Label" parent="."] + +visibility/visible = false +focus/ignore_mouse = true +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 0 +margin/left = 190.0 +margin/top = 170.0 +margin/right = 267.0 +margin/bottom = 184.0 +text = "The Winner!" +percent_visible = 1.0 +lines_skipped = 0 +max_lines_visible = -1 + +[node name="winner_right" type="Label" parent="."] + +visibility/visible = false +focus/ignore_mouse = true +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 0 +margin/left = 380.0 +margin/top = 170.0 +margin/right = 457.0 +margin/bottom = 184.0 +text = "The Winner!" +percent_visible = 1.0 +lines_skipped = 0 +max_lines_visible = -1 + +[node name="exit_game" type="Button" parent="."] + +visibility/visible = false +focus/ignore_mouse = false +focus/stop_mouse = true +size_flags/horizontal = 2 +size_flags/vertical = 2 +margin/left = 280.0 +margin/top = 340.0 +margin/right = 360.0 +margin/bottom = 360.0 +toggle_mode = false +enabled_focus_mode = 2 +shortcut = null +text = "Exit Game" +flat = false + +[connection signal="pressed" from="exit_game" to="." method="_on_exit_game_pressed"] + + +[editable path="player1"] +[editable path="player2"] diff --git a/networking/pong_multiplayer/separator.png b/networking/pong_multiplayer/separator.png new file mode 100644 index 0000000000000000000000000000000000000000..56874a59cb3c02ef477be7641e0edfebd02cc311 GIT binary patch literal 203 zcmeAS@N?(olHy`uVBq!ia0vp^ObiT+6FAs_td{(ZH-HpdlDE4H!v_XC2D`Y&cEUgr z&H|6fVg?3oAe&p5kzv*x37{Z*iKnkC`&}LZF=dt9Il+5@LXst}5hc#~xw)x%B@E6* zsfi`2DGKG8B^e6tp1uL$jeO!jMaG^kjv*e$-<~oQWKiH?ImmzLQ9K`?lXEj`+WI{= rZ{78rG&TC{nLX`^5^WIl^PJ32PkHg#9|Qb>+8I1u{an^LB{Ts5E{QsD literal 0 HcmV?d00001