Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Add FlashPunk as of r94a60de.
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Jan 27, 2012
0 parents commit 6de9afa
Show file tree
Hide file tree
Showing 60 changed files with 11,677 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/flashpunk/.gitignore
@@ -0,0 +1 @@
*~
41 changes: 41 additions & 0 deletions lib/flashpunk/README.markdown
@@ -0,0 +1,41 @@
ABOUT
=====

FlashPunk is a free ActionScript 3 library designed for developing 2D Flash games. It provides you with a fast, clean framework to prototype and develop your games in. This means that most of the dirty work (timestep, animation, input, and collision to name a few) is already coded for you and ready to go, giving you more time and energy to concentrate on the design and testing of your game.

FEATURES
========

* Framerate-independent and fixed-framerate timestep support.
* Fast & manageable rectangle, pixel, and grid collision system.
* Helper classes for animations, tilemaps, text, backdrops, and more.
* Sound effect support with volume, panning, and fading/crossfading.
* Powerful motion tweening for linear, curved, and path-based movement.
* Z-sorted render lists for easy depth management.
* Simple keyboard and mouse input state checking.
* Quick & efficient particle effects and emitters.
* Handy console for real-time debugging and information tracking.

OVERVIEW
========

This is a quick overview of some of the features and how they've changed since FlashPunk's previous versions.

### TIMESTEPS
Support for both fixed-framerate and framerate-independent timesteps. Previously, FlashPunk used a fixed-framerate timestep decoupled from the drawing rate, meaning that if you set your game to run at 60 FPS, it would try to run at that rate constantly, and skip rendering frames occasionally if it was falling behind. Now, in Engine’s constructor, you can choose whether you want a fixed-framerate or not. If not, Flash Player will update using ENTER_FRAME using your frameRate as a target, and you can use FP’s elapsed property to determine how much time has passed since the last frame and use that to determine movement and timer rates, something that Flixel developers will be used to.

### GRAPHICS
The Actor and Acrobat classes from previous versions no longer exist, all your game objects will extend the Entity class, and what they draw to the screen will depend on what you assign their Graphic property to. FlashPunk provides a useful set of Graphic components for different uses, such as Image for still images (optionally transformed), Spritemap for animated sprite strips, or Emitter as an efficient particle emitter option.
Note that a Graphiclist type also exists, which can be assigned to an Entity and contain multiple different Graphic types, all which can have different offsets, transformations, and animations. This provides a useful system for multi-sprite objects in your game (for example: an overhead display object that contains multiple hearts and a powerup meter).

### COLLISION
Collision works largely the same, except has also been improved using a component system similar to graphics. All Entity objects still use a Hitbox rectangle as their default collision bounds, but you can assign a specialized Mask to an Entity for more advanced collision. For example, you can assign it a Pixelmask type for pixel-perfect collision, or a Grid type to determine a large area of solid/nonsolid grid cells. This latter type works well in combination with the Tilemap graphic type, allowing a single Entity object to possibly handle an entire scene’s collision and rendering. Using a single Grid mask for collision is significantly faster than using a bunch of invisible Entity objects’ hitboxes.

### TWEENING
One of FlashPunk’s more specific and powerful features are the new Tween classes. All Entities and Worlds can have any amount of Tweens added to it. Currently the Tweens are divided into 3 categories: motion, sound, and miscellaneous tweens. Motion tweens provide you with a powerful set of objects useful for planned motion. For example, if I wanted an Entity to move from one point to another, I could assign it a LinearMotion tween and have its position sync with that tween’s x and y position when it updates (an Entity’s tweens are updated immediately before the Entity’s update() function itself is called). Classes for curved (quadratic and cubic bezier), paths (linear paths and quadratic paths), and circular motion are also provided. Tweens can be made even more effective when combined with Easing functions, allowing smooth transitions and other functionality.

### DEBUGGING
FlashPunk now has a console which lets you view lots of useful information in real-time, such as FPS, frame timing info, Entity count, and user-logged information. The user can select Entities and move them around while the console is paused, pan the camera, and also view user-specified properties for each Entity in the debug panel.

### AND MORE...
There are also many other various changes and improvements across the board, all which I cannot list off the top of my head. Suffice to say, version 0.87 has a total of 16 different classes, and the new version has over 40. FlashPunk is still simple to use and prototype in, but with version 1.0 onward, I aim to make it not only effectively simple and fast, but contain powerful and efficient internal systems for those who know how to use them.
22 changes: 22 additions & 0 deletions lib/flashpunk/license.txt
@@ -0,0 +1,22 @@
Copyright (c) 2010 Chevy Ray Johnston

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.
329 changes: 329 additions & 0 deletions lib/flashpunk/net/flashpunk/Engine.as
@@ -0,0 +1,329 @@
package net.flashpunk
{
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
import flash.utils.getTimer;

import net.flashpunk.utils.Draw;
import net.flashpunk.utils.Input;

/**
* Main game Sprite class, added to the Flash Stage. Manages the game loop.
*/
public class Engine extends MovieClip
{
/**
* If the game should stop updating/rendering.
*/
public var paused:Boolean = false;

/**
* Cap on the elapsed time (default at 30 FPS). Raise this to allow for lower framerates (eg. 1 / 10).
*/
public var maxElapsed:Number = 0.0333;

/**
* The max amount of frames that can be skipped in fixed framerate mode.
*/
public var maxFrameSkip:uint = 5;

/**
* The amount of milliseconds between ticks in fixed framerate mode.
*/
public var tickRate:uint = 4;

/**
* Constructor. Defines startup information about your game.
* @param width The width of your game.
* @param height The height of your game.
* @param frameRate The game framerate, in frames per second.
* @param fixed If a fixed-framerate should be used.
*/
public function Engine(width:uint, height:uint, frameRate:Number = 60, fixed:Boolean = false)
{
// global game properties
FP.width = width;
FP.height = height;
FP.halfWidth = width/2;
FP.halfHeight = height/2;
FP.assignedFrameRate = frameRate;
FP.fixed = fixed;
FP.timeInFrames = fixed;

// global game objects
FP.engine = this;
FP.screen = new Screen;
FP.bounds = new Rectangle(0, 0, width, height);
FP._world = new World;
FP.camera = FP._world.camera;
Draw.resetTarget();

// miscellaneous startup stuff
if (FP.randomSeed == 0) FP.randomizeSeed();
FP.entity = new Entity;
FP._time = getTimer();

// on-stage event listener
addEventListener(Event.ADDED_TO_STAGE, onStage);
}

/**
* Override this, called after Engine has been added to the stage.
*/
public function init():void
{

}

/**
* Updates the game, updating the World and Entities.
*/
public function update():void
{
if (FP.tweener.active && FP.tweener._tween) FP.tweener.updateTweens();
if (FP._world.active)
{
if (FP._world._tween) FP._world.updateTweens();
FP._world.update();
}
FP._world.updateLists();
if (FP._goto) checkWorld();
}

/**
* Renders the game, rendering the World and Entities.
*/
public function render():void
{
// timing stuff
var t:Number = getTimer();
if (!_frameLast) _frameLast = t;

// render loop
FP.screen.swap();
Draw.resetTarget();
FP.screen.refresh();
if (FP._world.visible) FP._world.render();
FP.screen.redraw();

// more timing stuff
t = getTimer();
_frameListSum += (_frameList[_frameList.length] = t - _frameLast);
if (_frameList.length > 10) _frameListSum -= _frameList.shift();
FP.frameRate = 1000 / (_frameListSum / _frameList.length);
_frameLast = t;
}

/**
* Override this; called when game gains focus.
*/
public function focusGained():void
{

}

/**
* Override this; called when game loses focus.
*/
public function focusLost():void
{

}

/**
* Sets the game's stage properties. Override this to set them differently.
*/
public function setStageProperties():void
{
stage.frameRate = FP.assignedFrameRate;
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.displayState = StageDisplayState.NORMAL;
}

/** @private Event handler for stage entry. */
private function onStage(e:Event = null):void
{
// remove event listener
removeEventListener(Event.ADDED_TO_STAGE, onStage);

// add focus change listeners
stage.addEventListener(Event.ACTIVATE, onActivate);
stage.addEventListener(Event.DEACTIVATE, onDeactivate);

// set stage properties
FP.stage = stage;
setStageProperties();

// enable input
Input.enable();

// switch worlds
if (FP._goto) checkWorld();

// game start
init();

// start game loop
_rate = 1000 / FP.assignedFrameRate;
if (FP.fixed)
{
// fixed framerate
_skip = _rate * (maxFrameSkip + 1);
_last = _prev = getTimer();
_timer = new Timer(tickRate);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
else
{
// nonfixed framerate
_last = getTimer();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}

/** @private Framerate independent game loop. */
private function onEnterFrame(e:Event):void
{
// update timer
_time = _gameTime = getTimer();
FP._flashTime = _time - _flashTime;
_updateTime = _time;
FP.elapsed = (_time - _last) / 1000;
if (FP.elapsed > maxElapsed) FP.elapsed = maxElapsed;
FP.elapsed *= FP.rate;
_last = _time;

// update console
if (FP._console) FP._console.update();

// update loop
if (!paused) update();

// update input
Input.update();

// update timer
_time = _renderTime = getTimer();
FP._updateTime = _time - _updateTime;

// render loop
if (!paused) render();

// update timer
_time = _flashTime = getTimer();
FP._renderTime = _time - _renderTime;
FP._gameTime = _time - _gameTime;
}

/** @private Fixed framerate game loop. */
private function onTimer(e:TimerEvent):void
{
// update timer
_time = getTimer();
_delta += (_time - _last);
_last = _time;

// quit if a frame hasn't passed
if (_delta < _rate) return;

// update timer
_gameTime = _time;
FP._flashTime = _time - _flashTime;

// update console
if (FP._console) FP._console.update();

// update loop
if (_delta > _skip) _delta = _skip;
while (_delta >= _rate)
{
FP.elapsed = _rate * FP.rate * 0.001;

// update timer
_updateTime = _time;
_delta -= _rate;
_prev = _time;

// update loop
if (!paused) update();

// update input
Input.update();

// update timer
_time = getTimer();
FP._updateTime = _time - _updateTime;
}

// update timer
_renderTime = _time;

// render loop
if (!paused) render();

// update timer
_time = _flashTime = getTimer();
FP._renderTime = _time - _renderTime;
FP._gameTime = _time - _gameTime;
}

/** @private Switch Worlds if they've changed. */
private function checkWorld():void
{
if (!FP._goto) return;
FP._world.end();
FP._world.updateLists();
if (FP._world && FP._world.autoClear && FP._world._tween) FP._world.clearTweens();
FP._world = FP._goto;
FP._goto = null;
FP.camera = FP._world.camera;
FP._world.updateLists();
FP._world.begin();
FP._world.updateLists();
}

private function onActivate (e:Event):void
{
FP.focused = true;
focusGained();
FP.world.focusGained();
}

private function onDeactivate (e:Event):void
{
FP.focused = false;
focusLost();
FP.world.focusLost();
}

// Timing information.
/** @private */ private var _delta:Number = 0;
/** @private */ private var _time:Number;
/** @private */ private var _last:Number;
/** @private */ private var _timer:Timer;
/** @private */ private var _rate:Number;
/** @private */ private var _skip:Number;
/** @private */ private var _prev:Number;

// Debug timing information.
/** @private */ private var _updateTime:uint;
/** @private */ private var _renderTime:uint;
/** @private */ private var _gameTime:uint;
/** @private */ private var _flashTime:uint;

// FrameRate tracking.
/** @private */ private var _frameLast:uint = 0;
/** @private */ private var _frameListSum:uint = 0;
/** @private */ private var _frameList:Vector.<uint> = new Vector.<uint>;
}
}

0 comments on commit 6de9afa

Please sign in to comment.