Advertisement
nanenj

[JavaScript] [FB Question] RPG Character Level-up Code

Jun 4th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* So, I know you're new and that's why you're asking.  I'm going to try and show you a way to do this without going way over your head, but, it might take a small leap of trust and understanding.  
  2.  
  3. So, let's take a look at what you have and just kind of enumerate things, I know it's in your code, but since there's so much repeated, let's trim down what we're looking at right now.
  4.  
  5. You have rawCharacter.   Let's just call that 'Character' */
  6.  
  7. function Character() { }
  8.  
  9. /* What are the parts of the character?  Well, you have a statblock and a classname.  You also have a series of strings that are "LOW", "MID", and "HIGH".  Let's ignore those strings for just a moment and form the rest of a character.*/
  10.  
  11. function Character(className) {
  12.     this.className = className;
  13.     this.hp = 35;
  14.     this.mp = 10;
  15.     . . .
  16. }
  17.  
  18. /* So, now we can do what you have before to an extent.*/
  19.  
  20. var charInstance = new Character("Arcane Ranger");
  21.  
  22. /* All good.  It would give you a rather generic character.  It has stats that you can reference, it has the class name, but, it doesn't have any of the different behavior you want just yet, does it?
  23.  
  24. Before we dive further, let's take a second to talk about what a class in JavaScript is, and what we're really doing when we intend on making a class.   In JavaScript, a class is just an object, not even a -special- object in most forms of JavaScript, but we call it a class because we can use a constructor, to make an object that has that specific interface.   What do I mean by interface?  Well.  If I know an object has '.hp, .mp, .atk' methods that return integer values.  That's what I mean by interface.  So, you can say when you make a class, it has a known interface.  That's what you're defining.
  25.  
  26. So, we want character to be responsible for levelling up, but, we want that behavior to be based on the character's class.  Instead of the character's class being a string, let's make a CharacterClass, class.*/
  27.  
  28. function CharacterClass() { }
  29.  
  30. /*Sweet.  What do we want it to be responsible for, well, first.  It's going to have a name.  So, let's make that the first parameter.*/
  31.  
  32. function CharacterClass(name) {
  33.     this.name = name;
  34. }
  35.  
  36. /* Now, here's something neat you can do.  Did you know you can pass functions inside of JS?*/
  37.  
  38. function CharacterClass(name, levelUpBehavior) {
  39.     this.name = name;
  40.     this.levelUp = levelUpBehavior;
  41. }
  42.  
  43. /* I see you lookin' at me weird now.
  44.  
  45. But, imagine this for a second.*/
  46.  
  47. var ArcaneRanger = new CharacterClass("Arcane Ranger", function(character) {
  48.     character.hp += 10;
  49.     character.mp += 5;
  50.     . . .
  51. });
  52.  
  53. /* So, now you've separated this, and you've gotten rid of some of the duplication in a way.  You've also implemented what's known as the Strategy pattern.  It allows you to pass the functionality the class needs, into the class so that it can be treated specifically how you want it.
  54.  
  55. I've left some out, as you did request for this not to be done for you, and I'm trying to explain and give examples to show without giving it all away.  If you'd like more info, let's talk more.  Also, if you're willing set up a jsfiddle and DM me a link to the collaboration URL and we'll pair through some of this.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement