diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/BuilderObject.gd | 45 | ||||
-rw-r--r-- | Objects/BuilderObject.tscn | 12 |
2 files changed, 49 insertions, 8 deletions
diff --git a/Objects/BuilderObject.gd b/Objects/BuilderObject.gd index 2be50da..91465bc 100644 --- a/Objects/BuilderObject.gd +++ b/Objects/BuilderObject.gd @@ -3,19 +3,28 @@ extends Node2D var is_colliding = false -var is_dragged = false +var is_dragging = false +var is_rotating = false var mouse_offset = Vector2(0,0) -var last_position = Vector2(0,0) +var last_position = null +var rotation_offset_angle = 0 +var last_rotation = 0 + +# todo: if is focused via click, show rotation picker in edge func _ready(): $CollisionShape.texture.width = $Area2D/CollisionShape2D.shape.size.x func _process(_delta): - if is_dragged: + if is_dragging: position = get_global_mouse_position() + mouse_offset + + if is_rotating: + var distance = position - get_global_mouse_position() + rotation = atan2(distance.y, distance.x) - rotation_offset_angle func _on_area_2d_input_event(_viewport, event: InputEvent, _shape_idx): @@ -30,16 +39,38 @@ func drag_start(): mouse_offset = position - get_global_mouse_position() last_position = position - is_dragged = true + is_dragging = true func drag_end(): - if is_dragged and is_colliding: - if last_position != Vector2(0,0): + if is_colliding: + if last_position: position = last_position else: queue_free() - is_dragged = false + is_dragging = false + + +func _on_rotate_gui_input(event: InputEvent): + get_viewport().set_input_as_handled() + + if event.is_action_pressed("rotate_start"): + rotate_start() + if event.is_action_released("rotate_start"): + rotate_end() + + +func rotate_start(): + var distance = position - get_global_mouse_position() + rotation_offset_angle = atan2(distance.y, distance.x) - rotation + last_rotation = rotation + is_rotating = true + +func rotate_end(): + if is_colliding: + rotation = last_rotation + + is_rotating = false func _on_area_2d_area_entered(_area): diff --git a/Objects/BuilderObject.tscn b/Objects/BuilderObject.tscn index 749ad2a..3d85ff3 100644 --- a/Objects/BuilderObject.tscn +++ b/Objects/BuilderObject.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=3 uid="uid://p0ay1mp7v772"] +[gd_scene load_steps=7 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"] +[ext_resource type="Texture2D" uid="uid://djasmoqj87h1r" path="res://icon.svg" id="3_ios8i"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_vqo4b"] size = Vector2(80, 64) @@ -33,6 +34,15 @@ texture = ExtResource("2_hvvj8") region_enabled = true region_rect = Rect2(2, 13, 60, 49) +[node name="Rotate" type="TextureRect" parent="."] +offset_left = -40.0 +offset_top = -32.0 +offset_right = 88.0 +offset_bottom = 96.0 +scale = Vector2(0.08, 0.08) +texture = ExtResource("3_ios8i") + [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="input_event" from="Area2D" to="." method="_on_area_2d_input_event"] +[connection signal="gui_input" from="Rotate" to="." method="_on_rotate_gui_input"] |