81 lines
2.4 KiB
GDScript
81 lines
2.4 KiB
GDScript
class_name PlayerCitizenSystem
|
|
extends RefCounted
|
|
|
|
var player_state := PlayerStateRecord.create_default()
|
|
var economy: RefCounted
|
|
var _record_event: Callable
|
|
|
|
|
|
func configure(economy_state: RefCounted, record_event: Callable) -> void:
|
|
economy = economy_state
|
|
_record_event = record_event
|
|
|
|
|
|
func advance_needs(food_modifier: float) -> void:
|
|
player_state.set_hunger(player_state.get_hunger() + 0.125 * food_modifier)
|
|
player_state.set_energy(maxf(player_state.get_energy() - 0.0625, 0.0))
|
|
|
|
|
|
func add_carried(item_id: StringName, amount: float) -> void:
|
|
player_state.add_inventory(item_id, amount)
|
|
|
|
|
|
func get_carried(item_id: StringName) -> float:
|
|
return player_state.get_inventory_amount(item_id)
|
|
|
|
|
|
func deposit(item_id: StringName, event_position: Vector3) -> float:
|
|
if item_id.is_empty():
|
|
return 0.0
|
|
var storage := _storage_for(item_id)
|
|
if storage == null:
|
|
return 0.0
|
|
var deposited := storage.deposit(item_id, player_state.get_inventory_amount(item_id))
|
|
player_state.remove_inventory(item_id, deposited)
|
|
economy.sync_resource(item_id)
|
|
if deposited > 0.0:
|
|
_record_event.call(
|
|
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
|
SimulationIds.PLAYER_INVENTORY_ID,
|
|
storage.get_storage_id(),
|
|
item_id,
|
|
deposited,
|
|
event_position
|
|
)
|
|
return deposited
|
|
|
|
|
|
func eat(event_position: Vector3) -> bool:
|
|
if player_state.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0:
|
|
player_state.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0)
|
|
_apply_meal(event_position, SimulationIds.PLAYER_INVENTORY_ID)
|
|
return true
|
|
var pantry := _storage_for(SimulationIds.RESOURCE_FOOD)
|
|
if pantry != null and pantry.get_amount(SimulationIds.RESOURCE_FOOD) > 0.0:
|
|
var withdrawn := pantry.withdraw(SimulationIds.RESOURCE_FOOD, 1.0)
|
|
economy.sync_resource(SimulationIds.RESOURCE_FOOD)
|
|
if withdrawn <= 0.0:
|
|
return false
|
|
_apply_meal(event_position, pantry.get_storage_id())
|
|
return true
|
|
return false
|
|
|
|
|
|
func _apply_meal(event_position: Vector3, source_id: StringName) -> void:
|
|
player_state.set_hunger(maxf(player_state.get_hunger() - 55.0, 0.0))
|
|
player_state.set_energy(minf(player_state.get_energy() + 10.0, 100.0))
|
|
_record_event.call(
|
|
SimulationIds.EVENT_ITEM_CONSUMED,
|
|
source_id,
|
|
&"consumed",
|
|
SimulationIds.RESOURCE_FOOD,
|
|
1.0,
|
|
event_position
|
|
)
|
|
|
|
|
|
func _storage_for(item_id: StringName) -> StorageStateRecord:
|
|
if economy == null or not economy.has_method("get_storage_for_resource"):
|
|
return null
|
|
return economy.get_storage_for_resource(item_id) as StorageStateRecord
|