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 21.7 Housekeeping and bugfixing #80

Closed Keith1039 closed 3 weeks ago

Keith1039 commented 3 weeks ago

Pygame-RPG 21.7 Housekeeping and bugfixing

This change focuses on two main changes. Changes were made to allow the NPCManager changes to be saved in JSON using SaveManager. This ensures that the progress that the player makes regarding the NPC stays consistent. (i.e. no repeatable quests or dialogue). The second change is creating the Utils package that has shared functions between classes. This allows for reduced redundancy and a simpler time making changes to functions shared across multiple classes.

NPCManager: This class now has a function called save_and_empty(). This function saves the changes to the Dialogue attribute to the dictionary before emptying the current group of sprites.

SaveManager: This class now has a reference to the NPCManager class in the form of the npcManager attribute. The strip_non_json_and_save() and load_data() functions received some changes to allow the saving and the loading of the NPCManager information.

strip_non_json_and_save(): When this function is called, the NPCManager object calls the save_and_empty() function to ensure all the changes made to the Dialogue list. It then calls get_NPCs() to reset the NPCs in the group, allowing them to be drawn once more. A new entry into the dictionary is the NPC dictionary in the NPCManager class. It is saved under the key, NPCDict. Since it is a dictionary, there's no special actions needed to save it to JSON.

load_data(): This function now sets the NPCDict attribute of the NPCManager class to the saved dictionary under the NPCDict key. It then calls NPCManager.get_NPCs() with the context from the screenManager class in order to reset the NPCs on the screen.

The Utils package was created with two python files, Getters.py and Algos.py.

Getters.py: This python file has simple retrieval methods for the JSON dictionaries. Functions such as get_item_json() can be found here. There is no need to test these functions as they are simple and easy to debug. Because all of these functions are now in one place, whenever a python file had to manually do one of these actions, they can just import Utils and used the defined functions in this class.

Algos.py: Algos is a python file that contains more complex algorithms that multiple classes need. For example, the get_max_animation_val() function is used by the Entity classes (Knight and Enemy) but is also used by the NPC class and maybe some future Sprite classes as well. Linking all of theses classes through inheritance so that they can get access to this function would be time consuming and inefficient so it was decided to simply place the function here.

The get_max_animation_val() function now takes in 3 values, the location of the sprites, the name and the animation status. Using this, it finds the highest value of the given animation using the special sort function defined alongside it. Now, these classes can import Utils and call the Utils.get_max_animation_val() function when trying to find their max animation value.

Unlike the functions in Getters.py, the functions in Algos.py do need tests.

Other Changes:

PR checklist