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