The_Dark_Side_of_Earth/items/generic/item_spawn.gd

54 lines
1.8 KiB
GDScript

class_name ItemSpawn extends Node2D
static var item_pool = ResourceLoader.load("res://items/generic/item_pool.tres","",ResourceLoader.CACHE_MODE_IGNORE)
@export var rarity_bonus : float = 0
@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
@export var spawn_petal = true
@export var petal_scene : PackedScene = ResourceLoader.load("res://petal.tscn")
var remove_after_spawn = false
# Choose the item pool this spawn location draws from
func choose_pool() -> Array[PackedScene]:
var unique_chance = unique_base_chance + unique_bonus_multiplier * rarity_bonus
var rare_chance = rare_base_chance + rare_bonus_multiplier * rarity_bonus
var random = randf()
if random < unique_chance && item_pool.unique.size() > 0:
# Unique items are removed from the pool when picked
remove_after_spawn = true
return item_pool.unique
elif random < unique_chance + rare_chance || guarantee_rare:
return item_pool.rare
# If an item is common, no petal spawns
spawn_petal = false
return item_pool.common
# Places a petal holding this item
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
func _ready():
# Pick a random pool and a random item from it, then remove it if unique.
var pool = choose_pool()
var index = randi_range(0, pool.size() - 1)
var packed_scene : PackedScene = pool[index]
if remove_after_spawn:
item_pool.unique.remove_at(index)
# Place the item, possibly inside a petal
var object = packed_scene.instantiate()
add_child.call_deferred(object)
object.reparent.call_deferred(get_parent())
if spawn_petal: instantiate_petal(object)