I've worked on some js games with html5, and I want to implement one of the features now in C#, but I'm kinda lost when it comes to classes instead of objects with properties....
Example #1:
var lootedItem;
var maxLikelihood = 0;
var loot = {
silver : 3,
bronze : 3,
copper : 3,
platinum: 1,
iron : 5,
stone : 5,
gold : 2
};
var playerInventory = {
silver : 0,
bronze : 0,
copper : 0,
platinum: 0,
iron : 0,
stone : 0,
gold : 0
};
2 basic objects with the same properties. In Javascript, you can simply loop through an object's properties, and for each property, add it to an array.
var lootArray = []; //array to hold our properties
for (var p in loot){ //for property in objectname
if(loot.hasOwnProperty(p)){
lootArray.push(p); //add to array
maxLikelihood = Math.max(maxLikelihood, loot);
}
}
But in C# it seems arrays are not recommended? Lists or Enumerations are? I could almost use an enumeration in place of the "loot" object, but the playerInventory changes and aren't enums constants?
I love this object:property setup because I can then make some basic functions to display/update, store, retrieve, add, subtract, or clear the playerInventory very easily like:
/*Randomly pick an item from the loot object and give it to the corresponding inventory object for the player */
function addLoot(){ //Add an item to players inventory
lootedItem = lootArray[Math.floor(Math.random() * lootArray.length)]; //pick an entry from the loot object at random
playerInventory[lootedItem] = +playerInventory[lootedItem] + loot[lootedItem]; //update the corresponding property
allLoot(); //display updated results
storeIt(); //store results for persistence
}
/*Display the players inventory in a list of key:value pairs*/
function allLoot(){
var r = ['Player Inventory\n'];
for(var i = 0; i < lootArray.length; ++i){
r.push(lootArray + (lootArray.length < 8? '\t : ' : ' : ') + playerInventory[lootArray]);
}
if(document.getElementById('allloot') != null) {
document.getElementById('allloot').firstChild.nodeValue = r.join('\n');
}
}
/* Store the players inventory values in localstorage, could be mysql or some other storage medium */
function storeIt() {
for(var i = 0; i < lootArray.length; ++i){
localStorage.setItem(lootArray, playerInventory[lootArray]);
}
}
/*Retrieve existing entries from storage from previous sessions to maintain persistence*/
function retrieveIt(){
var key;
for(var i = 0; i < localStorage.length; ++i){
key = localStorage.key(i);
if(playerInventory.hasOwnProperty(key)){
playerInventory[key] = localStorage.getItem(key);
}
}
}
That worked extremely well for me. When I need to "randomly" generate loot (player killed something or looted a chest or bought something in town) I can simply call addLoot() for each random item I want to generate. Same for skills, stats, etc etc etc I can also call individual entries, like playerInventory[2] if I want to manually manipulate the 3rd property in playerInventory, like buying/selling a non random item, or for crafting to check for/subtract ingredients and add created item....
But as far as classes I'm at a loss. Do I create 2 classes, one for playerInventory, the other for the potential loot table? I should mention in js I have a dozen of these object pairs with helper functions, for stats, skills, spells, loot, crafting, etc etc etc so that would be 2 dozen classes using that route.
Otherwise maybe an enumeration and a list, the enumeration holds the potential loot values, the list is the players inventory which is updated?
Could someone pseudo the above code for me how it would be done with a class??? Can you just have 2 objects with properties in the same class with helper "methods" to manipulate the properties?????
Is there a classical alternative to this:
for (var p in loot){
if(loot.hasOwnProperty(p)){
//yadayadayada
}
}