pc_pain.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. class Pain{
  2. get OWNER(){return State.variables.pc}
  3. _pain:{[key: string]:number} = {
  4. }
  5. pain(bodypart){
  6. return this._pain[bodypart] || 0;
  7. }
  8. get painByBodyparts():{[key: string]:number}{
  9. var result = {};
  10. var painRaw:{[key: string]:Array<number>} = {};
  11. for (const [bodypart, currentPain] of Object.entries(this._pain)) {
  12. painRaw[bodypart] = [currentPain];
  13. }
  14. for (const [bodypart, pain] of Object.entries(this.painOfActiveEffects)) {
  15. painRaw[bodypart] ??= [];
  16. painRaw[bodypart] = painRaw[bodypart].concat(pain);
  17. }
  18. for(const [bodypartId,painValues] of Object.entries(painRaw)){
  19. let painValuesSorted:Array<number> = painValues.toSorted((a,b) => b - a);
  20. let i=0;
  21. let sum = 0;
  22. for(const painValue of painValuesSorted){
  23. sum += painValue / Math.pow(2,i++);
  24. }
  25. if(sum > 0)
  26. result[bodypartId] = sum;
  27. }
  28. return result;
  29. }
  30. painDeteriorate(timeToAdd){
  31. for (const pain_key of Object.keys(this._pain))
  32. this.painInc(pain_key,timeToAdd*this.painDeteriorationRate);
  33. }
  34. get painDeteriorationRate(){
  35. return 1/120;
  36. }
  37. get painOfActiveEffects():{[key: string]:number}{
  38. return this.OWNER.painOfActiveEffects;
  39. }
  40. get painRaw(){
  41. let sum = 0;
  42. let hurtingBodyParts = [];
  43. for (const [bodypart, currentPain] of Object.entries(this.painByBodyparts)) {
  44. if(currentPain > 0){
  45. hurtingBodyParts.push({b: bodypart, p: currentPain});
  46. }
  47. }
  48. hurtingBodyParts.sort((a,b) => b.p - a.p);
  49. let i=0;
  50. for(let hurtingBodyPart of hurtingBodyParts){
  51. sum += hurtingBodyPart.p / Math.pow(2,i++);
  52. }
  53. sum = Math.clamp(sum,0,100);
  54. let result = {
  55. sum: sum,
  56. hurtingBodyParts: hurtingBodyParts
  57. }
  58. return result;
  59. }
  60. /**
  61. *
  62. * @param {string} bodypart The ID of the bodypart where the pain has to be increased.
  63. * @param {number} v The amount by which to increase the pain.
  64. */
  65. painInc(bodypart,v){
  66. let c = this.pain(bodypart);
  67. this.painSet(bodypart,c+v);
  68. }
  69. painIncAll(v){
  70. for (const [bodypart, currentPain] of Object.entries(this._pain)) {
  71. this.painInc(bodypart,v);
  72. }
  73. }
  74. painSet(bodypart,v){
  75. const bodypartData = setup.getBodypart(bodypart);
  76. const bodypartId = bodypartData.id;
  77. this._pain[bodypartId] = Math.clamp(v,0,100);
  78. console.log('Pain Set',bodypartId,v);
  79. }
  80. get painTotal(){
  81. const painCalculated = this.painRaw.sum;
  82. return painCalculated;
  83. }
  84. constructor(pain={}){this._pain=pain}
  85. _init(data){
  86. Object.keys(data).forEach(function (pn) {
  87. this[pn] = clone(data[pn]);
  88. }, this);
  89. return this;
  90. }
  91. clone = function () {
  92. return (new Pain())._init(this);
  93. };
  94. toJSON = function () {
  95. var ownData = {};
  96. Object.keys(this).forEach(function (data) {
  97. if(typeof this[data] !== "function")
  98. ownData[data] = clone(this[data]);
  99. }, this);
  100. return JSON.reviveWrapper('(new setup.Pain())._init($ReviveData$)', ownData);
  101. };
  102. }
  103. setup.Pain = Pain;