This PR focuses on implementing a way to define and handle events. First, an event needed to be strictly defined and stored as JSON. After some thought, an event was to have the following properties,
Range: this is a tuple/list that has the two x
coordinates that the Knight character needs to be within to trigger the event. Can be an empty tuple/list.
Event Type: A string that indicates what type of event it is
Activated: A boolean that indicates whether this event has been triggered before
Repeatable: A boolean that indicates whether the event can be triggered more than once
Dialogue Path: If the event has dialogue, this field gives the path to the text file containing the dialogue. Can be empty string.
Context: A string representing the context needed for the event to be triggered, if context is empty string then the event can be triggered regardless of context.
Items Gained: A dictionary containing item entries that are gained when the event is processed.
Using this structure, events can be declared and also handled efficiently. To handle these Events, a new manager class was created, EventManager.
EventManager: Event manager is an event handler meant to handle the custom events that the game creates. It has the following attributes, eventDict, events, knight and dialogueManager. It has two main functions, push_event() and process_events().
eventDict: Reference to the main event dictionary
events: A list of events represented by dictionaries. Due to how python dictionaries work, any change to the these dictionaries will reflect on the master dictionary.
knight: Reference to the knight object
dialogueManager: Reference to the DialogueManager object
push_event(): This function takes an a string, eventName, and adds the dictionary associated with this key (if it exists) into the events list.
process_events(): this function takes in a string, context, and an integer, x. The function goes through the events list and evaluates each event dictionary. It uses context and x to determine whether the event is triggered or not. If the event is not triggered, it stays in the events list. Otherwise, the event dictionary gets processed by the value of it's keys and is removed from the events list. This function does not always determine whether the event has been activated. Sometimes, it simply sends the event over to another manager (dialogue events get sent to DialogueManager and it determines whether it has been dealt with).
Because of these changes, DialogueManager and SaveManager received a large amount of changes.
DialogueManager: Received 2 new attributes event and nextEvents. event is either none or an event dictionary and nextEvents is a list of dialogue events that have not yet been processed. The functions load_file(), get_new_text() and draw_dialogue() also received changes.
load_file(): This function checks if the event is not activated or the event is repeatable. If the event passes this check, it will set the event to the event attribute, it will then fill the dialogue list with the dialogue from the specified file from the event.
get_new_text(): This function now has the condition for when we've reached the end of the dialogue list. It indicates that the event has been completed by setting the Activated key to True. It then sets event to None. If there more events in the nextEvents list, it will try to load the latest one using load_file().
draw_dialogue(): Now has a condition at the start that checks if the dialogue list has text. It also no longer returns anything.
SaveManager: This class now takes in a reference to the EventManager class. This is so that it can save the event dictionary from the class. strip_non_json_and_save() is updated to save the event dictionary and load_data() has also been updated to load the information back into the object.
Some other changes:
Knight class now has the add_all_to_inventory() function which is basically just a more convenient way to add items to the knight's inventory.
Changed the key name for some dictionaries (ScreenManager.json) and as such some other files needed to be changed (Object_Animation_Manager)
Added the events dictionary in JSON (Events.json)
Added a new script to update the Events.json file
Added a new test dialogue in the event_text folder (Test_dialogue2.txt)
updated save files
updated test files
Integrated Rpg2.py file with the EventManager
PR checklist
[x] All Pytests pass
[x] All changes are documented somewhere in the commit
[x] Rpg2.py is tested and works even with the changes
Pygame-RPG 20: Creating an event handling method
This PR focuses on implementing a way to define and handle events. First, an event needed to be strictly defined and stored as JSON. After some thought, an event was to have the following properties,
Range
: this is a tuple/list that has the two x coordinates that the Knight character needs to be within to trigger the event. Can be an empty tuple/list.Event Type
: A string that indicates what type of event it isActivated
: A boolean that indicates whether this event has been triggered beforeRepeatable
: A boolean that indicates whether the event can be triggered more than onceDialogue Path
: If the event has dialogue, this field gives the path to the text file containing the dialogue. Can be empty string.Context
: A string representing the context needed for the event to be triggered, if context is empty string then the event can be triggered regardless of context.Items Gained
: A dictionary containing item entries that are gained when the event is processed.Using this structure, events can be declared and also handled efficiently. To handle these Events, a new manager class was created,
EventManager
.EventManager
: Event manager is an event handler meant to handle the custom events that the game creates. It has the following attributes,eventDict
,events
,knight
anddialogueManager
. It has two main functions,push_event()
andprocess_events()
.eventDict
: Reference to the main event dictionaryevents
: A list of events represented by dictionaries. Due to how python dictionaries work, any change to the these dictionaries will reflect on the master dictionary.knight
: Reference to the knight objectdialogueManager
: Reference to the DialogueManager objectpush_event()
: This function takes an a string, eventName, and adds the dictionary associated with this key (if it exists) into theevents
list.process_events()
: this function takes in a string, context, and an integer, x. The function goes through the events list and evaluates each event dictionary. It uses context and x to determine whether the event is triggered or not. If the event is not triggered, it stays in theevents
list. Otherwise, the event dictionary gets processed by the value of it's keys and is removed from theevents
list. This function does not always determine whether the event has been activated. Sometimes, it simply sends the event over to another manager (dialogue events get sent to DialogueManager and it determines whether it has been dealt with).Because of these changes,
DialogueManager
andSaveManager
received a large amount of changes.DialogueManager
: Received 2 new attributesevent
andnextEvents
.event
is either none or an event dictionary andnextEvents
is a list of dialogue events that have not yet been processed. The functionsload_file()
,get_new_text()
anddraw_dialogue()
also received changes.load_file()
: This function checks if the event is not activated or the event is repeatable. If the event passes this check, it will set the event to theevent
attribute, it will then fill thedialogue
list with the dialogue from the specified file from the event.get_new_text()
: This function now has the condition for when we've reached the end of thedialogue
list. It indicates that the event has been completed by setting theActivated
key to True. It then setsevent
to None. If there more events in thenextEvents
list, it will try to load the latest one usingload_file()
.draw_dialogue()
: Now has a condition at the start that checks if the dialogue list has text. It also no longer returns anything.SaveManager
: This class now takes in a reference to theEventManager
class. This is so that it can save the event dictionary from the class.strip_non_json_and_save()
is updated to save the event dictionary andload_data()
has also been updated to load the information back into the object.Some other changes:
Knight class now has the
add_all_to_inventory()
function which is basically just a more convenient way to add items to the knight's inventory.Changed the key name for some dictionaries (ScreenManager.json) and as such some other files needed to be changed (Object_Animation_Manager)
Added the events dictionary in JSON (Events.json)
Added a new script to update the Events.json file
Added a new test dialogue in the event_text folder (Test_dialogue2.txt)
updated save files
updated test files
Integrated Rpg2.py file with the EventManager
PR checklist