{"version":3,"sources":["../../../src/classes/quest/QuestStoredClass.ts"],"names":["QUEST_CATEGORY","QuestStoredClass","StoredClassModel","id","_stages","props","value","index","currentStage","prevStage","currentStageIndex","nextCurrentStage","newCurrentStage"],"mappings":"mDAMMA,CAAiB,CAAA,gBAAA,CACFC,EAArB,cAA8CC,gBAAuD,CACjG,WACIC,CAAAA,CAAAA,CACiBC,EACjBC,CASI,CAAA,GACN,CACE,KAAA,CAAML,EAAgBG,CAAE,CAAA,CAZP,aAAAC,CAajB,CAAA,IAAA,CAAK,SAAWC,CAAM,CAAA,OAAA,CACtB,KAAK,YAAeA,CAAAA,CAAAA,CAAM,YAC9B,CAEA,IAAI,QAA2B,CAC3B,OAAO,KAAK,OAChB,CAEA,IAAI,iBAAwC,EAAA,CACxC,OAAO,IAAK,CAAA,kBAAA,CAA2B,mBAAmB,CAC9D,CACA,IAAY,iBAAkBC,CAAAA,CAAAA,CAA2B,CACrD,IAAK,CAAA,kBAAA,CAAmB,oBAAqBA,CAAK,EACtD,CAEA,IAAI,YAAA,EAA2C,CAC3C,IAAIC,CAAAA,CAAQ,KAAK,iBACjB,CAAA,GAAI,EAAAA,CAAU,GAAA,SAAA,EAAaA,GAAS,IAAK,CAAA,MAAA,CAAO,QAGhD,OAAO,IAAA,CAAK,OAAOA,CAAK,CAC5B,CAEA,IAAI,OAAA,EAAmB,CACnB,OAAO,IAAA,CAAK,oBAAsB,SACtC,CAEA,IAAI,SAAqB,EAAA,CACrB,OAAI,IAAK,CAAA,iBAAA,GAAsB,UACpB,KAEJ,CAAA,IAAA,CAAK,kBAAoB,IAAK,CAAA,MAAA,CAAO,OAAS,CACzD,CAEA,IAAI,MAAkB,EAAA,CAClB,OAAO,IAAK,CAAA,kBAAA,CAA4B,QAAQ,CAAK,EAAA,KACzD,CACA,IAAI,MAAA,CAAOD,EAAgB,CACvB,IAAA,CAAK,mBAAmB,QAAUA,CAAAA,CAAK,EAC3C,CAEQ,QAAA,CACR,IAAI,OAA4E,EAAA,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AAA,EAAgB,KAAK,EAAE,CAAA,8BAAA,CAAgC,EAE5E,CACJ","file":"QuestStoredClass.mjs","sourcesContent":["import { OnRunProps } from \"@drincs/nqtr\";\nimport { StoredClassModel } from \"@drincs/pixi-vn\";\nimport { QuestInterface, StageInterface } from \"../../interface\";\nimport { QuestBaseInternalInterface } from \"../../interface/quest/QuestInterface\";\nimport StageStoredClass from \"./StageStoredClass\";\n\nconst QUEST_CATEGORY = \"__nqtr-quest__\";\nexport default class QuestStoredClass extends StoredClassModel implements QuestBaseInternalInterface {\n    constructor(\n        id: string,\n        private readonly _stages: StageInterface[],\n        props: {\n            /**\n             * The function that will be executed when the quest starts.\n             */\n            onStart?: (stage: QuestInterface, props: OnRunProps) => void;\n            /**\n             * The function that will be executed when a stage end in the quest.\n             */\n            onNextStage?: (stage: QuestInterface, props: OnRunProps) => void;\n        } = {}\n    ) {\n        super(QUEST_CATEGORY, id);\n        this._onStart = props.onStart;\n        this._onNextStage = props.onNextStage;\n    }\n\n    get stages(): StageInterface[] {\n        return this._stages;\n    }\n\n    get currentStageIndex(): number | undefined {\n        return this.getStorageProperty<number>(\"currentStageIndex\");\n    }\n    private set currentStageIndex(value: number | undefined) {\n        this.setStorageProperty(\"currentStageIndex\", value);\n    }\n\n    get currentStage(): StageInterface | undefined {\n        let index = this.currentStageIndex;\n        if (index === undefined || index >= this.stages.length) {\n            return undefined;\n        }\n        return this.stages[index];\n    }\n\n    get started(): boolean {\n        return this.currentStageIndex !== undefined;\n    }\n\n    get completed(): boolean {\n        if (this.currentStageIndex === undefined) {\n            return false;\n        }\n        return this.currentStageIndex > this.stages.length - 1;\n    }\n\n    get failed(): boolean {\n        return this.getStorageProperty<boolean>(\"failed\") || false;\n    }\n    set failed(value: boolean) {\n        this.setStorageProperty(\"failed\", value);\n    }\n\n    private _onStart?: (stage: QuestInterface, props: OnRunProps) => void;\n    get onStart(): undefined | ((stage: QuestInterface, props: OnRunProps) => void) {\n        return this._onStart;\n    }\n\n    private _onNextStage?: (stage: QuestInterface, props: OnRunProps) => void;\n    get onNextStage(): undefined | ((stage: QuestInterface, props: OnRunProps) => void) {\n        return this._onNextStage;\n    }\n\n    start(props: OnRunProps): void {\n        if (this.started) {\n            console.warn(`[NQTR] Quest ${this.id} is already started`);\n            return;\n        }\n        if (this.stages.length === 0) {\n            console.error(`[NQTR] Quest ${this.id} has no stages`);\n            return;\n        }\n        this.currentStageIndex = 0;\n        let currentStage = this.currentStage;\n        if (currentStage && currentStage.start) {\n            this.onStart && this.onStart(this as any as QuestInterface, props);\n            return currentStage.start(props);\n        } else {\n            console.error(`[NQTR] Quest ${this.id} has no start stage`);\n        }\n    }\n\n    tryToGoNextStage(props: OnRunProps): boolean {\n        if (!this.started) {\n            return false;\n        }\n        if (this.completed) {\n            return false;\n        }\n        let currentStage = this.currentStage;\n        if (!currentStage) {\n            console.error(`[NQTR] Quest ${this.id} has no current stage`);\n            return false;\n        }\n        if (currentStage.completed) {\n            return this.goNextStage(props);\n        }\n        return false;\n    }\n\n    completeCurrentStageAndGoNext(props: OnRunProps): boolean {\n        let currentStage = this.currentStage;\n        if (!currentStage) {\n            console.error(`[NQTR] Quest ${this.id} has no current stage`);\n            return false;\n        }\n        currentStage.completed = true;\n        return this.goNextStage(props);\n    }\n\n    goNextStage(props: OnRunProps): boolean {\n        if (!this.started) {\n            console.warn(`[NQTR] Quest ${this.id} is not started`);\n            return false;\n        }\n        if (this.completed) {\n            console.warn(`[NQTR] Quest ${this.id} is already completed`);\n            return false;\n        }\n        let prevStage = this.currentStage;\n        let currentStageIndex = this.currentStageIndex;\n        if (!prevStage || currentStageIndex === undefined) {\n            console.error(`[NQTR] Quest ${this.id} has no current stage`);\n            return false;\n        }\n        this.currentStageIndex = currentStageIndex + 1;\n        this.onNextStage && this.onNextStage(this as any as QuestInterface, props);\n        if (prevStage && prevStage.onEnd) {\n            prevStage.onEnd(prevStage, props);\n        }\n        let nextCurrentStage = this.currentStage;\n        if (nextCurrentStage) {\n            (nextCurrentStage as any as StageStoredClass).inizialize();\n            if (this.currentStageMustStart) {\n                this.startCurrentStage(props);\n            }\n        }\n\n        return true;\n    }\n\n    get currentStageMustStart(): boolean {\n        let currentStage = this.currentStage;\n        if (!currentStage) {\n            return false;\n        }\n        return !currentStage.started && currentStage.canStart && !currentStage.completed;\n    }\n\n    startCurrentStage(props: OnRunProps): void {\n        let newCurrentStage = this.currentStage;\n        if (newCurrentStage && this.currentStageMustStart) {\n            newCurrentStage.start(props);\n        } else {\n            console.warn(`[NQTR] Quest ${this.id} can't start the current stage`);\n        }\n    }\n}\n"]}