grunk/src/ui/hud/grunk_gauge/grunk_gauge.gd
Rob Kelly f2de25ab0f
All checks were successful
linting & formatting / build (push) Successful in 21s
Diegetic grunk gauge
2025-04-18 18:36:19 -06:00

62 lines
1.6 KiB
GDScript

extends Control
## Goes up when u collect tha grunk!
const BUFFER_RUMBLE_FACTOR := 3.0
const RUMBLE_DECAY := 10
const NEEDLE_ANGLE_MIN := 0.0
const NEEDLE_ANGLE_MAX := PI
const NEEDLE_ACCEL := 3.0
const NEEDLE_VELOCITY_MAX := 4.0
const NEEDLE_ANGLE_EPSILON := 0.05
const TANK_WARNING_BUFFER_PCT := 0.1
@export var debug_collect: float:
set = on_grunk_collected
var _needle_velocity := 0.0
var _base_rumble := 0.0
@onready var rumbler: Rumbler = %Rumbler
@onready var needle: TextureRect = %NeedleTexture
func _ready() -> void:
Game.manager.grunk_collected.connect(on_grunk_collected)
Game.manager.grunk_emptied.connect(on_grunk_emptied)
func get_target_rotation() -> float:
return remap(
Game.manager.grunk_tank,
0,
Game.manager.grunk_tank_limit,
NEEDLE_ANGLE_MIN,
NEEDLE_ANGLE_MAX
)
func on_grunk_collected(_delta: float) -> void:
var buffer := remap(Game.manager.get_tank_fill_pct(), 1 - TANK_WARNING_BUFFER_PCT, 1, 0, 1)
_base_rumble = BUFFER_RUMBLE_FACTOR * buffer
func on_grunk_emptied(_amount: float) -> void:
_base_rumble = 0.0
func _process(delta: float) -> void:
var weight := 1 - exp(-RUMBLE_DECAY * delta)
rumbler.intensity = lerpf(rumbler.intensity, _base_rumble, weight)
var diff := get_target_rotation() - needle.rotation
var target_velocity := 0.0
if diff > NEEDLE_ANGLE_EPSILON:
target_velocity = NEEDLE_VELOCITY_MAX
elif diff < -NEEDLE_ANGLE_EPSILON:
target_velocity = -NEEDLE_VELOCITY_MAX
_needle_velocity = lerpf(_needle_velocity, target_velocity, 1 - exp(-NEEDLE_ACCEL * delta))
needle.rotation += _needle_velocity * delta