Untitled
unknown
plain_text
a year ago
3.4 kB
12
Indexable
extends Node2D
@export var coin_scene: PackedScene
@export var min_impulse: float = 100.0 # Minimum impulse strength
@export var max_impulse: float = 180.0 # Maximum impulse strength
@export var linear_damp: float = 1.0 # Damping for linear velocity
@export var angular_damp: float = 1.0 # Damping for angular velocity
@export var body: Node2D
@export var bounciness: float = 0.6 # Bounciness for the coins
@export var gravity_scale: float = 1 # Reduced gravity scale
@export var stop_time: float = 0.25 # Time after which coins stop moving
@onready var target_position: Vector2
var number_of_coins = 5
func _ready():
target_position = body.global_position
await get_tree().create_timer(1).timeout
body.queue_free()
emit_coins()
func emit_coins():
for i in range(number_of_coins):
var coin = coin_scene.instantiate() as RigidBody2D
coin.global_position = target_position
add_child(coin)
# Enable reduced gravity for the coin
coin.gravity_scale = gravity_scale
# Apply damping to prevent coins from flying away
coin.linear_damp = linear_damp
coin.angular_damp = angular_damp
# Set bounciness for the coins
var physics_material = PhysicsMaterial.new()
physics_material.bounce = bounciness
coin.physics_material_override = physics_material
# Create a random direction vector with an upward component
var angle = randf() * TAU # Random angle in radians
var direction = Vector2(cos(angle), sin(angle)) + Vector2(0, -1) # Adding upward component
direction = direction.normalized() # Normalize to maintain consistent impulse strength
var impulse = direction * randf_range(min_impulse, max_impulse)
# Apply impulse to scatter the coins
coin.apply_central_impulse(impulse)
# Add a timer to stop the coin's movement
var timer = Timer.new()
timer.wait_time = stop_time
timer.one_shot = true
timer.connect("timeout", Callable(self, "_stop_coin").bind(coin))
add_child(timer)
timer.start()
func _stop_coin(coin: RigidBody2D):
coin.freeze = true
_create_bounce_tween(coin)
func _create_bounce_tween(coin: RigidBody2D):
var initial_pos = coin.position
var bounce_height = 7 # Adjust the height of the bounce as needed
var bounce_duration = 0.3 # Adjust the duration of the bounce as needed
var horizontal_offset = 2 # Small horizontal movement during the bounce
# Stagger the start of the tween based on coin's position to create a more natural effect
var stagger_delay = initial_pos.distance_to(target_position) * 0.002 # Adjust the factor as needed
await get_tree().create_timer(stagger_delay).timeout
var tween = get_tree().create_tween()
# First bounce with horizontal movement
tween.tween_property(coin, "position", initial_pos + Vector2(horizontal_offset, -bounce_height), bounce_duration / 2).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_OUT)
tween.tween_property(coin, "position", initial_pos + Vector2(horizontal_offset, 0), bounce_duration / 2).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN)
# Second smaller bounce with horizontal movement
tween.tween_property(coin, "position", initial_pos + Vector2(horizontal_offset * 2, -bounce_height / 2), bounce_duration / 2).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_OUT)
tween.tween_property(coin, "position", initial_pos + Vector2(horizontal_offset * 2, 0), bounce_duration / 2).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN)
Editor is loading...
Leave a Comment