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

@@ -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