Added a simple loot table and set the base data in rover game. Also fixed a bad assert in item_generator

This commit is contained in:
akshay committed 2022-08-20 15:22:32 -04:00
1 parent 073e2f74d6
commit cbbfe694ed
4 files changed
+44 -12

No files matched your search

+1 -1
View File
@@ -30,7 +30,7 @@ func on_gen_timer_timeout():
assert(false, "gen timer should be disabled while broken")
func on_item_slot_item_changed(item):
assert(item == null, "item dump adds should be disabled while broken")
pass
func on_item_dump_item_dump_completed():
ctx.full_heal()
+22
View File
@@ -0,0 +1,22 @@
extends Reference
var item_map = {}
var total_weight = 0
func _init(items, weights):
item_map.clear()
# Calculate total weight and accumulate the weight for each item
for i in items.size():
total_weight += weights[i]
var item : Resource = items[i]
assert(item != null)
item_map[item] = 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
+15 -10
View File
@@ -3,17 +3,20 @@ extends Spatial
signal game_over
signal got_treasure(item_type)
const LootTable = preload("res://scripts/loot_table.gd")
const RockScene = preload("res://scenes/rock.tscn")
onready var timer : Timer = $Timer
export var play_time: float = 20.0
export (Array,Resource) var loot_items = []
export (Array,int) var loot_weights = []
const loot_table = {
#preload("res://")
}
var main: Node
var treasure := [] # is array of item_types
var loot_table = null
func _ready():
$Timer.start(play_time)
loot_table = LootTable.new(loot_items, loot_weights)
timer.start(play_time)
spawn_rocks(10)
func spawn_rocks(count: int):
@@ -21,17 +24,19 @@ func spawn_rocks(count: int):
locations.shuffle()
for i in range(min(count, locations.size())):
var location = locations[i]
var rock = preload("res://scenes/rock.tscn").instance()
var rock = RockScene.instance()
rock.treasure = loot_table.roll_for_loot()
assert(rock.treasure != null)
location.add_child(rock)
func _on_collection_area_area_entered(area):
if area.has_meta("owner"):
area = area.get_meta("owner")
emit_signal("got_treasure", area.treasure)
area.queue_free()
emit_signal("got_treasure", preload("res://item_types/ore.tres"))
func _process(delta):
$"%progress_bar".value = $Timer.time_left * 100.0 / $Timer.wait_time
$"%progress_bar".value = timer.time_left * 100.0 / timer.wait_time
func _on_Timer_timeout():
emit_signal("game_over")