Extract MatchParticipantComponent from Player entity and fix HealthComponent tests

This commit is contained in:
Squinty 2024-04-27 13:32:03 +00:00
parent d238c29f76
commit b339bf34f0
23 changed files with 149 additions and 124 deletions

View file

@ -14,9 +14,16 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
class_name ExplosiveDamageComponent extends Area3D
@export var damage : int = 100
@export var damage : int = 35
@export var impulse_force : int = 1000
var damage_dealer : Player
var damage_dealer : MatchParticipantComponent
func _ready() -> void:
# this component only scans to apply explosion damage/impulse
# at the moment layers: 1 Object, 3 Damage, 4 Objective
monitorable = false
monitoring = true
collision_mask = 0b10000000_00000000_00000000_00001101
func _physics_process(_delta : float) -> void:
for body in get_overlapping_bodies():

View file

@ -1,9 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://ds1hekx1dq1bg"]
[ext_resource type="Script" path="res://entities/components/explosive_damage_component.gd" id="1_2uehk"]
[node name="ExplosiveDamage" type="Area3D"]
collision_layer = 0
collision_mask = 13
monitorable = false
script = ExtResource("1_2uehk")

View file

@ -1,6 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://2t8ql8pkxv6c"]
[ext_resource type="Script" path="res://entities/components/flag_carry_component.gd" id="1_b6ee8"]
[node name="FlagCarryComponent" type="Node3D"]
script = ExtResource("1_b6ee8")

View file

@ -19,17 +19,21 @@ class_name HealthComponent extends Area3D
set(value):
health = value
health_changed.emit(value)
@export var _player : Player
@export var match_participant_component : MatchParticipantComponent
signal health_zeroed(killer_id : int)
signal health_changed(value : float)
func _ready() -> void:
# only collide with the "Damage" layer, disable monitoring completely
collision_layer = 0b00000000_00000000_00000000_00000100
monitoring = false
collision_mask = 0
heal_full()
@rpc("call_local", "reliable")
func damage(amount : float, damage_dealer_player_id : int, damage_dealer_team_id : int) -> void:
if (damage_dealer_team_id == _player.team_id) and (damage_dealer_player_id != _player.player_id):
if (damage_dealer_team_id == match_participant_component.team_id) and (damage_dealer_player_id != match_participant_component.player_id):
return
health = clampf(health - amount, 0.0, max_health)

View file

@ -1,8 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://bof3mg7wgxrmn"]
[ext_resource type="Script" path="res://entities/components/health_component.gd" id="1_00xis"]
[node name="HealthComponent" type="Area3D"]
collision_layer = 4
collision_mask = 0
script = ExtResource("1_00xis")

View file

@ -0,0 +1,26 @@
class_name MatchParticipantComponent extends MultiplayerSynchronizer
signal player_id_changed(new_player_id : int)
signal nickname_changed(new_nickname : String)
@export var player_id : int:
set(value):
player_id = value
player_id_changed.emit(player_id)
@export var team_id : int = 1
@export var nickname : String = "<Newblood>":
set(value):
nickname = value
nickname_changed.emit(nickname)
func _ready() -> void:
replication_config = SceneReplicationConfig.new()
root_path = "."
_set_replication_for(NodePath("player_id").get_as_property_path())
_set_replication_for(NodePath("team_id").get_as_property_path())
_set_replication_for(NodePath("nickname").get_as_property_path())
func _set_replication_for(property_node_path : NodePath) -> void:
replication_config.add_property(property_node_path)
replication_config.property_set_replication_mode(property_node_path, SceneReplicationConfig.REPLICATION_MODE_ON_CHANGE)

View file

@ -35,12 +35,6 @@ enum PlayerState { PLAYER_ALIVE, PLAYER_DEAD }
@export_group("State")
@export var player_state : PlayerState = PlayerState.PLAYER_ALIVE
@export var player_id : int = 1:
set(id):
player_id = id
$PlayerInput.set_multiplayer_authority(id)
@export var team_id : int = 1
@export var nickname : String
@onready var input : PlayerInput = $PlayerInput
@onready var camera : Camera3D = $Smoothing/SpringArm3D/Camera3D
@ -56,6 +50,7 @@ enum PlayerState { PLAYER_ALIVE, PLAYER_DEAD }
@onready var tp_player : Vanguard = $Smoothing/ThirdPerson/PlayerMesh
@onready var _game_settings : Settings = get_node("/root/GlobalSettings")
@onready var jetpack_particles : Array = $Smoothing/ThirdPerson/PlayerMesh/JetpackFX.get_children()
@onready var match_participant_component : MatchParticipantComponent = $MatchParticipantComponent
signal died(player : Player, killer_id : int)
signal energy_changed(energy : float)
@ -65,17 +60,16 @@ var g : float = ProjectSettings.get_setting("physics/3d/default_gravity") # in m
var gravity : Vector3 = g * ProjectSettings.get_setting("physics/3d/default_gravity_vector")
var _jumping : bool = false
@rpc("call_local", "reliable")
func set_nickname(value : String) -> void:
nickname = value
func _ready() -> void:
energy_changed.connect(hud._on_energy_changed)
match_participant_component.player_id_changed.connect(_setup_pawn)
match_participant_component.player_id_changed.connect(input.update_multiplayer_authority)
match_participant_component.nickname_changed.connect(iff.set_nickname)
health_component.health_changed.connect(hud._on_health_changed)
health_component.health_changed.connect(iff._on_health_changed)
health_component.health_changed.emit(health_component.health)
health_component.health_zeroed.connect(die)
energy_changed.connect(hud._on_energy_changed)
input.fired_primary.connect(_fire_primary)
input.jumped.connect(_jump)
input.throwed_flag.connect(_throw_flag)
@ -83,22 +77,22 @@ func _ready() -> void:
input.MOUSE_SENSITIVITY = _game_settings.mouse_sensitivity
input.inverted_y_axis = _game_settings.inverted_y_axis
func _setup_pawn(_new_player_id : int) -> void:
if _is_pawn():
camera.current = true
camera.fov = _game_settings.fov
pawn_player = self
# set the spring arm translation to be about head height level
$Smoothing/SpringArm3D.transform = Transform3D().translated(Vector3(0, collision_shape.shape.height / 2, 0) * 0.9)
$Smoothing.remove_child($Smoothing/ThirdPerson)
$Smoothing/ThirdPerson.hide()
else:
# set the iff attachment translation to be about head height level
$Smoothing/ThirdPerson/IFFAttachment.transform = Transform3D().translated(Vector3(0, collision_shape.shape.height / 2, 0) * 0.9)
remove_child(hud)
hud.hide()
weapon.hide()
func _is_pawn() -> bool:
return player_id == multiplayer.get_unique_id()
return multiplayer.get_unique_id() == match_participant_component.player_id
func _fire_primary() -> void:
if _is_player_dead():

View file

@ -1,18 +1,19 @@
[gd_scene load_steps=41 format=3 uid="uid://cbhx1xme0sb7k"]
[gd_scene load_steps=42 format=3 uid="uid://cbhx1xme0sb7k"]
[ext_resource type="Script" path="res://entities/player/player.gd" id="1_mk68k"]
[ext_resource type="PackedScene" uid="uid://drbefw6akui2v" path="res://entities/player/assets/vanguard.tscn" id="2_beyex"]
[ext_resource type="Shape3D" uid="uid://cb8esdlnottdn" path="res://entities/player/collision_shape.tres" id="2_vjqny"]
[ext_resource type="PackedScene" uid="uid://bcv81ku26xo" path="res://interfaces/hud/hud.tscn" id="3_ccety"]
[ext_resource type="PackedScene" uid="uid://c8co0qa2omjmh" path="res://entities/weapons/space_gun/space_gun.tscn" id="4_6jh57"]
[ext_resource type="PackedScene" uid="uid://bof3mg7wgxrmn" path="res://entities/components/health_component.tscn" id="4_jxn63"]
[ext_resource type="Script" path="res://entities/components/match_participant_component.gd" id="6_lrose"]
[ext_resource type="Script" path="res://entities/player/player_input.gd" id="6_ymcrr"]
[ext_resource type="PackedScene" uid="uid://dsysi2rd3bu76" path="res://interfaces/hud/iffs/iff.tscn" id="7_8hc80"]
[ext_resource type="PackedScene" uid="uid://2t8ql8pkxv6c" path="res://entities/components/flag_carry_component.tscn" id="8_ej011"]
[ext_resource type="Script" path="res://entities/components/flag_carry_component.gd" id="8_pdfbn"]
[ext_resource type="PackedScene" uid="uid://d3l7fvbdg6m5g" path="res://entities/flag/assets/flag.glb" id="9_fce2y"]
[ext_resource type="Script" path="res://addons/smoothing/smoothing.gd" id="11_k330l"]
[ext_resource type="Texture2D" uid="uid://ct1v5iadtpadm" path="res://entities/player/assets/jetpackfx/smoke_01.png" id="12_ypuho"]
[ext_resource type="Texture2D" uid="uid://doxo4vfn0bjlp" path="res://entities/player/assets/jetpackfx/smoke_02.png" id="13_wvbf0"]
[ext_resource type="Script" path="res://entities/components/health_component.gd" id="14_ctgxn"]
[ext_resource type="Texture2D" uid="uid://dmf12llra7aq5" path="res://entities/player/assets/jetpackfx/Particle01.png" id="14_vughy"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_clur0"]
@ -65,21 +66,12 @@ properties/0/replication_mode = 1
properties/1/path = NodePath(".:position")
properties/1/spawn = true
properties/1/replication_mode = 1
properties/2/path = NodePath(".:player_id")
properties/2/path = NodePath("HealthComponent:health")
properties/2/spawn = true
properties/2/replication_mode = 2
properties/3/path = NodePath("HealthComponent:health")
properties/3/path = NodePath(".:player_state")
properties/3/spawn = true
properties/3/replication_mode = 2
properties/4/path = NodePath(".:player_state")
properties/4/spawn = true
properties/4/replication_mode = 2
properties/5/path = NodePath(".:nickname")
properties/5/spawn = true
properties/5/replication_mode = 2
properties/6/path = NodePath(".:team_id")
properties/6/spawn = true
properties/6/replication_mode = 2
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_5j4ew"]
properties/0/path = NodePath(".:direction")
@ -238,14 +230,15 @@ monitorable = false
[node name="CollisionShape3D" type="CollisionShape3D" parent="Sensor"]
shape = ExtResource("2_vjqny")
[node name="HUD" parent="." instance=ExtResource("3_ccety")]
[node name="HealthComponent" parent="." node_paths=PackedStringArray("_player") instance=ExtResource("4_jxn63")]
_player = NodePath("..")
[node name="HealthComponent" type="Area3D" parent="." node_paths=PackedStringArray("match_participant_component")]
script = ExtResource("14_ctgxn")
match_participant_component = NodePath("../MatchParticipantComponent")
[node name="CollisionShape3D" type="CollisionShape3D" parent="HealthComponent"]
shape = ExtResource("2_vjqny")
[node name="HUD" parent="." instance=ExtResource("3_ccety")]
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_hg307")
@ -261,6 +254,9 @@ root_path = NodePath(".")
replication_config = SubResource("SceneReplicationConfig_5j4ew")
script = ExtResource("6_ymcrr")
[node name="MatchParticipantComponent" type="MultiplayerSynchronizer" parent="."]
script = ExtResource("6_lrose")
[node name="Smoothing" type="Node3D" parent="."]
script = ExtResource("11_k330l")
target = NodePath("..")
@ -280,19 +276,20 @@ unique_name_in_owner = true
[node name="SpaceGun" parent="Smoothing/SpringArm3D/Inventory" node_paths=PackedStringArray("holder") instance=ExtResource("4_6jh57")]
transform = Transform3D(-1, 0, 2.53518e-06, 0, 1, 0, -2.53518e-06, 0, -1, 0.15, -0.3, -0.55)
holder = NodePath("../../../..")
holder = NodePath("../../../../MatchParticipantComponent")
[node name="SpineIKTarget" type="Node3D" parent="Smoothing/SpringArm3D"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
[node name="FlagCarryComponent" parent="Smoothing/SpringArm3D" node_paths=PackedStringArray("sensor", "carrier") instance=ExtResource("8_ej011")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0.150603)
[node name="FlagCarryComponent" type="Node3D" parent="Smoothing/SpringArm3D" node_paths=PackedStringArray("sensor", "carrier")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
visible = false
script = ExtResource("8_pdfbn")
sensor = NodePath("../../../Sensor")
carrier = NodePath("../../..")
[node name="FlagMesh" parent="Smoothing/SpringArm3D/FlagCarryComponent" instance=ExtResource("9_fce2y")]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, -0.602515, 0)
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 1.559e-08, -0.462981, -0.178329)
[node name="ThirdPerson" type="Node3D" parent="Smoothing"]
@ -330,6 +327,7 @@ draw_pass_1 = SubResource("QuadMesh_uc7ts")
[node name="IFFAttachment" type="Marker3D" parent="Smoothing/ThirdPerson"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.27457, 0)
[node name="IFF" parent="Smoothing/ThirdPerson" node_paths=PackedStringArray("player", "attach_point") instance=ExtResource("7_8hc80")]
[node name="IFF" parent="Smoothing/ThirdPerson" node_paths=PackedStringArray("player", "match_participant_component", "attach_point") instance=ExtResource("7_8hc80")]
player = NodePath("../../..")
match_participant_component = NodePath("../../../MatchParticipantComponent")
attach_point = NodePath("../IFFAttachment")

View file

@ -26,6 +26,10 @@ signal fired_primary
signal throwed_flag
func _ready() -> void:
update_multiplayer_authority(0)
func update_multiplayer_authority(player_id : int) -> void:
set_multiplayer_authority(player_id)
var has_authority : bool = is_multiplayer_authority()
set_process(has_authority)
set_process_unhandled_input(has_authority)

View file

@ -16,13 +16,11 @@ class_name DummyTarget extends RigidBody3D
@export var respawn_time := 0.0 # seconds
@onready var health_component: HealthComponent = $HealthComponent
@onready var collision_shape_3d: CollisionShape3D = $CollisionShape3D
var start_pos : Vector3
func _ready() -> void:
health_component.health_zeroed.connect(spawn)
start_pos = global_position
$TargetMesh/AnimationPlayer.play("gunTwoHanded")
@ -30,7 +28,6 @@ func spawn(_killer_id : int) -> void:
hide()
collision_shape_3d.disabled = true
await get_tree().create_timer(respawn_time).timeout
health_component.heal_full()
global_position = start_pos
show()
collision_shape_3d.disabled = false

View file

@ -1,9 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://dpnu1lvfncx6q"]
[gd_scene load_steps=4 format=3 uid="uid://dpnu1lvfncx6q"]
[ext_resource type="Script" path="res://entities/target_dummy/target_dummy.gd" id="1_iup5v"]
[ext_resource type="Shape3D" uid="uid://cb8esdlnottdn" path="res://entities/player/collision_shape.tres" id="2_i5k5j"]
[ext_resource type="PackedScene" uid="uid://chuein4frnvwt" path="res://entities/target_dummy/assets/player_mesh.glb" id="4_fuync"]
[ext_resource type="PackedScene" uid="uid://bof3mg7wgxrmn" path="res://entities/components/health_component.tscn" id="4_l1exy"]
[node name="DummyTarget" type="RigidBody3D"]
collision_layer = 2147483649
@ -19,7 +18,5 @@ script = ExtResource("1_iup5v")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = ExtResource("2_i5k5j")
[node name="HealthComponent" parent="." instance=ExtResource("4_l1exy")]
[node name="TargetMesh" parent="." instance=ExtResource("4_fuync")]
transform = Transform3D(0.75, 0, 0, 0, 0.75, 0, 0, 0, 0.75, 0, 0, 0)

View file

@ -25,9 +25,10 @@ class_name Projectile extends Node3D
@onready var projectile_trail : Trail3D = $Smoothing/ProjectileTrail
var velocity : Vector3 = Vector3.ZERO
var shooter : Player
var shooter : MatchParticipantComponent
func _ready() -> void:
assert(shooter)
var lifespan_timer : Timer = Timer.new()
lifespan_timer.wait_time = lifespan
lifespan_timer.one_shot = true

View file

@ -14,13 +14,14 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
extends Node3D
var shooter : Player
var shooter : MatchParticipantComponent
@onready var fire : GPUParticles3D = $Fire
@onready var explosive_damage : ExplosiveDamageComponent = $ExplosiveDamageComponent
var explosion_effect_pending : bool = false
func _ready() -> void:
assert(shooter)
explosive_damage.damage_dealer = shooter
fire.emitting = true
fire.finished.connect(func() -> void: queue_free())

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=9 format=3 uid="uid://8atq41j7wd55"]
[ext_resource type="Script" path="res://entities/weapons/space_gun/projectile_explosion.gd" id="1_fp5td"]
[ext_resource type="PackedScene" uid="uid://ds1hekx1dq1bg" path="res://entities/components/explosive_damage_component.tscn" id="2_d4sf4"]
[ext_resource type="Script" path="res://entities/components/explosive_damage_component.gd" id="2_28ymv"]
[sub_resource type="Curve" id="Curve_rg204"]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]
@ -25,7 +25,8 @@ emission_energy_multiplier = 4.0
[sub_resource type="SphereMesh" id="SphereMesh_k3pnh"]
material = SubResource("StandardMaterial3D_wpu51")
[sub_resource type="SphereShape3D" id="SphereShape3D_mlo2k"]
[sub_resource type="SphereShape3D" id="SphereShape3D_nj68w"]
margin = 0.05
radius = 5.0
[node name="ProjectileExplosion" type="Node3D"]
@ -41,8 +42,8 @@ fixed_fps = 60
process_material = SubResource("ParticleProcessMaterial_3mf41")
draw_pass_1 = SubResource("SphereMesh_k3pnh")
[node name="ExplosiveDamageComponent" parent="." instance=ExtResource("2_d4sf4")]
damage = 35
[node name="ExplosiveDamageComponent" type="Area3D" parent="."]
script = ExtResource("2_28ymv")
[node name="CollisionShape3D" type="CollisionShape3D" parent="ExplosiveDamageComponent"]
shape = SubResource("SphereShape3D_mlo2k")
shape = SubResource("SphereShape3D_nj68w")

View file

@ -16,7 +16,7 @@ extends Node3D
class_name SpaceGun
@export var PROJECTILE : PackedScene
@export var holder : Player
@export var holder : MatchParticipantComponent
@onready var nozzle : Node3D = $Nozzle
@onready var inventory : Node3D = get_parent()