Cleaned and improved some event stuff

This commit is contained in:
2025-05-26 13:11:27 -05:00
parent c1d8dd46d8
commit d6a2f4b567
27 changed files with 296 additions and 108 deletions

View File

@@ -31,4 +31,9 @@ func make(inventory:Inventory = null) -> void:
for output in outputs:
inventory.addItem(output.type, output.quantity)
timesMade += 1
timesMade += 1
func getTransContext() -> TransContext:
var ctx = TransContext.new()
ctx.addTrans("title", title)
return ctx

View File

@@ -38,6 +38,9 @@ func getRaycastInteractable() -> EntityInteractable:
return interactable
func _process(delta: float) -> void:
if !entity or entity.isPaused():
return
match interactorType:
InteractorType.PLAYER_INPUT:
_processPlayerInput()
@@ -49,6 +52,10 @@ func _process(delta: float) -> void:
func _processPlayerInput() -> void:
if !Input.is_action_just_pressed("interact"):
return
# Conditions where player cannot interact.
if UI.hasInteractionFocus():
return
var interactable:EntityInteractable = getRaycastInteractable()
if interactable:

View File

@@ -22,11 +22,9 @@ func onEntityInteract(
interactor:EntityInteractor,
interactee:EntityInteractable
) -> void:
print("Interact?")
var event = EventGetItem.new()
event.itemType = itemType;
event.quantity = quantity;
event.removeNode = self
EVENT.addEvent(event)
event.onEntityInteract(interactor, interactee)
get_parent().remove_child(self)
self.queue_free()

View File

@@ -2,15 +2,13 @@ class_name Event extends Node
const Entity = preload("res://scripts/Entity/Entity.gd");
var inEventSystemTree:bool = false;
var started:bool = false;
var ended:bool = false;
var interactor:EntityInteractor = null
var interactee:EntityInteractable = null
# Godot Methods
func _init() -> void:
pass
func _process(delta: float) -> void:
if !started || ended:
return
@@ -21,6 +19,11 @@ func _process(delta: float) -> void:
# Event methods (cleaned up)
func start() -> void:
assert(
self.is_inside_tree(),
"Event is trying to start but the node is not part of the scene tree. "+
"Refer to EventSystem.addEvent"
)
assert(started == false)
started = true
@@ -34,6 +37,8 @@ func isDone() -> bool:
func end() -> void:
assert(ended == false)
ended = true
if self.inEventSystemTree:
EVENT.removeEvent(self)
func isEndingEvent() -> bool:
return false

View File

@@ -17,41 +17,46 @@ func start() -> void:
if interactee && interactor:
if turnInteractee && interactee.entityDirection && interactor.characterBody:
var turn = EventEntityTurn.new()
turn.name = "Conversation Turn Interactee"
turn.entity = interactee.entityDirection
turn.direction = turn.entity.getDirectionToFace(interactor.characterBody.global_position)
addExtraEvent(turn, 0)
addChildEvent(turn, 0)
if turnInteractor && interactor.entityDirection && interactee.characterBody:
var turn = EventEntityTurn.new()
turn.name = "Conversation Turn Interactor"
turn.entity = interactor.entityDirection
turn.direction = turn.entity.getDirectionToFace(interactee.characterBody.global_position)
addExtraEvent(turn, 0)
addChildEvent(turn, 0)
# Create start pause event
if (pauseInteractee && interactee.entity) || (pauseInteractor && interactor.entity):
var startPause = EventPause.new()
startPause.name = "Conversation Start Pause"
startPause.pauseType = startPauseType
startPause.entities = entities
if pauseInteractee && interactee.entity:
startPause.includeInteractee = pauseInteractee
if pauseInteractor && interactor.entity:
startPause.includeInteractor = pauseInteractor
addExtraEvent(startPause, 0)
addChildEvent(startPause, 0)
# Create end pause event.
endPauseEvent = EventPause.new()
endPauseEvent.name = "Conversation End Pause"
endPauseEvent.pauseType = endPauseType
endPauseEvent.entities = entities
if pauseInteractee && interactee.entity:
endPauseEvent.includeInteractee = pauseInteractee
if pauseInteractor && interactor.entity:
endPauseEvent.includeInteractor = pauseInteractor
addExtraEvent(endPauseEvent, -1)
addChildEvent(endPauseEvent, -1)
# Pass off to event group
super.start()
func end() -> void:
print("Ending conversation event: ", self)
# Manually end pause
if endPauseEvent != null && !endPauseEvent.started:
endPauseEvent.start()

View File

@@ -1,11 +1,25 @@
class_name EventTextbox extends "res://scripts/Event/Event.gd"
# @export var text:Array[String] = [ "Hello Text" ];
@export_multiline var text:String = "Hello Text"
@export_multiline var text:String = ""
var textPlural:String = ""
var count:int = 1
var transContext:TransContext = null
func start() -> void:
super.start()
VN.getTextbox().setText(self.text);
if text.is_empty() && transContext == null:
push_error("EventTextbox text is empty and no TransContext provided.")
return
if transContext == null:
transContext = TransContext.new()
if textPlural.is_empty():
VN.getTextbox().setText(transContext.trans(text))
else:
VN.getTextbox().setText(transContext.transPlural(text, textPlural, count));
func isDone() -> bool:
return super.isDone() && VN.getTextbox().isClosed;

View File

@@ -80,13 +80,4 @@ func reset() -> void:
for child in childEvents:
child.reset()
# Send to the parent to reset the extra events
super.reset()
func startChild(child:Event) -> void:
# Inherits some properties from this event.
child.interactee = self.interactee
child.interactor = self.interactor
child.start()
if child.isEndingEvent():
self.end()
super.reset()

View File

@@ -1,7 +1,7 @@
class_name EventWithChildren extends "res://scripts/Event/Event.gd"
var childEvents:Array[Event] = []
var extraEvents:Array[Event] = []
var addedEvents:Array[Event] = []
func start():
super.start()
@@ -22,16 +22,29 @@ func _updateChildEvents() -> void:
childEvents.append(child)
func _cleanupExtraEvents():
for event in extraEvents:
for event in addedEvents:
remove_child(event)
event.queue_free()
extraEvents = []
addedEvents = []
_updateChildEvents()
func addExtraEvent(child:Event, position:int) -> void:
func addChildEvent(child:Event, position:int = -1) -> void:
assert(started == false || ended == true)
# Add the child to the extra events list
extraEvents.append(child)
if position < 0:
position = childEvents.size() + position + 1
add_child(child)
move_child(child, position)
_updateChildEvents()
addedEvents.append(child)
_updateChildEvents()
func startChild(child:Event) -> void:
assert(child.get_parent() == self)
# Inherits some properties from this event.
child.interactee = self.interactee
child.interactor = self.interactor
child.start()
if child.isEndingEvent():
self.end()

View File

@@ -1,5 +1,7 @@
class_name EventGetItem extends "res://scripts/Event/Item/EventItem.gd"
const EventConversation = preload("res://scripts/Event/EventConversation.gd")
enum GetType {
FOUND,
GIVEN,
@@ -7,14 +9,20 @@ enum GetType {
@export var showText: bool = true
@export var getType:GetType = GetType.FOUND;
@export var removeNode:Node = null
var conversationEvent:EventConversation = null
var textboxEvent:EventTextbox = null
func start() -> void:
super.start()
getInventory().addItem(itemType, quantity)
# Should show text?
if !showText:
super.start()
return
# What text to show?
var textKey:String
match getType:
GetType.FOUND:
@@ -22,12 +30,29 @@ func start() -> void:
GetType.GIVEN:
textKey = "event.get_item.given"
_:
pass
super.start()
return
# Create translation context
var ctx = TransContext.new()
ctx.addInteger("quantity", quantity)
ctx.addContext("item", ITEM.getItem(itemType).getTransContext())
VN.getTextbox().setText(ctx.transPlural(textKey, textKey + "_plural", quantity));
# Create conversation
conversationEvent = EventConversation.new()
addChildEvent(conversationEvent)
# Create textbox
textboxEvent = EventTextbox.new()
textboxEvent.transContext = ctx
textboxEvent.text = textKey
textboxEvent.textPlural = textKey + "_plural"
textboxEvent.count = quantity
conversationEvent.addChildEvent(textboxEvent)
# Begin processing
super.start()
startChild(conversationEvent)
func isDone() -> bool:
if !super.isDone():
@@ -35,5 +60,13 @@ func isDone() -> bool:
if !showText:
return true
return VN.getTextbox().isClosed;
return conversationEvent.isDone()
func end() -> void:
if removeNode:
var parent = removeNode.get_parent()
if parent:
parent.remove_child(removeNode)
removeNode.queue_free()
removeNode = null
super.end()

View File

@@ -1,4 +1,4 @@
class_name EventItem extends Event
class_name EventItem extends "res://scripts/Event/Flow/EventWithChildren.gd"
const Inventory = preload("res://scripts/Item/Inventory.gd")
@export var itemType:Item.Type = Item.Type.POTION

View File

@@ -1,8 +1,13 @@
class_name Map extends Node
@export var mapName:String = "Map"
@export_multiline var title:String = ""
@export var mapEnterEvent:Event = null
func _ready() -> void:
if mapEnterEvent:
mapEnterEvent.start()
mapEnterEvent.start()
func getTransContext() -> TransContext:
var ctx = TransContext.new()
ctx.set("title", title)
return ctx

View File

@@ -1,4 +1,4 @@
class_name MainMenuScene extends Node
func _enter_tree() -> void:
pass
pass

View File

@@ -47,4 +47,25 @@ func eventAreFlagsOff(event:SpecialEvent, flagsToCheck:int) -> bool:
func eventIsAnyOfFlagsOff(event:SpecialEvent, flagsToCheck:int) -> bool:
if !eventFlags.has(event):
eventFlags[event] = 0;
return (eventFlags[event] & flagsToCheck) != flagsToCheck;
return (eventFlags[event] & flagsToCheck) != flagsToCheck;
# Adds an event that is created by code into the scene tree and lets it be
# managed properly by the event system.
func addEvent(event:Event) -> void:
assert(
!event.is_inside_tree(),
"Event is already inside the scene tree, cannot add it again."
)
$StartedEvents.add_child(event)
event.inEventSystemTree = true
func removeEvent(event:Event) -> void:
assert(
event.inEventSystemTree,
"Event is not inside event system tree."
)
event.inEventSystemTree = false
$StartedEvents.remove_child(event)
event.queue_free()

View File

@@ -32,4 +32,4 @@ func getItem(type:Item.Type) -> Item:
return ITEM_MAP[type]
func isStackable(itemType:Item.Type) -> bool:
return getItem(itemType).stackable
return getItem(itemType).stackable

View File

@@ -39,15 +39,6 @@ func _init() -> void:
var preferred = OS.get_locale()
self.setLocaleFromLocaleString(preferred)
func _enter_tree() -> void:
# Test
print("Hello World!")
var ctx = TransContext.new()
ctx.addInteger("quantity", 2)
ctx.addContext("item", ITEM.getItem(Item.Type.POTION).getTransContext())
print(ctx.trans("test"))
func setLocaleFromLocaleString(localeString:String) -> void:
var parts:PackedStringArray = localeString.split("_")

View File

@@ -4,13 +4,36 @@ var QUEST_MENU:QuestMenu
var DEBUG_MENU:DebugMenu
var INVENTORY_MENU:FullInventoryMenu
var EVENT_FLAG_MENU:EventFlagMenu
var VN_TEXTBOX:VNTextbox
func _ready() -> void:
QUEST_MENU = $QuestMenu
DEBUG_MENU = $DebugMenu
INVENTORY_MENU = $FullInventory
EVENT_FLAG_MENU = $EventFlagMenu
VN_TEXTBOX = $VNTextbox
func _process(delta: float) -> void:
# This needs to always be at the end of the parent node's tree
get_parent().move_child(self, get_parent().get_child_count() - 1)
func hasInteractionFocus() -> bool:
# Returns true if the UI system has focus, basically if any UI menu or
# element is open in a way that prevents interaction with the game world.
if QUEST_MENU.isOpen():
return true
if DEBUG_MENU.isOpen():
return true
if INVENTORY_MENU.isOpen():
return true
if EVENT_FLAG_MENU.isOpen():
return true
if !VN_TEXTBOX.isClosed:
return true
return false

View File

@@ -106,8 +106,6 @@ func build(
# Merge with our dictionary
dict.merge(subDict)
print("Trans context before key prepend: ", dict)
if key != "":
# Now prepend the parent key to all keys in the dictionary
var newDict:Dictionary[String, String] = {}

View File

@@ -2,6 +2,8 @@ class_name EventFlagMenu extends Panel
@export var flagList:ItemList
@export var grid:GridContainer
@export var btnClose:Button
var checkboxes:Array[CheckBox] = []
var selectedEvent:int = -1
@@ -24,6 +26,7 @@ func _ready() -> void:
EVENT.eventFlagUpdated.connect(_on_EventFlagUpdated)
flagList.item_selected.connect(_on_EventSelected)
btnClose.pressed.connect(close)
func _exit_tree() -> void:
EVENT.eventFlagUpdated.disconnect(_on_EventFlagUpdated)

View File

@@ -70,7 +70,7 @@ func getCountOfCharactersToScrollInView() -> int:
func recalculateWrapping():
# Reset label to default.
label.text = text;
label.advancedText = text;
label.visible_characters = -1;
label.fit_content = true;
isMoreViews = false;
@@ -98,7 +98,7 @@ func recalculateWrapping():
wrappedText += "\n";
wrappedText += text[i];
label.text = wrappedText;
label.advancedText = wrappedText;
label.fit_content = false;
label.visible_characters = 0;