feat: add completition of work only after work is done
This commit is contained in:
+28
-14
@@ -1,6 +1,11 @@
|
||||
class_name SimNPC
|
||||
extends RefCounted
|
||||
|
||||
const TASK_STATE_IDLE := "idle"
|
||||
const TASK_STATE_TRAVELING := "traveling"
|
||||
const TASK_STATE_WORKING := "working"
|
||||
const TASK_STATE_COMPLETE := "complete"
|
||||
|
||||
var id: int
|
||||
var npc_name: String
|
||||
var profession: String
|
||||
@@ -9,6 +14,7 @@ var energy: float
|
||||
var strength: float
|
||||
var intelligence: float
|
||||
var current_task: String = "idle"
|
||||
var task_state: String = TASK_STATE_IDLE
|
||||
var position: Vector3
|
||||
|
||||
var is_starving := false
|
||||
@@ -31,7 +37,6 @@ func _init(
|
||||
intelligence = _intelligence
|
||||
hunger = randf_range(20.0, 80.0)
|
||||
energy = randf_range(40.0, 100.0)
|
||||
|
||||
position = Vector3(randf_range(-8.0, 8.0), 0.0, randf_range(-8.0, 8.0))
|
||||
|
||||
func simulate_tick(village: SimVillage) -> void:
|
||||
@@ -48,30 +53,30 @@ func simulate_tick(village: SimVillage) -> void:
|
||||
|
||||
energy = clamp(energy, 0.0, 100.0)
|
||||
|
||||
if not task_complete:
|
||||
task_progress += 1.0
|
||||
if task_progress >= task_duration:
|
||||
task_complete = true
|
||||
if task_state == TASK_STATE_IDLE or task_state == TASK_STATE_COMPLETE:
|
||||
choose_new_task(village)
|
||||
return
|
||||
|
||||
choose_new_task(village)
|
||||
|
||||
|
||||
func choose_new_task(village: SimVillage) -> void:
|
||||
task_progress = 0.0
|
||||
task_complete = false
|
||||
if task_state == TASK_STATE_TRAVELING:
|
||||
return
|
||||
|
||||
if task_state == TASK_STATE_WORKING:
|
||||
task_progress += 1.0
|
||||
|
||||
if task_progress >= task_duration:
|
||||
task_complete = true
|
||||
task_state = TASK_STATE_COMPLETE
|
||||
|
||||
func choose_new_task(village: SimVillage) -> void:
|
||||
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:
|
||||
@@ -107,3 +112,12 @@ func set_task(task: String, duration: float) -> void:
|
||||
task_duration = duration
|
||||
task_progress = 0.0
|
||||
task_complete = false
|
||||
task_state = TASK_STATE_TRAVELING
|
||||
|
||||
func start_working() -> void:
|
||||
if task_state != TASK_STATE_TRAVELING:
|
||||
return
|
||||
|
||||
task_state = TASK_STATE_WORKING
|
||||
task_progress = 0.0
|
||||
task_complete = false
|
||||
|
||||
@@ -56,6 +56,7 @@ func simulate_tick() -> void:
|
||||
|
||||
for npc in npcs:
|
||||
var old_task := npc.current_task
|
||||
var old_state := npc.task_state
|
||||
var was_complete := npc.task_complete
|
||||
|
||||
npc.simulate_tick(village)
|
||||
@@ -67,6 +68,10 @@ func simulate_tick() -> void:
|
||||
village.apply_npc_task(npc)
|
||||
village_was_changed = true
|
||||
|
||||
npc.task_complete = true
|
||||
npc.task_state = SimNPC.TASK_STATE_IDLE
|
||||
npc.current_task = "idle"
|
||||
|
||||
if DEBUG_LOGS:
|
||||
print(
|
||||
npc.npc_name,
|
||||
@@ -74,6 +79,8 @@ func simulate_tick() -> void:
|
||||
npc.profession,
|
||||
" | task: ",
|
||||
npc.current_task,
|
||||
" | state: ",
|
||||
npc.task_state,
|
||||
" | progress: ",
|
||||
npc.task_progress,
|
||||
"/",
|
||||
@@ -86,11 +93,24 @@ func simulate_tick() -> void:
|
||||
round(npc.energy)
|
||||
)
|
||||
|
||||
if old_state != npc.task_state:
|
||||
print("[SimulationManager] State changed: ", npc.npc_name, " ", old_state, " -> ", npc.task_state)
|
||||
|
||||
if village_was_changed:
|
||||
village_changed.emit(village)
|
||||
|
||||
if DEBUG_LOGS:
|
||||
print(village.get_summary())
|
||||
|
||||
func notify_npc_arrived(npc_id: int) -> void:
|
||||
for npc in npcs:
|
||||
if npc.id == npc_id:
|
||||
if npc.task_state == SimNPC.TASK_STATE_TRAVELING:
|
||||
npc.start_working()
|
||||
|
||||
if DEBUG_LOGS:
|
||||
print("[SimulationManager] ", npc.npc_name, " arrived and started working on: ", npc.current_task)
|
||||
return
|
||||
|
||||
func eat_food(amount: float) -> void:
|
||||
village.food -= amount
|
||||
|
||||
Reference in New Issue
Block a user