feat: let the player live as a citizen with needs and carried inventory

This commit is contained in:
Rijad Zuzo
2026-08-11 00:18:39 +02:00
parent f72aa659d2
commit 37f262fb84
20 changed files with 746 additions and 90 deletions
+62 -27
View File
@@ -1,5 +1,9 @@
extends Node
# gdlint: disable=max-file-lines
# The manager is the deliberate scene-tree facade for the simulation slice and
# intentionally keeps its orchestration readable in one authoritative file.
const SimulationEventLogScript := preload("res://simulation/events/SimulationEventLog.gd")
const VillageEconomyScript := preload("res://simulation/economy/VillageEconomy.gd")
const AnimalCareSystemScript := preload("res://simulation/animals/animal_care_system.gd")
@@ -25,6 +29,7 @@ signal opportunity_resolved(opportunity: OpportunityStateRecord, cause_event: Ec
signal opportunity_invalidated(opportunity: OpportunityStateRecord)
var village := SimVillage.new()
var player_system := PlayerCitizenSystem.new()
var npcs: Array[SimNPC] = []
@export var tick_interval := 1.2
@@ -89,6 +94,7 @@ func _ready() -> void:
village.debug_logs = debug_logs
economy.configure(village, debug_logs)
animal_care.configure(economy, active_world_adapter)
player_system.configure(economy, _record_player_event_at)
economy.initialize_storage()
village.update_modifiers()
village.update_priorities()
@@ -164,6 +170,7 @@ func simulate_tick() -> void:
if debug_logs:
print("--- Tick ", tick_count, " ---")
animal_care.advance(tick_count)
advance_player_needs()
var village_was_changed := false
_population_view.rebuild(npcs)
for npc in npcs:
@@ -984,9 +991,6 @@ func find_resource_node_for_player(from_position: Vector3, max_distance: float)
or not resource_state.can_extract()
):
continue
var storage := economy.get_storage_for_resource(resource_state.get_resource_id())
if storage == null or storage.get_available_capacity() <= 0.0:
continue
var distance := from_position.distance_squared_to(node.interaction_point.global_position)
if distance <= best_distance:
best_distance = distance
@@ -1000,16 +1004,12 @@ func harvest_resource_node(node: ResourceNode) -> float:
var resource_state := get_resource_state(node.node_id)
if resource_state == null or not resource_state.can_player_use_resource():
return 0.0
var storage := economy.get_storage_for_resource(resource_state.get_resource_id())
if storage == null:
return 0.0
var extracted := resource_state.extract(
minf(resource_state.get_yield_per_action(), storage.get_available_capacity())
)
var extracted := resource_state.extract(resource_state.get_yield_per_action())
if extracted <= 0.0:
return 0.0
var resource_id := resource_state.get_resource_id()
player_system.add_carried(resource_id, extracted)
var deposited := economy.deposit_resource(resource_state.get_resource_id(), extracted)
var event_position := (
node.interaction_point.global_position
if node.interaction_point != null
@@ -1017,25 +1017,17 @@ func harvest_resource_node(node: ResourceNode) -> float:
)
_record_economic_event_at(
SimulationIds.EVENT_RESOURCE_EXTRACTED,
-1,
SimulationIds.PLAYER_ACTOR_ID,
resource_state.get_node_id(),
(
SimulationIds.STORAGE_VILLAGE_PANTRY
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
else (
SimulationIds.STORAGE_VILLAGE_WOODPILE
if resource_state.get_resource_id() == SimulationIds.RESOURCE_WOOD
else &"village"
)
),
resource_state.get_resource_id(),
deposited,
SimulationIds.PLAYER_INVENTORY_ID,
resource_id,
extracted,
event_position
)
if resource_state.get_amount_remaining() <= 0.0:
_record_narrative_event_at(
SimulationIds.EVENT_RESOURCE_DEPLETED,
-1,
SimulationIds.PLAYER_ACTOR_ID,
resource_state.get_node_id(),
"",
event_position
@@ -1044,20 +1036,60 @@ func harvest_resource_node(node: ResourceNode) -> float:
if debug_logs:
print(
"[SimulationManager] Player extracted ",
"[SimulationManager] Player carried ",
extracted,
" ",
resource_state.get_resource_id(),
resource_id,
" from ",
resource_state.get_node_id()
)
return extracted
func advance_player_needs() -> void:
player_system.advance_needs(village.food_modifier)
func get_player_state() -> PlayerStateRecord:
return player_system.player_state
func deposit_player_inventory(item_id: StringName) -> float:
var storage := economy.get_storage_for_resource(item_id)
var storage_node := (
StorageNode.get_by_id(storage.get_storage_id()) as StorageNode if storage != null else null
)
var deposited := player_system.deposit(
item_id, storage_node.get_interaction_position() if storage_node != null else Vector3.ZERO
)
village_changed.emit(village)
return deposited
func eat_food(amount: float) -> void:
economy.withdraw_resource(SimulationIds.RESOURCE_FOOD, amount)
func eat_player_food() -> bool:
var succeeded := player_system.eat(Vector3.ZERO)
village_changed.emit(village)
return succeeded
func _record_player_event_at(
event_type: StringName,
source_id: StringName,
destination_id: StringName,
item_id: StringName,
amount: float,
world_position: Vector3
) -> void:
_record_economic_event_at(
event_type,
SimulationIds.PLAYER_ACTOR_ID,
source_id,
destination_id,
item_id,
amount,
world_position
)
func add_safety(amount: float) -> void:
@@ -1125,6 +1157,7 @@ func create_state_record() -> SimulationStateRecord:
record.event_knowledge.append(known_event)
for opportunity in opportunity_system.get_all_sorted():
record.opportunities.append(opportunity)
record.player = player_system.player_state
return record
@@ -1175,6 +1208,8 @@ func restore_state(record: SimulationStateRecord) -> bool:
resource_states[resource_record.get_node_id()] = resource_record
register_loaded_resource_nodes()
animal_care.restore_state_records(record.animals, tick_count)
if record.player != null:
player_system.player_state = record.player
village_changed.emit(village)
state_restored.emit()
return true