3
0

NPC.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. setup.npcs = {};
  2. const enum EGender{
  3. MALE = 0,
  4. FEMALE = 1
  5. }
  6. class NPC{
  7. _npcID;
  8. /**
  9. * @static
  10. * @readonly
  11. * @type {NPCsDict}
  12. */
  13. static get DICT(){ return State.variables.npcs;}
  14. static _getHandler = function(target, prop){
  15. if(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop)?.get){
  16. return target[prop];
  17. }
  18. if(typeof target[prop] === 'function')
  19. return new Proxy(target[prop], {
  20. apply: (functionName, thisArg, argumentsList) => Reflect.apply(functionName, target, argumentsList)
  21. });
  22. return target.get(prop);
  23. }
  24. static handler = {
  25. get(target, prop) {
  26. if(prop.endsWith('_possessive')){
  27. prop = prop.replace('_possessive','');
  28. let value = NPC._getHandler(target,prop);
  29. if(value.endsWith('s'))
  30. return value+"'"; //https://dictionary.cambridge.org/grammar/british-grammar/possession-john-s-car-a-friend-of-mine
  31. return value + "'s";
  32. }
  33. return NPC._getHandler(target,prop);
  34. },
  35. set(target, prop, value, receiver){
  36. //console.warn(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop));
  37. if(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop)?.set){
  38. target[prop] = value;
  39. return true;
  40. }
  41. target.set(prop,value);
  42. return true;
  43. }
  44. };
  45. get appearanceDemand(){return this.get('ad');}
  46. get age(){
  47. let dayOfBirth = this.get('dob');
  48. let day = dayOfBirth % 100;
  49. let month = Math.floor((dayOfBirth % 10000)/100);
  50. let year = Math.floor(dayOfBirth / 10000);
  51. return State.variables.time.ageOfDate(day,month,year);
  52. }
  53. set age(v){
  54. let birthday = State.variables.time.dateOfAge(v);
  55. this.birthday = birthday;
  56. }
  57. get birthday(){
  58. let dayOfBirth = this.get('dob');
  59. let day = dayOfBirth % 100;
  60. let month = Math.floor((dayOfBirth % 10000)/100);
  61. let year = Math.floor(dayOfBirth / 10000);
  62. return new Date(Date.UTC(year,month-1,day));
  63. }
  64. set birthday(v){
  65. let dob = v.getUTCFullYear()*10000 + (v.getUTCMonth()+1)*100 + v.getUTCDate();
  66. this.set('dob',dob);
  67. }
  68. get dick_girth(){
  69. let thdick = this.get('thdick');
  70. switch(thdick){
  71. case 'skinny':
  72. return 0;
  73. case 'slim':
  74. return 5;
  75. case 'well proportioned':
  76. return 10;
  77. case 'thicker than average':
  78. return 15;
  79. case 'thick':
  80. return 20;
  81. case 'massive':
  82. return 25;
  83. case 'monstrous':
  84. return 30;
  85. default :
  86. return 0;
  87. }
  88. }
  89. get dick_size(){ //Average: 3
  90. let ds_dick = this.get('dick',15);
  91. let ds_dick_girth = this.dick_girth;
  92. let ds_average = (ds_dick + ds_dick_girth) / 2;
  93. return Math.max(Math.ceil(ds_average / 5),0);
  94. }
  95. get fam(){
  96. return this.get('fam',0);
  97. }
  98. set fam(v){
  99. this.set('fam',v);
  100. }
  101. get fullname(){return this.get('firstname')+' '+this.get('lastname');}
  102. get location(){
  103. return NPC.DICT.location(this._npcID);
  104. }
  105. get title(){
  106. switch (this.get('gender')) {
  107. case 0:
  108. return 'Mr.'
  109. case 1:
  110. return 'Mrs.'
  111. default:
  112. return 'Mrx.'
  113. }
  114. }
  115. get titlename(){return `${this.title} ${this.get('lastname')}`;}
  116. get usedname(){
  117. const presetUsedName = this.get('usedname');
  118. if(presetUsedName)
  119. return presetUsedName;
  120. switch (this.get('nameScheme')) {
  121. case 'title_lastname':
  122. return this.titlename;
  123. case 'firstname':
  124. return this.get('firstname');
  125. default:
  126. return this.get('nickname') || this.get('firstname');
  127. }
  128. }
  129. get(key,def=undefined){
  130. return NPC.DICT.get(this._npcID,key,def);
  131. }
  132. set(key,v){
  133. return NPC.DICT.set(this._npcID,key,v);
  134. }
  135. endActivity(activity=undefined){
  136. console.warn('endActivity',this);
  137. return NPC.DICT.endActivity(this._npcID,activity);
  138. }
  139. constructor(npcId=undefined){
  140. if(npcId)
  141. this._npcID = npcId;
  142. }
  143. _init(nPCsDict){
  144. Object.keys(nPCsDict).forEach(function (pn) {
  145. this[pn] = clone(nPCsDict[pn]);
  146. }, this);
  147. return this;
  148. }
  149. clone = function () {
  150. return (new setup.NPC())._init(this);
  151. };
  152. toJSON = function () {
  153. var ownData = {};
  154. Object.keys(this).forEach(function (pn) {
  155. if(typeof this[pn] !== "function")
  156. ownData[pn] = clone(this[pn]);
  157. }, this);
  158. return JSON.reviveWrapper('(new setup.NPC())._init($ReviveData$)', ownData);
  159. };
  160. }
  161. setup.NPC = NPC;