CyberTower/scripts/ui/reward_claim_screen.gd
2024-02-17 00:55:13 +08:00

58 lines
1.2 KiB
GDScript

extends Control
@export var card: PackedScene
@export var tower: Tower
@export var player: Player
@export var items = [
{
id = "QUICK_TRIGGER",
title = "Quick Trigger",
subtitle = "Save your finger",
description = "Reduce the delay between two shot.",
weight = 0.1,
}
]
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 = []
for idx in range(3):
items.append(pick_item())
for item in items:
var instance = card.instantiate()
instance.tower = tower
instance.player = player
instance.id = item["id"]
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