GodotComponentTest/Components/MovableComponent.gd

28 lines
716 B
GDScript
Raw Normal View History

2022-08-17 00:50:30 +02:00
extends Node2D
export var target = Vector2 (0, 0)
export var pos = Vector2 (0, 0)
export var max_speed = 50
export var spring_k = 10
export var spring_b = 0.5
var velocity = Vector2(0, 0)
const pos_error_eps = 0.01
signal position_updated
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if (target - pos).length_squared() > pos_error_eps * pos_error_eps:
var acceleration = spring_k * (target - pos) - spring_b * velocity
velocity = velocity + acceleration * delta
pos = pos + velocity * delta
emit_signal("position_updated", pos)