Vast improvements and a working finite state machine, working on new guild member registration.
This commit is contained in:
39
fsm/interact_with_employee.gd
Normal file
39
fsm/interact_with_employee.gd
Normal file
@@ -0,0 +1,39 @@
|
||||
extends StateNode
|
||||
|
||||
@export var employee_name : String = ""
|
||||
@export var speech_bubble : String = ""
|
||||
@export var wait_duration : float = 1
|
||||
@export var interaction_args : Array[String] = []
|
||||
var wait_remaining : float = 0
|
||||
|
||||
var employee : GuildEmployee
|
||||
var actor
|
||||
|
||||
func exit() -> void:
|
||||
actor.show_speech_bubble("")
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if wait_remaining > 0:
|
||||
wait_remaining -= delta
|
||||
if wait_remaining <= 0:
|
||||
wait_remaining = 0
|
||||
employee.interact(self, interaction_args[0])
|
||||
complete_state()
|
||||
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
wait_remaining = wait_duration
|
||||
actor = subject
|
||||
|
||||
if len(args) > 0 and args[0] != "":
|
||||
subject.show_speech_bubble(speech_bubble)
|
||||
|
||||
#TODO: Possibly an error later with preexisting and extended lists
|
||||
for arg in args.slice(1):
|
||||
interaction_args.append(arg)
|
||||
var emp = Guild.hall.employees.get(employee_name)
|
||||
|
||||
if emp == null:
|
||||
printerr("Employee %s not found!" % employee_name)
|
||||
complete_state()
|
||||
else:
|
||||
employee = emp
|
||||
1
fsm/interact_with_employee.gd.uid
Normal file
1
fsm/interact_with_employee.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bewrajxqdutsu
|
||||
27
fsm/machines/newbie.gd
Normal file
27
fsm/machines/newbie.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends StateMachine
|
||||
|
||||
|
||||
func advance_state() -> void:
|
||||
var last_state = curr_state
|
||||
var args : Array = [actor]
|
||||
exit_state(curr_state)
|
||||
match(last_state.name):
|
||||
"Wait":
|
||||
if last_state.next_state != "":
|
||||
if !states.has(last_state.next_state):
|
||||
printerr("Tried to switch to missing state %s" % last_state.next_state)
|
||||
pass
|
||||
enter_state(states[last_state.next_state])
|
||||
args.append_array(last_state.next_state_args)
|
||||
else:
|
||||
pass
|
||||
"Queue":
|
||||
enter_state(states["Wait"])
|
||||
args.append_array(["busy", "Register"])
|
||||
"Register":
|
||||
enter_state(states["Register"])
|
||||
"Leave":
|
||||
enter_state(states["Leave"])
|
||||
|
||||
if curr_state != null:
|
||||
curr_state.execute.callv(args)
|
||||
1
fsm/machines/newbie.gd.uid
Normal file
1
fsm/machines/newbie.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dthatmb3if73u
|
||||
23
fsm/machines/receptionist.gd
Normal file
23
fsm/machines/receptionist.gd
Normal file
@@ -0,0 +1,23 @@
|
||||
extends StateMachine
|
||||
|
||||
|
||||
func advance_state() -> void:
|
||||
var last_state = curr_state
|
||||
var args : Array = [actor]
|
||||
exit_state(curr_state)
|
||||
match(last_state.name):
|
||||
"Wait":
|
||||
if last_state.next_state != "":
|
||||
if !states.has(last_state.next_state):
|
||||
printerr("Tried to switch to missing state %s" % last_state.next_state)
|
||||
pass
|
||||
enter_state(states[last_state.next_state])
|
||||
args.append_array(last_state.next_state_args)
|
||||
else:
|
||||
pass
|
||||
"Advance Queue":
|
||||
enter_state(states["Wait"])
|
||||
args.append_array(["", "Advance Queue"])
|
||||
|
||||
if curr_state != null:
|
||||
curr_state.execute.callv(args)
|
||||
1
fsm/machines/receptionist.gd.uid
Normal file
1
fsm/machines/receptionist.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cggu0yihq0unt
|
||||
43
fsm/machines/state_machine.gd
Normal file
43
fsm/machines/state_machine.gd
Normal file
@@ -0,0 +1,43 @@
|
||||
class_name StateMachine extends Node
|
||||
|
||||
@export var starting_state : String = ""
|
||||
var states : Dictionary[String, StateNode] = {}
|
||||
var curr_state : StateNode = null
|
||||
var actor = null
|
||||
func _ready() -> void:
|
||||
for child in get_children():
|
||||
if child is StateNode:
|
||||
states[child.name] = child
|
||||
child.state_machine = self
|
||||
child.completed.connect(_on_state_completed)
|
||||
|
||||
|
||||
func start() -> void:
|
||||
if starting_state != "":
|
||||
var state : StateNode = states.get(starting_state)
|
||||
if state == null:
|
||||
printerr("Starting state not found! Expected %s" % [starting_state])
|
||||
else:
|
||||
enter_state(state)
|
||||
curr_state.execute(actor)
|
||||
|
||||
func exit_state(state : StateNode) -> void:
|
||||
curr_state.exit()
|
||||
curr_state = null
|
||||
pass
|
||||
|
||||
func enter_state(state : StateNode) -> void:
|
||||
curr_state = state
|
||||
curr_state.enter()
|
||||
pass
|
||||
|
||||
|
||||
func advance_state() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_state_completed(state : StateNode) -> void:
|
||||
if state == curr_state:
|
||||
advance_state()
|
||||
else:
|
||||
printerr("Wrong state completed! %s when expecting %s" % [state.name, curr_state])
|
||||
1
fsm/machines/state_machine.gd.uid
Normal file
1
fsm/machines/state_machine.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://u81fhi8157bg
|
||||
22
fsm/machines/test.gd
Normal file
22
fsm/machines/test.gd
Normal file
@@ -0,0 +1,22 @@
|
||||
extends StateMachine
|
||||
|
||||
func advance_state() -> void:
|
||||
var last_state = curr_state
|
||||
var args : Array = [actor]
|
||||
exit_state(curr_state)
|
||||
match(last_state.name):
|
||||
"Wait":
|
||||
if last_state.next_state != "":
|
||||
if !states.has(last_state.next_state):
|
||||
printerr("Tried to switch to missing state %s" % last_state.next_state)
|
||||
pass
|
||||
enter_state(states[last_state.next_state])
|
||||
args.append_array(last_state.next_state_args)
|
||||
else:
|
||||
pass
|
||||
"Test":
|
||||
enter_state(states["Wait"])
|
||||
args.append_array(["busy", "Test"])
|
||||
|
||||
if curr_state != null:
|
||||
curr_state.execute.callv(args)
|
||||
1
fsm/machines/test.gd.uid
Normal file
1
fsm/machines/test.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://djd8pv5xbgud3
|
||||
6
fsm/nodes/advance_queue.gd
Normal file
6
fsm/nodes/advance_queue.gd
Normal file
@@ -0,0 +1,6 @@
|
||||
extends StateNode
|
||||
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
if !subject.busy:
|
||||
subject.queue.try_advance()
|
||||
complete_state()
|
||||
1
fsm/nodes/advance_queue.gd.uid
Normal file
1
fsm/nodes/advance_queue.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://csicx3fpxv7xt
|
||||
1
fsm/nodes/leave.gd
Normal file
1
fsm/nodes/leave.gd
Normal file
@@ -0,0 +1 @@
|
||||
extends StateNode
|
||||
1
fsm/nodes/leave.gd.uid
Normal file
1
fsm/nodes/leave.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b0ewnwcibhu21
|
||||
51
fsm/nodes/queue.gd
Normal file
51
fsm/nodes/queue.gd
Normal file
@@ -0,0 +1,51 @@
|
||||
extends StateNode
|
||||
|
||||
@export var employee : String = ""
|
||||
|
||||
var target
|
||||
var actor
|
||||
var in_queue
|
||||
var queued_at : GuildQueue
|
||||
var next_state : String
|
||||
var next_state_args : Array = []
|
||||
|
||||
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
actor = subject
|
||||
#find the receptionist queue
|
||||
var queue = Guild.hall.employees[employee].queue
|
||||
#join the queue
|
||||
join_queue(queue)
|
||||
reposition_queue(queue.length-1)
|
||||
|
||||
subject.nav_agent.navigation_finished.connect(_on_navigation_finished)
|
||||
|
||||
func _on_navigation_finished() -> void:
|
||||
actor.nav_agent.navigation_finished.disconnect(_on_navigation_finished)
|
||||
if !in_queue:
|
||||
complete_state()
|
||||
|
||||
func join_queue(queue : GuildQueue) -> void:
|
||||
queue.add_member(actor)
|
||||
in_queue = true
|
||||
queued_at = queue
|
||||
queue.advanced.connect(_on_queue_advanced)
|
||||
|
||||
func leave_queue() -> void:
|
||||
in_queue = false
|
||||
queued_at.advanced.disconnect(_on_queue_advanced)
|
||||
queued_at = null
|
||||
|
||||
func reposition_queue(idx : int) -> void:
|
||||
var queue = queued_at
|
||||
#if zero approach the receptionist with the intent to use her service at navigation complete.
|
||||
actor.approach(queue.global_position + idx * queue.direction * 75)
|
||||
|
||||
func _on_queue_advanced() -> void:
|
||||
#find our place within the queue
|
||||
var idx : int = queued_at.members.find(actor)
|
||||
#We aren't in the queue, time to advance
|
||||
if idx < 0:
|
||||
leave_queue()
|
||||
else:
|
||||
reposition_queue(idx)
|
||||
1
fsm/nodes/queue.gd.uid
Normal file
1
fsm/nodes/queue.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://y85swbbk8kbd
|
||||
17
fsm/nodes/state_node.gd
Normal file
17
fsm/nodes/state_node.gd
Normal file
@@ -0,0 +1,17 @@
|
||||
class_name StateNode extends Node
|
||||
|
||||
var state_machine : StateMachine = null
|
||||
signal completed(state : StateNode)
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
pass
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
pass
|
||||
|
||||
func complete_state() -> void:
|
||||
completed.emit(self)
|
||||
1
fsm/nodes/state_node.gd.uid
Normal file
1
fsm/nodes/state_node.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c27ctn874rdns
|
||||
13
fsm/nodes/test.gd
Normal file
13
fsm/nodes/test.gd
Normal file
@@ -0,0 +1,13 @@
|
||||
extends StateNode
|
||||
|
||||
|
||||
@export var message : String = ""
|
||||
var target
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
subject.approach(Game.player.global_position)
|
||||
subject.nav_agent.navigation_finished.connect(_on_navigation_finished)
|
||||
target = subject
|
||||
|
||||
func _on_navigation_finished() -> void:
|
||||
target.nav_agent.navigation_finished.disconnect(_on_navigation_finished)
|
||||
complete_state()
|
||||
1
fsm/nodes/test.gd.uid
Normal file
1
fsm/nodes/test.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://e8we6nmaob1k
|
||||
34
fsm/nodes/wait.gd
Normal file
34
fsm/nodes/wait.gd
Normal file
@@ -0,0 +1,34 @@
|
||||
extends StateNode
|
||||
|
||||
|
||||
@export var wait_duration : float = 1
|
||||
var wait_remaining : float = 0
|
||||
var next_state : String
|
||||
var next_state_args : Array = []
|
||||
var actor
|
||||
func exit() -> void:
|
||||
actor.show_speech_bubble("")
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if wait_remaining > 0:
|
||||
wait_remaining -= delta
|
||||
if wait_remaining <= 0:
|
||||
wait_remaining = 0
|
||||
complete_state()
|
||||
|
||||
func execute(subject, ...args : Array) -> void:
|
||||
wait_remaining = wait_duration
|
||||
actor = subject
|
||||
|
||||
if len(args) > 0 and args[0] != "":
|
||||
subject.show_speech_bubble(args[0])
|
||||
|
||||
if len(args) > 1 and args[1] != "":
|
||||
next_state = args[1]
|
||||
else:
|
||||
next_state = ""
|
||||
|
||||
if len(args) > 2 and args[2] != "":
|
||||
next_state_args = args[2]
|
||||
else:
|
||||
next_state_args = []
|
||||
1
fsm/nodes/wait.gd.uid
Normal file
1
fsm/nodes/wait.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dl3b5aywu1hf6
|
||||
Reference in New Issue
Block a user