compi
screensaver
/**
* Copyright gokiburi ( http://wonderfl.net/user/gokiburi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8b5n
*/
package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.Shape;
import flash.events.Event;
import flash.display.StageQuality;
[SWF(width=1600, height=900, frameRate=60)]
public class compi extends Sprite {
public static const degToRad : Number = Math.PI / 180;
public static const totalDepths : uint = 8;
public var canvas : Sprite = new Sprite();
public var lye : LyeShape = new LyeShape();
public var lyeList : DSLL = new DSLL();
public static var centerX : Number;
public static var centerY : Number;
public static var backgroundSwatch : Swatch;
public static var canvasWidth : Number = 1600;
public static var canvasHeight : Number = 900;
public static function randomN( magnitude : Number ) : Number
{
return Math.random() * magnitude * 2 - magnitude;
}
public static function randomP( magnitude : Number ) : Number
{
return Math.random() * magnitude;
}
public function addLyeShape() : void
{
var lye : LyeShape = new LyeShape();
lye.x = canvasWidth / 2;
lye.y = canvasHeight / 2;
lye.depth = Math.floor(randomP(totalDepths));
lye.color.blendSwatch( backgroundSwatch, 1-lye.depth/(totalDepths-1) );
lyeList.addNode( lye , lye.depth );
}
public function compi() {
backgroundSwatch = new Swatch();
backgroundSwatch.setRGBA( 0, 0, 0, 1.0 );
stage.quality = StageQuality.MEDIUM;
stage.addChild(canvas);
stage.addEventListener("enterFrame", step );
}
public function step( e:Event ) : void {
canvas.graphics.clear();
canvas.graphics.beginFill( backgroundSwatch.getColor(), backgroundSwatch.a );
canvas.graphics.drawRect( 0, 0, canvasWidth , canvasHeight );
canvas.graphics.endFill();
addLyeShape();
var _next : DSLLNode = lyeList._first;
while( _next != null )
{
var _nlye : LyeShape = _next as LyeShape;
if ( _nlye .isAlive )
{
_nlye .update();
_nlye .render( canvas.graphics );
if ( !_nlye .isAlive ){
lyeList.removeNode( _next );
}
}
_next = _next._next;
}
}
}
}
internal class Swatch
{
public var r : uint;
public var g : uint;
public var b : uint;
public var a : Number;
function Swatch()
{
r = 0;
g = 0;
b = 0;
a = 1.0;
}
public function setRGBA( r : uint, g: uint, b: uint, a:Number ) : void
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public function getColor() : uint
{
return ( this.r << 16 | this.g << 8 | this.b );
}
public function getColorA() : uint
{
return ( this.r << 24 | this.g << 16 | this.b << 8 | this.a );
}
public function blend( r : uint, g : uint, b : uint, a:Number, percent:Number ) : void
{
this.r += (r - this.r) * percent;
this.g += (g - this.g) * percent;
this.b += (b - this.b) * percent;
this.a += (a - this.a) * percent;
}
public function blendSwatch( $swatch : Swatch, percent : Number ) : void
{
this.r += ($swatch.r - this.r) * percent;
this.g += ($swatch.g - this.g) * percent;
this.b += ($swatch.b - this.b) * percent;
this.a += ($swatch.a - this.a) * percent;
}
}
internal class DSLLNode
{
public var _next : DSLLNode;
public var _prev : DSLLNode;
public var _depth : Number;
}
internal class DSLL
{
public var _first : DSLLNode;
public var _last : DSLLNode;
public function addNode( $dsllNode : DSLLNode, depth : Number ) : void
{
$dsllNode._depth = depth;
if ( this._first == null )
{
this._first = $dsllNode;
this._last = $dsllNode;
} else if ( this._first == this._last ){
if ( $dsllNode._depth < this._first._depth )
{
this._first._prev = $dsllNode;
$dsllNode._next = this._first;
this._first = $dsllNode;
} else {
this._first._next = $dsllNode;
$dsllNode._prev = this._first;
this._last = $dsllNode;
}
} else {
var _current : DSLLNode = this._first;
while ( _current != null && $dsllNode._depth > _current._depth )
{
_current = _current._next;
}
if ( _current == this._first )
{
$dsllNode._next = this._first;
this._first._prev = $dsllNode;
this._first = $dsllNode;
} else if ( _current == null )
{
$dsllNode._prev = this._last;
this._last._next = $dsllNode;
this._last = $dsllNode;
} else {
$dsllNode._next = _current;
$dsllNode._prev = _current._prev;
_current._prev._next = $dsllNode;
_current._prev = $dsllNode;
}
}
}
public function removeNode( $dsllNode:DSLLNode ) : void
{
if ( this._first == $dsllNode)
{
this._first = $dsllNode._next;
this._first._prev = null;
} else if ( this._last == $dsllNode){
this._last = $dsllNode._prev;
this._last._next = null;
} else {
var _next : DSLLNode = $dsllNode._next;
var _prev : DSLLNode = $dsllNode._prev;
_prev._next = _next;
_next._prev = _prev;
}
}
}
import flash.display.Graphics;
internal class LyeShape extends DSLLNode
{
public var x : Number;
public var y : Number;
public var vx : Number;
public var vy : Number;
public var color : Swatch;
public var sides : Number;
public var radius : Number;
public var rotation : Number;
public var lifeTime : Number;
public var isAlive : Boolean;
public var depth : Number;
public var vs : Number;
function LyeShape()
{
this.x = 0;
this.y = 0;
this.vx = compi.randomN(5);
this.vy = compi.randomN(5);
this.color = new Swatch()
this.color.setRGBA( 128, 128, 128, 1.0 );
this.sides = 4;
this.radius = compi.randomP(56) + 8;
this.rotation = compi.randomP(360);
this.lifeTime = 512;
this.isAlive = true;
this.depth = 0;
this.vs = this.radius / this.lifeTime;
}
private function die() : void
{
this.isAlive = false;
}
public function update() : void
{
if ( isInsideCanvas() )
{
if ( this.lifeTime > 0 )
{
this.vx *= .995;
this.vy *= .995;
this.x += this.vx;
this.y += this.vy;
this.radius -= this.vs;
this.rotation += this.vx;
this.lifeTime--;
} else {
this.die();
}
} else {
this.die();
}
}
public function pointInRectValuesCollisionCheck( px : Number, py : Number, rx : Number, ry : Number, rw : Number, rh : Number ) : Boolean
{
var hw : Number = rw * 0.5;
var hh : Number = rh * 0.5;
rx += hw;
ry += hh;
var px : Number = hw - Math.abs(rx - px);
if ( px > 0 )
{
var py : Number = hh - Math.abs(ry - py);
if ( py > 0 )
{
return true;
}
}
return false;
}
public function isInsideCanvas() : Boolean
{
return pointInRectValuesCollisionCheck( this.x, this.y, -this.radius, -this.radius, compi.canvasWidth + this.radius * 2, compi.canvasHeight + this.radius * 2 );
}
public function render( graphics : Graphics ) : void
{
var shapeAngle : Number = 360 / this.sides * compi.degToRad;
var selfAngle : Number = this.rotation * compi.degToRad;
graphics.beginFill( this.color.getColor(), this.color.a );
for ( var i : uint = 0; i < this.sides; i++ )
{
if ( i == 0 ){
graphics.moveTo( this.x + Math.cos( shapeAngle * i + selfAngle ) * this.radius, this.y + Math.sin( shapeAngle * i + selfAngle ) * this.radius );
} else {
graphics.lineTo( this.x + Math.cos( shapeAngle * i + selfAngle ) * this.radius, this.y + Math.sin( shapeAngle * i + selfAngle ) * this.radius );
}
}
graphics.endFill();
}
}