Ticket #2475: timer.diff
File timer.diff, 2.6 KB (added by , 10 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Timer.js
7 7 { 8 8 this.id = 0; 9 9 this.time = 0; 10 this.timers = {};10 this.timers = new Map(); 11 11 }; 12 12 13 Timer.prototype.Serialize = function() 14 { 15 var r = {}; 16 r.id = this.id; 17 r.time = this.time; 18 r.timers = []; 19 for (var t of this.timers.entries()) 20 r.timers.push(t); 21 print(uneval(r)); 22 return r; 23 }; 24 25 Timer.prototype.Deserialize = function(data) 26 { 27 this.id = data.id 28 this.time = data.time; 29 this.timers = new Map(data.timers); 30 }; 31 13 32 /** 14 33 * Returns time since the start of the game, in integer milliseconds. 15 34 */ … … 27 46 Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data) 28 47 { 29 48 var id = ++this.id; 30 this.timers [id] = [ent, iid, funcname, this.time + time, 0, data];49 this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]); 31 50 return id; 32 51 }; 33 52 … … 45 64 if (typeof repeattime != "number" || !(repeattime > 0)) 46 65 error("Invalid repeattime to SetInterval of "+funcname); 47 66 var id = ++this.id; 48 this.timers [id] = [ent, iid, funcname, this.time + time, repeattime, data];67 this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]); 49 68 return id; 50 69 }; 51 70 … … 54 73 */ 55 74 Timer.prototype.CancelTimer = function(id) 56 75 { 57 delete this.timers[id];76 this.timers.delete(id); 58 77 }; 59 78 60 79 … … 66 85 // Collect the timers that need to run 67 86 // (We do this in two stages to avoid deleting from the timer list while 68 87 // we're in the middle of iterating through it) 69 var run = [];70 for ( var id in this.timers)88 var run = new Set(); 89 for (let id of this.timers.keys()) 71 90 { 72 if (this.timers [id][3] <= this.time)73 run. push(id);91 if (this.timers.get(id)[3] <= this.time) 92 run.add(id); 74 93 } 75 for ( var i = 0; i < run.length; ++i)94 for (let id of run) 76 95 { 77 var id = run[i]; 78 79 var t = this.timers[id]; 96 var t = this.timers.get(id); 80 97 if (!t) 81 98 continue; // an earlier timer might have cancelled this one, so skip it 82 99 … … 84 101 if (!cmp) 85 102 { 86 103 // The entity was probably destroyed; clean up the timer 87 delete this.timers[id];104 this.timers.delete(id); 88 105 continue; 89 106 } 90 107 … … 103 120 t[3] += t[4]; 104 121 // Add it to the list to get re-executed if it's soon enough 105 122 if (t[3] <= this.time) 106 run. push(id);123 run.add(id); 107 124 } 108 125 else 109 126 { 110 127 // Non-repeating time - delete it 111 delete this.timers[id];128 this.timers.delete(id); 112 129 } 113 130 } 114 131 }