flag refactoring

This commit is contained in:
anyreso 2024-05-09 01:17:10 -04:00
parent 69c8b65093
commit 6ba368c76e
21 changed files with 389 additions and 198 deletions

View file

@ -12,51 +12,43 @@
#
# 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
## 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
@export var mesh : Node3D
var _carried_flag : Flag
func _ready() -> void:
sensor.body_entered.connect(_sensor_on_body_entered)
func _physics_process(_delta : float) -> void:
func _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()
_carried_flag.global_rotation = Vector3(.0, global_rotation.y, .0)
func _is_carrying() -> bool:
return _carried_flag != null
func _sensor_on_body_entered(collider : Flag) -> void:
if collider is Flag:
_grab(collider)
func grab(flag : Flag) -> void:
if not _is_carrying():
flag.grabbed.emit(owner)
_carried_flag = flag
show()
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)
func _release(inherited_velocity : Vector3, throw_speed : float, dropper : Player) -> void:
if not _is_carrying():
return
# update carried flag global rotation based on component global rotation
_carried_flag.global_rotation = Vector3(.0, global_rotation.y, .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 = owner.global_position + (global_basis.z * 1.7)
_carried_flag.dropped.emit(dropper)
_carried_flag = null
hide()