123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- class Pain{
- get OWNER(){return State.variables.pc}
- _pain:{[key: string]:number} = {
- }
- pain(bodypart){
- return this._pain[bodypart] || 0;
- }
- get painByBodyparts():{[key: string]:number}{
- var result = {};
- var painRaw:{[key: string]:Array<number>} = {};
- for (const [bodypart, currentPain] of Object.entries(this._pain)) {
- painRaw[bodypart] = [currentPain];
- }
- for (const [bodypart, pain] of Object.entries(this.painOfActiveEffects)) {
- painRaw[bodypart] ??= [];
- painRaw[bodypart] = painRaw[bodypart].concat(pain);
- }
- for(const [bodypartId,painValues] of Object.entries(painRaw)){
- let painValuesSorted:Array<number> = painValues.toSorted((a,b) => b - a);
- let i=0;
- let sum = 0;
- for(const painValue of painValuesSorted){
- sum += painValue / Math.pow(2,i++);
- }
- if(sum > 0)
- result[bodypartId] = sum;
- }
- return result;
- }
- painDeteriorate(timeToAdd){
- for (const pain_key of Object.keys(this._pain))
- this.painInc(pain_key,timeToAdd*this.painDeteriorationRate);
- }
- get painDeteriorationRate(){
- return 1/120;
- }
- get painOfActiveEffects():{[key: string]:number}{
- return this.OWNER.painOfActiveEffects;
- }
- get painRaw(){
- let sum = 0;
- let hurtingBodyParts = [];
- for (const [bodypart, currentPain] of Object.entries(this.painByBodyparts)) {
- if(currentPain > 0){
- hurtingBodyParts.push({b: bodypart, p: currentPain});
- }
- }
- hurtingBodyParts.sort((a,b) => b.p - a.p);
- let i=0;
- for(let hurtingBodyPart of hurtingBodyParts){
- sum += hurtingBodyPart.p / Math.pow(2,i++);
- }
- sum = Math.clamp(sum,0,100);
- let result = {
- sum: sum,
- hurtingBodyParts: hurtingBodyParts
- }
- return result;
- }
- /**
- *
- * @param {string} bodypart The ID of the bodypart where the pain has to be increased.
- * @param {number} v The amount by which to increase the pain.
- */
- painInc(bodypart,v){
- let c = this.pain(bodypart);
- this.painSet(bodypart,c+v);
- }
- painIncAll(v){
- for (const [bodypart, currentPain] of Object.entries(this._pain)) {
- this.painInc(bodypart,v);
- }
- }
- painSet(bodypart,v){
- const bodypartData = setup.getBodypart(bodypart);
- const bodypartId = bodypartData.id;
- this._pain[bodypartId] = Math.clamp(v,0,100);
- console.log('Pain Set',bodypartId,v);
- }
- get painTotal(){
- const painCalculated = this.painRaw.sum;
- return painCalculated;
- }
- constructor(pain={}){this._pain=pain}
- _init(data){
- Object.keys(data).forEach(function (pn) {
- this[pn] = clone(data[pn]);
- }, this);
- return this;
- }
- clone = function () {
- return (new Pain())._init(this);
- };
- toJSON = function () {
- var ownData = {};
- Object.keys(this).forEach(function (data) {
- if(typeof this[data] !== "function")
- ownData[data] = clone(this[data]);
- }, this);
- return JSON.reviveWrapper('(new setup.Pain())._init($ReviveData$)', ownData);
- };
- }
- setup.Pain = Pain;
|