armory/Sources/armory/logicnode/TimerNode.hx

67 lines
1.1 KiB
Haxe
Raw Normal View History

2018-10-21 19:45:41 +02:00
package armory.logicnode;
class TimerNode extends LogicNode {
2018-10-22 10:10:33 +02:00
var time = 0.0;
2018-10-21 19:45:41 +02:00
var duration = 0.0;
var repeat = 0;
2018-10-21 20:43:28 +02:00
var running = false;
var repetitions = 0;
2018-10-21 19:45:41 +02:00
public function new(tree:LogicTree) {
super(tree);
}
function update() {
2018-10-22 10:10:33 +02:00
time += iron.system.Time.delta;
if (time >= duration) {
2018-10-21 20:43:28 +02:00
repeat--;
2018-10-22 10:10:33 +02:00
runOutput(0);
2018-10-21 20:43:28 +02:00
if (repeat != 0) {
2018-10-22 10:10:33 +02:00
time = 0;
2018-10-21 20:43:28 +02:00
repetitions++;
2018-10-21 19:45:41 +02:00
}
else {
2018-10-22 10:10:33 +02:00
removeUpdate();
runOutput(1);
2019-04-20 22:24:34 +02:00
time = 0;
repetitions = 0;
2018-10-21 19:45:41 +02:00
}
}
}
2018-10-22 10:10:33 +02:00
override function run(from:Int) {
if (from == 0) { // Start
duration = inputs[3].get();
repeat = inputs[4].get();
if (!running) {
running = true;
tree.notifyOnUpdate(update);
}
}
else if (from == 1) { // Pause
removeUpdate();
}
else { // Stop
removeUpdate();
time = 0;
repetitions = 0;
}
2018-10-21 19:45:41 +02:00
}
2019-04-20 22:24:34 +02:00
inline function removeUpdate() {
if (running) {
running = false;
tree.removeUpdate(update);
}
}
2018-10-22 10:10:33 +02:00
2018-10-21 19:45:41 +02:00
override function get(from:Int):Dynamic {
2018-10-21 20:43:28 +02:00
if (from == 1) return running;
2018-10-22 10:10:33 +02:00
else if (from == 2) return time;
else if (from == 3) return duration - time;
else if (from == 4) return time / duration;
2018-10-21 20:43:28 +02:00
else return repetitions;
2018-10-21 19:45:41 +02:00
}
}