As someone new to the world of object-oriented programming, I am currently working on modeling a character in a game with various levels, classes, and equipment choices.
My ultimate goal is to create a "dresser" feature where players can try on different equipment, see how it affects their parameters, and calculate costs. I have already started programming the basic structure (here), but as my first attempt using html, css, and javascript, it's quite messy. I want to approach this project more efficiently this time :)
Let's say we have an object for the character simulation:
var Lord = function(){
this.Level = 1;
this.Gender = 'Male';
this.Faction = 'Knight';
this.Attack = 0;
this.Defense = 1;
this.SpellPower = 0;
this.Knowledge = 0;
this.Luck = 0;
this.Morale = 1;
this.Initiative = 0;
this.addParameter = function(Parameter, amount){
this[Parameter] += amount;
};
this.changeLevelTo = function(Level){
this.Level = Level;
};
this.changeGenderTo = function(Gender){
this.Gender = Gender;
};
this.changeFactionTo = function(Faction){
this.Faction = Faction;
//adjust default stats based on new faction
};
};
My issue lies in the fact that different factions provide stat boosts that cannot be reallocated, while leveling up allows for reallocating points spent on parameters. Additionally, equipping items grants stat boosts that also cannot be reallocated unless unequipped.
In my previous attempts, I used arrays to represent default stat boosts from factions, total boosts from equipment, and manually allocated stats. The final array would display the sum of these values, allowing players to only readjust points in one specific array.
How can I effectively implement this using object-oriented programming? Despite reading about concepts like encapsulation, inheritance, and polymorphism, I still struggle to fully grasp how to apply them practically.
-
-
Responses
This website seems a bit challenging to navigate :o
I will consider qternion's answer for guidance:
var Faction = function(atk, def, sp, kn, luk, mor, ini){
this.Attack = atk;
this.Defense = def;
this.SpellPower = sp;
this.Knowledge = kn;
this.Luck = luk;
this.Morale = mor;
this.Initiative = ini;
}
var Knight = new Faction(0,1,0,0,0,1,0);
var Wizard = new Faction(0,0,0,1,0,1,0);
To enhance the above code using prototypes, I will refer to
Thank you to everyone for your valuable contributions :)