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 8.44 kB
'use strict';var pixiVn=require('@drincs/pixi-vn');var l={};function Q(o){try{let e=l[o];if(!e){console.error(`[NQTR] Room ${o} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Room ${o}`,e);return}}var N="___nqtr-current_room_memory___",g=class{get rooms(){return Object.values(l)}get locations(){let e={};return Object.values(l).forEach(t=>{e[t.location.id]=t.location;}),Object.values(e)}get maps(){let e={};return Object.values(l).forEach(t=>{e[t.location.map.id]=t.location.map;}),Object.values(e)}get currentRoom(){let e=pixiVn.storage.getVariable(N);if(!e){console.error("[NQTR] The current room has not yet been set");return}let t=Q(e);if(!t){console.error(`[NQTR] Current room ${e} not found`);return}return t}set currentRoom(e){if(typeof e!="string"&&(e=e.id),!Q(e)){console.error(`[NQTR] The room ${e} is not registered, so it can't be set as current room`);return}pixiVn.storage.setVariable(N,e);}get currentLocation(){return this.currentRoom?.location}get currentMap(){return this.currentRoom?.location.map}clearExpiredActivities(){Object.entries(l).forEach(([e,t])=>{t.clearExpiredActivities();});}};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:s=[],weekLength:n=7,weekendStartDay:a=n-1,weekDaysNames:u=[]}=e;c.minDayHours=t,c.maxDayHours=r,c.defaultTimeSpent=i,c.timeSlots=s,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 m=new p,T=new g;var H={};function R(o){try{let e=H[o];if(!e){console.error(`[NQTR] Activity ${o} not found`);return}return e}catch(e){console.error(`[NQTR] Error while getting Activity ${o}`,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:s,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(s===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."),s&&n&&s>=n)throw new Error("[NQTR] The from day must be less than the to day.");if(s&&(a.fromDay=s),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,s={};i===0&&console.warn("[NQTR] The to day must be greater than 0, so it will be ignored."),i&&(s.toDay=i);let n=this.additionalActivitiesIds;if(n.includes(t.id)){if(Object.keys(s).length){this.editExcludedActivityScheduling(t.id,s);return}n=n.filter(a=>a!==t.id),this.setStorageProperty("additionalActivitiesIds",n);}else if(this.defaultActivitiesIds.includes(t.id)){if(Object.keys(s).length){this.editExcludedActivityScheduling(t.id,s);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,s=this.excludedActivitiesIds;i.forEach(n=>{if(n in t){let{toDay:a}=t[n];a&&a<m.currentDay&&(this.removeActivityScheduling(n),i=i.filter(u=>u!==n));}}),s.forEach(n=>{if(n in r){let{toDay:a}=r[n];a&&a<m.currentDay&&(this.removeActivityScheduling(n),s=s.filter(u=>u!==n));}});}get activities(){let t=[];return Object.entries(this.activeActivityScheduling).forEach(([r,i])=>{let s=R(r),{fromDay:n=s?.fromDay,fromHour:a=s?.fromHour,toDay:u=s?.toDay,toHour:k=s?.toHour}=i;s&&m.nowIsBetween(a,k)&&!(n&&n>m.currentDay)&&!(u&&u<m.currentDay)&&t.push(s);}),Object.entries(this.excludedActivitiesScheduling).forEach(([r,i])=>{let s=R(r);s&&s.isActive&&!(i.toDay&&i.toDay>=m.currentDay)&&t.push(s);}),this.defaultActivities.forEach(r=>{r.isActive&&!t.find(i=>i.id===r.id)&&t.push(r);}),t}};var M="__nqtr-map__",A=class extends v{constructor(e,t=[]){super(M,e,t);}get locations(){return T.locations.filter(e=>e.map.id===this.id)}};var _=class extends A{constructor(e,t={}){super(e,t.activities||[]),this.defaultName=t.name||"",this._image=t.image;}defaultName;get name(){return this.getStorageProperty("name")||this.defaultName}set name(e){this.setStorageProperty("name",e);}_image;get image(){return this._image}};module.exports=_;//# sourceMappingURL=MapBaseModel.js.map //# sourceMappingURL=MapBaseModel.js.map