UNPKG

@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 10.8 kB
'use strict';var pixiVn=require('@drincs/pixi-vn');var m={};function _(s){try{let e=m[s];if(!e){console.error(`[NQTR] Room ${s} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Room ${s}`,e);return}}var C="___nqtr-current_room_memory___",g=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(C);if(!e){console.error("[NQTR] The current room has not yet been set");return}let t=_(e);if(!t){console.error(`[NQTR] Current room ${e} not found`);return}return t}set currentRoom(e){if(typeof e!="string"&&(e=e.id),!_(e)){console.error(`[NQTR] The room ${e} is not registered, so it can't be set as current room`);return}pixiVn.storage.setVariable(C,e);}get currentLocation(){return this.currentRoom?.location}get currentMap(){return this.currentRoom?.location.map}clearExpiredActivities(){Object.entries(m).forEach(([e,t])=>{t.clearExpiredActivities();});}};var b={},S={};function w(s){try{let e=b[s];if(!e){console.error(`[NQTR] Commitment ${s} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Commitment ${s}`,e);return}}var D="___nqtr-temporary_commitment___",y=class{get fixedRoutine(){return Object.values(S)}set fixedRoutine(e){e.forEach(t=>{S[t.id]&&console.warn(`[NQTR] Commitment id ${t.id} already exists, it will be overwritten`),S[t.id]=t;});}get temporaryRoutine(){let e=pixiVn.storage.getVariable(D);return e?e.map(r=>w(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(!w(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(D,t);}find(e){return w(e)}remove(e){Array.isArray(e)||(e=[e]);let t=e.map(i=>i.id),r=pixiVn.storage.getVariable(D);r&&(r=r.filter(i=>!t.includes(i)),pixiVn.storage.setVariable(D,r));}clearExpiredRoutine(){Object.values(b).forEach(e=>{e.expired&&delete b[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 k.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:a=n-1,weekDaysNames:u=[]}=e;c.minDayHours=t,c.maxDayHours=r,c.defaultTimeSpent=i,c.timeSlots=o,c.weekLength=n,a>=c.weekLength?console.warn(`[NQTR] Weekend start day should be less than week length ${n}, so will be ignored`):c.weekendStartDay=a,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,k=new g,R=new y;var P={};function T(s){try{let e=P[s];if(!e){console.error(`[NQTR] Activity ${s} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Activity ${s}`,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,a={};if(i){if(i.from>=i.to)throw new Error("[NQTR] The from hour must be less than the to hour.");a.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&&(a.fromDay=o),n&&(a.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(a).length){this.editActivityScheduling(t.id,a);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(a).length&&this.editActivityScheduling(t.id,a);}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(a=>a!==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 a=this.excludedActivitiesIds;a.push(t.id),this.setStorageProperty("excludedActivitiesIds",a);}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:a}=t[n];a&&a<l.currentDay&&(this.removeActivityScheduling(n),i=i.filter(u=>u!==n));}}),o.forEach(n=>{if(n in r){let{toDay:a}=r[n];a&&a<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=T(r),{fromDay:n=o?.fromDay,fromHour:a=o?.fromHour,toDay:u=o?.toDay,toHour:O=o?.toHour}=i;o&&l.nowIsBetween(a,O)&&!(n&&n>l.currentDay)&&!(u&&u<l.currentDay)&&t.push(o);}),Object.entries(this.excludedActivitiesScheduling).forEach(([r,i])=>{let o=T(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 M="__nqtr-room__",I=class extends v{constructor(t,r,i=[]){super(M,t,i);this._location=r;}get routine(){return R.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}};var Q=class extends I{constructor(e,t,r={}){super(e,t,r.activities||[]),this.defaultName=r.name||"",this._image=r.image,this.defaultDisabled=r.disabled||false,this.defaultHidden=r.hidden||false,this._icon=r.icon;}defaultName;get name(){return this.getStorageProperty("name")||this.defaultName}set name(e){this.setStorageProperty("name",e);}_image;get image(){return this._image}defaultDisabled;get disabled(){let e=this.getStorageProperty("disabled")||this.defaultDisabled;return typeof e=="string"?pixiVn.getFlag(e):e}set disabled(e){this.setStorageProperty("disabled",e);}defaultHidden;get hidden(){let e=this.getStorageProperty("hidden")||this.defaultHidden;return typeof e=="string"?pixiVn.getFlag(e):e}set hidden(e){this.setStorageProperty("hidden",e);}_icon;get icon(){return this._icon}get routine(){return R.currentRoutine.filter(e=>e.room.id===this.id)}};module.exports=Q;//# sourceMappingURL=RoomBaseModel.js.map //# sourceMappingURL=RoomBaseModel.js.map