open-fpsz/entities/components/flag_carry_component.gd

63 lines
2.2 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 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)