mirror of
https://gitlab.com/open-fpsz/open-fpsz.git
synced 2026-07-13 15:34:53 +00:00
♻️ refactoring
This commit is contained in:
parent
e564b6d2b6
commit
3c7aeadf62
8 changed files with 7 additions and 7 deletions
65
components/damages/explosive_damage.gd
Normal file
65
components/damages/explosive_damage.gd
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
# 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/>.
|
||||
## This class defines an explosive damage area.
|
||||
##
|
||||
## It detects nearby bodies within a radius defined by a child [CollisionShape3D]
|
||||
## or [CollisionPolygon3D] and also initiates a blast that applies impulse and
|
||||
## damages to detected bodies based on their distance from the explosion.
|
||||
class_name ExplosiveDamage extends Area3D
|
||||
|
||||
## The base amount of damage.
|
||||
@export var damage: int = 1
|
||||
## The magnitude of blast force, in Newtons.
|
||||
@export var blast_force: int = 1500
|
||||
## A factor that determines how damage decreases with distance from the explosion center.
|
||||
@export var falloff: Curve
|
||||
## The entity responsible for dealing damage.
|
||||
var source: Node = null
|
||||
# The number of physics frames to process for blast detection.
|
||||
var _blast_frames: int = 0
|
||||
|
||||
func _init() -> void:
|
||||
if not body_shape_entered.is_connected(_on_body_shape_entered):
|
||||
body_shape_entered.connect(_on_body_shape_entered)
|
||||
|
||||
# queue free area after processing few frames (enough for body detection)
|
||||
func _physics_process(_delta: float) -> void:
|
||||
_blast_frames += 1
|
||||
if _blast_frames >= 2:
|
||||
queue_free()
|
||||
|
||||
# overlapping bodies signal handler
|
||||
func _on_body_shape_entered(_body_rid: RID, body: Node, _body_shape_idx: int, local_shape_idx: int) -> void:
|
||||
if multiplayer.is_server():
|
||||
# retrieve area shape
|
||||
var shape : SphereShape3D = shape_owner_get_shape(
|
||||
shape_find_owner(local_shape_idx), local_shape_idx)
|
||||
# retrieve vector from current node origin to body global center of mass
|
||||
var direction: Vector3 = (body.global_transform.origin + body.center_of_mass) - global_transform.origin
|
||||
# sample curve texture if any to get falloff value at current distance
|
||||
var weight := 1.0
|
||||
if falloff:
|
||||
weight = falloff.sample(direction.length() / shape.radius)
|
||||
# handle blast
|
||||
if body is RigidBody3D:
|
||||
var impulse: Vector3 = direction.normalized() * (1000 * weight)
|
||||
# apply body impulse based on distance from explosion origin
|
||||
body.apply_impulse(impulse, global_transform.origin)
|
||||
# handle blast damages
|
||||
if body is Player:
|
||||
# deal damage based on distance from explosion origin
|
||||
body.damage.emit(source, body, damage * weight)
|
||||
if body is Flag:
|
||||
body.respawn_timer.start(clampf(body.respawn_timer.time_left + 5., 0., body.dropped_duration_max))
|
||||
48
components/flag_carry_component.gd
Normal file
48
components/flag_carry_component.gd
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# 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/>.
|
||||
## This component allows its entity to interact with flags
|
||||
class_name FlagCarryComponent extends Node3D
|
||||
|
||||
@export var throw_force : int = 12
|
||||
@export var mesh : Node3D
|
||||
|
||||
var _flag : Flag
|
||||
|
||||
func grab(flag : Flag) -> void:
|
||||
if flag.state < Flag.FlagState.TAKEN:
|
||||
show()
|
||||
_flag = flag
|
||||
flag.grabbed.emit(self)
|
||||
|
||||
func drop(inherited_velocity : Vector3 = Vector3.ZERO) -> void:
|
||||
_release(inherited_velocity, 0)
|
||||
|
||||
func throw(inherited_velocity : Vector3 = Vector3.ZERO, _force: int = throw_force) -> void:
|
||||
_release(inherited_velocity, _force)
|
||||
|
||||
func _release(inherited_velocity : Vector3 = Vector3.ZERO, _force: int = throw_force) -> void:
|
||||
if not _flag or !is_inside_tree(): return
|
||||
_flag.dropped.emit(self)
|
||||
var impulse : Vector3 = _flag.mass * (inherited_velocity + global_basis.z * _force)
|
||||
_flag.apply_central_impulse(impulse) # wake up the flag
|
||||
# @NOTE: throw the flag from some distance in front of the player to avoid regrabbing mid-air
|
||||
# @FIXME: there is a small chance to send the flag under the map
|
||||
_flag.global_position = global_position + (global_basis.z * 1.7)
|
||||
# rotate carried flag based on flag carry global rotation
|
||||
_flag.global_rotation = Vector3(.0, global_rotation.y, .0)
|
||||
# drop reference to flag
|
||||
_flag = null
|
||||
# hide carried flag mesh
|
||||
hide()
|
||||
59
components/health.gd
Normal file
59
components/health.gd
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
# 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/>.
|
||||
## This class defines a Health component for entities.
|
||||
class_name Health extends Area3D
|
||||
|
||||
## An entity with this component can be either dead or alive.
|
||||
enum HealthState { DEAD, ALIVE }
|
||||
|
||||
## Emitted when a peer damaged this component too much.
|
||||
signal killed(by_peer_id:int)
|
||||
## Emitted when value is exhausted.
|
||||
signal exhausted()
|
||||
## Emitted when the value is updated.
|
||||
signal updated(new_value:int)
|
||||
|
||||
## Emitted when the health component is damaged.
|
||||
signal damaged(source: Node, target: Node, amount: int)
|
||||
|
||||
@export var collider:CollisionShape3D
|
||||
@export var max_value:int = 255
|
||||
@export var value:int = 255:
|
||||
set = set_value
|
||||
@export var state : HealthState = HealthState.ALIVE
|
||||
|
||||
func _ready() -> void:
|
||||
# only collide with the layer 3 named "Damage", disable monitoring completely
|
||||
collision_layer = 0b00000000_00000000_00000000_00000100
|
||||
collision_mask = 0
|
||||
monitoring = false
|
||||
|
||||
func set_value(new_value:int) -> void:
|
||||
value = clampi(new_value, 0, max_value)
|
||||
updated.emit(value)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func damage(amount:int, by_peer_id:int) -> void:
|
||||
value -= amount
|
||||
if value == 0 and state != HealthState.DEAD:
|
||||
state = HealthState.DEAD
|
||||
killed.emit(by_peer_id)
|
||||
exhausted.emit(owner)
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func heal(amount:int = max_value) -> void:
|
||||
# add strictly positive amount to value
|
||||
value += clampi(amount, 1, max_value)
|
||||
state = HealthState.ALIVE
|
||||
77
components/inventory.gd
Normal file
77
components/inventory.gd
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# This file is part of open-fpsz.
|
||||
# copyright (c) 2024 - anyreso & open-fpsz contributors
|
||||
#
|
||||
# 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/>.
|
||||
@tool
|
||||
## This component allows its entity to hold [Weapon] nodes.
|
||||
class_name Inventory extends Node3D
|
||||
|
||||
signal selection_changed(index : int)
|
||||
|
||||
signal selected(node: Node)
|
||||
signal unselected(node: Node)
|
||||
|
||||
const _max_items = 3
|
||||
|
||||
## This is the total capacity of inventory.
|
||||
@export_range(0, _max_items) var capacity : int = _max_items:
|
||||
set(value):
|
||||
capacity = clamp(value, 1, _max_items)
|
||||
|
||||
@export_range(0, _max_items - 1) var cursor : int = 0:
|
||||
set = set_cursor
|
||||
|
||||
# Function to set the cursor position in the inventory
|
||||
func set_cursor(new_cursor: int) -> void:
|
||||
# Ensure the cursor is within the valid range
|
||||
if new_cursor >= 0 and new_cursor < _max_items:
|
||||
# emit unselected signal
|
||||
_emit_child(unselected, cursor)
|
||||
# update cursor position
|
||||
cursor = new_cursor
|
||||
# emit selected signal
|
||||
_emit_child(selected, cursor)
|
||||
|
||||
# helper function to emit the selected signal for the current cursor position
|
||||
func _emit_child(sig: Signal, idx: int) -> void:
|
||||
var child : Node = get_child(idx)
|
||||
if child:
|
||||
sig.emit(child)
|
||||
|
||||
func _ready() -> void:
|
||||
unselected.connect(_on_unselected)
|
||||
selected.connect(_on_selected)
|
||||
|
||||
func _on_unselected(node: Node) -> void:
|
||||
node.hide()
|
||||
|
||||
func _on_selected(node: Node) -> void:
|
||||
node.show()
|
||||
|
||||
## This method overrides [method Node.add_child] to make sure not to exceed
|
||||
## inventory capacity.
|
||||
func _add_child(node: Node, force_readable_name: bool = false,
|
||||
internal: InternalMode = InternalMode.INTERNAL_MODE_DISABLED) -> void:
|
||||
if get_child_count() <= capacity:
|
||||
super.add_child(node, force_readable_name, internal)
|
||||
else:
|
||||
push_warning("inventory is full")
|
||||
|
||||
## This method moves the [member selection] cursor to specified item index.
|
||||
func select(index : int) -> void:
|
||||
cursor = wrapi(index, 0, get_child_count())
|
||||
|
||||
## This method cycles through items in the inventory.
|
||||
func cycle(shift : int = 1) -> void:
|
||||
select(cursor + shift)
|
||||
Loading…
Add table
Add a link
Reference in a new issue