feat: add proper death from starvation and survival tasks urgency

This commit is contained in:
Rijad Zuzo
2026-06-21 23:37:58 +02:00
parent eef2068747
commit b5e2493c2a
6 changed files with 165 additions and 20 deletions
+61 -12
View File
@@ -1,7 +1,7 @@
class_name SimNPC
extends RefCounted
@export var DEBUG_LOGS := true
const DEBUG_LOGS := true
const TASK_STATE_IDLE := "idle"
const TASK_STATE_TRAVELING := "traveling"
@@ -20,6 +20,9 @@ var task_state: String = TASK_STATE_IDLE
var position: Vector3
var is_starving := false
var starvation_ticks := 0
var starvation_death_threshold := 20
var is_dead := false
var task_duration := 0.0
var task_progress := 0.0
@@ -42,18 +45,13 @@ func _init(
position = Vector3(randf_range(-8.0, 8.0), 0.0, randf_range(-8.0, 8.0))
func simulate_tick(village: SimVillage) -> void:
hunger += 5.0 * village.food_modifier
energy -= 2.0
if is_dead:
return
hunger = clamp(hunger, 0.0, 100.0)
energy = clamp(energy, 0.0, 100.0)
update_needs(village)
is_starving = hunger >= 90.0
if is_starving:
energy -= 8.0
energy = clamp(energy, 0.0, 100.0)
if is_dead:
return
if task_state == TASK_STATE_IDLE or task_state == TASK_STATE_COMPLETE:
choose_new_task(village)
@@ -68,8 +66,56 @@ func simulate_tick(village: SimVillage) -> void:
if task_progress >= task_duration:
task_complete = true
task_state = TASK_STATE_COMPLETE
func update_needs(village: SimVillage) -> void:
hunger += 3.0 * village.food_modifier
match task_state:
TASK_STATE_IDLE:
energy -= 0.5
TASK_STATE_TRAVELING:
energy -= 2.0
TASK_STATE_WORKING:
if current_task != "rest":
energy -= 3.0
TASK_STATE_COMPLETE:
energy -= 0.25
hunger = clamp(hunger, 0.0, 100.0)
energy = clamp(energy, 0.0, 100.0)
is_starving = hunger >= 90.0
if is_starving:
starvation_ticks += 1
else:
starvation_ticks = 0
if starvation_ticks >= starvation_death_threshold:
die_from_starvation()
func die_from_starvation() -> void:
is_dead = true
current_task = "dead"
task_state = TASK_STATE_IDLE
task_progress = 0.0
task_duration = 0.0
task_complete = true
if DEBUG_LOGS:
print("[SimNPC] ", npc_name, " died from starvation.")
func choose_new_task(village: SimVillage) -> void:
if is_dead:
return
if is_starving:
if village.food > 0:
set_task("eat", 1.0)
else:
set_task("gather_food", 4.0)
return
if hunger > 75.0:
if village.food > 0:
set_task("eat", 2.0)
@@ -77,7 +123,7 @@ func choose_new_task(village: SimVillage) -> void:
set_task("gather_food", 5.0)
return
if energy < 30.0:
if energy < 25.0:
set_task("rest", 4.0)
return
@@ -94,6 +140,9 @@ func choose_new_task(village: SimVillage) -> void:
set_task("study", 6.0)
_:
set_task("wander", 4.0)
func should_eat_immediately_after_task() -> bool:
return current_task == "gather_food" and is_starving and not is_dead
func choose_best_work_task(village: SimVillage) -> String:
var food_score := calculate_task_score(