24 lines
557 B
GDScript
24 lines
557 B
GDScript
extends Reference
|
|
|
|
class_name SimpleLootTable
|
|
|
|
var item_map = {}
|
|
var total_weight = 0
|
|
|
|
func _init(in_item_map : Dictionary):
|
|
item_map.clear()
|
|
total_weight = 0
|
|
# Calculate total weight and accumulate the weight for each item
|
|
for key in in_item_map.keys():
|
|
total_weight += in_item_map[key]
|
|
item_map[key] = total_weight
|
|
|
|
func roll_for_loot() -> Resource:
|
|
randomize()
|
|
var rng = randi() % total_weight
|
|
for item in item_map.keys():
|
|
# if the RNG is <= item cumulated weight then drop that item
|
|
if rng <= item_map[item]:
|
|
return item
|
|
return null
|