102 lines
2.6 KiB
GDScript
102 lines
2.6 KiB
GDScript
extends Interaction
|
|
|
|
@export var scavengeDifficulty:int = 5
|
|
@export var dropItemClasses:Array[Resource]
|
|
@export var dropItemProbabilities:Array[int]
|
|
@export var xpRewards:Array[int]
|
|
@export var dropItemLocationOffsets:Array[Vector2]
|
|
|
|
var groundItemScene = preload("res://Objects/GroundItems/GroundItem.tscn")
|
|
|
|
var nextDropLocationIndex = 0
|
|
var dropItems:Array[Item]
|
|
|
|
var itemsDropped = []
|
|
var scavengeProgress = 0
|
|
|
|
var scavenging = false
|
|
|
|
var rng = RandomNumberGenerator.new()
|
|
|
|
func _ready():
|
|
add_action("Scavenge", scavenge)
|
|
initialize_items()
|
|
initialize_dropped_items_array()
|
|
|
|
$ScavengeBar.max_value = scavengeDifficulty
|
|
|
|
GameVariables.player.moved.connect(stop_scavenging)
|
|
|
|
func scavenge():
|
|
if itemsDropped == []:
|
|
return
|
|
scavenging = true
|
|
$ScavengeBar.visible = true
|
|
$ScavengeBar.global_position = GameVariables.player.global_position + Vector2(-70, -120)
|
|
|
|
func _process(delta):
|
|
if scavenging:
|
|
progress_scavenging(delta)
|
|
|
|
func stop_scavenging():
|
|
scavenging = false
|
|
scavengeProgress = 0
|
|
$ScavengeBar.visible = false
|
|
|
|
func progress_scavenging(delta):
|
|
var scavengeSpeed = LevelManager.get_skill("scavenging").get_scavenging_speed(scavengeDifficulty)
|
|
scavengeProgress += scavengeSpeed * delta
|
|
|
|
if scavengeProgress >= scavengeDifficulty:
|
|
scavengeProgress = 0
|
|
item_scavenged()
|
|
|
|
update_scavenge_bar()
|
|
|
|
func update_scavenge_bar():
|
|
$ScavengeBar.value = scavengeProgress
|
|
|
|
func item_scavenged():
|
|
var totalProbabilities = 0
|
|
for probability in dropItemProbabilities:
|
|
totalProbabilities += probability
|
|
|
|
var itemResult = randi_range(1, totalProbabilities)
|
|
var percentageChecked = 0
|
|
for i in range(dropItemProbabilities.size()):
|
|
var currentChance = dropItemProbabilities[i]
|
|
percentageChecked += currentChance
|
|
if itemResult <= percentageChecked:
|
|
spawn_item(dropItems[i])
|
|
award_xp(i)
|
|
break
|
|
|
|
func award_xp(itemIndex):
|
|
LevelManager.add_XP("scavenging", xpRewards[itemIndex])
|
|
|
|
func spawn_item(item:Item):
|
|
var itemInSpot = itemsDropped[nextDropLocationIndex]
|
|
if is_instance_valid(itemInSpot) and itemInSpot != null:
|
|
itemInSpot.queue_free()
|
|
|
|
var newGroundItem = groundItemScene.instantiate()
|
|
newGroundItem.set_item(item.duplicate())
|
|
|
|
newGroundItem.position = dropItemLocationOffsets[nextDropLocationIndex]
|
|
get_parent().add_child(newGroundItem)
|
|
|
|
itemsDropped[nextDropLocationIndex] = newGroundItem
|
|
|
|
nextDropLocationIndex += 1
|
|
if nextDropLocationIndex >= dropItemLocationOffsets.size():
|
|
nextDropLocationIndex = 0
|
|
get_tree().call_group("FlagManager", "Scavanged")
|
|
|
|
func initialize_items():
|
|
for script in dropItemClasses:
|
|
dropItems.append(script.new())
|
|
|
|
func initialize_dropped_items_array():
|
|
for i in dropItemLocationOffsets:
|
|
itemsDropped.append(null)
|