Added a new Weapon instance variable to the Player that is the equipped weapon, which is just a pointer to a weapon in the player’s acquired weapon list. The equipped weapon is initially set to null, which means the player does not have a weapon equipped.
When the equipment screen is displayed, a new ResistorKit menu is created which takes the Player’s acquired weapon names as parameters. This is a little wasteful, since memory used for the menu object has to be allocated each time the screen is displayed, so there is some room for optimization later. I’ve also added the damage values as a part of the menu item, so it’s easy to tell how much damage the weapon does for testing. One enhancement I could make is to automatically sort the weapons by damage value.
I commented out all of my socket code for now, and now the confirm button just sets the player’s equipped weapon to the object in the acquired weapons list at the index of the index of the currently selected menu item. I think it’s safe to say that the menu items and the acquired weapons list will always be in sync, because it is not possible to add or remove weapons from the list on the equipment screen. Ideally, I should make the menu accept a pointer to the acquired weapons list as a parameter, and then have a toString type message display the weapon name and damage. However, I would then lose some of the portability of the ResistorKit libraries, because not all games will have Weapon objects especially not defined the same way as mine.
At the bottom of the screen I now display the equipped weapon in yellow. I’ll probably end up making the equipment display and equipment select screens separate, but for simplicity I will leave both on the same screen for now. I still display the list of armor now with defense value, but defense has not been implemented yet.
Now that the player has a way to equip a weapon, I have to apply that value to the projectile. Thankfully, I already added a damage instance variable to the projectile class. Therefore, I just have to assign the weapon’s damage to the projectile when it is fired. One problem is that the player does not start with any weapons, which means the player is unable to shoot. However, there is a chicken and egg problem here, because enemies don’t drop weapons until they are shot and defeated. Therefore, I programmatically added a weapon drop to the first room so that the player can equip it to defeat enemies. This was easily accomplished by calling the dropItem method when the first room is instantiated, passing the first weapon ID and the x,y coordinates as parameters. In the future I would like to have static items placed by the level editor, which would require the player to open or shoot a treasure chest. Additionally, I also had to add a check on the equipment screen to ensure that the selected weapon is in the bounds of the acquired weapon list, because index 0 in a zero sized list out of bounds and causes a crash.
One more slight glitch to fix. If the player is running and then the Start button is pressed to go to the equipment screen, then they will continue to run after returning to the game even if the directional controls are not pressed.