♻️ refactoring

This commit is contained in:
anyreso 2024-11-06 23:08:03 -05:00
parent e564b6d2b6
commit 3c7aeadf62
8 changed files with 7 additions and 7 deletions

View file

@ -1,48 +0,0 @@
# 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()

View file

@ -1,59 +0,0 @@
# 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

View file

@ -1,77 +0,0 @@
# 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)

View file

@ -4,10 +4,10 @@
[ext_resource type="PackedScene" uid="uid://bbeecp3jusppn" path="res://interfaces/hud/iffs/IFF.tscn" id="2_s5wgp"]
[ext_resource type="PackedScene" uid="uid://bcv81ku26xo" path="res://interfaces/hud/hud.tscn" id="3_ccety"]
[ext_resource type="Shape3D" uid="uid://cb8esdlnottdn" path="res://entities/player/resources/collider.tres" id="4_8kvcy"]
[ext_resource type="Script" path="res://entities/components/health.gd" id="4_55muf"]
[ext_resource type="Script" path="res://entities/components/inventory.gd" id="6_du54c"]
[ext_resource type="Script" path="res://components/health.gd" id="4_55muf"]
[ext_resource type="Script" path="res://components/inventory.gd" id="6_du54c"]
[ext_resource type="PackedScene" uid="uid://drbefw6akui2v" path="res://entities/player/vanguard.tscn" id="8_eiy7q"]
[ext_resource type="Script" path="res://entities/components/flag_carry_component.gd" id="8_pdfbn"]
[ext_resource type="Script" path="res://components/flag_carry_component.gd" id="8_pdfbn"]
[ext_resource type="Texture2D" uid="uid://ct1v5iadtpadm" path="res://entities/player/assets/jetpackfx/smoke_01.png" id="9_4pant"]
[ext_resource type="PackedScene" uid="uid://c8co0qa2omjmh" path="res://entities/weapons/space_gun/space_gun.tscn" id="9_achlo"]
[ext_resource type="Texture2D" uid="uid://doxo4vfn0bjlp" path="res://entities/player/assets/jetpackfx/smoke_02.png" id="10_5b1bx"]
@ -401,7 +401,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
layers = 2
[node name="Barrels" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun" index="2"]
transform = Transform3D(-1, -7.45058e-09, 8.98726e-08, 8.42847e-08, 0, 1, 0, 1, 0, 1.19209e-07, 4.76837e-07, -0.55315)
transform = Transform3D(-1, 0, 8.56817e-08, 8.47504e-08, 3.72529e-09, 1, -7.45058e-09, 1, 0, 0, -4.76837e-07, -0.55315)
[node name="Skeleton3D" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature" index="0"]
bones/0/rotation = Quaternion(0, 0.707107, 0.707107, 0)

View file

@ -1,65 +0,0 @@
# 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))

View file

@ -40,7 +40,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
autoplay = "idle"
[node name="Barrels" type="BoneAttachment3D" parent="." index="2"]
transform = Transform3D(-1, 0, 8.74227e-08, 8.74227e-08, 0, 1, 0, 1, 0, -2.32831e-10, 0.692504, 0.756307)
transform = Transform3D(-1, 0, 8.74227e-08, 8.74227e-08, 0, 1, 0, 1, 0, -2.32831e-10, 0, -0.55315)
bone_name = "barrels"
bone_idx = 1
use_external_skeleton = true

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=13 format=3 uid="uid://bb43ae1f5j8lw"]
[ext_resource type="Script" path="res://entities/weapons/grenade_launcher/explosion.gd" id="1_xb4jo"]
[ext_resource type="Script" path="res://entities/projectiles/damages/explosive_damage.gd" id="2_r5wb7"]
[ext_resource type="Script" path="res://components/damages/explosive_damage.gd" id="2_r5wb7"]
[ext_resource type="Material" uid="uid://v7s5w3uw4hmb" path="res://entities/weapons/grenade_launcher/assets/resources/particle_process_material_sparks.tres" id="3_4u6ue"]
[ext_resource type="Material" uid="uid://c80t026c2gpxx" path="res://entities/weapons/grenade_launcher/assets/resources/explosion_material.tres" id="3_tsixm"]
[ext_resource type="Material" uid="uid://bbqjhgs44rnss" path="res://entities/weapons/grenade_launcher/assets/resources/material_flare.tres" id="4_634rq"]

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=12 format=3 uid="uid://00uv1dfxlv47"]
[ext_resource type="Script" path="res://entities/weapons/space_gun/explosion.gd" id="1_3xpsk"]
[ext_resource type="Script" path="res://entities/projectiles/damages/explosive_damage.gd" id="2_beh8d"]
[ext_resource type="Script" path="res://components/damages/explosive_damage.gd" id="2_beh8d"]
[sub_resource type="Curve" id="Curve_l54ao"]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]