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
[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 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 ofaniTracker
once every 10 frames, it is possible for a class that inherits fromSprite
class to have a faultaniTracker
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 ofTrue
is given to the function however, it will bypass theaniTracker % 10 == 0
conditional and execute the rest of the code. This ensures that theaniTracker
attribute will always be an allowed value i.e. never surpassing the limit. This is going to be called during thesave_and_empty()
functions for theNPC
andObject
classes. As for theKnight
class, there is a conditional that checks to see if theaniTracker
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 toset_image_and_rect()
the function is a lot more explicit in it's conditions, resembling the conditionals in theupdate()
functions of theObject
andKnight
classes.A new change to the
update()
function for theKnight
andObject
class was that if theaniTracker
value is set to -1, that indicates that the state the object is in is permanent. If this is the case, theaniTracker
value will no longer update and the sprite cannot be changed. This is useful for the "Death" animation status for theKnight
class and the "Opening" animation status for theObject
class. Because of this, theset_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, theset_image_and_rect()
function will check for this. If this is the case, it will set the "spot" variable to the string value ofmaxAniVal
. 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