diff --git a/Godot/Magnet/magnet.gd b/Godot/Magnet/magnet.gd new file mode 100644 index 0000000..00f0ea2 --- /dev/null +++ b/Godot/Magnet/magnet.gd @@ -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 diff --git a/Godot/root_node.tscn b/Godot/root_node.tscn index 6923721..bbf4de7 100644 --- a/Godot/root_node.tscn +++ b/Godot/root_node.tscn @@ -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")