forked from MichaelMacha/SteamMultiplayerPeerExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.gd
More file actions
78 lines (60 loc) · 2.14 KB
/
player.gd
File metadata and controls
78 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class_name Player
extends CharacterBody2D
const MOTION_SPEED = 180.0
const BOMB_RATE = 0.5
@export
var stunned = false
@export var synced_position : Vector2
@onready
var inputs = $Inputs
var last_bomb_time = BOMB_RATE
var current_anim = ""
func _ready():
print("Original Player spawned")
stunned = false
if str(name).is_valid_int():
get_node("Inputs/InputsSync").set_multiplayer_authority(str(name).to_int())
func _physics_process(delta):
var spawner = get_node("../../BombSpawner")
#print("Multiplayer Peer: ", multiplayer.multiplayer_peer)
#if multiplayer.multiplayer_peer == null or str(multiplayer.get_unique_id()) == str(name):
if str(multiplayer.get_unique_id()) == str(name):
# The client which this player represent will update the controls state, and notify it to everyone.
inputs.update()
#if multiplayer.multiplayer_peer == null or is_multiplayer_authority():
if is_multiplayer_authority():
# The server updates the position that will be notified to the clients.
# And increase the bomb cooldown spawning one if the client wants to.
last_bomb_time += delta
#if not stunned and is_multiplayer_authority() and inputs.bombing and last_bomb_time >= BOMB_RATE:
if not stunned and inputs.bombing and last_bomb_time >= BOMB_RATE:
last_bomb_time = 0.0
#var spawner = get_node("../../BombSpawner")
spawner.spawn([position, str(name).to_int()])
if not stunned:
# Everybody runs physics. I.e. clients tries to predict where they will be during the next frame.
velocity = inputs.motion * MOTION_SPEED
move_and_slide()
# Also update the animation based on the last known player input state
var new_anim = "standing"
if inputs.motion.y < 0:
new_anim = "walk_up"
elif inputs.motion.y > 0:
new_anim = "walk_down"
elif inputs.motion.x < 0:
new_anim = "walk_left"
elif inputs.motion.x > 0:
new_anim = "walk_right"
if stunned:
new_anim = "stunned"
if new_anim != current_anim:
current_anim = new_anim
get_node("anim").play(current_anim)
func set_player_name(value):
get_node("label").text = value
@rpc("call_local")
func exploded(_by_who):
if stunned:
return
stunned = true
get_node("anim").play("stunned")