The_Dark_Side_of_Earth/items/generic/item_spawn.gd

56 lines
1.8 KiB
GDScript3
Raw Normal View History

2025-09-17 12:17:32 +02:00
class_name ItemSpawn extends Node2D
2025-09-23 18:46:21 +02:00
static var item_pool = ResourceLoader.load("res://items/generic/item_pool.tres","",ResourceLoader.CACHE_MODE_IGNORE)
2025-09-17 12:17:32 +02:00
2025-09-23 12:20:33 +02:00
@export var rarity_bonus : float = 0
2025-09-17 12:17:32 +02:00
@export var guarantee_rare : bool = false
@export var unique_base_chance = 0.24
@export var rare_base_chance = 0
@export var unique_bonus_multiplier = .05
@export var rare_bonus_multiplier = 0
2025-09-17 12:17:32 +02:00
@export var spawn_petal = true
@export var petal_scene : PackedScene = ResourceLoader.load("res://vines_petals/petal.tscn")
var packed_item_scene : PackedScene
2025-10-11 12:47:24 +02:00
2025-09-17 12:17:32 +02:00
var remove_after_spawn = false
2025-10-14 11:57:01 +02:00
# Choose the item pool this spawn location draws from
2025-09-17 12:37:09 +02:00
func choose_pool() -> Array[PackedScene]:
spawn_petal = false
2025-09-17 12:17:32 +02:00
var unique_chance = unique_base_chance + unique_bonus_multiplier * rarity_bonus
2025-09-23 12:20:33 +02:00
var rare_chance = rare_base_chance + rare_bonus_multiplier * rarity_bonus
2025-09-17 12:17:32 +02:00
var random = randf()
2025-09-23 15:50:34 +02:00
if random < unique_chance && item_pool.unique.size() > 0:
2025-10-22 03:31:05 +02:00
# Unique items are removed from the pool when picked
2025-09-17 12:17:32 +02:00
remove_after_spawn = true
2025-09-23 15:50:34 +02:00
return item_pool.unique
2025-09-17 12:17:32 +02:00
elif random < unique_chance + rare_chance || guarantee_rare:
2025-09-23 15:50:34 +02:00
return item_pool.rare
return item_pool.common
2025-10-11 12:47:24 +02:00
2025-10-14 11:57:01 +02:00
# Places a petal holding this item
2025-10-11 12:47:24 +02:00
func instantiate_petal(item):
var petal : Petal = petal_scene.instantiate()
get_parent().call_deferred("add_child", petal)
petal.item = item
petal.global_position = global_position
2025-09-17 12:17:32 +02:00
func _ready():
2025-10-14 11:57:01 +02:00
# Pick a random pool and a random item from it, then remove it if unique.
2025-09-17 12:17:32 +02:00
var pool = choose_pool()
var index = randi_range(0, pool.size() - 1)
packed_item_scene = pool[index]
2025-09-17 14:05:11 +02:00
if remove_after_spawn:
2025-09-23 15:50:34 +02:00
item_pool.unique.remove_at(index)
2025-10-14 11:57:01 +02:00
# Place the item, possibly inside a petal
if packed_item_scene == null: return
var object = packed_item_scene.instantiate()
2025-09-17 12:37:09 +02:00
add_child.call_deferred(object)
2025-09-17 12:53:16 +02:00
object.reparent.call_deferred(get_parent())
2025-10-11 12:47:24 +02:00
if spawn_petal: instantiate_petal(object)