feat: give the player a real carried inventory and deposit transactions
This commit is contained in:
@@ -7,6 +7,7 @@ const STACK_THRESHOLD := 90.0
|
||||
const DEATH_STANDING_PENALTY := 0.5
|
||||
const RESET_HUNGER := 20.0
|
||||
const RESET_ENERGY := 70.0
|
||||
const DEFAULT_CARRY_CAPACITY := 4.0
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
@@ -28,6 +29,8 @@ static func create() -> PlayerStateRecord:
|
||||
"starvation_death_threshold": 600,
|
||||
"is_dead": false,
|
||||
"deaths": 0,
|
||||
"inventory": {},
|
||||
"carry_capacity": DEFAULT_CARRY_CAPACITY,
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -68,6 +71,28 @@ static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord:
|
||||
or deaths < 0
|
||||
):
|
||||
return null
|
||||
var inventory := {}
|
||||
var carry_capacity := DEFAULT_CARRY_CAPACITY
|
||||
if record_data.has("inventory"):
|
||||
if not record_data["inventory"] is Dictionary:
|
||||
return null
|
||||
var normalized := {}
|
||||
for raw_item_id in record_data["inventory"]:
|
||||
var item_id := String(raw_item_id)
|
||||
var amount := float(record_data["inventory"][raw_item_id])
|
||||
if (
|
||||
item_id.is_empty()
|
||||
or normalized.has(item_id)
|
||||
or not is_finite(amount)
|
||||
or amount < 0.0
|
||||
):
|
||||
return null
|
||||
normalized[item_id] = amount
|
||||
inventory = normalized
|
||||
if record_data.has("carry_capacity"):
|
||||
carry_capacity = float(record_data["carry_capacity"])
|
||||
if not is_finite(carry_capacity) or carry_capacity < 0.0:
|
||||
return null
|
||||
return (
|
||||
PlayerStateRecord
|
||||
. new(
|
||||
@@ -80,6 +105,8 @@ static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord:
|
||||
"starvation_death_threshold": death_threshold,
|
||||
"is_dead": bool(record_data["is_dead"]),
|
||||
"deaths": deaths,
|
||||
"inventory": inventory,
|
||||
"carry_capacity": carry_capacity,
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -109,6 +136,47 @@ func get_deaths() -> int:
|
||||
return int(data["deaths"])
|
||||
|
||||
|
||||
func get_inventory_amount(item_id: StringName) -> float:
|
||||
return float(data["inventory"].get(String(item_id), 0.0))
|
||||
|
||||
|
||||
func get_carry_capacity() -> float:
|
||||
return float(data["carry_capacity"])
|
||||
|
||||
|
||||
func get_carried_total() -> float:
|
||||
var total := 0.0
|
||||
for amount in data["inventory"].values():
|
||||
total += float(amount)
|
||||
return total
|
||||
|
||||
|
||||
func get_available_carry() -> float:
|
||||
return maxf(get_carry_capacity() - get_carried_total(), 0.0)
|
||||
|
||||
|
||||
func add_inventory(item_id: StringName, amount: float) -> float:
|
||||
if item_id.is_empty() or not is_finite(amount) or amount <= 0.0 or is_dead():
|
||||
return 0.0
|
||||
var accepted := minf(amount, get_available_carry())
|
||||
if accepted <= 0.0:
|
||||
return 0.0
|
||||
data["inventory"][String(item_id)] = get_inventory_amount(item_id) + accepted
|
||||
return accepted
|
||||
|
||||
|
||||
func remove_inventory(item_id: StringName, requested_amount: float) -> float:
|
||||
if item_id.is_empty() or not is_finite(requested_amount) or requested_amount <= 0.0:
|
||||
return 0.0
|
||||
var removed := minf(requested_amount, get_inventory_amount(item_id))
|
||||
if removed <= 0.0:
|
||||
return 0.0
|
||||
data["inventory"][String(item_id)] = get_inventory_amount(item_id) - removed
|
||||
if is_equal_approx(get_inventory_amount(item_id), 0.0):
|
||||
data["inventory"].erase(String(item_id))
|
||||
return removed
|
||||
|
||||
|
||||
func eat(amount: float) -> float:
|
||||
if amount <= 0.0 or is_dead():
|
||||
return 0.0
|
||||
|
||||
@@ -772,6 +772,8 @@ static func _is_valid_supply_resolution(
|
||||
):
|
||||
return false
|
||||
if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED:
|
||||
if actor_id < 0:
|
||||
return StringName(event.data["source_id"]) == &"player_carry"
|
||||
return (
|
||||
npc_ids.has(actor_id)
|
||||
and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id)
|
||||
|
||||
Reference in New Issue
Block a user