Advertisement
Guest User

hitpoints

a guest
Jun 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Work with hitpoints.
  3.  * @type {{hitpoints}}
  4.  */
  5. export const PlayerHitpoints = (function() {
  6.     // fixed hitpoints object
  7.     let _hitpointsFixed = {};
  8.  
  9.  
  10.  
  11.     /**
  12.      * Set hitpoints for the specified frame.
  13.      * @param {Number} frame frame number
  14.      * @param {Array} hitpoints array of initial hitpoints
  15.      */
  16.     const _setHitpoints = function(frame = 0, hitpoints = []) {
  17.         // validate
  18.         if ('undefined' === typeof frame || null === frame) {return;}
  19.         if ('undefined' === typeof hitpoints || !Array.isArray(hitpoints) || !hitpoints.length) {return;}
  20.  
  21.         // add hitpoints data
  22.         _hitpointsFixed[frame] = {};
  23.  
  24.         // loop through body parts
  25.         hitpoints[0].forEach(function (item, i) {
  26.             _hitpointsFixed[frame][item] = hitpoints[2][i];
  27.         });
  28.     };
  29.  
  30.     // declare public variables and/or functions
  31.     return {
  32.         /**
  33.          * Populate the fixed hitpoints object.
  34.          * @param {Object} hitpointsOriginal original hitpoints object
  35.          * @return {Object} fixed hitpoints object
  36.          */
  37.         hitpoints: function(hitpointsOriginal) {
  38.             if ('undefined' === typeof hitpointsOriginal || 'object' !== typeof hitpointsOriginal) {return _hitpointsFixed;}
  39.  
  40.             // loop through hitpoints object
  41.             Object.keys(hitpointsOriginal).map(function (objectKey) {
  42.                 // add data for the frame
  43.                 _setHitpoints(objectKey, hitpointsOriginal[objectKey]);
  44.             });
  45.  
  46.             // result
  47.             return _hitpointsFixed;
  48.         }
  49.     }
  50. })();
  51.  
  52.  
  53. // original hitpoints object for all frames
  54. const hitpointsOriginal = {
  55.     // hitpoints for the 0 frame
  56.     0: [
  57.         ['HitFace', 'HitNeck', 'HitHead', 'HitPelvis', 'HitAbdomen', 'HitDiaphragm', 'HitChest', 'HitBody', 'HitArms', 'HitHands', 'HitLegs'],
  58.         ['', 'neck', 'head', 'pelvis', 'spine1', 'spine2', 'spine3', 'body', '', 'hands', 'legs'],
  59.         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  60.     ],
  61.  
  62.     // hitpoints for the 1 frame (body parts differ from the first array: not all elements exist and the order may be corrupted)
  63.     1: [
  64.         ['HitNeck', 'HitPelvis', 'HitHead'],
  65.         ['neck', 'pelvis', 'head'],
  66.         [1, 1, 0.5]
  67.     ]
  68. };
  69.  
  70. console.log(PlayerHitpoints.hitpoints(hitpointsOriginal));
  71.  
  72. const result = {
  73.     0: {
  74.         HitAbdomen: 0,
  75.         HitArms: 0,
  76.         HitBody: 0,
  77.         HitChest: 0,
  78.         HitDiaphragm: 0,
  79.         HitFace: 0,
  80.         HitHands: 0,
  81.         HitHead: 0,
  82.         HitLegs: 0,
  83.         HitNeck: 0,
  84.         HitPelvis: 0
  85.     },
  86.     1: {
  87.         HitNeck: 1,
  88.         HitPelvis: 1,
  89.         HitHead: 0.5
  90.     }
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement