@drincs/nqtr
Version:
A complete system introducing the concepts of location, time and event, producing the framework of a not-quite-point-and-click adventure game.
2 lines • 9.91 kB
JavaScript
;var pixiVn=require('@drincs/pixi-vn');var m={};function R(a){try{let e=m[a];if(!e){console.error(`[NQTR] Room ${a} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Room ${a}`,e);return}}var E="___nqtr-current_room_memory___",h=class{get rooms(){return Object.values(m)}get locations(){let e={};return Object.values(m).forEach(t=>{e[t.location.id]=t.location;}),Object.values(e)}get maps(){let e={};return Object.values(m).forEach(t=>{e[t.location.map.id]=t.location.map;}),Object.values(e)}get currentRoom(){let e=pixiVn.storage.getVariable(E);if(!e){console.error("[NQTR] The current room has not yet been set");return}let t=R(e);if(!t){console.error(`[NQTR] Current room ${e} not found`);return}return t}set currentRoom(e){if(typeof e!="string"&&(e=e.id),!R(e)){console.error(`[NQTR] The room ${e} is not registered, so it can't be set as current room`);return}pixiVn.storage.setVariable(E,e);}get currentLocation(){return this.currentRoom?.location}get currentMap(){return this.currentRoom?.location.map}clearExpiredActivities(){Object.entries(m).forEach(([e,t])=>{t.clearExpiredActivities();});}};var I={},b={};function S(a){try{let e=I[a];if(!e){console.error(`[NQTR] Commitment ${a} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Commitment ${a}`,e);return}}var x="___nqtr-temporary_commitment___",y=class{get fixedRoutine(){return Object.values(b)}set fixedRoutine(e){e.forEach(t=>{b[t.id]&&console.warn(`[NQTR] Commitment id ${t.id} already exists, it will be overwritten`),b[t.id]=t;});}get temporaryRoutine(){let e=pixiVn.storage.getVariable(x);return e?e.map(r=>S(r)).filter(r=>r!==undefined):[]}get allRoutine(){return [...this.fixedRoutine,...this.temporaryRoutine]}add(e){Array.isArray(e)||(e=[e]);let t=e.map(r=>{if(!S(r.id)){console.warn(`[NQTR] Commitment ${r.id} not found, it will be ignored`);return}return r.id}).filter(r=>r!==undefined);pixiVn.storage.setVariable(x,t);}find(e){return S(e)}remove(e){Array.isArray(e)||(e=[e]);let t=e.map(i=>i.id),r=pixiVn.storage.getVariable(x);r&&(r=r.filter(i=>!t.includes(i)),pixiVn.storage.setVariable(x,r));}clearExpiredRoutine(){Object.values(I).forEach(e=>{e.expired&&delete I[e.id];});}get currentRoutine(){let e={};return [...this.temporaryRoutine,...this.fixedRoutine].reverse().forEach(t=>{t.isActive&&(t.characters.length>0?t.characters.every(i=>!e[i.id]||t.priority>e[i.id].priority)&&t.characters.forEach(i=>{e[i.id]=t;}):console.error(`[NQTR] The commitment ${t.id} has no characters assigned`));}),Object.values(e)}get currentRoomRoutine(){return C.currentRoom?.routine||[]}getCommitmentByCharacter(e){this.currentRoutine.forEach(t=>{if(t.characters.map(r=>r.id).includes(e.id))return t});}};var d="___nqtr-time_manager_data___";var c=class{static minDayHours=0;static maxDayHours=24;static defaultTimeSpent=1;static timeSlots=[];static weekLength=7;static weekendStartDay=this.weekLength-1;static weekDaysNames=[]};var p=class{initialize(e){let{minDayHours:t=0,maxDayHours:r=24,defaultTimeSpent:i=1,timeSlots:o=[],weekLength:n=7,weekendStartDay:s=n-1,weekDaysNames:u=[]}=e;c.minDayHours=t,c.maxDayHours=r,c.defaultTimeSpent=i,c.timeSlots=o,c.weekLength=n,s>=c.weekLength?console.warn(`[NQTR] Weekend start day should be less than week length ${n}, so will be ignored`):c.weekendStartDay=s,u.length!==n?console.warn(`[NQTR] Week days names should be equal to week length ${n}, so will be ignored`):c.weekDaysNames=u;}get minDayHours(){return c.minDayHours}get maxDayHours(){return c.maxDayHours}get defaultTimeSpent(){return c.defaultTimeSpent}get timeSlots(){return c.timeSlots}get weekLength(){return c.weekLength}get weekendStartDay(){return c.weekendStartDay}get weekDaysNames(){return c.weekDaysNames}get currentHour(){let e=pixiVn.storage.getVariable(d)||{};return e.hasOwnProperty("currentHour")&&typeof e.currentHour=="number"?e.currentHour:this.minDayHours}set currentHour(e){let t=pixiVn.storage.getVariable(d)||{};typeof e=="number"?t.currentHour=e:delete t.currentHour,pixiVn.storage.setVariable(d,t);}get currentDay(){let e=pixiVn.storage.getVariable(d)||{};return e.hasOwnProperty("currentDay")&&typeof e.currentDay=="number"?e.currentDay:0}set currentDay(e){let t=pixiVn.storage.getVariable(d)||{};typeof e=="number"?t.currentDay=e:delete t.currentDay,pixiVn.storage.setVariable(d,t);}get isWeekend(){return this.currentWeekDayNumber>=this.weekendStartDay}get currentWeekDayNumber(){return this.currentDay%this.weekLength+1}get currentDayName(){let e=this.currentWeekDayNumber-1;return e>=this.weekDaysNames.length?(console.warn(`[NQTR] Week day name is not defined for day ${e}`,this.weekDaysNames),""):this.weekDaysNames[e]}get currentTimeSlot(){if(this.timeSlots.length===0)return console.warn("[NQTR] Time slots are not defined"),0;for(let e=0;e<this.timeSlots.length;e++){let t=this.timeSlots[e];if(this.timeSlots.length>e+1&&this.nowIsBetween(t.startHour,this.timeSlots[e+1].startHour))return e}return this.timeSlots.length-1}increaseHour(e=this.defaultTimeSpent){let t=this.currentHour+e;return t>=this.maxDayHours&&(this.increaseDay(),t=this.minDayHours+(t-this.maxDayHours)),this.currentHour=t,this.currentHour}increaseDay(e=this.minDayHours,t=1){let r=this.currentDay+t;return this.currentDay=r,this.currentHour=e,this.currentDay}nowIsBetween(e,t){e===undefined&&(e=this.minDayHours-1),t===undefined&&(t=this.maxDayHours+1);let r=this.currentHour;return e<t?r>=e&&r<t:r>=e||r<t}};var l=new p,C=new h,N=new y;var H={};function D(a){try{let e=H[a];if(!e){console.error(`[NQTR] Activity ${a} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Activity ${a}`,e);return}}var v=class extends pixiVn.StoredClassModel{constructor(t,r,i=[]){super(t,r);this.defaultActivities=i;}get defaultActivitiesIds(){return this.defaultActivities.map(t=>t.id)}get activeActivityScheduling(){return this.getStorageProperty("activeActivityScheduling")||{}}get excludedActivitiesScheduling(){return this.getStorageProperty("excludedActivitiesScheduling")||{}}removeActivityScheduling(t){let r=this.activeActivityScheduling;delete r[t],this.setStorageProperty("activeActivityScheduling",r);let i=this.excludedActivitiesScheduling;delete i[t],this.setStorageProperty("excludedActivitiesScheduling",i);}editActivityScheduling(t,r){this.removeActivityScheduling(t);let i=this.activeActivityScheduling;i[t]=r,this.setStorageProperty("activeActivityScheduling",i);}editExcludedActivityScheduling(t,r){this.removeActivityScheduling(t);let i=this.excludedActivitiesScheduling;i[t]=r,this.setStorageProperty("excludedActivitiesScheduling",i);}get additionalActivitiesIds(){return this.getStorageProperty("additionalActivitiesIds")||[]}get excludedActivitiesIds(){return this.getStorageProperty("excludedActivitiesIds")||[]}addActivity(t,r={}){let{hours:i,fromDay:o,toDay:n}=r,s={};if(i){if(i.from>=i.to)throw new Error("[NQTR] The from hour must be less than the to hour.");s.fromHour=i.from;}if(o===0&&console.warn("[NQTR] The from day must be greater than 0, so it will be ignored."),n===0&&console.warn("[NQTR] The to day must be greater than 0, so it will be ignored."),o&&n&&o>=n)throw new Error("[NQTR] The from day must be less than the to day.");if(o&&(s.fromDay=o),n&&(s.toDay=n),this.defaultActivitiesIds.includes(t.id)){console.warn(`[NQTR] Activity with id ${t.id} already exists, so it will be ignored.`);return}let u=this.additionalActivitiesIds;if(u.includes(t.id)){if(Object.keys(s).length){this.editActivityScheduling(t.id,s);return}else if(this.excludedActivitiesIds.includes(t.id)){this.removeActivityScheduling(t.id),console.log(`[NQTR] Activity with id ${t.id} was excluded, so it will be associated with this class again.`);return}console.warn(`[NQTR] Activity with id ${t.id} already exists, so it will be ignored.`);return}u.push(t.id),this.setStorageProperty("additionalActivitiesIds",u),Object.keys(s).length&&this.editActivityScheduling(t.id,s);}removeActivity(t,r={}){let{toDay:i}=r,o={};i===0&&console.warn("[NQTR] The to day must be greater than 0, so it will be ignored."),i&&(o.toDay=i);let n=this.additionalActivitiesIds;if(n.includes(t.id)){if(Object.keys(o).length){this.editExcludedActivityScheduling(t.id,o);return}n=n.filter(s=>s!==t.id),this.setStorageProperty("additionalActivitiesIds",n);}else if(this.defaultActivitiesIds.includes(t.id)){if(Object.keys(o).length){this.editExcludedActivityScheduling(t.id,o);return}let s=this.excludedActivitiesIds;s.push(t.id),this.setStorageProperty("excludedActivitiesIds",s);}else console.warn(`[NQTR] Activity with id ${t.id} not found, so it will be ignored.`);}clearExpiredActivities(){let t=this.activeActivityScheduling,r=this.excludedActivitiesScheduling,i=this.additionalActivitiesIds,o=this.excludedActivitiesIds;i.forEach(n=>{if(n in t){let{toDay:s}=t[n];s&&s<l.currentDay&&(this.removeActivityScheduling(n),i=i.filter(u=>u!==n));}}),o.forEach(n=>{if(n in r){let{toDay:s}=r[n];s&&s<l.currentDay&&(this.removeActivityScheduling(n),o=o.filter(u=>u!==n));}});}get activities(){let t=[];return Object.entries(this.activeActivityScheduling).forEach(([r,i])=>{let o=D(r),{fromDay:n=o?.fromDay,fromHour:s=o?.fromHour,toDay:u=o?.toDay,toHour:k=o?.toHour}=i;o&&l.nowIsBetween(s,k)&&!(n&&n>l.currentDay)&&!(u&&u<l.currentDay)&&t.push(o);}),Object.entries(this.excludedActivitiesScheduling).forEach(([r,i])=>{let o=D(r);o&&o.isActive&&!(i.toDay&&i.toDay>=l.currentDay)&&t.push(o);}),this.defaultActivities.forEach(r=>{r.isActive&&!t.find(i=>i.id===r.id)&&t.push(r);}),t}};var $="__nqtr-room__",T=class extends v{constructor(t,r,i=[]){super($,t,i);this._location=r;}get routine(){return N.currentRoutine.filter(t=>t.room.id===this.id)}get location(){return this._location}get characters(){let t=[];return this.routine.forEach(r=>{t.push(...r.characters);}),t}};module.exports=T;//# sourceMappingURL=RoomStoredClass.js.map
//# sourceMappingURL=RoomStoredClass.js.map