Skill.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /// <reference path="../common/GameObject.ts" />
  2. const maxLevel = 1000;
  3. class Skill extends GameObject{
  4. $skillExperienceGainCallback: (gain:number) => any = (gain:number) => undefined;
  5. muta = 0;
  6. private get difficultyAdjustment(){return 60;}
  7. private _experience = 0;
  8. public get experience(){
  9. return this._experience;
  10. }
  11. public set experience(v:number){
  12. if(typeof v != "number")
  13. throw new Error(`Unexpected value for Skill.experience.set(): '${v}'`);
  14. const newExperience = Math.max(0,v);
  15. const gain = newExperience - this._experience;
  16. this.$skillExperienceGainCallback(gain);
  17. this._experience = newExperience;
  18. }
  19. public get level(){
  20. return this.experienceToLevel(this._experience);
  21. }
  22. public set level(v:number){
  23. this.experience = this.levelToExperience(v);
  24. }
  25. levelInitialize(level: number, overwrite = false){
  26. if(this._experience == 0 || overwrite)
  27. this._experience = this.levelToExperience(level);
  28. }
  29. private experienceRequiredForLevelN(n:number){
  30. if(!n)
  31. return 0;
  32. return Math.floor(73 / 2730 * this.difficultyAdjustment * n**2 + 1);
  33. }
  34. private experienceToLevel(exp:number){
  35. for(let i = 1; i<= maxLevel; i++){
  36. if(exp < this.experienceRequiredForLevelN(i))
  37. return i - 1;
  38. }
  39. }
  40. private levelToExperience(lvl:number){
  41. return this.experienceRequiredForLevelN(lvl);
  42. }
  43. //#region SYSTEM
  44. constructor(
  45. ){
  46. super();
  47. }
  48. clone = function () {
  49. return (new setup.Skill())._init(this.clonableFields);
  50. };
  51. toJSON = function () {
  52. return JSON.reviveWrapper('(new setup.Skill())._init($ReviveData$)', this.ownData);
  53. };
  54. //#endregion
  55. }
  56. setup.Skill = Skill;