mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 23:10:08 +01:00
Created C# networked Pong
This commit is contained in:
@@ -22,16 +22,20 @@ func _process(delta):
|
||||
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.
|
||||
# Only the 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 puppet 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.
|
||||
# Only the puppet will decide when the ball is out in
|
||||
# the right side, which is 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)
|
||||
|
||||
@@ -40,7 +40,7 @@ func _player_disconnected(_id):
|
||||
|
||||
# Callback from SceneTree, only for clients (not server).
|
||||
func _connected_ok():
|
||||
pass # We don't need this function.
|
||||
pass # This function is not needed for this project.
|
||||
|
||||
|
||||
# Callback from SceneTree, only for clients (not server).
|
||||
@@ -59,7 +59,8 @@ func _server_disconnected():
|
||||
|
||||
func _end_game(with_error = ""):
|
||||
if has_node("/root/Pong"):
|
||||
# Erase immediately, otherwise network might show errors (this is why we connected deferred above).
|
||||
# Erase immediately, otherwise network might show
|
||||
# errors (this is why we connected deferred above).
|
||||
get_node("/root/Pong").free()
|
||||
show()
|
||||
|
||||
|
||||
@@ -4,42 +4,42 @@ const MOTION_SPEED = 150
|
||||
|
||||
export var left = false
|
||||
|
||||
var motion = 0
|
||||
var you_hidden = false
|
||||
var _motion = 0
|
||||
var _you_hidden = false
|
||||
|
||||
onready var _screen_size_y = get_viewport_rect().size.y
|
||||
|
||||
func _process(delta):
|
||||
# Is the master of the paddle.
|
||||
if is_network_master():
|
||||
motion = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
||||
_motion = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
||||
|
||||
if not you_hidden and motion != 0:
|
||||
if not _you_hidden and _motion != 0:
|
||||
_hide_you_label()
|
||||
|
||||
motion *= MOTION_SPEED
|
||||
_motion *= MOTION_SPEED
|
||||
|
||||
# Using unreliable to make sure position is updated as fast
|
||||
# as possible, even if one of the calls is dropped.
|
||||
rpc_unreliable("set_pos_and_motion", position, motion)
|
||||
rpc_unreliable("set_pos_and_motion", position, _motion)
|
||||
else:
|
||||
if not you_hidden:
|
||||
if not _you_hidden:
|
||||
_hide_you_label()
|
||||
|
||||
translate(Vector2(0, motion * delta))
|
||||
translate(Vector2(0, _motion * delta))
|
||||
|
||||
# Set screen limits.
|
||||
position.y = clamp(position.y, 16, _screen_size_y - 16)
|
||||
|
||||
|
||||
# Synchronize position and speed to the other peers.
|
||||
puppet func set_pos_and_motion(p_pos, p_motion):
|
||||
position = p_pos
|
||||
motion = p_motion
|
||||
puppet func set_pos_and_motion(pos, motion):
|
||||
position = pos
|
||||
_motion = motion
|
||||
|
||||
|
||||
func _hide_you_label():
|
||||
you_hidden = true
|
||||
_you_hidden = true
|
||||
get_node("You").hide()
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
extends Node2D
|
||||
|
||||
signal game_finished()
|
||||
|
||||
const SCORE_TO_WIN = 10
|
||||
|
||||
var score_left = 0
|
||||
var score_right = 0
|
||||
|
||||
signal game_finished()
|
||||
|
||||
onready var player2 = $Player2
|
||||
onready var score_left_node = $ScoreLeft
|
||||
onready var score_right_node = $ScoreRight
|
||||
@@ -23,7 +23,8 @@ func _ready():
|
||||
else:
|
||||
# For the client, give control of player 2 to itself.
|
||||
player2.set_network_master(get_tree().get_network_unique_id())
|
||||
print("unique id: ", get_tree().get_network_unique_id())
|
||||
|
||||
print("Unique id: ", get_tree().get_network_unique_id())
|
||||
|
||||
|
||||
sync func update_score(add_to_left):
|
||||
|
||||
@@ -21,19 +21,14 @@ position = Vector2( 320, 200 )
|
||||
texture = ExtResource( 2 )
|
||||
|
||||
[node name="Player1" parent="." instance=ExtResource( 3 )]
|
||||
modulate = Color( 0, 1, 1, 1 )
|
||||
position = Vector2( 32.49, 188.622 )
|
||||
left = true
|
||||
|
||||
[node name="Sprite" parent="Player1" index="0"]
|
||||
self_modulate = Color( 0, 1, 1, 1 )
|
||||
|
||||
[node name="Player2" parent="." instance=ExtResource( 3 )]
|
||||
self_modulate = Color( 1, 0, 1, 1 )
|
||||
modulate = Color( 1, 0, 1, 1 )
|
||||
position = Vector2( 608.88, 188.622 )
|
||||
|
||||
[node name="Sprite" parent="Player2" index="0"]
|
||||
self_modulate = Color( 1, 0, 1, 1 )
|
||||
|
||||
[node name="Ball" parent="." instance=ExtResource( 4 )]
|
||||
position = Vector2( 320.387, 189.525 )
|
||||
|
||||
|
||||
@@ -64,4 +64,3 @@ move_up={
|
||||
|
||||
quality/driver/driver_name="GLES2"
|
||||
quality/2d/use_pixel_snap=true
|
||||
viewport/default_clear_color=Color( 0, 0, 0, 1 )
|
||||
|
||||
Reference in New Issue
Block a user