open-fpsz/entities/components/explosive_damage/explosive_damage_component.gd
2024-05-12 21:57:44 -04:00

53 lines
2.1 KiB
GDScript

# 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
## Emitted when the scale tween is finished
signal finished
@export var damage : float = .35
@export var impulse_force : int = 1000
var damage_dealer : MatchParticipant
var _damaged_objects : Dictionary= {
"bodies": [],
"areas": []
}
func _ready() -> void:
# scale explosive damage aoe
var tween : Tween = get_tree().create_tween()
tween.set_trans(Tween.TRANS_SINE)
tween.tween_property($CollisionShape3D,
"scale", Vector3.ONE, .3).from(Vector3.ZERO)
tween.tween_property($CollisionShape3D,
"scale", Vector3.ZERO, .1).from(Vector3.ONE)
tween.finished.connect(func() -> void: finished.emit(); queue_free())
func _on_body_entered(body: Node3D) -> void:
if body is RigidBody3D and body not in _damaged_objects["bodies"]:
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)
_damaged_objects["bodies"].append(body)
func _on_area_shape_entered(_area_rid: RID, area: Area3D, _area_shape_index: int, _local_shape_index: int) -> void:
if area is HealthComponent and area not in _damaged_objects["areas"]:
if not is_multiplayer_authority():
return
assert(damage_dealer)
area.damage.rpc(damage, damage_dealer.player_id, damage_dealer.team_id)
_damaged_objects["areas"].append(area)