📄 goengine.as
字号:
/** * Copyright (c) 2007 Moses Gunesch * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */package org.goasap { import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Dictionary; import flash.utils.Timer; import flash.utils.getQualifiedClassName; import flash.utils.getTimer; import org.goasap.errors.DuplicateManagerError; import org.goasap.interfaces.IManageable; import org.goasap.interfaces.IManager; import org.goasap.interfaces.IUpdatable; import org.goasap.interfaces.ILiveManager; /** * Provides <code>update</code> calls to <code>IUpdatable</code> instances on their specified <code>pulseInterval</code>. * * <p><b>Using these Docs</b></p> * * <p><i>Protected methods and properties have been excluded in almost all * cases, but are documented in the classes. Exceptions include key protected * methods or properties that are integral for writing subclasses or understanding * the basic mechanics of the system. Many Go classes can be used as is without * subclassing, so the documentation offers an uncluttered view of their public * usage.</i></p> * * <p><b>Introduction to Go</b></p> * * <p>The Go ActionScript Animation Platform ("GOASAP") is a lightweight, portable * set of generic base classes for buliding AS3 animation tools. It provides structure * and core functionality, but does not define the specifics of animation-handling * classes like tweens.</p> * * <p><i>Important: Store your custom Go classes in a package bearing your * own classpath, not in the core package! This will help avoid confusion * with other authors' work.</i></p> * * <p>You may modify any class in the goasap package to suit your project's needs.</p> * * <p>Go is a community initiative led by Moses Gunesch at * <a href="http://www.mosessupposes.com/" target="_top">MosesSupposes.com</a>. Please visit the * <a href="http://www.goasap.org/" target="_top">Go website</a> for more information.</p> * * <p><b>GoEngine</b></p> * * <p>GoEngine sits at the center of the Go system, and along with the IUpdatable * interface is the only required element for using Go. GoEngine references two other * interfaces for adding system-wide managers, IManager and IManageable. * All other classes in the go package are merely one suggestion of how a * system could be structured within Go, and may be considered optional * elements. To create an API using the provided classes, you simply need * to extend the item classes LinearGo and PhysicsGo to create animation items.</p> * * <p>GoEngine serves two purposes: first, it keeps a large system efficient * by stacking and running updates on blocks of items. Note that any IUpdatable * instance may specify its own pulseInterval; items with matching pulses * are grouped into queues for efficiency. Its second purpose is centralization. * By using a single hub for pulse-driven items of all types, management classes * can be attached to GoEngine to run processes across items. This is done voluntarily * by the end-user with <code>addManager()</code>, which keeps management entirely * compile-optional and extensible. See the documentation for <code>IManager</code> * to learn more about Go's management architeture.</p> * * <p>You normally don't need to modify this class to use Go. While Go items typically * only use <code>addItem</code> and <code>removeItem</code>, your project's code might * use GoEngine to register managers, or to pause, resume or stop all Go animation in * a SWF at once.</p> * * <p></i>{In the game of Go, the wooden playing board, or Goban, features a grid * on which black & white go-ishi stones are laid at its intersections.}</i></p> * * @see org.goasap.items.LinearGo LinearGo * @see org.goasap.interfaces.IManager IManager * @author Moses Gunesch */ public class GoEngine { // -== Constants ==- public static const INFO:String = "GoASAP 0.4.9 (c) Moses Gunesch, MIT Licensed."; // -== Settable Class Defaults ==- /** * A pulseInterval that runs on the player's natural framerate, * which is often most efficient. */ public static const ENTER_FRAME : int = -1; // -== Protected Properties ==- // Note: Various formats for item data have been experimented with including breaking the item lists out into // a GoEngineList class, which was nicer-looking but did not perform well. Since GoEngine doesn't normally // require active work, this less-pretty but efficient flat-data format was opted for. A minor weakness of this // format is its use of a Dictionary, which means update calls are not ordered like they would be with an Array. // The Dictionary stores items' pulseInterval values, which is safer than relying on items to not change them. // Tests also show that Dictionary performs faster than Array for accessing and deleting items. private static var managerTable : Object = new Object(); // registration list of IManager instances private static var managers : Array = new Array(); // ordered registration list of IManager instances private static var liveManagers : uint = 0; private static var timers : Dictionary = new Dictionary(false); // key: pulseInterval, value: Timer for that pulse private static var items : Dictionary = new Dictionary(false); // key: IUpdatable item, value: pulseInterval at add. private static var itemCounts : Dictionary = new Dictionary(false); // key: pulseInterval, value: item count for that pulse private static var pulseSprite : Sprite; // used for ENTER_FRAME pulse private static var paused : Boolean = false; // These additional lists enables caching of items that are added during the update cycle for the same pulse. // This prevents groups & sequences from going out of sync by ensuring that each cycle completes before new items are added. private static var lockedPulses : Dictionary = new Dictionary(false); // key: pulseInterval, value: true private static var delayedPulses : Dictionary = new Dictionary(false); // key: pulseInterval, value: true private static var addQueue : Dictionary = new Dictionary(false); // key: IUpdatable item, value: true // -== Public Class Methods ==- /** * @param className A string naming the manager class, such as "OverlapMonitor". * @return The manager instance, if registered. * @see #addManager() * @see #removeManager() */ public static function getManager(className:String) : IManager { return managerTable[ className ]; } /** * Enables the extending of this class' functionality with a tight * coupling to an IManager. * * <p>Tight coupling is crucial in such a time-sensitive context; * standard events are too asynchronous. All items that implement * IManageable are reported to registered managers as they add and * remove themselves from GoEngine.</p> * * <p>Managers normally act as singletons within the Go system (which * you are welcome to modify). This method throws a DuplicateManagerError * if an instance of the same manager class is already registered. Use a * try/catch block when calling this method if your program might duplicate * managers, or use getManager() to check for prior registration.</p> * * @param instance An instance of a manager you wish to add. * @see #getManager() * @see #removeManager() */ public static function addManager( instance:IManager ):void { var className:String = getQualifiedClassName(instance); className = className.slice(className.lastIndexOf("::")+2); if (managerTable[ className ]) { throw new DuplicateManagerError( className ); return; } managerTable[ className ] = instance; managers.push(instance); if (instance is ILiveManager) liveManagers++; } /** * Unregisters any manager set in <code>addManager</code>. * * @param className A string naming the manager class, such as "OverlapMonitor". * @see #getManager() * @see #addManager() */ public static function removeManager( className:String ):void { managers.splice(managers.indexOf(managerTable[ className ]), 1); if (managerTable[ className ] is ILiveManager) liveManagers--; delete managerTable[ className ]; // leave last } /** * Test whether an item is currently stored and being updated by the engine. * * @param item Any object implementing IUpdatable * @return Whether the IUpdatable is in the engine */ public static function hasItem( item:IUpdatable ):Boolean { return (items[ item ]!=null); } /** * Adds an IUpdatable instance to an update-queue corresponding to * the item's pulseInterval property. * * @param item Any object implementing IUpdatable that wishes * to receive update calls on a pulse. * * @return Returns false only if this item was already in the * engine under the same pulse. (If an existing item is added * but the pulseInterval has changed it will be removed, * re-added, and true will be returned.) * * @see #removeItem() */ public static function addItem( item:IUpdatable ):Boolean { // Group items by pulse for efficient update cycles. var interval:int = item.pulseInterval; if (items[ item ]) { if (items[ item ] == item.pulseInterval) return false; else removeItem(item); } if (lockedPulses[ interval ]==true) { // this prevents items from being added during an update loop in progress. delayedPulses[ interval ] = true; // flags update to clear the queue when the in-progress loop completes. addQueue[ item ] = true; // for tightest syncing of item groups, read the documentation under GoItem.update().
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -