Re structure of attackEnemy function - UQdeco2800/2022-studio-2 GitHub Wiki
Summary
This wiki page gives a summary of a key code improvement we made within the our primary attack function - attackEnemy()
.
Why did we do this?
Through this re-structure, our attackEnemy() function was reduced from 66 lines of code to less than 30 lines of code.
Changes | Before | After |
---|---|---|
Code Lines | 66 | 30 |
Code Complexity | 25 | <15 |
While thoroughly combing through all of our implemented classes to iron out any code smells, one of the most considerable one found was how convoluted our attackEnemy function was.
The creation of the sequence diagram earlier in the sprint also lead to similar findings.
This function encompasses the main functionality of what happens when a player hits SPACE
and is pivotal to the success of the game.
Upon review, we agreed that it was highly difficult to follow.
What did we do?
Therefore, it was decided to break the attackEnemy function into a number of smaller modules which improve readability and make it much more easy for the programmers to troubleshoot and debug in the future.
The new attack function is explained below and is very simple to comprehend.
First, in the attackEnemy()
function, the universal code used regardless of weapon type is run.
Then, two key cases were created: if the weapon is not null, then we trigger attackWithPhysicalWeapon.
if (weaponEquipped.getComponent(PhysicalWeaponStatsComponent.class) != null) {
attackWithPhysicalWeapon(weaponEquipped, auraEquipped);
Then else, if no weapon is equipped, trigger attackWithNoWeapon.
else {
attackWithNoWeapon();
}
These two key functions then branch off further into attackRanged()
and playAttackAnimation()
which in turn trigger subsequent functions which follow a similar modular logic.
Then playAttackSounds()
is triggered, which then plays a sound relevant to the equipped weapon, regardless of weapon type.
This was previously all done in the same function.
Back to Combat Items Contents Page
Author
- Lachlan Benson
- GitHub: @LachlanBenson
- Discord: Lachlan.Benson#4926
- Slack: Lachlan Benson