37 lines
861 B
GDScript3
Raw Permalink Normal View History

2025-01-18 13:09:17 +11:00
extends CharacterBody2D
2025-01-18 21:16:20 +11:00
const MAGNET_FORCE: int = 1000
2025-01-18 13:09:17 +11:00
func _ready():
add_to_group("magnets")
max_slides = 1
func _physics_process(delta):
var acceleration: Vector2 = Vector2.ZERO
2025-01-18 13:09:17 +11:00
2025-01-18 21:16:20 +11:00
#calc acceleration to other magnets
2025-01-18 13:09:17 +11:00
for iter in get_tree().get_nodes_in_group("magnets"):
if iter == self:
continue
2025-01-18 21:16:20 +11:00
var dir: Vector2 = (iter.position - position).normalized()
var dist: float = (iter.position - position).length()
acceleration += dir * (MAGNET_FORCE / dist)
#if moving, rotate to face that direction
if velocity >= Vector2.ONE:
var dest: float = acceleration.angle() + TAU/4
var change: float = dest - rotation
#rotate faster when closer to 0
rotate(change)
2025-01-18 13:09:17 +11:00
#apply to velocity
velocity += acceleration * delta
2025-01-18 21:16:20 +11:00
#move and stop on collision
var collided: bool = move_and_slide()
2025-01-18 13:09:17 +11:00
if collided:
velocity = Vector2.ZERO