WaitEvent.hx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package hxd;
  2. class WaitEvent {
  3. var updateList : Array<Float -> Bool> ;
  4. public function new() {
  5. updateList = [];
  6. }
  7. public inline function hasEvent() {
  8. return updateList.length > 0;
  9. }
  10. public function clear() {
  11. updateList = [];
  12. }
  13. public function add( callb ) {
  14. updateList.push(callb);
  15. }
  16. public function remove( callb : Float->Bool ) {
  17. for( e in updateList )
  18. if( Reflect.compareMethods(e, callb) ) {
  19. updateList.remove(e);
  20. return true;
  21. }
  22. return false;
  23. }
  24. public function wait( time : Float, callb : Void -> Void ) {
  25. function tmp(dt:Float) {
  26. time -= dt;
  27. if( time < 0 ) {
  28. callb();
  29. return true;
  30. }
  31. return false;
  32. }
  33. updateList.push(tmp);
  34. }
  35. public function waitUntil( callb ) {
  36. updateList.push(callb);
  37. }
  38. public function update(dt:Float) {
  39. var i = 0;
  40. var max = updateList.length;
  41. while (i < updateList.length) {
  42. if( i == max ) break;
  43. var f = updateList[i];
  44. if(f(dt)) {
  45. updateList.remove(f);
  46. max--;
  47. } else
  48. ++i;
  49. }
  50. }
  51. }