Ver Fonte

add history to `PersonalityScale`

Stephan Fuchs há 3 meses atrás
pai
commit
9505b12e21

+ 30 - 10
sugarcube/src/playerCharacter/PlayerCharacter.ts

@@ -370,25 +370,45 @@ class PlayerCharacter{
 			}
 		//#endregion
 
-		_personalityValues:{[key:string]:number} = {};
+		_personalityValues:{
+			[key:string]:{				
+				/**
+				 * Index 0: DaystartS, Index 1: Current at that day
+				 * @type {number[][]}
+				 */
+				history:number[][]
+			}
+		} = {};
 
 		personalityScale(scaleId:string):PersonalityScale{
 			if(scaleId in this._personalityValues)
-				return PersonalityScale.get(scaleId,this._personalityValues[scaleId]);
-			return this.#personalityScaleInitialize(scaleId);
+				return PersonalityScale.get(scaleId,this);
+			return this.personalityScaleInitialize(scaleId);
 		}
 
-		#personalityScaleInitialize(scaleId:string){
-			let scale = PersonalityScale.get(scaleId);
-			const start = scale.start;
-			this._personalityValues[scaleId] = start;
-			return scale.apply(start);
+		get personalityScales():{[key:string]:PersonalityScale}{
+			let result:{[key:string]:PersonalityScale} = {};
+			for(const scaleId of Object.keys(this._personalityValues))
+				result[scaleId] = this.personalityScale(scaleId);
+			return result;
 		}
 
-		personalitySet(scaleId:string,v:number){
-			this._personalityValues[scaleId] = v;
+		personalityScaleInitialize(scaleId:string,v:number=undefined){
+			let start:number;
+			if(v === undefined){
+				const scaleData = PersonalityScale.get(scaleId,this);
+				start = scaleData.start;
+			}else
+				start = v;
+
+			const daystart = State.variables.time.daystart;
+
+			this._personalityValues[scaleId] = {history:[[daystart,start],[daystart-1,start]]};
+
+			return PersonalityScale.get(scaleId,this);
 		}
 
+
 	//#endregion
 
 	//#region Mental Capacity

+ 73 - 49
sugarcube/src/playerCharacter/_submodules/personality/PersonalityScale.ts

@@ -1,56 +1,80 @@
 /// <reference path="../../../_shared/Initializing.ts"/>
 
+const personalityScaleHistoryMaxsize = 10;
+
 class PersonalityScale extends Initializing implements PersonalityScaleDefinition{
 
-    id:string;
-    start:number;
-    hidden?: boolean;
-    interval: number[];
-    label: string | string[];
-    desc?: string;
-    image?: string;
-    msg_tooHigh?: string;
-    msg_tooLow?: string;
-    functions?: { [key: string]: (any: any) => any; };
-
-
-
-    #value = 0;
-
-    static get(scaleId:string,playerValue:number=0):PersonalityScale{
-        return (new PersonalityScale(PersonalityScale.#staticData(scaleId))).apply(playerValue).init({id:scaleId});
-    }
-
-    static #staticData(scaleId:string):{ [key: string]: any; }{
-        return setup.personalityScales[scaleId];
-    }
-
-    apply(playerValue:number){
-        this.#value = playerValue;
-        return this;
-    }
-
-    get current():number{
-        return this.#value;
-    }
-    set current(v:number){
-        v = Math.clamp(v,this.interval[0],this.interval[1]);
-        this.#value = v;
-        State.variables.pc.personalitySet(this.id,v);
-    }
-
-    requiredWillpower(targetValue:number){
-        let effecitveDifference = Math.max(0,targetValue-this.#value);
-        if(effecitveDifference <= 30)
-            return Math.ceil(Math.pow(2,effecitveDifference/10)*10)-10;
-        return Infinity;
-    }
-
-    constructor(ps={}){
-        super();
-        this.init(ps);
-    }
-    
+	id:string;
+	start:number;
+	hidden?: boolean;
+	interval: number[];
+	label: string | string[];
+	desc?: string;
+	image?: string;
+	msg_tooHigh?: string;
+	msg_tooLow?: string;
+	functions?: { [key: string]: (any: any) => any; };
+
+	#pc: PlayerCharacter;
+
+	static get(scaleId:string,pc:PlayerCharacter):PersonalityScale{
+		const newScale = new PersonalityScale(PersonalityScale.#staticData(scaleId));
+		newScale.id = scaleId;
+		newScale.#pc = pc;
+		return newScale;
+	}
+
+	static #staticData(scaleId:string):{ [key: string]: any; }{
+		return setup.personalityScales[scaleId];
+	}
+
+	get current():number{
+		return this.#dataEntry.history[0][1];
+	}
+	set current(v:number){
+		v = Math.clamp(v,this.interval[0],this.interval[1]);
+		this.#dataEntry.history[0][1] = v;
+	}
+
+	get currentYesterday(){
+		return this.#dataEntry.history[1][1];
+	}
+
+	get #dataEntry(){
+		const daystartS = State.variables.time.daystartS;
+
+		let dataEntry = this.#pc._personalityValues[this.id];
+
+		if(dataEntry.history[0][0] != daystartS)
+			dataEntry.history.unshift([daystartS,dataEntry.history[0][1]]);
+
+		if(dataEntry.history.length > personalityScaleHistoryMaxsize)
+			dataEntry.history.pop();
+
+		return dataEntry;
+	}
+
+	moveTo(v:number):number{
+		const current = this.current;
+		const diffference = v - current;
+		const inreaseRaw = diffference / 10;
+		const increaseEffective = Math.ceilAbs(inreaseRaw);
+		this.current += increaseEffective;
+		return this.current;
+	}
+
+	requiredWillpower(targetValue:number){
+		/*let effecitveDifference = Math.max(0,targetValue-this.#value);
+		if(effecitveDifference <= 30)
+			return Math.ceil(Math.pow(2,effecitveDifference/10)*10)-10;
+		return Infinity;*/
+	}
+
+	constructor(ps={}){
+		super();
+		this.init(ps);
+	}
+	
 }
 
 setup.PersonalityScale = PersonalityScale;