The_Dark_Side_of_Earth/utils/earth_aligner.gd

38 lines
931 B
GDScript3
Raw Normal View History

extends Node2D
@export var center = Vector2.ZERO
var angle = 0
2025-10-15 16:00:47 +02:00
# Quick access to relative cardinal directions
var up : Vector2 :
get():
return global_from_local(Vector2.UP)
var down : Vector2 :
get():
return global_from_local(Vector2.DOWN)
var right : Vector2 :
get():
return global_from_local(Vector2.RIGHT)
var left : Vector2 :
get():
return global_from_local(Vector2.LEFT)
func _ready() -> void:
2025-09-17 12:53:16 +02:00
align()
2025-09-23 12:36:15 +02:00
func _process(_delta: float) -> void:
2025-09-17 12:53:16 +02:00
align()
2025-10-15 16:00:47 +02:00
# Aligns the parent to the center
2025-09-17 12:53:16 +02:00
func align():
2025-10-15 16:00:47 +02:00
angle = -(get_parent().position - center).angle_to(Vector2.UP)
get_parent().rotation = angle;
2025-09-16 14:59:40 +02:00
2025-10-15 16:00:47 +02:00
# Converts directions in local coordinates into global coordinates
2025-09-16 14:59:40 +02:00
func global_from_local (_velocity: Vector2) -> Vector2:
return _velocity.rotated(angle)
2025-10-15 16:00:47 +02:00
# Converts directions in global coordinates into local coordinates
2025-09-16 14:59:40 +02:00
func local_from_global (_velocity: Vector2) -> Vector2:
return _velocity.rotated(-angle)