Basic inventory, item and storage
This commit is contained in:
parent
b0b659acd4
commit
66a623e568
11 changed files with 155 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
[gd_resource type="Resource" script_class="Constructible" load_steps=5 format=3 uid="uid://yn1iesx30nfu"]
|
[gd_resource type="Resource" script_class="Prototype" load_steps=5 format=3 uid="uid://yn1iesx30nfu"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_mqcr0"]
|
[ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_mqcr0"]
|
||||||
[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_sh8t1"]
|
[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_sh8t1"]
|
||||||
|
|
22
scripts/inventory/inventory.gd
Normal file
22
scripts/inventory/inventory.gd
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
@abstract
|
||||||
|
extends Resource
|
||||||
|
|
||||||
|
class_name Inventory
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
resource_local_to_scene = true
|
||||||
|
|
||||||
|
@abstract
|
||||||
|
func add(item : Stack) -> Stack
|
||||||
|
|
||||||
|
@abstract
|
||||||
|
func pop() -> Stack
|
||||||
|
|
||||||
|
@abstract
|
||||||
|
func take(item : Item,amount: int) -> Stack
|
||||||
|
|
||||||
|
@abstract
|
||||||
|
func find(item : Item) -> int
|
||||||
|
|
||||||
|
func has(item : Item) -> bool:
|
||||||
|
return find(item) != -1
|
1
scripts/inventory/inventory.gd.uid
Normal file
1
scripts/inventory/inventory.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://01aqkh38trcr
|
59
scripts/inventory/inventory_slot.gd
Normal file
59
scripts/inventory/inventory_slot.gd
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
@tool
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
class_name InventorySlot
|
||||||
|
|
||||||
|
@export_storage var held_item : Item:
|
||||||
|
set(value):
|
||||||
|
if held_item == null and value != null:
|
||||||
|
held_item = value
|
||||||
|
amount += 1
|
||||||
|
return
|
||||||
|
held_item = value
|
||||||
|
get:
|
||||||
|
return held_item
|
||||||
|
|
||||||
|
@export_storage var amount : int:
|
||||||
|
set(value):
|
||||||
|
if value <= 0:
|
||||||
|
held_item = null
|
||||||
|
amount = 0
|
||||||
|
return
|
||||||
|
if held_item == null:
|
||||||
|
return
|
||||||
|
if value > held_item.stack_size:
|
||||||
|
amount = held_item.stack_size
|
||||||
|
return
|
||||||
|
amount = value
|
||||||
|
get:
|
||||||
|
return amount
|
||||||
|
|
||||||
|
func extract_stack(extract_amount: int) -> Stack:
|
||||||
|
var return_stack : Stack
|
||||||
|
if extract_amount > amount:
|
||||||
|
return_stack = Stack.new(held_item,amount)
|
||||||
|
else:
|
||||||
|
return_stack = Stack.new(held_item,extract_amount)
|
||||||
|
|
||||||
|
self.amount = 0
|
||||||
|
|
||||||
|
return return_stack
|
||||||
|
|
||||||
|
func extract() -> Stack:
|
||||||
|
var return_stack : Stack = Stack.new(held_item,amount)
|
||||||
|
self.amount = 0
|
||||||
|
|
||||||
|
return return_stack
|
||||||
|
|
||||||
|
func merge_stack(stack: Stack) -> Stack:
|
||||||
|
if held_item == null:
|
||||||
|
held_item = stack.held_item
|
||||||
|
amount = stack.amount
|
||||||
|
return null
|
||||||
|
if stack.held_item != held_item:
|
||||||
|
return null
|
||||||
|
var return_stack_amount = max(0,stack.amount-amount)
|
||||||
|
amount += stack.amount - return_stack_amount
|
||||||
|
if return_stack_amount == 0:
|
||||||
|
return null
|
||||||
|
return Stack.new(held_item,return_stack_amount)
|
1
scripts/inventory/inventory_slot.gd.uid
Normal file
1
scripts/inventory/inventory_slot.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://bd4ojfqrl8idm
|
11
scripts/inventory/stack.gd
Normal file
11
scripts/inventory/stack.gd
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
extends RefCounted
|
||||||
|
|
||||||
|
class_name Stack
|
||||||
|
|
||||||
|
func _init(item: Item = null, amount: int = 0) -> void:
|
||||||
|
held_item = item
|
||||||
|
self.amount = amount
|
||||||
|
|
||||||
|
@export_storage var held_item : Item
|
||||||
|
|
||||||
|
@export_storage var amount : int
|
1
scripts/inventory/stack.gd.uid
Normal file
1
scripts/inventory/stack.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://h78lmbi3vbfe
|
50
scripts/inventory/storage.gd
Normal file
50
scripts/inventory/storage.gd
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
@tool
|
||||||
|
extends Inventory
|
||||||
|
|
||||||
|
class_name Storage
|
||||||
|
|
||||||
|
@export var capacity : int:
|
||||||
|
set(value):
|
||||||
|
if value < 0:
|
||||||
|
return
|
||||||
|
if value == capacity:
|
||||||
|
return
|
||||||
|
capacity = value
|
||||||
|
internal_array.resize(capacity)
|
||||||
|
for i in range(capacity):
|
||||||
|
internal_array[i] = InventorySlot.new()
|
||||||
|
get:
|
||||||
|
return capacity
|
||||||
|
|
||||||
|
@export_storage var internal_array : Array[InventorySlot] = []
|
||||||
|
|
||||||
|
func find(item : Item) -> int:
|
||||||
|
for i in range(len(internal_array)):
|
||||||
|
if internal_array[i].held_item == item:
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
|
||||||
|
func add(stack: Stack) -> Stack:
|
||||||
|
var found_index : int = find(stack.held_item)
|
||||||
|
if found_index != -1:
|
||||||
|
stack = internal_array[found_index].merge_stack(stack)
|
||||||
|
if stack == null:
|
||||||
|
return null
|
||||||
|
for i in range(len(internal_array)):
|
||||||
|
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
|
||||||
|
stack = internal_array[i].merge_stack(stack)
|
||||||
|
if stack == null:
|
||||||
|
return null
|
||||||
|
return stack
|
||||||
|
|
||||||
|
func pop() -> Stack:
|
||||||
|
for i in range(len(internal_array)):
|
||||||
|
if internal_array[i].held_item != null:
|
||||||
|
return internal_array[i].extract()
|
||||||
|
return null
|
||||||
|
|
||||||
|
func take(item: Item,amount: int) -> Stack:
|
||||||
|
var found = find(item)
|
||||||
|
if found == -1:
|
||||||
|
return null
|
||||||
|
return internal_array[found].extract_stack(amount)
|
1
scripts/inventory/storage.gd.uid
Normal file
1
scripts/inventory/storage.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://1scdy7mttx5h
|
7
scripts/item.gd
Normal file
7
scripts/item.gd
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
extends Resource
|
||||||
|
|
||||||
|
class_name Item
|
||||||
|
|
||||||
|
@export var display_name : StringName
|
||||||
|
@export var preview : Texture2D
|
||||||
|
@export var stack_size : int
|
1
scripts/item.gd.uid
Normal file
1
scripts/item.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://p5327ibxtyfs
|
Loading…
Add table
Add a link
Reference in a new issue