2
0

PlayerCharacter.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /// <reference path="common/GameObject.ts" />
  2. class PlayerCharacter extends GameObject{
  3. //#region Attributes, Proficiencies and Skills
  4. //#region Attributes
  5. private _attributes = new setup.Skills();
  6. public get attributes(){
  7. return this.attributesProxy;
  8. }
  9. private $attributesProxy:undefined|{[skillId:string]:Skill} = undefined;
  10. private get attributesProxy(){
  11. this.$attributesProxy ??= setup.Skills.createProxy(this._attributes);
  12. return this.$attributesProxy;
  13. }
  14. //#endregion
  15. //#region Proficiencies
  16. public get proficiencies(){
  17. return this.proficienciesProxy;
  18. }
  19. private $proficienciesProxy:undefined|{[skillId:string]:Skill} = undefined;
  20. private get proficienciesProxy(){
  21. const pc = this;
  22. this.$proficienciesProxy ??= new Proxy({}, {
  23. get(playerCharacter, proficiencyId){
  24. if(typeof proficiencyId != "string"){
  25. if(proficiencyId == Symbol.toStringTag)
  26. return "{}";
  27. throw new Error(`Unexpected symbol in Proficiencies.Proxy.get(): '${proficiencyId.toString()}'`);
  28. }
  29. try{
  30. if(setup.proficiencies[proficiencyId])
  31. return {level: setup.proficiencies[proficiencyId].calc(pc.attributes,pc.skills)};
  32. }
  33. catch(e){
  34. console.error(e);
  35. }
  36. return {level: 0};
  37. },
  38. ownKeys(playerCharacter) {
  39. return Object.keys(setup.proficiencies);
  40. },
  41. }) as unknown as {[proficiencyId:string]:Skill};
  42. return this.$proficienciesProxy;
  43. }
  44. //#endregion
  45. //#region Skills
  46. private _skills = new setup.Skills();
  47. public get skills(){
  48. return this.skillsProxy;
  49. }
  50. public skillEperienceGainCallback(skillId:string, gain:number){
  51. const attributeGainFactor = 0.2;
  52. const skillDefinition = setup.skills[skillId] ?? {attributes:[]};
  53. for(const attributeId of skillDefinition.attributes)
  54. this.attributes[attributeId].experience += gain * attributeGainFactor;
  55. }
  56. private $skillsProxy:undefined|{[skillId:string]:Skill} = undefined;
  57. private get skillsProxy(){
  58. this.$skillsProxy ??= setup.Skills.createProxy(this._skills);
  59. return this.$skillsProxy;
  60. }
  61. //#endregion
  62. //#endregion
  63. //#region SYSTEM
  64. constructor(){
  65. super();
  66. this._postInit();
  67. }
  68. _postInit(){
  69. this._skills.skillExperienceGainCallback = (skillId:string, gain:number) => this.skillEperienceGainCallback(skillId, gain);
  70. }
  71. clone = function () {
  72. return (new setup.PlayerCharacter())._init(this.clonableFields);
  73. };
  74. toJSON = function () {
  75. return JSON.reviveWrapper('(new setup.PlayerCharacter())._init($ReviveData$)', this.ownData);
  76. };
  77. //#endregion
  78. }
  79. setup.PlayerCharacter = PlayerCharacter;