2025-05-31 22:21:22 +01:00
|
|
|
extends Node
|
|
|
|
class_name Player
|
|
|
|
|
|
|
|
@export var session_id: int = 0
|
|
|
|
@export var player_name: String = ""
|
|
|
|
@export var player_pronouns: String = ""
|
|
|
|
|
2025-06-21 14:35:32 +01:00
|
|
|
var game_state: GameState = null
|
|
|
|
|
|
|
|
var waiting_actions: Dictionary = {}
|
|
|
|
var prompt_queue: Array[Prompt] = []
|
|
|
|
var message_context: Dictionary = {}
|
|
|
|
|
|
|
|
var current_prompt: Prompt:
|
2025-05-31 22:21:22 +01:00
|
|
|
set(val):
|
|
|
|
if val != current_prompt:
|
|
|
|
current_prompt = val
|
|
|
|
prompt_changed.emit(current_prompt)
|
2025-06-21 14:35:32 +01:00
|
|
|
|
|
|
|
var playing: bool = false:
|
|
|
|
set(val):
|
|
|
|
if val != playing:
|
|
|
|
playing = val
|
|
|
|
playing_status_changed.emit(playing)
|
2025-05-31 22:21:22 +01:00
|
|
|
|
2025-06-21 14:35:32 +01:00
|
|
|
signal prompt_changed(prompt: Prompt)
|
|
|
|
signal prompt_answered(prompt: Prompt, answer: String)
|
|
|
|
signal prompt_queue_finished()
|
|
|
|
signal playing_status_changed(playing: bool)
|
|
|
|
signal message_spoken(message: String)
|
2025-05-31 22:21:22 +01:00
|
|
|
|
2025-06-21 14:35:32 +01:00
|
|
|
#@rpc("any_peer", "call_local", "reliable")
|
|
|
|
#func claim_authority(peer_id: int) -> void:
|
|
|
|
#set_multiplayer_authority(peer_id, true)
|
|
|
|
|
|
|
|
func send_message(message: String, additional_context: Dictionary = {}) -> void:
|
|
|
|
var prompt: Prompt = Prompt.create(message, Prompt.PromptType.CONFIRMATION)\
|
|
|
|
.with_context(message_context)\
|
|
|
|
.with_context(additional_context)
|
|
|
|
send_prompt(prompt)
|
|
|
|
|
|
|
|
func add_message_context(key: String, value: String) -> void:
|
|
|
|
message_context[key] = value
|
|
|
|
|
|
|
|
func add_message_context_dict(context: Dictionary) -> void:
|
|
|
|
message_context.merge(context, true)
|
2025-05-31 22:21:22 +01:00
|
|
|
|
2025-06-21 14:35:32 +01:00
|
|
|
func get_pronoun_context_dict(prefix: String = "") -> Dictionary:
|
|
|
|
var ret: Dictionary = {}
|
|
|
|
var they: String
|
|
|
|
var them: String
|
|
|
|
var their: String
|
|
|
|
var theirs: String
|
|
|
|
match player_pronouns:
|
|
|
|
"he/him":
|
|
|
|
they = "he"
|
|
|
|
them = "him"
|
|
|
|
their = "his"
|
|
|
|
theirs = "his"
|
|
|
|
"she/her":
|
|
|
|
they = "she"
|
|
|
|
them = "her"
|
|
|
|
their = "her"
|
|
|
|
theirs = "hers"
|
|
|
|
_:
|
|
|
|
they = "they"
|
|
|
|
them = "them"
|
|
|
|
their = "their"
|
|
|
|
theirs = "theirs"
|
|
|
|
ret[prefix+"they"] = they
|
|
|
|
ret[prefix+"them"] = them
|
|
|
|
ret[prefix+"their"] = their
|
|
|
|
ret[prefix+"theirs"] = theirs
|
|
|
|
ret[prefix+"themself"] = them+"self"
|
|
|
|
return ret
|
|
|
|
|
|
|
|
func get_context_dict(prefix: String) -> Dictionary:
|
|
|
|
var ret = get_pronoun_context_dict(prefix)
|
|
|
|
ret[prefix+"name"] = player_name.capitalize()
|
|
|
|
ret[prefix+"name's"] = player_name.capitalize() + "'s"
|
|
|
|
return ret
|
|
|
|
|
|
|
|
func send_action(action: Action, immediate: bool = false) -> void:
|
|
|
|
waiting_actions[action.prompt.id] = action
|
|
|
|
#connect("prompt_answered", action.prompt_answered.bind(self), CONNECT_ONE_SHOT)
|
|
|
|
send_prompt(action.prompt)
|
|
|
|
|
|
|
|
func send_prompt(prompt: Prompt) -> void:
|
|
|
|
if prompt != null:
|
|
|
|
_send_prompt.rpc(JSON.stringify(prompt.save()))
|
|
|
|
else:
|
|
|
|
_send_prompt.rpc("{}")
|
2025-05-31 22:21:22 +01:00
|
|
|
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
|
|
func _send_prompt(json_prompt: String) -> void:
|
2025-06-21 14:35:32 +01:00
|
|
|
print("Peer %d Player %d prompt received: %s" % [multiplayer.get_unique_id(),session_id, json_prompt])
|
|
|
|
var prompt: Prompt = Prompt.load(JSON.parse_string(json_prompt))
|
|
|
|
if current_prompt != null:
|
|
|
|
if current_prompt.interruptable:
|
|
|
|
submit_prompt_answer.rpc(current_prompt.id,"")
|
|
|
|
current_prompt = null
|
|
|
|
else:
|
|
|
|
prompt_queue.append(prompt)
|
|
|
|
if current_prompt == null:
|
|
|
|
current_prompt = prompt
|
|
|
|
if current_prompt != null:
|
|
|
|
current_prompt.context["my_name"] = name
|
|
|
|
#print("Peer %d Player %d prompt received: \"%s\" context: %s" % [multiplayer.get_unique_id(),session_id, current_prompt.text, current_prompt.context])
|
|
|
|
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
|
|
func submit_prompt_answer(prompt_id: int, answer: String) -> void:
|
|
|
|
print("Peer %d Player %d answer received: \"%s\"" % [multiplayer.get_unique_id(),session_id, answer])
|
|
|
|
prompt_answered.emit(current_prompt, answer)
|
|
|
|
if waiting_actions.has(prompt_id):
|
|
|
|
var action: Action = waiting_actions.get(prompt_id)
|
|
|
|
action.prompt_answered(answer,self)
|
|
|
|
waiting_actions.erase(prompt_id)
|
|
|
|
if prompt_queue.is_empty():
|
|
|
|
current_prompt = null
|
|
|
|
prompt_queue_finished.emit()
|
|
|
|
else:
|
|
|
|
current_prompt = prompt_queue.pop_front()
|
|
|
|
|
|
|
|
func speak(message: String) -> void:
|
|
|
|
message_spoken.emit(message)
|
|
|
|
|
|
|
|
func is_busy() -> bool:
|
|
|
|
return current_prompt != null or !prompt_queue.is_empty()
|