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 20.8 kB
import {StoredClassModel,getFlag,storage}from'@drincs/pixi-vn';var g={};function j(a){try{let e=g[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 V="___nqtr-current_room_memory___",R=class{get rooms(){return Object.values(g)}get locations(){let e={};return Object.values(g).forEach(t=>{e[t.location.id]=t.location;}),Object.values(e)}get maps(){let e={};return Object.values(g).forEach(t=>{e[t.location.map.id]=t.location.map;}),Object.values(e)}get currentRoom(){let e=storage.getVariable(V);if(!e){console.error("[NQTR] The current room has not yet been set");return}let t=j(e);if(!t){console.error(`[NQTR] Current room ${e} not found`);return}return t}set currentRoom(e){if(typeof e!="string"&&(e=e.id),!j(e)){console.error(`[NQTR] The room ${e} is not registered, so it can't be set as current room`);return}storage.setVariable(V,e);}get currentLocation(){return this.currentRoom?.location}get currentMap(){return this.currentRoom?.location.map}clearExpiredActivities(){Object.entries(g).forEach(([e,t])=>{t.clearExpiredActivities();});}};var E={},w={};function C(a){try{let e=E[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 Q="___nqtr-temporary_commitment___",T=class{get fixedRoutine(){return Object.values(w)}set fixedRoutine(e){e.forEach(t=>{w[t.id]&&console.warn(`[NQTR] Commitment id ${t.id} already exists, it will be overwritten`),w[t.id]=t;});}get temporaryRoutine(){let e=storage.getVariable(Q);return e?e.map(r=>C(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(!C(r.id)){console.warn(`[NQTR] Commitment ${r.id} not found, it will be ignored`);return}return r.id}).filter(r=>r!==undefined);storage.setVariable(Q,t);}find(e){return C(e)}remove(e){Array.isArray(e)||(e=[e]);let t=e.map(i=>i.id),r=storage.getVariable(Q);r&&(r=r.filter(i=>!t.includes(i)),storage.setVariable(Q,r));}clearExpiredRoutine(){Object.values(E).forEach(e=>{e.expired&&delete E[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 h.currentRoom?.routine||[]}getCommitmentByCharacter(e){this.currentRoutine.forEach(t=>{if(t.characters.map(r=>r.id).includes(e.id))return t});}};var f="___nqtr-time_manager_data___";var u=class{static minDayHours=0;static maxDayHours=24;static defaultTimeSpent=1;static timeSlots=[];static weekLength=7;static weekendStartDay=this.weekLength-1;static weekDaysNames=[]};var D=class{initialize(e){let{minDayHours:t=0,maxDayHours:r=24,defaultTimeSpent:i=1,timeSlots:n=[],weekLength:o=7,weekendStartDay:s=o-1,weekDaysNames:c=[]}=e;u.minDayHours=t,u.maxDayHours=r,u.defaultTimeSpent=i,u.timeSlots=n,u.weekLength=o,s>=u.weekLength?console.warn(`[NQTR] Weekend start day should be less than week length ${o}, so will be ignored`):u.weekendStartDay=s,c.length!==o?console.warn(`[NQTR] Week days names should be equal to week length ${o}, so will be ignored`):u.weekDaysNames=c;}get minDayHours(){return u.minDayHours}get maxDayHours(){return u.maxDayHours}get defaultTimeSpent(){return u.defaultTimeSpent}get timeSlots(){return u.timeSlots}get weekLength(){return u.weekLength}get weekendStartDay(){return u.weekendStartDay}get weekDaysNames(){return u.weekDaysNames}get currentHour(){let e=storage.getVariable(f)||{};return e.hasOwnProperty("currentHour")&&typeof e.currentHour=="number"?e.currentHour:this.minDayHours}set currentHour(e){let t=storage.getVariable(f)||{};typeof e=="number"?t.currentHour=e:delete t.currentHour,storage.setVariable(f,t);}get currentDay(){let e=storage.getVariable(f)||{};return e.hasOwnProperty("currentDay")&&typeof e.currentDay=="number"?e.currentDay:0}set currentDay(e){let t=storage.getVariable(f)||{};typeof e=="number"?t.currentDay=e:delete t.currentDay,storage.setVariable(f,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 d=new D,h=new R,N=new T;var Z="__nqtr-activity__",m=class extends StoredClassModel{constructor(t,r,i,n=Z){super(n,t);this._onRun=r;this._fromHour=i.fromHour,this._toHour=i.toHour,this._fromDay=i.fromDay,this._toDay=i.toDay;}_fromHour;get fromHour(){return this._fromHour}_toHour;get toHour(){return this._toHour}_fromDay;get fromDay(){return this._fromDay}_toDay;get toDay(){return this._toDay}get run(){return t=>this._onRun(this,t)}get expired(){return !!(this.toDay&&this.toDay<=d.currentDay)}get isActive(){return !(this.fromDay&&this.fromDay>d.currentDay||this.toDay&&this.toDay<d.currentDay||!d.nowIsBetween(this.fromHour,this.toHour))}};var H=class extends m{constructor(e,t,r){super(e,t,{fromHour:r.fromHour,toHour:r.toHour,fromDay:r.fromDay,toDay:r.toDay}),this.defaultName=r.name||"",this.defaultDisabled=r.disabled||false,this.defaultHidden=r.hidden||false,this._icon=r.renderIcon;}defaultName;get name(){return this.getStorageProperty("name")||this.defaultName}set name(e){this.setStorageProperty("name",e);}defaultDisabled;get disabled(){let e=this.getStorageProperty("disabled")||this.defaultDisabled;return typeof e=="string"?getFlag(e):e}set disabled(e){this.setStorageProperty("disabled",e);}defaultHidden;get hidden(){let e=this.getStorageProperty("hidden")||this.defaultHidden;return typeof e=="string"?getFlag(e):e}set hidden(e){this.setStorageProperty("hidden",e);}_icon;get icon(){return this._icon}};var ee="__nqtr-commitment__",y=class extends m{constructor(t,r,i,n,o){n=n||(()=>{});super(t,n,o,ee);this._characters=r;this._room=i;this.defaultExecutionType=o.executionType||"interaction",this.defaultPriority=o.priority;}defaultExecutionType;defaultPriority;get characters(){return this._characters}get room(){return this._room}get executionType(){return this.getStorageProperty("executionType")||this.defaultExecutionType}set executionType(t){this.setStorageProperty("executionType",t);}get priority(){return this.getStorageProperty("priority")||this.defaultPriority||0}set priority(t){this.setStorageProperty("priority",t);}};var O=class extends y{constructor(e,t,r,i){super(e,t?Array.isArray(t)?t:[t]:[],r,i.onRun,{executionType:i.executionType,priority:i.priority,fromHour:i.fromHour,toHour:i.toHour,fromDay:i.fromDay,toDay:i.toDay}),this._name=i.name||"",this._image=i.image,this.defaultDisabled=i.disabled||false,this.defaultHidden=i.hidden||false,this._icon=i.icon;}_name;get name(){return this._name}_image;get image(){return this._image}_icon;get icon(){return this._icon}defaultDisabled;get disabled(){let e=this.getStorageProperty("disabled")||this.defaultDisabled;return typeof e=="string"?getFlag(e):e}set disabled(e){this.setStorageProperty("disabled",e);}defaultHidden;get hidden(){if(this.fromDay&&this.fromDay>d.currentDay||!d.nowIsBetween(this.fromHour,this.toHour)||!this.expired)return true;let e=this.getStorageProperty("hidden")||this.defaultHidden;return typeof e=="string"?getFlag(e):e}set hidden(e){this.setStorageProperty("hidden",e);}};var te={};function q(a){try{let e=te[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 l=class extends 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:n,toDay:o}=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(n===0&&console.warn("[NQTR] The from day must be greater than 0, so it will be ignored."),o===0&&console.warn("[NQTR] The to day must be greater than 0, so it will be ignored."),n&&o&&n>=o)throw new Error("[NQTR] The from day must be less than the to day.");if(n&&(s.fromDay=n),o&&(s.toDay=o),this.defaultActivitiesIds.includes(t.id)){console.warn(`[NQTR] Activity with id ${t.id} already exists, so it will be ignored.`);return}let c=this.additionalActivitiesIds;if(c.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}c.push(t.id),this.setStorageProperty("additionalActivitiesIds",c),Object.keys(s).length&&this.editActivityScheduling(t.id,s);}removeActivity(t,r={}){let{toDay:i}=r,n={};i===0&&console.warn("[NQTR] The to day must be greater than 0, so it will be ignored."),i&&(n.toDay=i);let o=this.additionalActivitiesIds;if(o.includes(t.id)){if(Object.keys(n).length){this.editExcludedActivityScheduling(t.id,n);return}o=o.filter(s=>s!==t.id),this.setStorageProperty("additionalActivitiesIds",o);}else if(this.defaultActivitiesIds.includes(t.id)){if(Object.keys(n).length){this.editExcludedActivityScheduling(t.id,n);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,n=this.excludedActivitiesIds;i.forEach(o=>{if(o in t){let{toDay:s}=t[o];s&&s<d.currentDay&&(this.removeActivityScheduling(o),i=i.filter(c=>c!==o));}}),n.forEach(o=>{if(o in r){let{toDay:s}=r[o];s&&s<d.currentDay&&(this.removeActivityScheduling(o),n=n.filter(c=>c!==o));}});}get activities(){let t=[];return Object.entries(this.activeActivityScheduling).forEach(([r,i])=>{let n=q(r),{fromDay:o=n?.fromDay,fromHour:s=n?.fromHour,toDay:c=n?.toDay,toHour:J=n?.toHour}=i;n&&d.nowIsBetween(s,J)&&!(o&&o>d.currentDay)&&!(c&&c<d.currentDay)&&t.push(n);}),Object.entries(this.excludedActivitiesScheduling).forEach(([r,i])=>{let n=q(r);n&&n.isActive&&!(i.toDay&&i.toDay>=d.currentDay)&&t.push(n);}),this.defaultActivities.forEach(r=>{r.isActive&&!t.find(i=>i.id===r.id)&&t.push(r);}),t}};var ie="__nqtr-location__",S=class extends l{constructor(t,r,i=[]){super(ie,t,i);this._map=r;}get map(){return this._map}get rooms(){return h.rooms.filter(t=>t.location.id===this.id)}};var M=class extends S{constructor(e,t,r={}){super(e,t,r.activities||[]),this.defaultName=r.name||"",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);}defaultDisabled;get disabled(){let e=this.getStorageProperty("disabled")||this.defaultDisabled;return typeof e=="string"?getFlag(e):e}set disabled(e){this.setStorageProperty("disabled",e);}defaultHidden;get hidden(){let e=this.getStorageProperty("hidden")||this.defaultHidden;return typeof e=="string"?getFlag(e):e}set hidden(e){this.setStorageProperty("hidden",e);}_icon;get icon(){return this._icon}};var ne="__nqtr-map__",v=class extends l{constructor(e,t=[]){super(ne,e,t);}get locations(){return h.locations.filter(e=>e.map.id===this.id)}};var k=class extends v{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}};var ae="__nqtr-room__",I=class extends l{constructor(t,r,i=[]){super(ae,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}};var B=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"?getFlag(e):e}set disabled(e){this.setStorageProperty("disabled",e);}defaultHidden;get hidden(){let e=this.getStorageProperty("hidden")||this.defaultHidden;return typeof e=="string"?getFlag(e):e}set hidden(e){this.setStorageProperty("hidden",e);}_icon;get icon(){return this._icon}get routine(){return N.currentRoutine.filter(e=>e.room.id===this.id)}};var se="__nqtr-quest__",b=class extends StoredClassModel{constructor(t,r,i={}){super(se,t);this._stages=r;this._onStart=i.onStart,this._onNextStage=i.onNextStage;}get stages(){return this._stages}get currentStageIndex(){return this.getStorageProperty("currentStageIndex")}set currentStageIndex(t){this.setStorageProperty("currentStageIndex",t);}get currentStage(){let t=this.currentStageIndex;if(!(t===undefined||t>=this.stages.length))return this.stages[t]}get started(){return this.currentStageIndex!==undefined}get completed(){return this.currentStageIndex===undefined?false:this.currentStageIndex>this.stages.length-1}get failed(){return this.getStorageProperty("failed")||false}set failed(t){this.setStorageProperty("failed",t);}_onStart;get onStart(){return this._onStart}_onNextStage;get onNextStage(){return this._onNextStage}start(t){if(this.started){console.warn(`[NQTR] Quest ${this.id} is already started`);return}if(this.stages.length===0){console.error(`[NQTR] Quest ${this.id} has no stages`);return}this.currentStageIndex=0;let r=this.currentStage;if(r&&r.start)return this.onStart&&this.onStart(this,t),r.start(t);console.error(`[NQTR] Quest ${this.id} has no start stage`);}tryToGoNextStage(t){if(!this.started||this.completed)return false;let r=this.currentStage;return r?r.completed?this.goNextStage(t):false:(console.error(`[NQTR] Quest ${this.id} has no current stage`),false)}completeCurrentStageAndGoNext(t){let r=this.currentStage;return r?(r.completed=true,this.goNextStage(t)):(console.error(`[NQTR] Quest ${this.id} has no current stage`),false)}goNextStage(t){if(!this.started)return console.warn(`[NQTR] Quest ${this.id} is not started`),false;if(this.completed)return console.warn(`[NQTR] Quest ${this.id} is already completed`),false;let r=this.currentStage,i=this.currentStageIndex;if(!r||i===undefined)return console.error(`[NQTR] Quest ${this.id} has no current stage`),false;this.currentStageIndex=i+1,this.onNextStage&&this.onNextStage(this,t),r&&r.onEnd&&r.onEnd(r,t);let n=this.currentStage;return n&&(n.inizialize(),this.currentStageMustStart&&this.startCurrentStage(t)),true}get currentStageMustStart(){let t=this.currentStage;return t?!t.started&&t.canStart&&!t.completed:false}startCurrentStage(t){let r=this.currentStage;r&&this.currentStageMustStart?r.start(t):console.warn(`[NQTR] Quest ${this.id} can't start the current stage`);}};var $=class extends b{constructor(e,t,r){super(e,t,{onStart:r.onStart,onNextStage:r.onNextStage}),this._name=r.name||"",this._description=r.description||"",this._icon=r.icon,this._image=r.image,this._inDevelopment=r.inDevelopment||false;}_name;get name(){return this._name}_description;get description(){return this._description}_icon;get icon(){return this._icon}_image;get image(){return this._image}_inDevelopment;get inDevelopment(){return this._inDevelopment}};var de="__nqtr-stage__",_=class extends StoredClassModel{constructor(e,t={}){super(de,e),this._onStart=t.onStart,this._onEnd=t.onEnd,this._daysRequiredToStart=t.daysRequiredToStart,this._questsRequiredToStart=t.questsRequiredToStart||[];}_onStart;get onStart(){return this._onStart}_onEnd;get onEnd(){return this._onEnd}get completed(){return this.getStorageProperty("completed")||false}set completed(e){this.setStorageProperty("completed",e);}get started(){return this.getStorageProperty("started")||false}set started(e){this.setStorageProperty("started",e);}get prevStageEndDay(){return this.getStorageProperty("prevStageEndDay")}set prevStageEndDay(e){this.setStorageProperty("prevStageEndDay",e);}get startDay(){let e=this.prevStageEndDay;if(e!==undefined)return e+this.daysRequiredToStart}get canStart(){let e=this.daysRequiredToStart;if(e>0){let t=this.prevStageEndDay;if(t===undefined||t+e>d.currentDay)return false}return !(this.questsRequiredToStart.length>0&&!this.questsRequiredToStart.every(t=>t.quest.currentStageIndex&&t.quest.currentStageIndex>=t.stageNumber))}inizialize(){this.daysRequiredToStart>0&&(this.prevStageEndDay=d.currentDay,console.log(`[NQTR] Stage ${this.id} will start on day ${this.startDay}`));}start(e){this.canStart?(this.started=true,this.onStart&&this.onStart(this,e)):console.warn(`[NQTR] Stage ${this.id} can't start`);}_daysRequiredToStart;get daysRequiredToStart(){return this._daysRequiredToStart||0}_questsRequiredToStart;get questsRequiredToStart(){return this._questsRequiredToStart||[]}};var L=class extends _{constructor(e,t){super(e,{onStart:t.onStart,onEnd:t.onEnd,daysRequiredToStart:t.daysRequiredToStart,questsRequiredToStart:t.questsRequiredToStart}),this._name=t.name||"",this._flags=t.flags||[],this._description=t.description||"",this._adviceDescription=t.adviceDescription||"",this._image=t.image,this._flagsRequiredToStart=t.flagsRequiredToStart||[],this._requestDescriptionToStart=t.requestDescriptionToStart||"";}_name;get name(){return this._name}_description;get description(){return this._description}_adviceDescription;get adviceDescription(){return this._adviceDescription}_image;get image(){return this._image}_flags;get flags(){return this._flags}_flagsRequiredToStart;get flagsRequiredToStart(){return this._flagsRequiredToStart}_requestDescriptionToStart;get requestDescriptionToStart(){return this._requestDescriptionToStart}get completed(){return super.completed?true:this.flags.length>0?!!this.flags.every(e=>getFlag(e.flag)):false}set completed(e){super.completed=e;}get canStart(){return this.flagsRequiredToStart.length>0&&!this.flagsRequiredToStart.every(e=>getFlag(e.flag))?false:super.canStart}};export{H as ActivityBaseModel,m as ActivityStoredClass,O as CommitmentBaseModel,y as CommitmentStoredClass,M as LocationBaseModel,S as LocationStoredClass,k as MapBaseModel,v as MapStoredClass,$ as QuestBaseModel,b as QuestStoredClass,B as RoomBaseModel,I as RoomStoredClass,L as StageBaseModel,_ as StageStoredClass};//# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map