Files
godot-demo-projects/networking/multiplayer_pong/logic/paddle.gd
2022-12-03 15:05:21 -03:00

50 lines
1.1 KiB
GDScript

extends Area2D
const MOTION_SPEED = 150
@export var left = 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_multiplayer_authority():
_motion = Input.get_axis(&"move_up", &"move_down")
if not _you_hidden and _motion != 0:
_hide_you_label()
_motion *= MOTION_SPEED
# Using unreliable to make sure position is updated as fast
# as possible, even if one of the calls is dropped.
set_pos_and_motion.rpc(position, _motion)
else:
if not _you_hidden:
_hide_you_label()
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.
@rpc(unreliable) func set_pos_and_motion(pos, motion):
position = pos
_motion = motion
func _hide_you_label():
_you_hidden = true
get_node(^"You").hide()
func _on_paddle_area_enter(area):
if is_multiplayer_authority():
# Random for new direction generated checked each peer.
area.bounce.rpc(left, randf())