From 98619ae4437eb09e93b5803482cbdbf4fde180bf Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Wed, 11 Aug 2010 18:39:32 +0000 Subject: [PATCH] Setup a Ticker to fill in for the parts of java's FrameParticipant we might want. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@975 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/miso/client/Tickable.as | 25 +++++++ src/as/com/threerings/miso/client/Ticker.as | 71 +++++++++++++++++++ .../com/threerings/miso/util/MisoContext.as | 6 ++ 3 files changed, 102 insertions(+) create mode 100644 src/as/com/threerings/miso/client/Tickable.as create mode 100644 src/as/com/threerings/miso/client/Ticker.as diff --git a/src/as/com/threerings/miso/client/Tickable.as b/src/as/com/threerings/miso/client/Tickable.as new file mode 100644 index 00000000..cba8ebab --- /dev/null +++ b/src/as/com/threerings/miso/client/Tickable.as @@ -0,0 +1,25 @@ +// Nenya library - tools for developing networked games +// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/nenya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.miso.client { + +public interface Tickable +{ + function tick (tickStamp :int) :void; +} +} \ No newline at end of file diff --git a/src/as/com/threerings/miso/client/Ticker.as b/src/as/com/threerings/miso/client/Ticker.as new file mode 100644 index 00000000..d3f34611 --- /dev/null +++ b/src/as/com/threerings/miso/client/Ticker.as @@ -0,0 +1,71 @@ +// Nenya library - tools for developing networked games +// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/nenya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.miso.client { + +import flash.utils.getTimer; + +import flash.events.TimerEvent; +import flash.utils.Timer; + +/** + * Registers objects that wish to be sent a tick() call once per frame. + */ +public class Ticker +{ + public function start () :void + { + _t.addEventListener(TimerEvent.TIMER, handleTimer); + _t.start(); + } + + public function stop () :void + { + _t.removeEventListener(TimerEvent.TIMER, handleTimer); + _t.stop(); + + } + + public function handleTimer (event :TimerEvent) :void + { + tick(getTimer()); + } + + protected function tick (tickStamp :int) :void + { + for each (var tickable :Tickable in _tickables) { + tickable.tick(tickStamp); + } + } + + public function registerTickable (tickable :Tickable) :void + { + _tickables.push(tickable); + } + + public function removeTickable (tickable :Tickable) :void + { + _tickables.remove(tickable); + } + + protected var _tickables :Array = []; + + /** A timer that will fire every "frame". */ + protected var _t :Timer = new Timer(1); +} +} \ No newline at end of file diff --git a/src/as/com/threerings/miso/util/MisoContext.as b/src/as/com/threerings/miso/util/MisoContext.as index 4cd8463d..9ecd59ce 100644 --- a/src/as/com/threerings/miso/util/MisoContext.as +++ b/src/as/com/threerings/miso/util/MisoContext.as @@ -18,6 +18,7 @@ package com.threerings.miso.util { +import com.threerings.miso.client.Ticker; import com.threerings.miso.tile.MisoTileManager; /** @@ -31,5 +32,10 @@ public interface MisoContext * for the lifetime of the application. */ function getTileManager () :MisoTileManager; + + /** + * Returns a reference to the ticker, which gives regular heartbeat "ticks" every frame. + */ + function getTicker () :Ticker; } }