fully sync player movements over network
This commit is contained in:
@@ -3,7 +3,7 @@ extends CharacterBody3D
|
||||
const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
|
||||
|
||||
@onready var label : Label = $CanvasLayer/Label
|
||||
@onready var synchroniser = $Networking/MultiplayerSynchronizer
|
||||
@onready var networking = $Networking
|
||||
|
||||
@onready var body : Node3D = $Body
|
||||
@onready var head : Node3D = $Body/Head
|
||||
@@ -29,12 +29,15 @@ const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
|
||||
|
||||
var floor_snap := Vector3.ZERO
|
||||
|
||||
func _ready():
|
||||
synchroniser.set_multiplayer_authority(str(name).to_int())
|
||||
cam.current = is_local_authority()
|
||||
func _enter_tree():
|
||||
$Networking/MultiplayerSynchronizer.set_multiplayer_authority(str(name).to_int())
|
||||
$Body/Head/Camera3D.current = is_local_authority()
|
||||
if is_local_authority():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
|
||||
func is_local_authority():
|
||||
return synchroniser.get_multiplayer_authority() == multiplayer.get_unique_id()
|
||||
return $Networking/MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id()
|
||||
|
||||
func _process(_delta):
|
||||
if not is_local_authority():
|
||||
@@ -42,6 +45,7 @@ func _process(_delta):
|
||||
label.text = "H Velocity: %3.2f" % [Vector2(velocity.x, velocity.z).length()]
|
||||
label.text += "\nV Velocity: %3.2f" % [velocity.y]
|
||||
label.text += "\nOn floor: %s" % is_on_floor()
|
||||
label.text += "\nNetwork ID: %d" % multiplayer.get_unique_id()
|
||||
|
||||
func get_move_direction() -> Vector3:
|
||||
var input_dir := Vector2(
|
||||
@@ -62,7 +66,10 @@ func rotate_look(amount : Vector2) -> void:
|
||||
|
||||
func _physics_process(delta):
|
||||
if !is_local_authority():
|
||||
|
||||
if not networking.processed_position:
|
||||
position = networking.sync_position
|
||||
networking.processed_position = true
|
||||
velocity = networking.sync_velocity
|
||||
move_and_slide()
|
||||
return
|
||||
|
||||
@@ -96,6 +103,9 @@ func _physics_process(delta):
|
||||
floor_snap = DIST_FLOOR_SNAP
|
||||
elif was_on_floor and !is_on_floor():
|
||||
floor_snap = Vector3.ZERO
|
||||
|
||||
networking.sync_position = position
|
||||
networking.sync_velocity = velocity
|
||||
|
||||
func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p_accel : float):
|
||||
var current_speed : float = velocity.dot(p_target_dir)
|
||||
|
||||
@@ -58,32 +58,39 @@ func InitialPos(draw_time, ready_time):
|
||||
tween.tween_property(MODEL, "rotation", Vector3.ZERO, draw_time)
|
||||
tween.tween_callback(func(): state = INITIAL).set_delay(ready_time)
|
||||
|
||||
|
||||
|
||||
@rpc
|
||||
func Shoot():
|
||||
if CanShoot():
|
||||
if not FIREMODE:
|
||||
can_shoot = false
|
||||
if not MELEE:
|
||||
clip -= 1
|
||||
|
||||
state = SHOOT;
|
||||
tween = create_tween()
|
||||
tween.set_trans(Tween.TRANS_ELASTIC)
|
||||
tween.set_ease(Tween.EASE_OUT)
|
||||
tween.set_parallel(true)
|
||||
tween.tween_property(MODEL, "position", Vector3(.0, MOMENTUM.x, MOMENTUM.y), RECOIL_COOLDOWN / 2)
|
||||
var rand_rot_y = randf_range(-ANGULAR_MOMENTUM.y,ANGULAR_MOMENTUM.y)
|
||||
var rand_rot_z = randf_range(-ANGULAR_MOMENTUM.z,ANGULAR_MOMENTUM.z)
|
||||
tween.tween_property(MODEL, "rotation", Vector3(deg_to_rad(ANGULAR_MOMENTUM.x),deg_to_rad(rand_rot_y),deg_to_rad(rand_rot_z)),RECOIL_COOLDOWN /2)
|
||||
tween.tween_callback(func(): InitialPos(RECOIL_COOLDOWN / 2, SHOOTING_SPEED / 2)).set_delay(SHOOTING_SPEED / 2)
|
||||
rpc("rpc_AnimShoot")
|
||||
return true
|
||||
return false
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
func rpc_AnimShoot():
|
||||
state = SHOOT;
|
||||
tween = create_tween()
|
||||
tween.set_trans(Tween.TRANS_ELASTIC)
|
||||
tween.set_ease(Tween.EASE_OUT)
|
||||
tween.set_parallel(true)
|
||||
tween.tween_property(MODEL, "position", Vector3(.0, MOMENTUM.x, MOMENTUM.y), RECOIL_COOLDOWN / 2)
|
||||
var rand_rot_y = randf_range(-ANGULAR_MOMENTUM.y,ANGULAR_MOMENTUM.y)
|
||||
var rand_rot_z = randf_range(-ANGULAR_MOMENTUM.z,ANGULAR_MOMENTUM.z)
|
||||
tween.tween_property(MODEL, "rotation", Vector3(deg_to_rad(ANGULAR_MOMENTUM.x),deg_to_rad(rand_rot_y),deg_to_rad(rand_rot_z)),RECOIL_COOLDOWN /2)
|
||||
tween.tween_callback(func(): InitialPos(RECOIL_COOLDOWN / 2, SHOOTING_SPEED / 2)).set_delay(SHOOTING_SPEED / 2)
|
||||
|
||||
func CanShoot():
|
||||
return clip > 0 && state == INITIAL && can_shoot
|
||||
|
||||
|
||||
@rpc
|
||||
func Release():
|
||||
rpc("rpc_Release")
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
func rpc_Release():
|
||||
if !FIREMODE:
|
||||
can_shoot = true
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
extends Node3D
|
||||
|
||||
const CONFIG_PATH = "weapons.cfg"
|
||||
const WEAPON_SCENE = preload("res://entities/Weapon.scn")
|
||||
|
||||
@export var player_root : CharacterBody3D
|
||||
@onready var player_root = get_node("../../..")
|
||||
@onready var raycast = get_node("../Camera3D/AimCast") as RayCast3D
|
||||
#@onready var crosshair = $Crosshair
|
||||
|
||||
@@ -15,50 +16,53 @@ var current_weapon = null
|
||||
var mouse_mov : Vector3
|
||||
var sway_lerp = 5
|
||||
|
||||
func _ready():
|
||||
get_tree().get_current_scene().game_loaded.connect(init)
|
||||
|
||||
func init():
|
||||
func _ready():
|
||||
var config = Runtimeloader.loadConfig(CONFIG_PATH) as ConfigFile
|
||||
if(config == null):
|
||||
return
|
||||
|
||||
Init_Config(config)
|
||||
|
||||
var init_config = get_tree().get_current_scene().init_config
|
||||
|
||||
var init_config = get_node("/root/Game").init_config
|
||||
for w_name in init_config.get_value("PLAYER", "starting_weapons"):
|
||||
GiveWeapon(w_name)
|
||||
|
||||
|
||||
func GiveWeapon(weapon_name):
|
||||
if weapon_name in ALL_WEAPONS and not ALL_WEAPONS[weapon_name] in weapons:
|
||||
weapons.append(ALL_WEAPONS[weapon_name])
|
||||
ChangeWeapon(posmod(current_weapon_index, weapons.size()))
|
||||
ChangeWeapon((posmod(current_weapon_index, weapons.size())))
|
||||
|
||||
func TakeWeapon(weapon_name):
|
||||
if weapon_name in ALL_WEAPONS and ALL_WEAPONS[weapon_name] in weapons:
|
||||
weapons.erase(ALL_WEAPONS[weapon_name])
|
||||
ChangeWeapon(posmod(current_weapon_index, weapons.size()))
|
||||
ChangeWeapon((posmod(current_weapon_index, weapons.size())))
|
||||
|
||||
|
||||
func _input(event):
|
||||
if not player_root.is_local_authority():
|
||||
return
|
||||
if event is InputEventMouseMotion:
|
||||
mouse_mov.y = clampf(-event.relative.x, -0.1, 0.1)
|
||||
mouse_mov.x = clampf(event.relative.y, -0.1, 0.1)
|
||||
if event is InputEventKey and event.is_pressed():
|
||||
if event.keycode == KEY_T:
|
||||
GiveWeapon("CHAINSAW")
|
||||
|
||||
func _unhandled_input(_event):
|
||||
if not player_root.is_local_authority():
|
||||
return
|
||||
|
||||
if(weapons.size() == 0):
|
||||
return
|
||||
|
||||
if weapons.size() > 1 && Input.is_action_just_released("wheel_up"):
|
||||
ChangeWeapon(posmod((current_weapon_index + 1),weapons.size()))
|
||||
if weapons.size() > 1 && Input.is_action_just_released("wheel_down"):
|
||||
ChangeWeapon(posmod(current_weapon_index - 1,weapons.size()))
|
||||
ChangeWeapon(posmod((current_weapon_index - 1),weapons.size()))
|
||||
|
||||
if Input.is_action_just_pressed("reload"):
|
||||
current_weapon.Reload()
|
||||
|
||||
|
||||
func ChangeWeapon(i):
|
||||
if(current_weapon):
|
||||
current_weapon.hide()
|
||||
@@ -74,7 +78,10 @@ func ChangeWeapon(i):
|
||||
raycast.target_position.z = -current_weapon.RAY_LEN
|
||||
|
||||
var time = 0
|
||||
|
||||
func _process(delta):
|
||||
if not player_root.is_local_authority():
|
||||
return
|
||||
if(current_weapon == null):
|
||||
return
|
||||
|
||||
@@ -119,9 +126,9 @@ func Projectile():
|
||||
|
||||
func Init_Config(config):
|
||||
for weapon in config.get_sections():
|
||||
var root = Weapon.new()
|
||||
var root = WEAPON_SCENE.instantiate()
|
||||
root.name = weapon
|
||||
|
||||
|
||||
var model_path = config.get_value(weapon, "MODEL") as String
|
||||
var weapon_model
|
||||
if(model_path.ends_with(".glb") or model_path.ends_with(".gltf")):
|
||||
|
||||
Reference in New Issue
Block a user