Space Time
/**
* Copyright Geo877 ( http://wonderfl.net/user/Geo877 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/g5wG
*/
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import gs.*;
import gs.easing.*;
[SWF(backgroundColor="#000000", frameRate=60)]
public class Main extends Sprite {
private var camera:view3D;
private var drawing:MovieClip;
static public var pointsToDraw:Array;
private var nativeZ:Number = 10;
private var closeZ:Number = 0;
private var characters:Array;
public function Main():void{
if (stage) start();
else
addEventListener(Event.ADDED_TO_STAGE, start);
}
private function start(e:Event = null):void{
removeEventListener(Event.ADDED_TO_STAGE,start);
init();
var i:Number =800;
while(i>0){
pointsToDraw.push(new point3(Math.random()*300 - 150,Math.random()*300 - 150,Math.random()*300-150));
i--;
}
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
addEventListener(Event.ENTER_FRAME,mainLoop);
var timer:Timer = new Timer(1000, 0);
timer.addEventListener("timer", updateTime);
timer.start();
}
private function mouseUp(e:MouseEvent):void{
}
private var updateCalls:Number = 0;
private function updateTime(event:TimerEvent = null):void{
var rightNow:Date = new Date();
updateCalls++;
if(updateCalls>30){
setString(rightNow.toDateString());
}else{
var hours:String = ""+rightNow.getHours();
var mins:String = ""+rightNow.getMinutes();
var seconds:String = ""+rightNow.seconds;
if(Number(hours)<10)hours="0"+hours;
if(Number(mins)<10)mins="0"+mins;
if(Number(seconds)<10)seconds="0"+seconds;
//if(hours>12)hours-=12;
setString(hours+":"+mins+":"+seconds);
}
if(updateCalls>40)updateCalls=0;
}
private function textToCoords(str:String,resolution:int=10,scale:Number=1):Array{
var coords:Array = new Array();
var tf:TextField = new TextField();
tf.text = str;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.background = false;
tf.selectable = false;
var format:TextFormat = new TextFormat("Lucida Console",resolution,0xFFFFFF,false);
tf.setTextFormat(format);
var bd:BitmapData = new BitmapData(tf.width, tf.height,false,0x00000000);
bd.draw(tf);
for(var x:Number = 0;x<bd.width;x++){
for(var y:Number = 0;y<bd.height;y++){
var c:uint = bd.getPixel(x,y);
if(c >= 0xAAAAAA){
coords.push(new point3((x-bd.width/2)*scale,(y-bd.height/2)*scale,0));
}
}
}
return coords;
}
private var currentString:String=new String();
private var stringCoords:Array = new Array();
private var e_point_index:int = 0;
private var used_points:Array = new Array();
private function setString(str:String):void{
if(str==currentString)return;
currentString = str;
var i:Number= used_points.length-1;
while(i>=0){
TweenLite.to(pointsToDraw[used_points[i]], 3, {
x:Math.random()*300 - 150,
y:Math.random()*300 - 150,
z:Math.random()*300 - 150,
ease:Expo.easeOut});
i--;
}
used_points = new Array();
stringCoords = textToCoords(str,15,1.0);
i = 0;
e_point_index = 0;
while(i<=stringCoords.length-1){
TweenLite.to(pointsToDraw[e_point_index], 1, {
x:stringCoords[i].x,
y:stringCoords[i].y,
z:0,
ease:Expo.easeOut
});
used_points.push(e_point_index);
e_point_index++;
if(e_point_index>pointsToDraw.length-1)e_point_index=0;
i++;
}
}
private function mainLoop(e:Event = null):void{
/*camera.p.x+=Math.sin(-camera.r.y)*speed;
camera.p.z+=Math.cos(-camera.r.y)*speed;
camera.p.y-=Math.sin(-camera.r.x)*speed;*/
//camera.p.z+=speed;
//camera.r.x =(stage.stageHeight/2-mouseY)/300;
//camera.r.y =(stage.stageWidth/2-mouseX)/300;
//camera.r.z +=0.1;
draw();
last_mouseX = mouseX;
last_mouseY = mouseY;
}
private function init():void{
pointsToDraw = new Array();
camera = new view3D(0,0,0,Math.PI/2.5,stage.stageWidth/2,stage.stageHeight/2,stage.stageWidth,stage.stageHeight);
view_ratio = Math.tan(camera.fov/2);
camera.p.z = -100;
camera.r.y=Math.PI/3;
camera.r.x=Math.PI/10;
completedYMax();
//completedXMax();
drawing = new MovieClip();
addChild(drawing);
last_mouseX = mouseX;
last_mouseY = mouseY;
}
private function completedYMax():void{
TweenLite.to(camera.r, 30, {y:-Math.PI/3, ease:Cubic.easeInOut, onComplete:completedYMin});
}
private function completedYMin():void{
TweenLite.to(camera.r, 30, {y:Math.PI/3, ease:Cubic.easeInOut, onComplete:completedYMax});
}
private function completedXMax():void{
TweenLite.to(camera.r, 20, {x:-Math.PI/6, ease:Cubic.easeInOut, onComplete:completedXMin});
}
private function completedXMin():void{
TweenLite.to(camera.r, 20, {x:Math.PI/6, ease:Cubic.easeInOut, onComplete:completedXMax});
}
private function draw():void{
drawing.graphics.clear();
//drawing.graphics.lineStyle(2,0xFFFFFF,1);
var r:Number = 8;
var i:int = pointsToDraw.length-1;
while(i>=0){
drawing.graphics.beginFill(0xFFFFFF,1);
var pointData:Object = get2DPointData(pointsToDraw[i]);
if(pointData.visible){
//scale by halfwidth/t_width*v_r*nativeZ
/*drawing.graphics.drawRect(
pointData.point_2D.x,
pointData.point_2D.y,
((r*2)/pointData.t_width)*view_ratio*nativeZ,
((r*2)/pointData.t_width)*view_ratio*nativeZ);*/
drawing.graphics.drawCircle(pointData.point_2D.x,pointData.point_2D.y,(r/pointData.t_width)*view_ratio*nativeZ);
}
drawing.graphics.endFill();
i--;
}
}
public function get2DPointData(point_3D:point3):Object{
var point_2D:point2 = new point2();
var pointCopy:point3 = new point3(point_3D.x,point_3D.y,point_3D.z);
//rotate
rotatePointZ(pointCopy,-camera.r.z);
rotatePointY(pointCopy,-camera.r.y);
rotatePointX(pointCopy,-camera.r.x);
//move
pointCopy.x -= camera.p.x;
pointCopy.y -= camera.p.y;
pointCopy.z -= camera.p.z;
var t_width:Number = view_ratio*pointCopy.z;
point_2D.x = (pointCopy.x/t_width)*camera.display.width*0.5+camera.display.x;
point_2D.y = (pointCopy.y/t_width)*camera.display.height*0.5+camera.display.y;
var visible:Boolean = false;
if(pointCopy.z>=closeZ){visible = true};
return {point_2D:point_2D,t_width:t_width,visible:visible};
}
static public function rotatePointX(point:point3,angleRads:Number):void{
var tempy:Number = point.y;var tempz:Number = point.z;
point.z = tempz*Math.cos(angleRads) - tempy*Math.sin(angleRads);
point.y = tempz*Math.sin(angleRads) + tempy*Math.cos(angleRads);
}
static public function rotatePointY(point:point3,angleRads:Number):void{
var tempx:Number = point.x;var tempz:Number = point.z;
point.x = tempx*Math.cos(angleRads) - tempz*Math.sin(angleRads);
point.z = tempx*Math.sin(angleRads) + tempz*Math.cos(angleRads);
}
static public function rotatePointZ(point:point3,angleRads:Number):void{
var tempx:Number = point.x;var tempy:Number = point.y;
point.x = tempx*Math.cos(angleRads) - tempy*Math.sin(angleRads);
point.y = tempx*Math.sin(angleRads) + tempy*Math.cos(angleRads);
}
/////------- # engine stuff # -------/////
private var view_ratio:Number;
private var last_mouseX:Number;
private var last_mouseY:Number;
//////////////////////////////////////////
}
}
import flash.geom.Rectangle;
class view3D {
public var p:point3;
public var fov:Number;
public var r:point3;
public var display:Rectangle;
public function view3D(px:Number=0,py:Number=0,pz:Number=0, pfov:Number=45,pdx:Number=250,pdy:Number=250,pdw:Number=500,pdh:Number=500):void{
p = new point3(px,py,pz);
fov = pfov;
r = new point3();
display = new Rectangle(pdx,pdy,pdw,pdh);
}
}
class point3 {
public var x:Number;public var y:Number;public var z:Number;
public function point3(px:Number=0,py:Number=0,pz:Number=0):void{x=px;y=py;z=pz;}
}
class point2 {
public var x:Number;public var y:Number;
public function point3(px:Number=0,py:Number=0):void{x=px;y=py;}
}