124 lines
2.9 KiB
GDScript
124 lines
2.9 KiB
GDScript
extends Node3D
|
|
|
|
class_name Actor
|
|
|
|
enum ACTOR_STATE {INACTIVE, ACTIVE, IDLE, PAUSED}
|
|
|
|
var actor_state = null: set = state_change
|
|
|
|
const red = preload("res://tiles/base_tile/red.tres")
|
|
const blue = preload("res://tiles/base_tile/lightblue.tres")
|
|
|
|
var current_place_path: Array
|
|
var current_room_path: Array
|
|
|
|
var temp_speed = 4
|
|
var time = 0
|
|
|
|
var current_tile = null: set = change_current_tile
|
|
var current_room = null
|
|
|
|
var lookup_actor_to_task = []
|
|
var current_task: Dictionary : set = task_change
|
|
|
|
@onready var task_creator: TaskCreator = $TaskCreator
|
|
|
|
signal current_tile_request
|
|
|
|
func _process(delta: float) -> void:
|
|
match actor_state:
|
|
ACTOR_STATE.ACTIVE:
|
|
get_current_tile()
|
|
|
|
if current_room_path:
|
|
|
|
if current_tile.grid_pos == current_task.location:
|
|
temp_complete_task()
|
|
|
|
else:
|
|
if self.position.distance_to(current_room_path[0]) > .1:
|
|
position += position.direction_to(Vector3(current_room_path[0])) * 6 * delta
|
|
else:
|
|
current_room_path.pop_front()
|
|
|
|
ACTOR_STATE.IDLE:
|
|
|
|
if not lookup_actor_to_task.is_empty():
|
|
current_task = lookup_actor_to_task[0]
|
|
else:
|
|
actor_state = ACTOR_STATE.PAUSED
|
|
|
|
ACTOR_STATE.PAUSED:
|
|
pass
|
|
|
|
func state_change(state):
|
|
actor_state = state
|
|
match state:
|
|
ACTOR_STATE.ACTIVE:
|
|
$SelfMesh.set_material_override(red)
|
|
ACTOR_STATE.IDLE:
|
|
$SelfMesh.set_material_override(null)
|
|
ACTOR_STATE.PAUSED:
|
|
$SelfMesh.set_material_override(blue)
|
|
await get_tree().create_timer(1).timeout
|
|
temp_create_task()
|
|
actor_state = ACTOR_STATE.IDLE
|
|
|
|
func task_change(task):
|
|
current_task = task
|
|
if current_task:
|
|
actor_state = ACTOR_STATE.ACTIVE
|
|
get_room_path()
|
|
|
|
func get_current_tile():
|
|
#Requests the tile that this actor is currently on
|
|
|
|
emit_signal("current_tile_request", self)
|
|
|
|
func change_current_tile(new_tile):
|
|
#General function for when this actor enters a new tile
|
|
|
|
if current_tile != new_tile:
|
|
current_tile = new_tile
|
|
get_room()
|
|
|
|
func get_room():
|
|
#Gets the room that the current tile is in
|
|
#TODO: can't handle tiles being in more than one room
|
|
|
|
if current_tile.lookup_tile_to_room:
|
|
current_room = current_tile.lookup_tile_to_room
|
|
else:
|
|
current_room = null
|
|
|
|
func temp_create_task():
|
|
var temp_task_target = Vector3i(randi_range(1, 31), 0, randi_range(1, 31))
|
|
task_creator.create_task(
|
|
self,
|
|
self,
|
|
temp_task_target,
|
|
)
|
|
|
|
func temp_complete_task():
|
|
if lookup_actor_to_task:
|
|
var completed_task = lookup_actor_to_task.pop_front()
|
|
current_task = {}
|
|
task_creator.complete_task(self, completed_task)
|
|
actor_state = ACTOR_STATE.IDLE
|
|
|
|
func get_room_path():
|
|
get_current_tile()
|
|
var start = current_tile.grid_pos
|
|
var end = current_task.location
|
|
|
|
if destination_in_current_room(end):
|
|
current_room_path = current_room.get_room_path(start, end)
|
|
else:
|
|
temp_complete_task()
|
|
|
|
func destination_in_current_room(destination):
|
|
for i in current_room.room_to_tile:
|
|
if i.grid_pos == destination:
|
|
return true
|
|
return false
|