added weighted random item drops based on player needs

This commit is contained in:
derek
2025-01-17 14:43:51 -06:00
parent cafcf57ef1
commit 5eb065830a
9 changed files with 83 additions and 31 deletions

View File

@@ -37,7 +37,7 @@ vec2 rotate_uv(vec2 uv, vec2 pivot, float rotation) {
uv -= pivot;
return vec2(
cosa * uv.x - sina * uv.y,
cosa * uv.y + sina * uv.x
cosa * uv.y + sina * uv.x
) + pivot;
}
@@ -47,13 +47,13 @@ vec2 rotate_uv(vec2 uv, vec2 pivot, float rotation) {
void fragment(){
vec2 polar_uv = polar_coordinates(rotate_uv(UV, vec2(0.5), floor(fract(TIME) * animation_speed) ) , vec2(0.5), 0.01, line_count);
vec3 lines = texture(noise, polar_uv).rgb;
float mask_value = length(UV - vec2(0.5));
float mask = inv_lerp(mask_size, mask_edge, mask_value);
float result = 1.0 - (mask * line_density);
result = smoothstep(result, result + line_faloff, lines.r);
COLOR.rgb = vec3(line_color.rgb);
COLOR.a = min(line_color.a, result);
}

View File

@@ -39,7 +39,7 @@ void fragment() {
ROUGHNESS = roughness_tex * roughness;
SPECULAR = specular;
NORMAL_MAP = texture(texture_normal,base_uv).rgb;
NORMAL_MAP_DEPTH = normal_scale;
}

View File

@@ -704,7 +704,7 @@ recoil_amount = Vector3(0.5, 0.2, 0.2)
spread = Vector3(90, 90, 1)
kick_amount = 10.0
max_ammo = 1
start_mags = 20
start_mags = 5
pellets_per_shot = 25
bullet_speed = 500.0
fire_pitch_scale_amt = 0.1

View File

@@ -13,3 +13,24 @@ func angle_velocity_aligned(source:Node, source_angle:Vector3, body:Node, max_an
return true
else:
return false
#pass in a dictornary of weighted random choices, returns id of selection
func weighted_random(choices):
var sum_of_choices = 0.0
#Get sum of all choices
for i in choices:
sum_of_choices += choices[i]
var random_number = randf_range(0,sum_of_choices)
print("----------------------------------------------------------------")
print("CHOICES: ",choices)
print("SUM OF CHOICES: ",sum_of_choices)
print("RANDOM NUMBER: ",random_number)
for i in choices:
if random_number < choices[i]:
print("SELECTION: ", i)
print("----------------------------------------------------------------")
return i
random_number -= choices[i]

View File

@@ -8,17 +8,15 @@ extends Node3D
@export var gun_1 : Resource
@export var gun_2 : Resource
@export_group("Drops")
@export var drop_chance_minimum = .1
@export var ammo_drop_enabled = true
@export var expected_ammo = {"light" : 200, "medium" : 50, "heavy" : 25,"shotgun" : 20, "rocket" : 3} #light, medium,heavy,shotgun,rocket
@export var stamina_drop_enabled = true
@export var health_drop_enabled = true
@export var money_drop_enabled = true
@onready var item_pickup = preload("res://assets/item_pickup.tscn")
@onready var crown = preload("res://assets/crown.tscn")
var ammo_drop = [[load("res://assets/ammo_pickup.tscn")],["ammo"]]
var stamina_drop = [[load("res://assets/stamina_pickup.tscn")],["stamina"]]
var health_drop = [[load("res://assets/health_pickup.tscn")],["health"]]
var money_drop = [[load("res://assets/money_pickup.tscn")],["money"]]
var dead_player = preload("res://assets/dead_cam.tscn")
const CLEARED_ANNOUNCE = preload("res://assets/cleared_announce.tscn")
const DEAD_ANNOUNCE = preload("res://assets/dead_announce.tscn")
@@ -73,15 +71,6 @@ func _ready():
#global randomize function
randomize()
#assign pickups to array
if ammo_drop_enabled == true:
pickups.append(ammo_drop)
if stamina_drop_enabled == true:
pickups.append(stamina_drop)
if health_drop_enabled == true:
pickups.append(health_drop)
if money_drop_enabled == true:
pickups.append(money_drop)
#clear spawned in objects
for node in get_tree().get_nodes_in_group("spawned"):
@@ -186,17 +175,60 @@ func die():
player.health_indicator.color = Color(0.471, 0, 0, 0)
func pickup_spawn():
##SET VARIABLES
var type = randi_range(0,3)
func pickup_spawn(randomized):
var pickup_type
var ammo_type
var value
if randomized:
#random item
pickup_type = randi_range(0,3)
#if item type is ammo, pick random ammo
if pickup_type == 0:
var player_ammo = player.ammo_reserve.keys()
ammo_type = int(player_ammo.pick_random())
#random value of pickup
value = randi_range(1,50)
else:
var health_weight = (1.0 - (health / start_health)) + drop_chance_minimum
var stamina_weight = (1.0 - (player.remaining_stamina / player.MAX_STAMINA)) + drop_chance_minimum
var money_weight = (1.0 - clamp(float(money) / float(500),0,1)) + drop_chance_minimum #fix this logic later once the economy makes sense
var ammo_weight = drop_chance_minimum
var ammo_type_weight = {}
# weight ammo player owns against expected ammo values
for i in player.ammo_reserve.keys():
var i_weight
match int(i):
0:
i_weight = 1.0 - clamp(float(player.ammo_reserve[str(i)]) / float(expected_ammo["light"]),0,1)
1:
i_weight = 1.0 - clamp(float(player.ammo_reserve[str(i)]) / float(expected_ammo["medium"]),0,1)
2:
i_weight = 1.0 - clamp(float(player.ammo_reserve[str(i)]) / float(expected_ammo["heavy"]),0,1)
3:
i_weight = 1.0 - clamp(float(player.ammo_reserve[str(i)]) / float(expected_ammo["shotgun"]),0,1)
4:
i_weight = 1.0 - clamp(float(player.ammo_reserve[str(i)]) / float(expected_ammo["rocket"]),0,1)
if i_weight > ammo_weight:
ammo_weight = i_weight + drop_chance_minimum
ammo_type_weight[i] = i_weight + drop_chance_minimum
pickup_type = HelperFuncs.weighted_random({"0" : ammo_weight, "1" : stamina_weight,"2" : health_weight,"3" : money_weight})
match pickup_type:
"0":
ammo_type = int(HelperFuncs.weighted_random(ammo_type_weight))
value = randi_range(1,20)
"1":
value = randi_range(int(player.MAX_STAMINA * .25),player.MAX_STAMINA)
"2":
value = randi_range(int(start_health * .25),start_health)
"3":
var bill_denoms = [5,10,20,50,100]
value = bill_denoms.pick_random()
if type == 0:
ammo_type = randi_range(0,4)
value = randi_range(1,10)
return {"pickup_type" : type,"ammo_type" : ammo_type,"value" : value}
return {"pickup_type" : pickup_type,"ammo_type" : ammo_type,"value" : value}
func save_quit():
SaveLoad.save_game_data(level_name)

View File

@@ -34,7 +34,7 @@ func _on_timer_timeout():
# Shoot that shit
var pickup_spawn = item_pickup.instantiate()
var item_stats = level_control.pickup_spawn()
var item_stats = level_control.pickup_spawn(false)
##SET VARIABLES
pickup_spawn.pickup_type = item_stats["pickup_type"]
pickup_spawn.ammo_type = item_stats["ammo_type"]

View File

@@ -36,7 +36,6 @@ func _ready():
add_to_group("persist")
#Enable mesh
match pickup_type:
0:
match ammo_type:

View File

@@ -651,7 +651,7 @@ func pickup_apply(type,ammo_type,value):
1: #STAMINA
remaining_stamina = clamp(remaining_stamina + value,0,100)
2: #HEALTH
level_control.health += value
level_control.health = clamp(level_control.health + value,0,level_control.start_health)
3: #MONEY
level_control.money += value

View File

@@ -208,7 +208,7 @@ func drop_loot(number_of_drops):
while number_of_drops > 0:
var pickup_spawn = level_control.item_pickup.instantiate()
var item_stats = level_control.pickup_spawn()
var item_stats = level_control.pickup_spawn(false)
##SET VARIABLES
pickup_spawn.pickup_type = item_stats["pickup_type"]