Advertisement
joyeusenoelle

Character() example in Twine/SugarCube

Apr 28th, 2024 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <<scriptwindow.Character = function(config) {
  2.   this.name = '';
  3.   this.running = 0;
  4.   this.jumping = 0;
  5.   this.climbing_trees = 0;
  6.  
  7.   this.skill_check = function(attr, dc) {
  8.     if (attr in Object.keys(this) {
  9.       const dieSize = 20; // change this for other kinds of dice
  10.       // Add 1 because otherwise this rolls 0-19
  11.       return (Math.floor(Math.random() * dieSize) + 1 + this[attr]) >= dc;
  12.     } else {
  13.       return this.name + " doesn't have the skill " + attr + '.';
  14.     }
  15.   };
  16.  
  17.   Object.keys(config).forEach(function(attr) {
  18.     if (attr in Object.keys(this) {
  19.       this[attr] = clone(config[attr]); // you have to do this so changing config[attr] doesn't change Character[attr]
  20.     }
  21.   }, this);
  22. }
  23.  
  24. Character.prototype.clone = function() {
  25.   return new Character(this);
  26. };
  27.  
  28. Character.prototype.toJSON = function () {
  29.   let ownData = {};
  30.   Object.keys(this).forEach(function(attr) {
  31.     ownData[attr] = clone(this[attr]);
  32.   }, this);
  33.   return JSON.reviveWrapper('new Character($ReviveData$)', ownData);
  34. };
  35. >>
  36.  
  37. <<set $You to new Character({
  38.   name: "My Character",
  39.   running: 1,
  40.   jumping: 2,
  41.   climbing_trees: 3
  42. })>>
  43.  
  44. <<if $You.skill_check("running", 5)>>...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement