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 2022-08-20 15:22:32 -04:00
parent 073e2f74d6
commit cbbfe694ed
4 changed files with 44 additions and 12 deletions

View File

@ -1,9 +1,12 @@
[gd_scene load_steps=8 format=2]
[gd_scene load_steps=11 format=2]
[ext_resource path="res://scenes/rover.tscn" type="PackedScene" id=1]
[ext_resource path="res://item_types/crystal.tres" type="Resource" id=2]
[ext_resource path="res://item_types/gem.tres" type="Resource" id=3]
[ext_resource path="res://assets/rover_level/RoverGame.glb" type="PackedScene" id=4]
[ext_resource path="res://scripts/rover_camera.gd" type="Script" id=5]
[ext_resource path="res://scripts/rover_game.gd" type="Script" id=6]
[ext_resource path="res://item_types/geode.tres" type="Resource" id=7]
[sub_resource type="BoxShape" id=7]
extents = Vector3( 21.7976, 4.38074, 5.34608 )
@ -23,6 +26,8 @@ extents = Vector3( 100, 1, 400 )
[node name="rover_game" type="Spatial"]
script = ExtResource( 6 )
loot_items = [ ExtResource( 7 ), ExtResource( 2 ), ExtResource( 3 ) ]
loot_weights = [ 60, 25, 15 ]
[node name="RoverGame" parent="." instance=ExtResource( 4 )]
__meta__ = {

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
scripts/loot_table.gd Normal file
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

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")