35 lines
1.0 KiB
GDScript3
35 lines
1.0 KiB
GDScript3
|
class_name Bumper extends Area3D
|
||
|
|
||
|
const BASE_SPIN_SPEED := 120.0
|
||
|
const MIN_LAUNCH_SPEED := 12.0
|
||
|
|
||
|
@export var launch_factor: float = 2.0
|
||
|
|
||
|
@onready var mesh := $'Mesh' as MeshInstance3D
|
||
|
@onready var audio_player := $'AudioStreamPlayer' as AudioStreamPlayer
|
||
|
|
||
|
func _ready() -> void:
|
||
|
body_entered.connect(propel)
|
||
|
mesh.set_instance_shader_parameter(
|
||
|
&'hue_shift',
|
||
|
launch_factor*MIN_LAUNCH_SPEED - 24.0
|
||
|
)
|
||
|
|
||
|
func propel(body: PhysicsBody3D) -> void:
|
||
|
if body is RigidBody3D:
|
||
|
var rbody := body as RigidBody3D
|
||
|
audio_player.play()
|
||
|
var launch_vector := -rbody.linear_velocity.project(
|
||
|
rbody.global_position + rbody.center_of_mass - global_position
|
||
|
)*launch_factor
|
||
|
if launch_vector.is_zero_approx():
|
||
|
launch_vector = Vector3.UP*launch_factor
|
||
|
elif launch_vector.length() < launch_factor*MIN_LAUNCH_SPEED:
|
||
|
launch_vector = (
|
||
|
launch_vector.normalized()*launch_factor*MIN_LAUNCH_SPEED
|
||
|
)
|
||
|
rbody.linear_velocity += launch_vector
|
||
|
|
||
|
func _process(delta: float) -> void:
|
||
|
global_rotation_degrees += Vector3.UP*BASE_SPIN_SPEED*launch_factor*delta
|