From 286d51bbcf2abcfaad1ff54710a58fba32732cf0 Mon Sep 17 00:00:00 2001 From: Damien Ostler Date: Sun, 7 Jan 2024 21:30:49 -0500 Subject: [PATCH] feat added item rotation when dragging --- .../scenes/InventorySlot.tscn | 22 +++++++++++++ grid_inventory_system/scenes/Item.tscn | 25 +++++++++++++++ grid_inventory_system/scenes/ItemGraphic.tscn | 7 ----- .../scripts/InventoryController.gd | 11 +++---- .../scripts/InventorySlot.gd | 11 +++++++ grid_inventory_system/scripts/Item.gd | 31 +++++++++++++++++++ grid_inventory_system/scripts/ItemGraphic.gd | 3 -- project.godot | 4 --- 8 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 grid_inventory_system/scenes/InventorySlot.tscn create mode 100644 grid_inventory_system/scenes/Item.tscn delete mode 100644 grid_inventory_system/scenes/ItemGraphic.tscn create mode 100644 grid_inventory_system/scripts/InventorySlot.gd create mode 100644 grid_inventory_system/scripts/Item.gd delete mode 100644 grid_inventory_system/scripts/ItemGraphic.gd diff --git a/grid_inventory_system/scenes/InventorySlot.tscn b/grid_inventory_system/scenes/InventorySlot.tscn new file mode 100644 index 0000000..5e890c7 --- /dev/null +++ b/grid_inventory_system/scenes/InventorySlot.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=3 format=3 uid="uid://b1nobn0glk4ru"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventorySlot.gd" id="1_wp4po"] +[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="2_1w5re"] + +[node name="InventorySlot" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_right = -1888.0 +offset_bottom = -1048.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_wp4po") + +[node name="TextureRect" type="TextureRect" parent="."] +layout_mode = 1 +offset_right = 10.0 +offset_bottom = 10.0 +texture = ExtResource("2_1w5re") +expand_mode = 1 diff --git a/grid_inventory_system/scenes/Item.tscn b/grid_inventory_system/scenes/Item.tscn new file mode 100644 index 0000000..93261d3 --- /dev/null +++ b/grid_inventory_system/scenes/Item.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://dtir5hovd6i3h"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/Item.gd" id="1_cs6rn"] +[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="2_uetec"] + +[node name="Item" type="Control" node_paths=PackedStringArray("graphic")] +clip_contents = true +layout_mode = 3 +anchors_preset = 0 +offset_right = 100.0 +offset_bottom = 100.0 +script = ExtResource("1_cs6rn") +graphic = NodePath("Graphic") + +[node name="Graphic" type="TextureRect" parent="."] +unique_name_in_owner = true +clip_contents = true +layout_mode = 0 +offset_right = 40.0 +offset_bottom = 40.0 +pivot_offset = Vector2(20, 20) +texture = ExtResource("2_uetec") +expand_mode = 1 + +[node name="Node" type="Node" parent="."] diff --git a/grid_inventory_system/scenes/ItemGraphic.tscn b/grid_inventory_system/scenes/ItemGraphic.tscn deleted file mode 100644 index da9d891..0000000 --- a/grid_inventory_system/scenes/ItemGraphic.tscn +++ /dev/null @@ -1,7 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://dtir5hovd6i3h"] - -[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemGraphic.gd" id="1_bih4t"] - -[node name="Item" type="TextureRect"] -expand_mode = 3 -script = ExtResource("1_bih4t") diff --git a/grid_inventory_system/scripts/InventoryController.gd b/grid_inventory_system/scripts/InventoryController.gd index 87ccf67..d809bb2 100644 --- a/grid_inventory_system/scripts/InventoryController.gd +++ b/grid_inventory_system/scripts/InventoryController.gd @@ -1,6 +1,6 @@ extends Control class_name InventoryController -const ITEM_BASE = preload("res://grid_inventory_system/scenes/ItemGraphic.tscn") +const ITEM_BASE = preload("res://grid_inventory_system/scenes/Item.tscn") @export var inventory_open_input:String = "inventory_open" @export var inventory_close_input:String = "inventory_close" @@ -40,8 +40,8 @@ func _process(delta): inventory_open = false inventory_background.visible=false - #if(Input.is_action_just_pressed(inventory_rotate_input) and inventory_open==true and inventory_item_dragged != null): - # inventory_item_dragged.rotation_degrees = inventory_item_dragged.rotation_degrees + 90.0 + if(Input.is_action_just_pressed(inventory_rotate_input) and inventory_open==true and inventory_item_dragged != null): + inventory_item_dragged.rotate_item(90) var cursor_pos = get_global_mouse_position() @@ -86,10 +86,7 @@ func use(cursor_pos): func pickup_item(item_id): var item = ITEM_BASE.instantiate() item.set_meta("id", item_id) - var dbItem = ItemDb.get_item(item_id) - item.item_config = dbItem - item.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) - item.texture = dbItem.item_texture + item.init_item(item_id) if not inventory_grid.insert_item_at_first_available_spot(item): item.queue_free() return false diff --git a/grid_inventory_system/scripts/InventorySlot.gd b/grid_inventory_system/scripts/InventorySlot.gd new file mode 100644 index 0000000..438eeba --- /dev/null +++ b/grid_inventory_system/scripts/InventorySlot.gd @@ -0,0 +1,11 @@ +extends Control + +enum slot_state { free, used } + +@export var in_use = false + +func used(): + in_use = true + +func free(): + in_use = false diff --git a/grid_inventory_system/scripts/Item.gd b/grid_inventory_system/scripts/Item.gd new file mode 100644 index 0000000..95a8f89 --- /dev/null +++ b/grid_inventory_system/scripts/Item.gd @@ -0,0 +1,31 @@ +extends Control +class_name ItemGraphic +@export var item_config:ItemConfiguration +@export var graphic:TextureRect + +func init_item(item_id): + var dbItem = ItemDb.get_item(item_id) + item_config = dbItem + graphic.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) + set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) + graphic.texture = dbItem.item_texture + graphic.pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0) + pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0) + graphic.position = Vector2() + graphic.position = Vector2() + graphic.position = Vector2() + graphic.position = Vector2() + graphic.position = Vector2() + graphic.position = Vector2() + graphic.position = Vector2() + print("TEST") + +func rotate_item(amount): + graphic.rotation_degrees = graphic.rotation_degrees+amount + if(graphic.rotation_degrees==90 or graphic.rotation_degrees==270): + set_size(Vector2(item_config.item_size.y, item_config.item_size.x)) + else: + set_size(Vector2(item_config.item_size.x, item_config.item_size.y)) + + if(graphic.rotation_degrees==360): + graphic.rotation_degrees=0 diff --git a/grid_inventory_system/scripts/ItemGraphic.gd b/grid_inventory_system/scripts/ItemGraphic.gd deleted file mode 100644 index 59d7753..0000000 --- a/grid_inventory_system/scripts/ItemGraphic.gd +++ /dev/null @@ -1,3 +0,0 @@ -extends TextureRect -class_name ItemGraphic -@export var item_config:ItemConfiguration diff --git a/project.godot b/project.godot index 9341f4e..63eb1bb 100644 --- a/project.godot +++ b/project.godot @@ -60,7 +60,3 @@ mouse={ "deadzone": 0.5, "events": [] } - -[rendering] - -renderer/rendering_method="gl_compatibility"