Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 30177ac
Showing
18 changed files
with
1,486 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.grapefrukt { | ||
import flash.utils.getTimer; | ||
/** | ||
* ... | ||
* @author Martin Jonasson (m@grapefrukt.com) | ||
*/ | ||
public class Timestep { | ||
|
||
private var _game_speed :Number = 1; | ||
private var _target_frametime :Number = 0.6; | ||
private var _max_speed :Number = 3; | ||
private var _smoothing :Number = .5; | ||
|
||
private var _real_speed :Number = 0.0; | ||
private var _last_frame_time :Number = 0.0; | ||
private var _delta :Number = 0.0; | ||
|
||
/** | ||
* Initializes the timestepper | ||
* @param fps The target framerate you wish to maintain | ||
* @param gameSpeed The game's speed, useful for slowdown effects or general speed tweaking. 1 = 100% speed. | ||
* @param maxSpeed The maximum size of a timeDelta, steps will not be bigger than this | ||
* @param smoothing How much to smooth the step size across ticks, 1 gives old value full priority (value will never change), 0 means no smoothing, so new value will be used. | ||
*/ | ||
public function Timestep(fps:int = 60, gameSpeed:Number = 1.0, maxSpeed:Number = 3.0, smoothing:Number = 0.5) { | ||
_target_frametime = 1000 / fps; | ||
_smoothing = smoothing; | ||
this.gameSpeed = gameSpeed; | ||
this.maxSpeed = maxSpeed; | ||
} | ||
|
||
/** | ||
* Call this function every frame to get a updated timeDelta | ||
* @return timeDelta | ||
*/ | ||
public function tick():Number { | ||
_real_speed = (getTimer() - _last_frame_time) / _target_frametime; | ||
_last_frame_time = getTimer(); | ||
|
||
if (_real_speed > _max_speed) _real_speed = _max_speed; | ||
|
||
_delta -= (_delta - _real_speed) * (1 - _smoothing); | ||
|
||
return _delta * _game_speed; | ||
} | ||
|
||
public function get timeDelta():Number { return _delta * _game_speed; } | ||
|
||
public function get maxSpeed():Number { return _max_speed; } | ||
public function set maxSpeed(value:Number):void { _max_speed = value; } | ||
|
||
public function get gameSpeed():Number { return _game_speed; } | ||
public function set gameSpeed(value:Number):void { _game_speed = value; } | ||
|
||
public function get smoothing():Number { return _smoothing; } | ||
public function set smoothing(value:Number):void { | ||
if (value > 1) value = 1; | ||
if (value < 0) value = 0; | ||
_smoothing = value; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.grapefrukt.debug | ||
{ | ||
import flash.display.Sprite; | ||
import flash.text.TextField; | ||
import flash.text.TextFormat; | ||
import flash.text.TextFieldAutoSize; | ||
import flash.utils.getTimer; | ||
import flash.events.Event; | ||
|
||
public class FPS extends Sprite | ||
{ | ||
private var frametimes:Array; | ||
private var last_tick:uint = 0; | ||
private var fps_text:TextField; | ||
private var target_fps:Number = 0; | ||
private var manual_update:Boolean = false; | ||
private var fps_label:String = "FPS"; | ||
|
||
private var _t:Number = 0; | ||
private var _sum:Number = 0; | ||
private var _average:Number = 0; | ||
|
||
private var insert_pos:int = 0; | ||
|
||
private var BUFFER_SIZE:uint = 0; | ||
|
||
public var speedFraction:Number = 1; | ||
|
||
public function FPS(_target_fps:Number, _label:String = "fps", _manual_update:Boolean = false, textColor:int = 0xffffff) { | ||
target_fps = _target_fps; | ||
BUFFER_SIZE = target_fps; | ||
|
||
fps_label = _label; | ||
manual_update = _manual_update; | ||
|
||
frametimes = new Array(); | ||
|
||
for (var i:int = 0; i < BUFFER_SIZE; i++) frametimes.push(0); | ||
|
||
last_tick = getTimer(); | ||
|
||
var textformat:TextFormat = new TextFormat("Arial"); | ||
|
||
fps_text = new TextField(); | ||
fps_text.textColor = textColor; | ||
fps_text.selectable = false; | ||
fps_text.autoSize = TextFieldAutoSize.LEFT; | ||
fps_text.setTextFormat(textformat); | ||
fps_text.defaultTextFormat = textformat; | ||
addChild(fps_text); | ||
|
||
if(manual_update == false) addEventListener(Event.ENTER_FRAME, tick); | ||
} | ||
|
||
public function tick(event:Event = null):void { | ||
_t = getTimer() - last_tick; | ||
last_tick = getTimer(); | ||
|
||
frametimes[insert_pos] = _t; | ||
insert_pos++; | ||
if (insert_pos > BUFFER_SIZE) insert_pos = 0; | ||
|
||
_sum = 0; | ||
for each (var i:uint in frametimes) _sum += i; | ||
|
||
_average = _sum / BUFFER_SIZE; | ||
|
||
speedFraction = (1000/target_fps)/_average; | ||
|
||
fps_text.text = fps_label + ": " + Number(1000 / _average).toFixed(1) + " (" + Number(speedFraction*100).toFixed(0) + "%)"; | ||
} | ||
|
||
public function get average():Number { return _average; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.grapefrukt.debug | ||
{ | ||
import flash.display.Sprite; | ||
import flash.text.TextField; | ||
import flash.text.TextFormat; | ||
import flash.text.TextFieldAutoSize; | ||
import flash.utils.getTimer; | ||
import flash.events.Event; | ||
import flash.system.System; | ||
import flash.utils.Timer; | ||
import flash.events.TimerEvent; | ||
|
||
public class MEM extends Sprite | ||
{ | ||
|
||
private var mem_text:TextField; | ||
private var update_timer:Timer; | ||
|
||
public function MEM(color:uint = 0xffffff) { | ||
|
||
var textformat:TextFormat = new TextFormat("Arial"); | ||
|
||
mem_text = new TextField(); | ||
mem_text.textColor = color; | ||
mem_text.selectable = false; | ||
mem_text.autoSize = TextFieldAutoSize.LEFT; | ||
mem_text.setTextFormat(textformat); | ||
mem_text.defaultTextFormat = textformat; | ||
addChild(mem_text); | ||
|
||
update_timer = new Timer(250); | ||
update_timer.start(); | ||
|
||
update_timer.addEventListener(TimerEvent.TIMER, onTimerCallback); | ||
} | ||
|
||
private function onTimerCallback(event:Event):void { | ||
mem_text.text = "mem: " + Number(System.totalMemory / (1024 * 1024)).toFixed(1) + " MB"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.grapefrukt.debug { | ||
|
||
import flash.display.Sprite; | ||
import flash.text.TextField; | ||
import flash.text.TextFieldAutoSize; | ||
import flash.text.TextFormat; | ||
|
||
public class TXT extends Sprite { | ||
|
||
private var txt_text:TextField; | ||
|
||
public function TXT(color:uint = 0xffffff, size:uint = 12){ | ||
|
||
var textformat:TextFormat = new TextFormat("Arial", size); | ||
|
||
txt_text = new TextField(); | ||
txt_text.textColor = color; | ||
txt_text.selectable = false; | ||
//txt_text.autoSize = TextFieldAutoSize.LEFT; | ||
txt_text.setTextFormat(textformat); | ||
txt_text.defaultTextFormat = textformat; | ||
addChild(txt_text); | ||
|
||
mouseChildren = false; | ||
mouseEnabled = false; | ||
|
||
txt_text.mouseWheelEnabled = true; | ||
|
||
txt_text.height = 580; | ||
txt_text.width = 400; | ||
} | ||
|
||
public function setText(str:String):void { | ||
txt_text.text = str + "\n"; | ||
} | ||
|
||
public function appendText(str:String):void { | ||
txt_text.appendText(str + "\n"); | ||
} | ||
|
||
public function set selectable(value:Boolean):void { | ||
txt_text.selectable = value; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.grapefrukt.display { | ||
|
||
import com.grapefrukt.display.utilities.ColorConverter; | ||
import flash.display.GradientType; | ||
import flash.display.Shape; | ||
import flash.display.SpreadMethod; | ||
import flash.display.Sprite; | ||
import flash.events.Event; | ||
import flash.geom.Matrix; | ||
|
||
/** | ||
* ... | ||
* @author Martin Jonasson | ||
*/ | ||
public class FadedBackground extends Sprite { | ||
|
||
private var _base_color :uint = 0x619928; | ||
private var _faded_color :uint; | ||
private var _gfx :Shape; | ||
private var _baseWidth :Number = 0; | ||
private var _baseHeight :Number = 0; | ||
|
||
private var _stageNormalWidth :Number = 0; | ||
private var _stageNormalHeight :Number = 0; | ||
private var _ratios:Array; | ||
|
||
public function FadedBackground(newColor:uint, stageNormalWidth:Number, stageNormalHeight:Number, fadedColor:uint = 0x000000, ratios:Array = null):void { | ||
_base_color = newColor; | ||
_stageNormalWidth = stageNormalWidth; | ||
_stageNormalHeight = stageNormalHeight; | ||
|
||
if(fadedColor == 0x000000){ | ||
var fadedColorHSB:Array = ColorConverter.UINTtoHSB(_base_color); | ||
fadedColorHSB[2] *= 0.3; | ||
fadedColor = ColorConverter.HSBtoUINT(fadedColorHSB[0], fadedColorHSB[1], fadedColorHSB[2]); | ||
} | ||
_faded_color = fadedColor; | ||
|
||
if(!ratios) ratios = [0x00, 0xFF] | ||
_ratios = ratios; | ||
|
||
_gfx = new Shape(); | ||
addChild(_gfx); | ||
|
||
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); | ||
} | ||
|
||
private function resizeHandler(event:Event):void { | ||
var stageRatio:Number = stage.stageWidth / stage.stageHeight; | ||
var bkgRatio:Number = _baseWidth / _baseWidth; | ||
|
||
if(stageRatio >= bkgRatio){ | ||
width = stage.stageWidth; | ||
height = width / _baseWidth * _baseWidth; | ||
} else { | ||
height = stage.stageHeight; | ||
width = height / _baseWidth * _baseWidth; | ||
} | ||
|
||
} | ||
|
||
private function addedToStageHandler(event:Event):void { | ||
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); | ||
redraw(); | ||
resizeHandler(null); | ||
stage.addEventListener(Event.RESIZE, resizeHandler); | ||
} | ||
|
||
public function redraw(newColor:int = -1):void { | ||
if(newColor >= 0) _base_color = newColor; | ||
|
||
graphics.clear(); | ||
var fillType:String = GradientType.RADIAL; | ||
var colors:Array = [_base_color, _faded_color]; | ||
var alphas:Array = [1, 1]; | ||
var ratios:Array = _ratios; | ||
var matr:Matrix = new Matrix(); | ||
matr.createGradientBox(300, 300, 0, -50, -50); | ||
var spreadMethod:String = SpreadMethod.PAD; | ||
_gfx.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod, null); | ||
_gfx.graphics.drawRect(0, 0, 200, 200); | ||
|
||
_baseWidth = _gfx.width; | ||
_baseWidth = _gfx.height; | ||
|
||
_gfx.x = -_baseWidth / 2; | ||
_gfx.y = -_baseWidth / 2; | ||
x = _stageNormalWidth / 2; | ||
y = _stageNormalHeight / 2; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.