@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.51 kB
JavaScript
import {CachedMap,storage}from'@drincs/pixi-vn';var u="___nqtr-time_manager_data___",b="___nqtr-current_room_memory___",L="___nqtr-last_event_memory___";var o;(a=>(a.log=(t,...n)=>console.log(`[NQTR] ${t}`,...n),a.warn=(t,...n)=>console.warn(`[NQTR] ${t}`,...n),a.error=(t,...n)=>console.error(`[NQTR] ${t}`,...n),a.info=(t,...n)=>console.info(`[NQTR] ${t}`,...n)))(o||={});var E=new CachedMap({cacheSize:20}),z;(a=>{function m(t){if(Array.isArray(t)){t.forEach(n=>a.add(n));return}E.set(t.id,t);}a.add=m;function e(t){try{let n=E.get(t);if(!n){console.warn(`[NQTR] Activity ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Activity ${t}`,n);return}}a.get=e;function r(){return Array.from(E.values())}a.values=r;function i(t){return E.has(t)}a.has=i;})(z||={});var f=new CachedMap({cacheSize:20}),_=new CachedMap({cacheSize:20}),$;(a=>{function m(t){if(Array.isArray(t)){t.forEach(n=>a.add(n));return}f.set(t.id,t);}a.add=m;function e(t){try{let n=f.get(t);if(!n){console.warn(`[NQTR] Commitment ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Commitment ${t}`,n);return}}a.get=e;function r(){return Array.from(f.values())}a.values=r;function i(t){return f.has(t)}a.has=i;})($||={});var c=$;var w=new CachedMap({cacheSize:20}),O;(a=>{function m(t){if(Array.isArray(t)){t.forEach(n=>m(n));return}w.set(t.id,t);}a.add=m;function e(t){try{let n=w.get(t);if(!n){console.warn(`[NQTR] Location ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Location ${t}`,n);return}}a.get=e;function r(){return Array.from(w.values())}a.values=r;function i(t){return w.has(t)}a.has=i;})(O||={});var g=O;var S=new CachedMap({cacheSize:20}),V;(a=>{function m(t){if(Array.isArray(t)){t.forEach(n=>m(n));return}S.set(t.id,t);}a.add=m;function e(t){try{let n=S.get(t);if(!n){console.warn(`[NQTR] Map ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Map ${t}`,n);return}}a.get=e;function r(){return Array.from(S.values())}a.values=r;function i(t){return S.has(t)}a.has=i;})(V||={});var h=V;var v=new CachedMap({cacheSize:5}),Y;(a=>{function m(t){if(Array.isArray(t)){t.forEach(n=>a.add(n));return}v.set(t.id,t);}a.add=m;function e(t){try{let n=v.get(t);if(!n){console.warn(`[NQTR] Quest ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Quest ${t}`,n);return}}a.get=e;function r(){return Array.from(v.values())}a.values=r;function i(t){return v.has(t)}a.has=i;})(Y||={});var C=Y;var A=new CachedMap({cacheSize:20}),q;(a=>{function m(t){if(Array.isArray(t)){t.forEach(Q=>m(Q));return}A.set(t.id,t);let n=t.location;g.add(n);let p=n?.map;p&&h.add(p);}a.add=m;function e(t){try{let n=A.get(t);if(!n){console.warn(`[NQTR] Room ${t} not found, you should register it first`);return}return n}catch(n){o.error(`Error while getting Room ${t}`,n);return}}a.get=e;function r(){return Array.from(A.values())}a.values=r;function i(t){return A.has(t)}a.has=i;})(q||={});var d=q;function y(m){storage.setVariable(L,m);}var T=class{get rooms(){return d.values()}get locations(){return g.values()}get maps(){return h.values()}get currentRoomId(){return storage.getVariable(b)}get currentRoom(){let e=this.currentRoomId;if(!e){o.error("The current room has not yet been set");return}let r=d.get(e);if(!r){o.error(`Current room ${e} not found`);return}return r}set currentRoom(e){if(typeof e!="string"&&(e=e.id),!d.get(e)){o.error(`The room ${e} is not registered, so it can't be set as current room`);return}let i=storage.getVariable(b);i!==e&&(y({type:"editroom",prev:i,value:e}),storage.setVariable(b,e));}get currentLocation(){return this.currentRoom?.location}get currentMap(){return this.currentRoom?.location.map}clearExpiredActivities(){d.values().forEach(e=>{e.clearExpiredActivities();});}};var I=class{get quests(){return C.values()}get startedQuests(){return this.quests.filter(e=>e.started)}get inProgressQuests(){return this.quests.filter(e=>e.inProgress)}get completedQuests(){return this.quests.filter(e=>e.completed)}get failedQuests(){return this.quests.filter(e=>e.failed)}get notStartedQuests(){return this.quests.filter(e=>!e.started)}find(e){return C.get(e)}async startsStageMustBeStarted(e){let r=this.quests.map(i=>i.currentStageMustStart?i.startCurrentStage(e):Promise.resolve());await Promise.all(r);}};var D="___nqtr-temporary_commitment___",R=class{get fixedRoutine(){return [..._.values()]}set fixedRoutine(e){e.forEach(r=>{_.has(r.id)&&console.warn(`[NQTR] Commitment id ${r.id} already exists, it will be overwritten`),_.set(r.id,r);});}get temporaryRoutine(){let e=storage.getVariable(D);return e?e.map(i=>c.get(i)).filter(i=>i!==void 0):[]}get allRoutine(){return [...this.fixedRoutine,...this.temporaryRoutine]}add(e){Array.isArray(e)||(e=[e]);let r=e.map(i=>{if(!c.get(i.id)){console.warn(`[NQTR] Commitment ${i.id} not found, it will be ignored`);return}return i.id}).filter(i=>i!==void 0);storage.setVariable(D,r);}find(e){return c.get(e)}remove(e){Array.isArray(e)||(e=[e]);let r=e.map(a=>a.id),i=storage.getVariable(D);i&&(i=i.filter(a=>!r.includes(a)),storage.setVariable(D,i));}clearExpiredRoutine(){c.values().forEach(e=>{e.expired&&f.delete(e.id);});}get currentRoutine(){let e={};return [...this.fixedRoutine,...this.temporaryRoutine].forEach(r=>{r.isActive&&(r.characters.length>0?r.characters.every(a=>!e[a.id]||r.priority>e[a.id].priority)&&r.characters.forEach(a=>{e[a.id]=r;}):o.error(`The commitment ${r.id} has no characters assigned`));}),Object.values(e)}getCommitmentByCharacter(e){this.currentRoutine.forEach(r=>{if(r.characters.map(i=>i.id).includes(e.id))return r});}};var s=class{static dayStartTime=0;static dayEndTime=24;static defaultTimeSpent=1;static timeSlots=[];static weekLength=7;static weekendStartDay=this.weekLength-1;static getDayName};var x=class{initialize(e){let{dayStartTime:r=0,dayEndTime:i=24,defaultTimeSpent:a=1,timeSlots:t=[],weekLength:n=7,weekendStartDay:p=n-1,getDayName:Q}=e;s.dayStartTime=r,s.dayEndTime=i,s.defaultTimeSpent=a,s.timeSlots=t,s.weekLength=n,p>=s.weekLength?console.warn(`[NQTR] Weekend start day should be less than week length ${n}, so will be ignored`):s.weekendStartDay=p,s.getDayName=Q;}get dayStartTime(){return s.dayStartTime}get dayEndTime(){return s.dayEndTime}get defaultTimeSpent(){return s.defaultTimeSpent}get timeSlots(){return s.timeSlots}get weekLength(){return s.weekLength}get weekendStartDay(){return s.weekendStartDay}get getDayName(){return s.getDayName}get currentTime(){let e=storage.getVariable(u)||{};return e.hasOwnProperty("currentTime")&&typeof e.currentTime=="number"?e.currentTime:this.dayStartTime}set currentTime(e){let r=storage.getVariable(u)||{},i={...r};typeof e=="number"?i.currentTime=e:delete i.currentTime,y({type:"edittime",prev:r,value:i}),storage.setVariable(u,i);}get currentDate(){let e=storage.getVariable(u)||{};return e.hasOwnProperty("currentDate")&&typeof e.currentDate=="number"?e.currentDate:0}set currentDate(e){let r=storage.getVariable(u)||{},i={...r};typeof e=="number"?i.currentDate=e:delete i.currentDate,y({type:"edittime",prev:r,value:i}),storage.setVariable(u,i);}get isWeekend(){return this.currentWeekDayNumber>=this.weekendStartDay}get currentWeekDayNumber(){return this.currentDate%this.weekLength+1}get currentDayName(){return this.getDayName?this.getDayName(this.currentWeekDayNumber-1):(console.warn("[NQTR] Week days names are not defined, so currentDayName will be empty"),"")}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 r=this.timeSlots[e];if(this.timeSlots.length>e+1&&this.nowIsBetween(r.startTime,this.timeSlots[e+1].startTime))return e}return this.timeSlots.length-1}increaseTime(e=this.defaultTimeSpent){let r=this.currentTime+e;return r>=this.dayEndTime&&(this.increaseDate(),r=this.dayStartTime+(r-this.dayEndTime)),this.currentTime=r,this.currentTime}increaseDate(e=1,r=this.dayStartTime){let i=this.currentDate+e;return this.currentDate=i,this.currentTime=r,this.currentDate}nowIsBetween(e,r){e===void 0&&(e=this.dayStartTime-1),r===void 0&&(r=this.dayEndTime+1);let i=this.currentTime;return e<r?i>=e&&i<r:i>=e||i<r}};var He=new x,Je=new T,Xe=new R,Ze=new I;export{Je as navigator,Ze as questsNotebook,Xe as routine,He as timeTracker};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map