36 lines
1.6 KiB
GDScript
36 lines
1.6 KiB
GDScript
extends Node2D
|
|
|
|
class_name Item
|
|
|
|
var inventory : Control
|
|
|
|
var dragging = false
|
|
|
|
var last_position = null
|
|
|
|
var layer = 0
|
|
|
|
func _process(delta):
|
|
if not dragging: return
|
|
global_position = get_global_mouse_position()
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseButton:
|
|
if ((event.button_index == MOUSE_BUTTON_LEFT and layer == 0) or (event.button_index == MOUSE_BUTTON_RIGHT and layer == 1)) and not event.pressed :
|
|
dragging = false
|
|
var inventory_position = inventory.global_to_inventory_coords(global_position)
|
|
if inventory_position != null and last_position != Vector2i(inventory_position):
|
|
var temp = inventory.items[layer][inventory_position.y][inventory_position.x]
|
|
inventory.items[layer][inventory_position.y][inventory_position.x] = self
|
|
inventory.items[layer][last_position.y][last_position.x] = temp
|
|
global_position = inventory.inventory_to_global_coords(Vector2i(inventory_position.x,inventory_position.y))
|
|
if last_position != null:
|
|
if temp != null:
|
|
inventory.items[layer][last_position.y][last_position.x].global_position = inventory.inventory_to_global_coords(Vector2i(last_position.x,last_position.y))
|
|
inventory.items[layer][last_position.y][last_position.x].last_position = Vector2i(last_position.x,last_position.y)
|
|
last_position = Vector2i(inventory_position.x,inventory_position.y)
|
|
elif last_position != null and inventory.items[layer][last_position.y][last_position.x] == null:
|
|
inventory.items[layer][last_position.y][last_position.x] = self
|
|
global_position = inventory.inventory_to_global_coords(Vector2i(last_position.x,last_position.y))
|
|
inventory.call_deferred("update_calculations")
|