feat: add task duration and task commitment
This commit is contained in:
+45
-23
@@ -13,6 +13,10 @@ var position: Vector3
|
||||
|
||||
var is_starving := false
|
||||
|
||||
var task_duration := 0.0
|
||||
var task_progress := 0.0
|
||||
var task_complete := true
|
||||
|
||||
func _init(
|
||||
_id: int,
|
||||
_npc_name: String,
|
||||
@@ -37,51 +41,69 @@ func simulate_tick(village: SimVillage) -> void:
|
||||
hunger = clamp(hunger, 0.0, 100.0)
|
||||
energy = clamp(energy, 0.0, 100.0)
|
||||
|
||||
if hunger > 75.0:
|
||||
if village.food > 0:
|
||||
current_task = "eat"
|
||||
hunger -= 25.0
|
||||
else:
|
||||
current_task = "gather_food"
|
||||
return
|
||||
|
||||
if energy < 30.0:
|
||||
current_task = "rest"
|
||||
energy += 20.0
|
||||
return
|
||||
|
||||
is_starving = hunger >= 90.0
|
||||
|
||||
if is_starving:
|
||||
energy -= 8.0
|
||||
|
||||
hunger = clamp(hunger, 0.0, 100.0)
|
||||
energy = clamp(energy, 0.0, 100.0)
|
||||
|
||||
if not task_complete:
|
||||
task_progress += 1.0
|
||||
if task_progress >= task_duration:
|
||||
task_complete = true
|
||||
return
|
||||
|
||||
choose_new_task(village)
|
||||
|
||||
|
||||
func choose_new_task(village: SimVillage) -> void:
|
||||
task_progress = 0.0
|
||||
task_complete = false
|
||||
|
||||
if hunger > 75.0:
|
||||
if village.food > 0:
|
||||
set_task("eat", 2.0)
|
||||
hunger -= 25.0
|
||||
else:
|
||||
set_task("gather_food", 5.0)
|
||||
return
|
||||
|
||||
if energy < 30.0:
|
||||
set_task("rest", 4.0)
|
||||
energy += 20.0
|
||||
return
|
||||
|
||||
if village.food < 15.0:
|
||||
current_task = "gather_food"
|
||||
set_task("gather_food", 5.0)
|
||||
return
|
||||
|
||||
if village.wood < 10.0:
|
||||
current_task = "gather_wood"
|
||||
set_task("gather_wood", 5.0)
|
||||
return
|
||||
|
||||
if village.safety < 40.0 and profession == "guard":
|
||||
current_task = "patrol"
|
||||
set_task("patrol", 6.0)
|
||||
return
|
||||
|
||||
if village.knowledge < 10.0 and profession == "scholar":
|
||||
current_task = "study"
|
||||
set_task("study", 6.0)
|
||||
return
|
||||
|
||||
match profession:
|
||||
"farmer":
|
||||
current_task = "gather_food"
|
||||
set_task("gather_food", 5.0)
|
||||
"woodcutter":
|
||||
current_task = "gather_wood"
|
||||
set_task("gather_wood", 5.0)
|
||||
"guard":
|
||||
current_task = "patrol"
|
||||
set_task("patrol", 6.0)
|
||||
"scholar":
|
||||
current_task = "study"
|
||||
set_task("study", 6.0)
|
||||
_:
|
||||
current_task = "wander"
|
||||
set_task("wander", 4.0)
|
||||
|
||||
func set_task(task: String, duration: float) -> void:
|
||||
current_task = task
|
||||
task_duration = duration
|
||||
task_progress = 0.0
|
||||
task_complete = false
|
||||
|
||||
@@ -52,13 +52,21 @@ func simulate_tick() -> void:
|
||||
tick_count += 1
|
||||
print("--- Tick ", tick_count, " ---")
|
||||
|
||||
var village_was_changed := false
|
||||
|
||||
for npc in npcs:
|
||||
var old_task := npc.current_task
|
||||
var was_complete := npc.task_complete
|
||||
|
||||
npc.simulate_tick(village)
|
||||
|
||||
if old_task != npc.current_task:
|
||||
npc_task_changed.emit(npc, old_task, npc.current_task)
|
||||
|
||||
if not was_complete and npc.task_complete:
|
||||
village.apply_npc_task(npc)
|
||||
village_was_changed = true
|
||||
|
||||
if DEBUG_LOGS:
|
||||
print(
|
||||
npc.npc_name,
|
||||
@@ -66,15 +74,21 @@ func simulate_tick() -> void:
|
||||
npc.profession,
|
||||
" | task: ",
|
||||
npc.current_task,
|
||||
" | progress: ",
|
||||
npc.task_progress,
|
||||
"/",
|
||||
npc.task_duration,
|
||||
" | complete: ",
|
||||
npc.task_complete,
|
||||
" | hunger: ",
|
||||
round(npc.hunger),
|
||||
" | energy: ",
|
||||
round(npc.energy)
|
||||
)
|
||||
|
||||
for npc in npcs:
|
||||
village.apply_npc_task(npc)
|
||||
if village_was_changed:
|
||||
village_changed.emit(village)
|
||||
|
||||
if DEBUG_LOGS:
|
||||
print(village.get_summary())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user