NPCs.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. class NPCs{
  2. npc(npcId:string):NPC{
  3. return new Proxy(new NPCAccessor(npcId),NPCAccessor.handler);
  4. }
  5. get(npcId:string,field:string,def:any=undefined):any{
  6. let data = this.npcData(npcId);
  7. if(field in data)
  8. return data[field];
  9. /*if("passage" in data){
  10. let resultFromPassage = setup.func(data.passage,'vars',field);
  11. if(resultFromPassage)
  12. return resultFromPassage;
  13. }*/
  14. return def;
  15. }
  16. getDynamicData(npcId:string){
  17. return State.variables.npcDynamic[npcId] ?? {};
  18. }
  19. getStaticData(npcId:string){
  20. return setup.npcStatic[npcId] ?? {};
  21. }
  22. npcData(npcId:string){
  23. let staticData = this.getStaticData(npcId);
  24. let dynamicData = this.getDynamicData(npcId);
  25. let data = Object.assign({},staticData,dynamicData);
  26. /*if("gender" in data)
  27. data = Object.assign({},setup.npcDefaults.gender[data.gender],data); //Apply the gender-defaults first.
  28. if("defaults" in data){
  29. for(let defaultId of data.defaults){
  30. let def = setup.npcDefaults[defaultId];
  31. data = Object.assign({},def,data); //Apply the defaults first. They could be overwritten by static or dynamic data
  32. }
  33. }*/
  34. return data;
  35. }
  36. set(npcId:string,field:string,v:any){
  37. State.variables.npcDynamic[npcId] ??= {};
  38. /*if(field in npcFieldBoundaries){
  39. let fieldRules = npcFieldBoundaries[field];
  40. if("min" in fieldRules)
  41. v = Math.max(v, fieldRules.min);
  42. if("max" in fieldRules)
  43. v = Math.min(v, fieldRules.max);
  44. }*/
  45. State.variables.npcDynamic[npcId][field] = v;
  46. console.log("NPC value set",npcId,field,v);
  47. }
  48. //#region System
  49. constructor(){}
  50. _init(nPCsDict){
  51. Object.keys(nPCsDict).forEach(function (pn) {
  52. this[pn] = clone(nPCsDict[pn]);
  53. }, this);
  54. return this;
  55. }
  56. clone = function () {
  57. return (new setup.NPCs())._init(this);
  58. };
  59. toJSON = function () {
  60. var ownData = {};
  61. Object.keys(this).forEach(function (pn) {
  62. if(typeof this[pn] !== "function")
  63. ownData[pn] = clone(this[pn]);
  64. }, this);
  65. return JSON.reviveWrapper('(new setup.NPCs())._init($ReviveData$)', ownData);
  66. };
  67. //#endregion
  68. }
  69. setup.NPCs = NPCs;