Networking
This commit is contained in:
@@ -35,6 +35,8 @@ func _enter_tree():
|
||||
if is_local_authority():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
func init():
|
||||
$Body/Head/Hand.set_weapons()
|
||||
|
||||
func is_local_authority():
|
||||
return $Networking/MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id()
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
extends Node3D
|
||||
|
||||
const CONFIG_PATH = "weapons.cfg"
|
||||
const WEAPON_SCENE = preload("res://entities/Weapon.scn")
|
||||
|
||||
@onready var player_root = get_node("../../..")
|
||||
@onready var raycast = get_node("../Camera3D/AimCast") as RayCast3D
|
||||
#@onready var crosshair = $Crosshair
|
||||
|
||||
var ALL_WEAPONS = {}
|
||||
var ALL_WEAPONS = []
|
||||
var weapons = []
|
||||
|
||||
var current_weapon_index = 0
|
||||
@@ -15,28 +14,39 @@ var current_weapon = null
|
||||
|
||||
var mouse_mov : Vector3
|
||||
var sway_lerp = 5
|
||||
|
||||
@onready var game = get_node("/root/Game") as Game
|
||||
|
||||
func _ready():
|
||||
var config = Runtimeloader.loadConfig(CONFIG_PATH) as ConfigFile
|
||||
if(config == null):
|
||||
return
|
||||
|
||||
Init_Config(config)
|
||||
|
||||
var init_config = get_node("/root/Game").init_config
|
||||
for w_name in init_config.get_value("PLAYER", "starting_weapons"):
|
||||
GiveWeapon(w_name)
|
||||
Init_Config(game.weapon_config)
|
||||
if not player_root.is_local_authority():
|
||||
for w_name in game.init_config.get_value("PLAYER", "starting_weapons"):
|
||||
GiveWeapon(w_name)
|
||||
|
||||
func set_weapons():
|
||||
for w_name in game.init_config.get_value("PLAYER", "starting_weapons"):
|
||||
rpc("GiveWeapon",w_name)
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
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())))
|
||||
for i in ALL_WEAPONS.size():
|
||||
if(ALL_WEAPONS[i].name != weapon_name):
|
||||
continue
|
||||
if(weapons.has(i)):
|
||||
return
|
||||
|
||||
weapons.append(i)
|
||||
if(current_weapon == null):
|
||||
rpc("ChangeWeapon", ((posmod(current_weapon_index, weapons.size()))))
|
||||
return
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
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())))
|
||||
for i in ALL_WEAPONS.size():
|
||||
if(ALL_WEAPONS[i].name != weapon_name):
|
||||
continue
|
||||
weapons.erase(i)
|
||||
if(current_weapon == ALL_WEAPONS[i]):
|
||||
rpc("ChangeWeapon", ((posmod(current_weapon_index, weapons.size()))))
|
||||
|
||||
func _input(event):
|
||||
if not player_root.is_local_authority():
|
||||
@@ -46,7 +56,7 @@ func _input(event):
|
||||
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")
|
||||
rpc("GiveWeapon","CHAINSAW")
|
||||
|
||||
func _unhandled_input(_event):
|
||||
if not player_root.is_local_authority():
|
||||
@@ -56,13 +66,13 @@ func _unhandled_input(_event):
|
||||
return
|
||||
|
||||
if weapons.size() > 1 && Input.is_action_just_released("wheel_up"):
|
||||
ChangeWeapon(posmod((current_weapon_index + 1),weapons.size()))
|
||||
rpc("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()))
|
||||
rpc("ChangeWeapon", (posmod((current_weapon_index - 1),weapons.size())))
|
||||
|
||||
if Input.is_action_just_pressed("reload"):
|
||||
current_weapon.Reload()
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
func ChangeWeapon(i):
|
||||
if(current_weapon):
|
||||
current_weapon.hide()
|
||||
@@ -72,7 +82,8 @@ func ChangeWeapon(i):
|
||||
return
|
||||
|
||||
current_weapon_index = i
|
||||
current_weapon = weapons[current_weapon_index]
|
||||
var w_index = weapons[current_weapon_index]
|
||||
current_weapon = ALL_WEAPONS[w_index]
|
||||
current_weapon.show()
|
||||
current_weapon.init()
|
||||
raycast.target_position.z = -current_weapon.RAY_LEN
|
||||
@@ -110,7 +121,7 @@ func _process(delta):
|
||||
bobOscillate = sin(time * 4 * (PI) + PI/4) * 0.015
|
||||
position.x = bobOscillate
|
||||
|
||||
|
||||
@rpc(call_local, any_peer)
|
||||
func HitScan():
|
||||
if(raycast.is_colliding()):
|
||||
var target = raycast.get_collider()
|
||||
@@ -168,5 +179,5 @@ func Init_Config(config):
|
||||
root.RELOAD_MOMENTUM = config.get_value(weapon, "RELOAD_MOMENTUM")
|
||||
root.RELOAD_ANGULAR_MOMENTUM = config.get_value(weapon, "RELOAD_ANGULAR_MOMENTUM")
|
||||
root.RELOAD_TIME = config.get_value(weapon, "RELOAD_TIME")
|
||||
|
||||
ALL_WEAPONS[root.name] = root
|
||||
|
||||
ALL_WEAPONS.append(root)
|
||||
|
||||
Reference in New Issue
Block a user