This commit is contained in:
Kayne Ruse 2025-01-18 13:09:17 +11:00
parent 84651180dc
commit 51821a6299
2 changed files with 70 additions and 1 deletions

23
Godot/Magnet/magnet.gd Normal file
View File

@ -0,0 +1,23 @@
extends CharacterBody2D
const MAGNET_FORCE = 100
func _ready():
add_to_group("magnets")
max_slides = 1
func _physics_process(delta):
var acceleration: Vector2 = Vector2()
#calc direction
for iter in get_tree().get_nodes_in_group("magnets"):
if iter == self:
continue
acceleration += (iter.position - position).normalized() * MAGNET_FORCE
#apply to velocity
velocity += acceleration * delta
var collided: bool = move_and_slide()
if collided:
velocity = Vector2.ZERO

View File

@ -1,3 +1,49 @@
[gd_scene format=3 uid="uid://dtl3ppk2620nr"]
[gd_scene load_steps=4 format=3 uid="uid://dtl3ppk2620nr"]
[ext_resource type="Texture2D" uid="uid://1agxuob1w1bt" path="res://icon.svg" id="1_apg7p"]
[ext_resource type="Script" path="res://Magnet/magnet.gd" id="1_wyfbh"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_gk0yk"]
size = Vector2(128, 128)
[node name="RootNode" type="Node2D"]
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(594, 136)
script = ExtResource("1_wyfbh")
[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D"]
texture = ExtResource("1_apg7p")
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("RectangleShape2D_gk0yk")
[node name="CharacterBody2D2" type="CharacterBody2D" parent="."]
position = Vector2(806, 194)
script = ExtResource("1_wyfbh")
[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D2"]
texture = ExtResource("1_apg7p")
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D2"]
shape = SubResource("RectangleShape2D_gk0yk")
[node name="CharacterBody2D3" type="CharacterBody2D" parent="."]
position = Vector2(252, 344)
script = ExtResource("1_wyfbh")
[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D3"]
texture = ExtResource("1_apg7p")
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D3"]
shape = SubResource("RectangleShape2D_gk0yk")
[node name="CharacterBody2D4" type="CharacterBody2D" parent="."]
position = Vector2(1082, 406)
script = ExtResource("1_wyfbh")
[node name="Sprite2D" type="Sprite2D" parent="CharacterBody2D4"]
texture = ExtResource("1_apg7p")
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D4"]
shape = SubResource("RectangleShape2D_gk0yk")