prosperon/scripts/ai.js

59 lines
988 B
JavaScript
Raw Normal View History

2024-02-25 17:31:48 -06:00
var ai = {
2023-11-27 14:29:55 -06:00
race(list) {
2023-11-27 17:04:04 -06:00
return function(dt) {
2023-11-27 14:29:55 -06:00
var good = false;
2024-04-01 08:13:57 -05:00
for (var i = 0; i < list.length; i++)
if (list[i].call(this,dt)) good=true;
2023-11-27 14:29:55 -06:00
return good;
};
},
2023-11-27 17:04:04 -06:00
sequence(list) {
var i = 0;
2024-04-01 08:13:57 -05:00
var fn = function(dt) {
2023-11-27 17:04:04 -06:00
while (i !== list.length) {
if (list[i].call(this,dt))
2024-04-01 08:13:57 -05:00
i++;
else
return false;
2023-11-27 17:04:04 -06:00
}
2024-04-01 08:13:57 -05:00
if (fn.done) fn.done();
2023-11-27 17:04:04 -06:00
return true;
};
2024-04-01 08:13:57 -05:00
fn.restart = function() { i = 0; };
return fn;
2023-11-27 17:04:04 -06:00
},
parallel(list) {
return function(dt) {
2023-11-27 14:29:55 -06:00
var good = true;
2023-11-27 17:04:04 -06:00
list.forEach(function(x){ if (!x.call(this,dt)) good = false; },this);
2023-11-27 14:29:55 -06:00
return good;
};
},
2024-04-01 08:13:57 -05:00
dofor(secs, fn) {
return ai.race([
ai.wait(secs),
fn
]);
2023-11-29 07:32:32 -06:00
},
wait(secs = 1) {
2023-11-27 14:29:55 -06:00
var accum = 0;
2023-11-27 17:04:04 -06:00
return function(dt) {
accum += dt;
2024-04-01 08:13:57 -05:00
if (accum >= secs) {
accum = 0;
2023-11-27 14:29:55 -06:00
return true;
2024-04-01 08:13:57 -05:00
}
2023-11-27 14:29:55 -06:00
return false;
};
},
};
return {ai};