2025-05-01 17:47:42 +01:00
|
|
|
extends Unit
|
|
|
|
class_name Citizen
|
|
|
|
|
|
|
|
@onready var body: Sprite3D = $Body
|
|
|
|
@onready var hands: Sprite3D = $Body/Hands
|
|
|
|
|
2025-05-05 11:12:05 +01:00
|
|
|
var task: Task = null
|
|
|
|
var working: bool = false
|
|
|
|
|
|
|
|
enum WorkStatus { NONE, CONSTRUCTING_BUILDING }
|
|
|
|
var work_status: WorkStatus
|
|
|
|
var work_building: Building = null
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
held_item_meshinstance = $"Body/Hands/Held Item"
|
|
|
|
|
2025-05-01 17:47:42 +01:00
|
|
|
func _process(delta: float) -> void:
|
|
|
|
super(delta)
|
|
|
|
var camera: Camera3D
|
|
|
|
if Engine.is_editor_hint():
|
|
|
|
camera = EditorInterface.get_editor_viewport_3d(0).get_camera_3d()
|
|
|
|
else:
|
|
|
|
camera = get_viewport().get_camera_3d()
|
|
|
|
var facing_direction: Vector3 = -global_basis.z
|
|
|
|
var camera_local_facing: Vector3 = facing_direction * camera.global_basis
|
|
|
|
|
|
|
|
var facing_away: bool = camera_local_facing.z < 0
|
|
|
|
var facing_right: bool = camera_local_facing.x > 0
|
|
|
|
|
|
|
|
body.frame = 1 if facing_away else 0
|
2025-05-05 11:12:05 +01:00
|
|
|
#hands.frame = 1 if facing_away else 0
|
2025-05-01 17:47:42 +01:00
|
|
|
body.flip_h = facing_right
|
|
|
|
hands.flip_h = facing_right
|
2025-05-05 11:12:05 +01:00
|
|
|
|
|
|
|
if !working and task != null:
|
|
|
|
working = true
|
|
|
|
var ok: bool = await task.execute(self)
|
|
|
|
if !ok:
|
|
|
|
CitizenManager._instance.add_task(task)
|
|
|
|
task = null
|
|
|
|
working = false
|
|
|
|
|
|
|
|
if work_status == WorkStatus.CONSTRUCTING_BUILDING:
|
|
|
|
work_building.build_progress += delta
|
|
|
|
if work_building.build_state == Building.BuildState.READY:
|
|
|
|
send_task_update(TaskStatus.DONE)
|
|
|
|
elif work_building.build_state != Building.BuildState.BUILDING:
|
|
|
|
send_task_update(TaskStatus.IMPOSSIBLE)
|
|
|
|
|
|
|
|
func assign_task(t: Task) -> void:
|
|
|
|
task = t
|
|
|
|
|
|
|
|
func is_idle() -> bool:
|
|
|
|
return task == null
|
|
|
|
|
|
|
|
func put_item_in_building_materials(building: Building) -> bool:
|
|
|
|
building._add_build_material(held_item)
|
|
|
|
held_item = null
|
|
|
|
return true
|
|
|
|
|
|
|
|
func put_item_in_building_storage(building: Building) -> bool:
|
|
|
|
if building.consumer == null:
|
|
|
|
return false
|
|
|
|
if !building.consumer.offer_item(held_item):
|
|
|
|
return false
|
|
|
|
held_item = null
|
|
|
|
return true
|
|
|
|
|
|
|
|
func build_building(building: Building) -> bool:
|
|
|
|
work_building = building
|
|
|
|
work_status = WorkStatus.CONSTRUCTING_BUILDING
|
|
|
|
var ok: bool = await wait_for_task_update() == TaskStatus.DONE
|
|
|
|
work_building = null
|
|
|
|
work_status = WorkStatus.NONE
|
|
|
|
return ok
|