Friday, November 14, 2008

Java: CharSi Update #10

Now that the Runeword Wizard portion of the program is completed I can turn my attention back to the main goal of this application, namely the ability to virtually equip a character with items and see those stats. To do this I need a list of available pieces of equipment that can be displayed in the item JList.

Similar to the way runewords are handled with the Runeword Wizard I decided upon text files, one for each possible item. Granted an access database or something similar would be a bit more efficient in the long run, but since I have no idea how to hook one up the application text files remain my only alternative beyond hard coding each item's stats into the class files.

Before these files could be created I needed to generate a list of all possible mods that can spawn on items. This is needed for two reasons; first to be able to consistently word a mod in the text files and secondly to lay the basis for the array of arrays necessary to compare possible mods to mods actually present and update the correct variables within the program. The second part is far into the future so I'm not worrying about that yet, however wording the mods right is something I must worry about. If I misspell a mod then when the text file is searched for that mod it won't be found due to a typo, and as a result the information displayed will be inaccurate. As a result I must be very careful when creating the text files, such as the one seen below.

Vampire Gaze
Grim Helm
Required level: 41
Required strength: 58
Defense: 252
Durability: 40
+100% Enhanced defense
Adds 6-22 cold damage over 4 seconds
15% Slower stamina drain
6-8% Life stolen per hit
6-8% Mana stolen per hit
Damage reduced by 15-20%
Magic damage reduced by 10-15

The files themselves are organized in subfolders that separate set/unique/white(normal) items based on whether they are normal, exceptional or elite. This makes displaying the list of item matches much easier in the JList component if all the normal set items are stored separate from the unique items.

Once the files were created, the code then needed to be generated that could search them and display the correct items dependent on which checkboxes the user had selected. A pair of methods were created for this purpose, one being the workhorse and the other calling the first dependent on what folder location was being utilized. A basic setup of the first method is below.

if(normalFlag){
if(whiteFlag){
location = location.concat("Normal/White/");
AddToList(location);
location = location.substring(0,11); //revert string back to original form
}
if(setFlag){
location = location.concat("Normal/Set/");
AddToList(location);
location = location.substring(0,11); //revert string back to original form
}
if(uniqueFlag){
location = location.concat("Normal/Unique/");
AddToList(location);
location = location.substring(0,11); //revert string back to original form
}
}

The folder location is passed to the workhorse method (AddToList) and all files in that location are dumped into an array of Strings. That list of Strings is then shortened to remove the .txt portion of the filename and then added to the DefaultListModel governing the item JList component. This allows the items to appear in the item JList component. Once in this component the JList Listener is then added that once an item is selected its corresponding text file is displayed in the JTextArea. The layout of the listener is the same as the method shown above.

The next step is to then transfer this same code to the class files for the other equipment types. I do realize that by grouping items in such broad terms the generated lists for the weapon types will be quite extensive. I've considered changing that class file into a JTabbedPane, one for each weapon type, except that there are fifteen different weapon types and that would lead to quite a bit of duplicate code. Another alternative is to add another set of checkboxes, one for each weapon type, that the user can select which weapon types to display. This would make the code quite a bit longer and require a new component added to the GUI to display these choices. I haven't decided which way I wish to go, or if I simply want to leave it as is and have a long list of items displayed. As a result I'm going to skip the weapon class update for now and concentrate on the others.

No comments: