Keith1039 / Pygame-RPG

A repository of me trying to make an RPG with a custom game engine with Pygame added in as support.
0 stars 0 forks source link

Pygame-RPG 20: Creating an event handling method #71

Closed Keith1039 closed 3 months ago

Keith1039 commented 3 months ago

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

PR checklist