mirror of
https://gitlab.com/open-fpsz/open-fpsz.git
synced 2026-07-15 16:34:48 +00:00
Refactor Multiplayer code
This commit is contained in:
parent
72f99eff07
commit
6c32664405
14 changed files with 127 additions and 59 deletions
32
entities/components/explosive_damage_component.gd
Normal file
32
entities/components/explosive_damage_component.gd
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# This file is part of open-fpsz.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
class_name ExplosiveDamageComponent extends Area3D
|
||||
|
||||
@export var damage : int = 100
|
||||
@export var impulse_force : int = 1000
|
||||
var damage_dealer : Player
|
||||
|
||||
func _physics_process(_delta : float) -> void:
|
||||
for body in get_overlapping_bodies():
|
||||
if body is RigidBody3D:
|
||||
var center_of_mass_global_position : Vector3 = body.center_of_mass + body.global_position
|
||||
var direction : Vector3 = ( center_of_mass_global_position - global_position).normalized()
|
||||
body.apply_central_impulse(direction * impulse_force)
|
||||
|
||||
for area in get_overlapping_areas():
|
||||
if area is HealthComponent and is_multiplayer_authority():
|
||||
(area as HealthComponent).damage.rpc(damage, damage_dealer.player_id, damage_dealer.team_id)
|
||||
|
||||
set_physics_process(false)
|
||||
9
entities/components/explosive_damage_component.tscn
Normal file
9
entities/components/explosive_damage_component.tscn
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[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")
|
||||
62
entities/components/flag_carry_component.gd
Normal file
62
entities/components/flag_carry_component.gd
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# This file is part of open-fpsz.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
class_name FlagCarryComponent extends Node3D
|
||||
## This component allows its entity to grab, carry and throw flags
|
||||
##
|
||||
## To work correctly the owner MUST set an attachment point for carried flags
|
||||
## and a sensor to detect when flags are within grab range
|
||||
|
||||
@export var max_throw_speed : float = 10.0
|
||||
@export var sensor : Area3D
|
||||
@export var carrier : Player
|
||||
|
||||
var _carried_flag : Flag
|
||||
|
||||
func _ready() -> void:
|
||||
sensor.body_entered.connect(_sensor_on_body_entered)
|
||||
|
||||
func _physics_process(_delta : float) -> void:
|
||||
if _is_carrying():
|
||||
_carried_flag.global_position = global_position
|
||||
_carried_flag.global_rotation = global_rotation
|
||||
|
||||
func _grab(flag : Flag) -> void:
|
||||
if not _is_carrying():
|
||||
flag.grab(carrier)
|
||||
_carried_flag = flag
|
||||
show()
|
||||
|
||||
func _release(inherited_velocity : Vector3, throw_speed : float, dropper : Player) -> void:
|
||||
if _is_carrying():
|
||||
_carried_flag.drop(dropper)
|
||||
_carried_flag.rotation_degrees.x = 0.0
|
||||
_carried_flag.linear_velocity = inherited_velocity + (global_basis.z * throw_speed)
|
||||
# Throw the flag from some distance in front of the player to avoid regrabbing mid-air
|
||||
_carried_flag.global_position = carrier.global_position + (global_basis.z * 1.7)
|
||||
_carried_flag = null
|
||||
hide()
|
||||
|
||||
func _is_carrying() -> bool:
|
||||
return _carried_flag != null
|
||||
|
||||
func _sensor_on_body_entered(collider : Flag) -> void:
|
||||
if collider is Flag:
|
||||
_grab(collider)
|
||||
|
||||
func drop(dropper : Player) -> void:
|
||||
_release(Vector3.ZERO, 0.0, dropper)
|
||||
|
||||
func throw(inherited_velocity : Vector3, dropper : Player) -> void:
|
||||
_release(inherited_velocity, max_throw_speed, dropper)
|
||||
6
entities/components/flag_carry_component.tscn
Normal file
6
entities/components/flag_carry_component.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[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")
|
||||
47
entities/components/health_component.gd
Normal file
47
entities/components/health_component.gd
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
# This file is part of open-fpsz.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
class_name HealthComponent extends Area3D
|
||||
|
||||
@export var max_health : float = 100.0
|
||||
@export var health : float = 100.0:
|
||||
set(value):
|
||||
health = value
|
||||
health_changed.emit(value)
|
||||
@export var _player : Player
|
||||
|
||||
signal health_zeroed(killer_id : int)
|
||||
signal health_changed(value : float)
|
||||
|
||||
func _ready() -> void:
|
||||
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):
|
||||
return
|
||||
|
||||
health = clampf(health - amount, 0.0, max_health)
|
||||
if health == 0.0:
|
||||
health_zeroed.emit(damage_dealer_player_id)
|
||||
|
||||
@rpc("call_local", "reliable")
|
||||
func _heal(amount : float) -> void:
|
||||
health = clampf(health + amount, 0.0, max_health)
|
||||
|
||||
func heal_full() -> void:
|
||||
if not is_multiplayer_authority():
|
||||
return
|
||||
|
||||
_heal.rpc(max_health)
|
||||
8
entities/components/health_component.tscn
Normal file
8
entities/components/health_component.tscn
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue