CyberTower/scripts/ui/reward_claim_screen.gd

58 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-02-16 16:55:13 +00:00
extends Control
@export var card: PackedScene
@export var tower: Tower
@export var player: Player
2024-02-17 04:04:43 +00:00
@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)
2024-02-16 16:55:13 +00:00
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 = []
2024-02-17 04:04:43 +00:00
var count = randi_range(1, 3)
for idx in range(count):
2024-02-16 16:55:13 +00:00
items.append(pick_item())
for item in items:
var instance = card.instantiate()
instance.tower = tower
instance.player = player
instance.id = item["id"]
2024-02-17 04:04:43 +00:00
instance.value = item["value"]
2024-02-16 16:55:13 +00:00
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