extends Node
class_name BezierCurve

@export var p0 : Vector3
@export var p1 : Vector3
@export var p2 : Vector3
@export var p3 : Vector3

func _init(point0 = Vector3.ZERO, point1 = Vector3.ZERO, point2 = Vector3.ZERO, point3 = Vector3.ZERO) -> void:
	p0 = point0
	p1 = point1
	p2 = point2
	p3 = point3

func to_straight_line(start : Vector3, end : Vector3) -> BezierCurve:
	var from_to = end - start
	p0 = start
	p1 = start + from_to * 0.333
	p2 = start + from_to * 0.666
	p3 = end

	return self

func eval(t : float = 0) -> Vector3:
	var u = 1 - t
	var tt = t * t
	var uu = u * u
	var uuu = uu * u
	var ttt = tt * t

	var p = uuu * p0
	p += 3 * uu * t * p1
	p += 3 * u * tt * p2
	p += ttt * p3

	return p

func eval2D(t : float = 0) -> Vector2:
	var u = 1 - t
	var tt = t * t
	var uu = u * u
	var uuu = uu * u
	var ttt = tt * t

	var p = uuu * p0
	p += 3 * uu * t * p1
	p += 3 * u * tt * p2
	p += ttt * p3

	return Vector2(p.x, p.y)
