summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-05-11 14:52:36 +0200
committerDaniel Weipert <git@mail.dweipert.de>2024-05-11 14:52:36 +0200
commit17716f198bf6b3e4d39d48d0394192a5cb640499 (patch)
tree78cf4c98483ad22e4ac137702f307a4478153857 /Objects
initial commit
Diffstat (limited to 'Objects')
-rw-r--r--Objects/BuilderObject.gd50
-rw-r--r--Objects/BuilderObject.tscn28
2 files changed, 78 insertions, 0 deletions
diff --git a/Objects/BuilderObject.gd b/Objects/BuilderObject.gd
new file mode 100644
index 0000000..9131120
--- /dev/null
+++ b/Objects/BuilderObject.gd
@@ -0,0 +1,50 @@
+class_name BuilderObject
+extends Node2D
+
+
+signal collided
+
+var is_colliding = false
+var is_dragged = false
+
+var mouse_offset = Vector2(0,0)
+var last_position = Vector2(0,0)
+
+
+func _process(_delta):
+ if $Area2D.get_overlapping_areas().size() > 0:
+ is_colliding = true
+ else:
+ is_colliding = false
+
+ if is_dragged:
+ position = get_global_mouse_position() + mouse_offset
+
+ if is_colliding:
+ modulate = Color("bb5a4b")
+ else:
+ modulate = Color("fff")
+
+
+func _on_area_2d_input_event(_viewport, event: InputEvent, _shape_idx):
+ if event.is_action_pressed("drag_start"):
+ drag_start()
+ if event.is_action_released("drag_start"):
+ drag_end()
+
+
+func drag_start():
+ if get_viewport():
+ mouse_offset = position - get_global_mouse_position()
+ last_position = position
+
+ is_dragged = true
+
+func drag_end():
+ if is_dragged and is_colliding:
+ if last_position != Vector2(0,0):
+ position = last_position
+ else:
+ queue_free()
+
+ is_dragged = false
diff --git a/Objects/BuilderObject.tscn b/Objects/BuilderObject.tscn
new file mode 100644
index 0000000..ad83377
--- /dev/null
+++ b/Objects/BuilderObject.tscn
@@ -0,0 +1,28 @@
+[gd_scene load_steps=4 format=3 uid="uid://p0ay1mp7v772"]
+
+[ext_resource type="Script" path="res://Objects/BuilderObject.gd" id="1_wjk1m"]
+[ext_resource type="Texture2D" uid="uid://dfjrmruj2mk0n" path="res://Assets/wooden_bench/32x32/bench-wood.png" id="2_hvvj8"]
+
+[sub_resource type="RectangleShape2D" id="RectangleShape2D_vqo4b"]
+size = Vector2(80, 64)
+
+[node name="BuilderObject" type="Node2D"]
+script = ExtResource("1_wjk1m")
+
+[node name="Area2D" type="Area2D" parent="."]
+collision_layer = 3
+collision_mask = 3
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
+shape = SubResource("RectangleShape2D_vqo4b")
+
+[node name="Sprite2D" type="Sprite2D" parent="."]
+texture = ExtResource("2_hvvj8")
+region_enabled = true
+region_rect = Rect2(2, 13, 60, 49)
+
+[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
+[connection signal="area_exited" from="Area2D" to="." method="_on_area_2d_area_exited"]
+[connection signal="area_shape_entered" from="Area2D" to="." method="_on_area_2d_area_shape_entered"]
+[connection signal="area_shape_exited" from="Area2D" to="." method="_on_area_2d_area_shape_exited"]
+[connection signal="input_event" from="Area2D" to="." method="_on_area_2d_input_event"]