Timer, Alarm, Event. Easy.
Posted:
Sun Jan 17, 2010 11:23 pm
by Kilinich
Rating: Filesize: 1.94 kB
Comments: 7
Ratings: 5
Key feature is every-frame evend handler "Scene.my.event" that can be attached to any global property.
For example:
- Code: Select all
App.Background -> {
cloudOpacity = {Scene.my.event(0.94999999)};
...};
And this is all the rest:
- Code: Select all
Scene.my.event := (n)=>{Scene.my.evHandle; n};
Scene.my.newAlarm := (aTime, aWhat)=>{
Scene.my.events = Scene.my.events ++ [[aTime, 0, 1]];
Scene.my.eventsCount = Scene.my.eventsCount + 1;
Scene.my.actions = Scene.my.actions ++ [aWhat];
scene.my.eventsCount-1
};
Scene.my.newEvent := (eTime, eRepeat, eCount, eAction)=>{
Scene.my.events = Scene.my.events ++ [[eTime, eRepeat, eCount]];
Scene.my.eventsCount = Scene.my.eventsCount + 1;
Scene.my.actions = Scene.my.actions ++ [eAction];
scene.my.eventsCount-1
};
Scene.my.newTimer := (tTime, tWhat, tRepeat)=>{
Scene.my.events = Scene.my.events ++ [[tTime + sim.time, tTime, tRepeat ? 9999999 : 1]];
Scene.my.eventsCount = Scene.my.eventsCount + 1;
Scene.my.actions = Scene.my.actions ++ [tWhat];
scene.my.eventsCount-1
};
Scene.my.delEvent := (i)=>{
newActions = [];
newEvents = [];
Scene.my.eventsCount = Scene.my.eventsCount - 1;
for(i, (n)=>{
newActions = newActions ++ [Scene.my.actions(n)];
newEvents = newEvents ++ [Scene.my.events(n)]
});
for(Scene.my.eventsCount - i, (n)=>{
newActions = newActions ++ [Scene.my.actions(n + i + 1)];
newEvents = newEvents ++ [Scene.my.events(n + i + 1)]
});
Scene.my.events = newEvents;
Scene.my.actions = newActions
};
Scene.my.eventsCount := 0;
Scene.my.events := [];
Scene.my.actions := [];
Scene.my.evHandle := {
sim.running ? {
DelEvents = [];
DelCount = 0;
Scene.my.frame = Scene.my.frame + 1;
for(scene.my.eventsCount, (ev)=>{
scene.my.events(ev)(0) < sim.time ? {
Scene.my.actions(ev)();
Scene.my.events(ev)(2) > 1 ? {
newEvents = [];
for(ev, (n)=>{newEvents = newEvents ++ [Scene.my.events(n)]});
newEvents = newEvents ++ [[scene.my.events(ev)(1) + sim.time, scene.my.events(ev)(1), scene.my.events(ev)(2) - 1]];
for(Scene.my.eventsCount - ev - 1, (n)=>{newEvents = newEvents ++ [Scene.my.events(n + ev + 1)]});
Scene.my.events = newEvents
} : {
DelEvents = DelEvents ++ [ev];
DelCount = DelCount + 1
}
} : {}
});
for(DelCount, (ev)=>{scene.my.delEvent(DelEvents(ev))})
} : {}
};
Scene.my.timers := [];
Scene.my.frame := 0;
Re: Timer, Alarm, Event. Easy.
Posted:
Sun Jan 17, 2010 11:32 pm
by Kilinich
Code can be simpler a lot, but I've tryed to optimize it for best performance.
Re: Timer, Alarm, Event. Easy.
Posted:
Fri Jan 22, 2010 12:37 pm
by Mystery
That is sweet. Defiantly stealing this, with your permission of course.
Re: Timer, Alarm, Event. Easy.
Posted:
Fri Jan 22, 2010 9:16 pm
by Kilinich
Mystery wrote::D That is sweet. Defiantly stealing this, with your permission of course.
That's why I made it
Recently, I've use events for debugging (stop sim at particular time) and time traveling (app.step and than undo). More than that - it's possible to automatically start/stop sim by events (using app.time).
Re: Timer, Alarm, Event. Easy.
Posted:
Sat May 22, 2010 6:35 am
by Kilinich
To make another events by yourself just call (type in console or on collision) one of functions:
To make a timer:
scene.my.newTimer(countdown, {{action}}, repeat);
for example: scene.my.newTimer(1, {{sim.running = false}}, true) will stop simulation every second.
To make an alarm:
scene.my.newAlarm(time, {{action}});
for example: scene.my.newAlarm(10, {{scene.camera.pan = [0,0]}}) will move focus to 0,0 at 10 second of sim.
To make custom event:
scene.my.newEvent(time, countdown, count, {{action}});
for example: scene.my.newEvent(7, 1, 10, {{scene.addCircle({pos=[0,0]})}}) will spawn 10 circles one by one every 1 sec after 7 sec sim time.
Re: Timer, Alarm, Event. Easy.
Posted:
Sat Mar 26, 2011 8:58 pm
by InfinityPlayer
Kilinich, where in Algodoo do i put all that code? I don't, as yet, fully understand these codes that people put up, cos they don't tell you where to put them. Unless you've put it in the above, i'm just not seeing it.
Re: Timer, Alarm, Event. Easy.
Posted:
Sat Mar 26, 2011 9:22 pm
by InfinityPlayer
Chronos wrote:InfinityPlayer wrote:Kilinich, where in Algodoo do i put all that code? I don't, as yet, fully understand these codes that people put up, cos they don't tell you where to put them. Unless you've put it in the above, i'm just not seeing it.
He said where.
Kilinich wrote:(type in console or on collision)
Thanks, Chronos. I think it's funny that the first person to reply to this request is a person named Chronos, which is Latin for time, in a post about timers, etc.
Re: Timer, Alarm, Event. Easy.
Posted:
Sat Mar 26, 2011 11:25 pm
by Kilinich