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.11 Optimizing Sprite classes #84

Closed Keith1039 closed 3 weeks ago

Keith1039 commented 3 weeks ago

Pygame-RPG 21.11 Optimizing Sprite classes

Since I made the sprite classes fairly quickly, this PR is to iron out some flaws with them. One such flaw is how they update, their update() function. Because I made them so that they only check the value of aniTracker once every 10 frames, it is possible for a class that inherits from Sprite class to have a fault aniTracker value that was saved. Since this game runs at 60 fps, it's improbable but possible.

As a result, I added a default value to update called force. force is set to false normally so when the groups call the function, it'll behave as normal. If a value of True is given to the function however, it will bypass the aniTracker % 10 == 0 conditional and execute the rest of the code. This ensures that the aniTracker attribute will always be an allowed value i.e. never surpassing the limit. This is going to be called during the save_and_empty() functions for the NPC and Object classes. As for the Knight class, there is a conditional that checks to see if the aniTracker value is out of the range and then if the conditional is true, forces an update.

Because the NPC class allows forced updates, to stop unnecessary calls to set_image_and_rect() the function is a lot more explicit in it's conditions, resembling the conditionals in the update() functions of the Object and Knight classes.

A new change to the update() function for the Knight and Object class was that if the aniTracker value is set to -1, that indicates that the state the object is in is permanent. If this is the case, the aniTracker value will no longer update and the sprite cannot be changed. This is useful for the "Death" animation status for the Knight class and the "Opening" animation status for the Object class. Because of this, the set_image_and_rect() function for both of these classes needed to be tweaked.

If the aniTracker value is -1 and is saved, the next time the image is loaded, the set_image_and_rect() function will check for this. If this is the case, it will set the "spot" variable to the string value of maxAniVal. This "spot" will be used in the file path to the png in use.

I also updated the "chestEmpty" event in the Events.json file to be repeatable. As always, the tests have been updated to fit these new behaviors and test them.

PR checklist