-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiSchedule.js
118 lines (113 loc) · 3.2 KB
/
aiSchedule.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";
// add clear update
// create an obj that keeps some functions que and try to run them while
// previous function returns true. then u delete it from the que and run the next one
waw.Schedule = function(tasks, interrupts) {
this.tasks = [];
this.interrupts = [];
this.currentTask = 0;
this.done = false;
if (arguments.length < 1)
return;
for (var i in tasks)
this.tasks.push(tasks[i]);
for (var j in interrupts)
this.interrupts.push(interrupts[j]);
};
waw.Schedule.prototype.trace = function () {
// console.info("trace currTask:" + this.currentTask + " Done:" + this.done);
for (var i in this.tasks)
console.log(this.tasks[i]);
for (var j in this.interrupts)
console.log(this.interrupts[j]);
};
waw.Schedule.prototype.reset = function () {
this.currentTask = 0;
this.done = false;
// console.info(" reset tasks que");
};
waw.Schedule.prototype.stop = function () {
this.currentTask = 0;
this.done = true;
// console.info(" stop tasks que");
};
waw.Schedule.prototype.addTask = function (f) {
if (!f) throw "argument should be a function";
this.tasks.push(f);
// console.info(" added " + f + " to tasks");
};
waw.Schedule.prototype.addInterrupt = function (t) {
if (!t) throw "argument should be a string with interrupt condition name";
this.interrupts.push(t);
// console.info(" added " + t + " to interrupts");
};
waw.Schedule.prototype.isDone = function (conditions) {
// console.info(" isDone?");
if (this.done) {
this.reset();
// console.info(" all tasks are done");
return true;
}
for (var i in conditions)
if (this.interrupts.indexOf(conditions[i]) >= 0) {
this.reset();
// console.info(" all tasks are done by right interrupt");
return true;
}
// console.info(" nope");
return false;
};
waw.Schedule.prototype.update = function (env) {
// console.info(" tasks len " + this.tasks.length + ", interrupts len " + this.interrupts.length);
if (this.done) {
// console.info(" no update: all tasks are done");
return false;
}
if (this.tasks.length <= 0) {
// console.info(" no tasks");
return false;
}
if (this.tasks[this.currentTask].apply(env)) { //if func returns true, delete this from the que
this.currentTask++;
// console.info(" que func run with true");
if (this.currentTask > this.tasks.length - 1)
this.stop();
return true;
}
// console.info(" que func run with false");
return false;
};
/*
console.info("start");
var my_tasks = [function () {
console.log("1f");
return true;
},
function () {
console.log("2f");
return true;
}, function () {
console.log("2FFf");
return false;
},
function () {
console.log("3f");
return true;
}];
var my_interrupts = ["nope", "idle", "run", "died", "seeEnemy"];
var q = new Schedule(my_tasks, my_interrupts);
console.info(q);
q.trace();
q.update();
q.isDone([]);
q.update();
q.isDone();
q.update();
q.isDone();
q.update();
q.isDone(["sss","seeEnemy"]);
q.update();
q.isDone(["run"]);
q.update();
q.isDone();
*/