Añadidos drops, ahora cae cerveza o cafe
This commit is contained in:
1 parent
bbecc76dbe
commit
8bbf70e8cb
11 files changed
+282
-1
No files matched your search
@@ -0,0 +1,52 @@
|
||||
extends Node
|
||||
class_name DropManager
|
||||
|
||||
const PICKUP_SCENE := preload("res://pickups/pickup.tscn")
|
||||
|
||||
|
||||
static func roll_drop(drop_table: PickupDropTable) -> Array[PickupData]:
|
||||
var result: Array[PickupData] = []
|
||||
if drop_table == null:
|
||||
return result
|
||||
|
||||
if randf() > drop_table.drop_chance:
|
||||
return result
|
||||
|
||||
var available: Array[PickupDrop] = drop_table.drops.duplicate()
|
||||
available = available.filter(func(d): return d.pickup != null)
|
||||
|
||||
var count := randi_range(drop_table.min_drops, drop_table.max_drops)
|
||||
if count < 0:
|
||||
count = 0
|
||||
if count > available.size():
|
||||
count = available.size()
|
||||
|
||||
for i in range(count):
|
||||
if available.is_empty():
|
||||
break
|
||||
var total_weight: float = 0.0
|
||||
for d in available:
|
||||
total_weight += max(d.weight, 0.0)
|
||||
if total_weight <= 0.0:
|
||||
break
|
||||
var r := randf() * total_weight
|
||||
var selected: PickupDrop = null
|
||||
for d in available:
|
||||
r -= max(d.weight, 0.0)
|
||||
if r <= 0.0:
|
||||
selected = d
|
||||
break
|
||||
if selected == null:
|
||||
selected = available.back()
|
||||
result.append(selected.pickup)
|
||||
available.erase(selected)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
static func spawn_drops(drops_data: Array[PickupData], parent: Node, position: Vector2) -> void:
|
||||
for data in drops_data:
|
||||
var pickup := PICKUP_SCENE.instantiate() as Pickup
|
||||
pickup.pickup_data = data
|
||||
parent.add_child(pickup)
|
||||
pickup.global_position = position
|
||||
Reference in new issue
Block a user