Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Work with hitpoints.
- * @type {{hitpoints}}
- */
- export const PlayerHitpoints = (function() {
- // fixed hitpoints object
- let _hitpointsFixed = {};
- /**
- * Set hitpoints for the specified frame.
- * @param {Number} frame frame number
- * @param {Array} hitpoints array of initial hitpoints
- */
- const _setHitpoints = function(frame = 0, hitpoints = []) {
- // validate
- if ('undefined' === typeof frame || null === frame) {return;}
- if ('undefined' === typeof hitpoints || !Array.isArray(hitpoints) || !hitpoints.length) {return;}
- // add hitpoints data
- _hitpointsFixed[frame] = {};
- // loop through body parts
- hitpoints[0].forEach(function (item, i) {
- _hitpointsFixed[frame][item] = hitpoints[2][i];
- });
- };
- // declare public variables and/or functions
- return {
- /**
- * Populate the fixed hitpoints object.
- * @param {Object} hitpointsOriginal original hitpoints object
- * @return {Object} fixed hitpoints object
- */
- hitpoints: function(hitpointsOriginal) {
- if ('undefined' === typeof hitpointsOriginal || 'object' !== typeof hitpointsOriginal) {return _hitpointsFixed;}
- // loop through hitpoints object
- Object.keys(hitpointsOriginal).map(function (objectKey) {
- // add data for the frame
- _setHitpoints(objectKey, hitpointsOriginal[objectKey]);
- });
- // result
- return _hitpointsFixed;
- }
- }
- })();
- // original hitpoints object for all frames
- const hitpointsOriginal = {
- // hitpoints for the 0 frame
- 0: [
- ['HitFace', 'HitNeck', 'HitHead', 'HitPelvis', 'HitAbdomen', 'HitDiaphragm', 'HitChest', 'HitBody', 'HitArms', 'HitHands', 'HitLegs'],
- ['', 'neck', 'head', 'pelvis', 'spine1', 'spine2', 'spine3', 'body', '', 'hands', 'legs'],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- ],
- // hitpoints for the 1 frame (body parts differ from the first array: not all elements exist and the order may be corrupted)
- 1: [
- ['HitNeck', 'HitPelvis', 'HitHead'],
- ['neck', 'pelvis', 'head'],
- [1, 1, 0.5]
- ]
- };
- console.log(PlayerHitpoints.hitpoints(hitpointsOriginal));
- const result = {
- 0: {
- HitAbdomen: 0,
- HitArms: 0,
- HitBody: 0,
- HitChest: 0,
- HitDiaphragm: 0,
- HitFace: 0,
- HitHands: 0,
- HitHead: 0,
- HitLegs: 0,
- HitNeck: 0,
- HitPelvis: 0
- },
- 1: {
- HitNeck: 1,
- HitPelvis: 1,
- HitHead: 0.5
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement