sunder/scenes/entities/player/scripts/jetpack.gd
2026-02-18 18:33:17 -05:00

62 lines
3 KiB
GDScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# This file is part of sunder.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
class_name Jetpack extends Node3D
@export var input:InputSynchronizer
@export var energy:Energy
## The [param energy.value] drain rate per second, as a percentage of [param energy.max_value]
@export_range(0,100, 1., "suffix:%") var drain_rate:float = 30
## The stutter threshold of [param energy.value] at which the jetpack needs to charge to be used again,
## as a percentage of [param energy.max_value]
@export_range(0,100, 1., "suffix:%") var stutter_treshold:float = 15
## This property indicates wether the [Jetpack] is stuttering
@export var stuttering:bool = false
@export_category("Thrust")
# @NOTE: If a body want to accelerate upward on a high-gravity planet, say by 10 m/s²:
# F = 80 * (19.6 + 10) = 80 * 29.6 = 2,368N
## The thrust vertical acceleration component, expressed in m/s²
@export_range(0, 100, .01, "suffix:m/s²") var vertical_acceleration:float = 10
# @NOTE: If a body want to accelerate horizontally, say by 15 m/s²:
# F = 80 * 15 = 1200 N
## The thrust horizontal acceleration component, expressed in m/s²
@export_range(0, 100, .01, "suffix:m/s²") var horizontal_acceleration:float = 15
@export_category("Effects")
## An array of [GPUParticles3D] children
@onready var emitters:Array = find_children("*", "GPUParticles3D")
## Wether all [GPUParticles3D] children have their [member GPUParticles3D.emitting] property set
@export var emitting:bool:
get: return emitters.map(func(child:Node) -> bool: return child.emitting).max()
set(value): emitters.map(func(child:Node) -> void: child.emitting = value)
## Wether all [GPUParticles3D] children have their [member GPUParticles3D.one_shot] property set
@export var one_shot:bool:
get: return emitters.map(func(child:Node) -> bool: return child.one_shot).max()
set(value): emitters.map(func(child:Node) -> void: child.one_shot = value)
func _ready() -> void:
assert(input and energy, "Jetpack requires 'input' and 'energy' properties to be assigned")
func _process(delta: float) -> void:
if stuttering:
stuttering = energy.value <= stutter_treshold / 100. * energy.max_value
elif input.jetting and energy.value <= 0:
stuttering = true
energy.charging = !input.jetting or stuttering
if !energy.charging:
energy.value -= drain_rate / 100. * energy.max_value * delta
if input.jetting and energy.value > 0 and not stuttering:
emitting = true