CyberTower/scripts/ui/reward_claim_screen.gd
2024-02-17 12:04:43 +08:00

58 lines
1.3 KiB
GDScript

extends Control
@export var card: PackedScene
@export var tower: Tower
@export var player: Player
@export var behaviour_asset: Resource
var items = []
func _ready():
var file = FileAccess.open(behaviour_asset.resource_path, FileAccess.READ)
var content = file.get_as_text()
items = JSON.parse_string(content)
func pick_item() -> Dictionary:
var total_weight: float = 0.0
for choice in items:
total_weight += choice["weight"]
var random_value: float = randf() * total_weight
var cumulative_weight: float = 0.0
for choice in items:
cumulative_weight += choice["weight"]
if random_value <= cumulative_weight:
return choice
return items.back()
func roll():
for child in $Panel/RewardContainer.get_children():
child.queue_free()
var items = []
var count = randi_range(1, 3)
for idx in range(count):
items.append(pick_item())
for item in items:
var instance = card.instantiate()
instance.tower = tower
instance.player = player
instance.id = item["id"]
instance.value = item["value"]
instance.title = item["title"]
instance.subtitle = item["subtitle"]
instance.description = item["description"]
instance.connect("claimed", _hide)
$Panel/RewardContainer.add_child(instance)
func _show():
roll()
get_tree().paused = true
visible = true
func _hide():
get_tree().paused = false
visible = false