Server side bullet registration

This commit is contained in:
Rendo 2025-11-28 20:50:42 +05:00
commit 26e5fa71e2
5 changed files with 47 additions and 1 deletions

View file

@ -6,6 +6,10 @@ enum TEAMS {
SPECTATE,
UNASSIGNED
}
const ATTACK_LAYER: int = 0b10000
const DEFENCE_LAYER: int = 0b100000
var player_nodes: Dictionary[int,Player] = {}
var dynamic_objects_spawner: MultiplayerSpawner
@ -33,3 +37,35 @@ func despawn_internal(path: NodePath) -> void:
return
get_node(path).queue_free()
func shoot(damage: int) -> void:
if multiplayer.get_unique_id() == 1:
shoot_internal(1,damage)
else:
shoot_internal.rpc_id(1,multiplayer.get_unique_id(),damage)
@rpc("any_peer","call_local","reliable")
func shoot_internal(id:int , damage: int) -> void:
if multiplayer.is_server() == false:
return
var player: Player = player_nodes[id]
var player_camera: Camera3D = player.get_node("Camera3D")
var space: PhysicsDirectSpaceState3D = player.get_world_3d().direct_space_state
var endpoint: Vector3 = player_camera.global_position - player_camera.global_basis.z * 100
var ray = PhysicsRayQueryParameters3D.create(player_camera.global_position,endpoint,1)
ray.exclude = [player.get_rid()]
ray.collide_with_areas = false
match player.team:
TEAMS.DEFENCE:
ray.collision_mask |= ATTACK_LAYER
TEAMS.ATTACK:
ray.collision_mask |= DEFENCE_LAYER
_:
ray.collision_mask |= ATTACK_LAYER | DEFENCE_LAYER
var collision = space.intersect_ray(ray)
if collision != {} and collision["collider"] is Player:
collision["collider"].take_damage.rpc_id(int(collision["collider"].name),damage)

View file

@ -5,6 +5,7 @@ extends Node3D
func _ready() -> void:
if not multiplayer.is_server():
queue_free()
return
match team:
Session.TEAMS.ATTACK:
@ -20,6 +21,7 @@ func _ready() -> void:
func spawn_player(id: int) -> void:
var player: PackedScene = load("res://scenes/molikman.tscn")
var inst: Player = player.instantiate()
Session.player_nodes[id] = inst
inst.name = str(id)
deferred_setup.bind(inst,team).call_deferred()
@ -27,6 +29,7 @@ func spawn_player(id: int) -> void:
func spawn_spectator(id: int) -> void:
var spectator: PackedScene = load("res://scenes/spectator.tscn")
var inst = spectator.instantiate()
inst.name = str(id)
deferred_setup.bind(inst,Session.TEAMS.SPECTATE).call_deferred()

View file

@ -45,3 +45,7 @@ func set_after_spawn(start_position: Vector3,new_team: int):
TEMP_start_pos = global_position
team = new_team as Session.TEAMS
spawned.emit()
@rpc("any_peer","call_local","reliable")
func take_damage(damage: int):
hp -= damage

View file

@ -37,7 +37,8 @@ func fire() -> void:
machine.animation_player.stop()
machine.animation_player.play(with_morphems("shoot"))
raycast.try_deal_damage(damage)
if is_multiplayer_authority():
Session.shoot(damage)
fire_timer.start()
machine.player_camera.recoil(horizontal_curve.sample(bullets_shot),vertical_curve.sample(bullets_shot))

View file

@ -100,12 +100,14 @@ func pick_up_weapon(data: Variant) -> Node:
scene.ammo = data["ammo"]
scene.remaining_ammo = data["remaining_ammo"]
scene.slot = data["slot"]
scene.set_multiplayer_authority(get_multiplayer_authority())
add(scene,scene.slot,true)
return scene
else:
var scene: WeaponSubStateMachine = load(data["scene_file_path"]).instantiate()
scene.set_multiplayer_authority(get_multiplayer_authority())
add(scene,scene.slot,true)