-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuest.js
95 lines (75 loc) · 2.41 KB
/
Quest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Created by Dean on 6/21/2015.
*/
var QUESTIFY = (function (QUESTIFY) {
"use strict";
function createQuest (strategy, actions, argumentsArr, descriptions) {
var that = {},
finishedMap = new Array(actions.length),
actionFinishedCallback = function () {},
questFinishedCallback = function () {};
(function initializeFinishedMap() {
for (var i = 0, il = finishedMap.length; i < il; i++) {
finishedMap[i] = false;
}
}());
function reportQuestFinished() {
questFinishedCallback();
}
function reportActionFinished(finishedAction, args) {
actionFinishedCallback(finishedAction, args);
}
function isFinished() {
return finishedMap.indexOf(false) === -1;
}
function completionPercentage() {
var finishedCount = 0;
for (var f = 0, fl = finishedMap.length; f < fl; f++) {
if (finishedMap[f] === true) { finishedCount++; }
}
return (finishedMap.length !== 0) ? finishedCount/finishedMap.length : 0;
}
function getActionDescription(actionNumber) {
if (actionNumber < descriptions.length && actionNumber >= 0) {
return descriptions[actionNumber];
}
return "";
}
function setActionDescription(actionNumber, description) {
if (actionNumber < descriptions.length && actionNumber >= 0) {
description[actionNumber] = description;
}
}
function getActionStatus(actionNumber) {
if (actionNumber < finishedMap.length && actionNumber >= 0) {
return finishedMap[actionNumber];
}
return false;
}
that.updateState = function() {
var action;
for (var a = 0, al = actions.length; a < al; a++) {
action = actions[a];
if (finishedMap[a] === false) {
finishedMap[a] = (action.update(argumentsArr[a]) === true);
//Update might have changed state
if (finishedMap[a] === false) { return; }
if (finishedMap[a] === true) { reportActionFinished(action, argumentsArr[a]); }
}
}
if (isFinished()) { reportQuestFinished(); }
};
that.actions = actions;
that.strategy = strategy;
that.isFinished = isFinished;
that.completionPercentage = completionPercentage;
that.actionFinishedCallback = actionFinishedCallback;
that.questFinishedCallback = questFinishedCallback;
that.getActionDescription = getActionDescription;
that.setActionDescription = setActionDescription;
that.getActionStatus = getActionStatus;
return that;
}
QUESTIFY.createQuest = createQuest;
return QUESTIFY;
}(QUESTIFY || {}));