Puzzle
A nice little puzzle i thought i'd fork and create using alternativa3d 8
package {
import alternativa.engine3d.controllers.SimpleObjectController;
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3D;
import alternativa.engine3d.core.Resource;
import alternativa.engine3d.core.View;
import alternativa.engine3d.core.Debug;
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.resources.BitmapTextureResource;
import alternativa.engine3d.objects.Sprite3D;
import alternativa.engine3d.objects.SkyBox;
import alternativa.engine3d.primitives.Box;
import alternativa.engine3d.core.events.MouseEvent3D;
import com.bit101.components.Label;
import caurina.transitions.Tweener;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="60")]
public class neunPuzzle extends Sprite {
private var scene:Object3D = new Object3D();
private var gridcontainer:Object3D = new Object3D();
private var controller:SimpleObjectController;
private var camera:Camera3D;
private var stage3D:Stage3D;
private var ui:Sprite = new Sprite();
private var _sArray:Array=[0,1,2,3,4,8,6,7,5];
private var myArray:Array=[
[99,99,99,99,99],
[99,0,1,2,99],
[99,3,4,5,99],
[99,6,7,8,99],
[99,99,99,99,99]
];
private var _BlockList:Array=new Array();
private var myArray_pos:Object={nineY:98, nineX:98, ue:98, st:98, mg:98, hd:98};
private var ckClick:Boolean=false;
private var rndArray:Array=[1000,-1000];
private var ckClear:Boolean=false;
public function neunPuzzle()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
camera = new Camera3D(1, 10000);
camera.view = new View(stage.stageWidth, stage.stageHeight, false, 0x000000, 0, 4);
addChild(camera.view);
addChild(camera.diagram);
//camera.debug = true;
camera.addToDebug(Debug.BOUNDS,Object3D);
// Initial position
camera.x = 0;
camera.z = -200;
camera.y = 0;
//camera.fov = Math.PI / 2;
controller = new SimpleObjectController(stage, camera, 200);
controller.lookAt(new Vector3D(0,0,0));
scene.addChild(camera);
camera.rotationZ = 180 * Math.PI / 180;
scene.addChild(gridcontainer);
stage3D = stage.stage3Ds[0];
stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
stage3D.requestContext3D();
}
private function onContextCreate(e:Event):void {
stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
var info:Label = new Label(ui, 10, 10, "http://davidejones.com");
addChild(ui);
setupScene();
stage.addEventListener(Event.ENTER_FRAME, EnterFrame);
stage.addEventListener(Event.RESIZE, Resize);
}
private function setupScene():void
{
//ShuffleArray
var sa:Number = _sArray.length;
while (sa--) {
var j:Number = Math.floor(Math.random()*(sa+1));
var t:Number = _sArray[sa];
_sArray[sa] = _sArray[j];
_sArray[j] = t;
}
funcReady1();
}
private function funcReady1():void {
var bg:back_ground = new back_ground();
var bgbd:BitmapData = new BitmapData(bg.width, bg.height, false, 0xFFFFFF);
bgbd.draw(bg);
var bgmat:TextureMaterial = new TextureMaterial(new BitmapTextureResource(bgbd,true));
var sbox:SkyBox = new SkyBox(1000, bgmat, bgmat, bgmat, bgmat, bgmat, bgmat);
scene.addChild(sbox);
for(var j:Number = 0; j < _sArray.length; j++)
{
if(j!=8){
var bmpData1:BitmapData = new BitmapData(50,50,false,0xFF0000);
var shapes:drawBlockFront=new drawBlockFront(j,String(j+1));
bmpData1.draw(shapes);
var mat1:TextureMaterial = new TextureMaterial(new BitmapTextureResource(bmpData1,true));
var bmpData2:BitmapData = new BitmapData(50,50,false,0x00FF00);
var shapes2:drawBlockSide=new drawBlockSide(j);
bmpData2.draw(shapes2);
var mat2:TextureMaterial = new TextureMaterial(new BitmapTextureResource(bmpData2,true));
var box:Box = new Box(50, 50, 50, 1, 1, 1, false, mat1);
box.addEventListener(MouseEvent3D.CLICK, funcRelease);
//box.getSurface(2).material = mat2;
box.name = ""+j;
gridcontainer.addChild(box);
_BlockList.push(box);
}
}
funcReady2();
}
private function funcReady2():void {
for (var j:Number =0 ; j < _sArray.length; j++) {
var tempBox:Box = _BlockList[_sArray[j]] as Box;
myArray[(Math.floor(j/3))+1][(j%3)+1]=_sArray[j];
if(_sArray[j]!=8) {
tempBox.y=Math.floor(j/3)*-60+60;
tempBox.x=j%3*60-60;
//tempBox.addEventListener(MouseEvent3D.CLICK, funcRelease);
} else if(_sArray[j]==8) {
myArray_pos["nineY"]=(Math.floor(j/3))+1;
myArray_pos["nineX"]=(j%3)+1;
}
}
ckAzimuth();
uploadResources(scene.getResources(true));
}
private function funcRelease(e:MouseEvent3D):void {
trace("clicked");
var t_C:Box=e.target as Box;
var _move:Number;
var yy:Number=myArray_pos["nineY"];
var xx:Number=myArray_pos["nineX"];
if(t_C.name==myArray_pos["ue"] && ckClick==true){
ckClick=false;
_move=t_C.y-60;
Tweener.addTween(t_C, {y:_move, time:0.6, transition:"easeOutBack"
,onComplete:function():void{
myArray[yy][xx]=t_C.name;
myArray[yy-1][xx]=8;
myArray_pos["nineY"]=yy-1;
ckAzimuth();
}
});
//Tweener.addTween(camera,{y:-20, time:1, transition:"easeOutQuad"});
Tweener.addTween(gridcontainer,{rotationX:-10 * Math.PI/180, time:1, transition:"easeOutQuad"});
}
else if(t_C.name==myArray_pos["st"]&&ckClick==true){
ckClick=false;
_move=t_C.y+60;
Tweener.addTween(t_C, {y:_move, time:0.6, transition:"easeOutBack"
,onComplete:function():void{
myArray[yy][xx]=t_C.name;
myArray[yy+1][xx]=8;
myArray_pos["nineY"]=yy+1;
ckAzimuth();
}
});
//Tweener.addTween(camera,{y:20, time:1, transition:"easeOutQuad"});
Tweener.addTween(gridcontainer,{rotationX:10 * Math.PI/180, time:1, transition:"easeOutQuad"});
}
else if(t_C.name==myArray_pos["hd"]&&ckClick==true){
ckClick=false;
_move=t_C.x+60;
Tweener.addTween(t_C, {x:_move, time:0.6, transition:"easeOutBack"
,onComplete:function():void{
myArray[yy][xx]=t_C.name;
myArray[yy][xx-1]=8;
myArray_pos["nineX"]=xx-1;
ckAzimuth();
}
});
//Tweener.addTween(camera,{x:-20, time:1, transition:"easeOutQuad"});
Tweener.addTween(gridcontainer,{rotationY:-10 * Math.PI/180, time:1, transition:"easeOutQuad"});
}
else if(t_C.name==myArray_pos["mg"]&&ckClick==true){
ckClick=false;
_move=t_C.x-60;
Tweener.addTween(t_C, {x:_move, time:0.6, transition:"easeOutBack"
,onComplete:function():void{
myArray[yy][xx]=t_C.name;
myArray[yy][xx+1]=8;
myArray_pos["nineX"]=xx+1;
ckAzimuth();
}
});
//Tweener.addTween(camera,{x:20, time:1, transition:"easeOutQuad"});
Tweener.addTween(gridcontainer,{rotationY:10 * Math.PI/180, time:1, transition:"easeOutQuad"});
}
}
private function ckAzimuth():void {
var _9y:Number=myArray_pos["nineY"];
var _9x:Number=myArray_pos["nineX"];
if(myArray[_9y-1][_9x]==99){myArray_pos["ue"]=99}else{myArray_pos["ue"]=myArray[_9y-1][_9x]};
if(myArray[_9y+1][_9x]==99){myArray_pos["st"]=99}else{myArray_pos["st"]=myArray[_9y+1][_9x]};
if(myArray[_9y][_9x-1]==99){myArray_pos["hd"]=99}else{myArray_pos["hd"]=myArray[_9y][_9x-1]};
if(myArray[_9y][_9x+1]==99){myArray_pos["mg"]=99}else{myArray_pos["mg"]=myArray[_9y][_9x+1]};
ckGoal();
}
private function ckGoal():void {
if(myArray[1][1]==0 && myArray[1][2]==1 && myArray[1][3]==2 && myArray[2][1]==3 && myArray[2][2]==4 && myArray[2][3]==5 && myArray[3][1]==6 && myArray[3][2]==7 && myArray[3][3]==8)
{
funcClear();
}
ckClick=true;
}
private function funcClear():void {
for(var j:int=0; j < 8; j++){
var rndX:Number=rndArray[Math.floor(Math.random()*2)];
var rndY:Number=rndArray[Math.floor(Math.random()*2)];
Tweener.addTween(_BlockList[j], {x:rndX,y:rndY,delay:1+j/10,time:1.5,transition : "easeOutQuad"});
}
/*
var letterMat = new Letter3DMaterial(0xE3DA37,1);
letterMat.doubleSided=true;
var strCo = new Text3D("Congratulations!", new HelveticaBold(), letterMat);
strCo.z=2000;
strCo.scaleX=strCo.scaleY=1.5;
rootNode.addChild(strCo);
Tweener.addTween(strCo, {rotationY:360,delay:2,time:5,transition : "easeOutQuad"});
Tweener.addTween(camera, {z:-800,y:10,x:0,delay:2,time:3,transition : "easeOutQuad"});
*/
}
private function uploadResources(resources:Vector.<Resource>):void {
for each (var resource:Resource in resources) {
resource.upload(stage3D.context3D);
}
}
private function EnterFrame(e:Event):void
{
//controller.update();
camera.render(stage3D);
}
private function Resize(e:Event = null):void {
camera.view.width = stage.stageWidth;
camera.view.height = stage.stageHeight;
}
}
}
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.text.*;
class drawBlockFront extends Sprite{
public var _color:Array=[0xFFCCA7,0xFCC9DC,0xFFCCA7,0xFFDA8C,0xFFB67F,0xFFDA8C,0xFFCCA7,0xFCC9DC];
public var _color1:Array=[0xFB5E79,0xFE8D8D,0xFB5E79,0xFF9575,0xFF7268,0xFF9575,0xFB5E79,0xFE8D8D];
public function drawBlockFront(nums:Number,str:String){
var block_sp:Sprite = new Sprite();
block_sp.name="block_sp";
block_sp.graphics.beginFill(_color[nums]);
block_sp.graphics.drawRect(0,0,50,50);
block_sp.graphics.endFill();
var _pic:drawFlower=new drawFlower(0xffffff);
block_sp.addChild(_pic);
_pic.alpha=0.8;
_pic.scaleX=_pic.scaleY=5;
_pic.x=25;
_pic.y=25;
if(nums==8){this.alpha=0;} else{
var fmt:TextFormat = new TextFormat();
fmt.color = _color1[nums];
fmt.size = 40;
fmt.font="Impact"
var _txt:TextField = new TextField();
_txt.name="_txt";
_txt.defaultTextFormat = fmt;
_txt.text=str;
_txt.x=15;
_txt.y=1;
_txt.selectable = false;
block_sp.addChild(_txt);
}
filters = [ new GlowFilter( _color1[nums], 1, 20, 20, 1,2,true ) ] ;
addChild(block_sp);
mouseChildren = false;
}}
class drawBlockSide extends Sprite{
public var _color:Array=[0xFFCCA7,0xFCC9DC,0xFFCCA7,0xFFDA8C,0xFF917A,0xFFDA8C,0xFFCCA7,0xFCC9DC];//FF9B9B
public var _color1:Array=[0xFB5E79,0xFE8D8D,0xFB5E79,0xFF9575,0xFF4074,0xFF9575,0xFB5E79,0xFE8D8D];//DD4969
public function drawBlockSide(nums:Number){
var block_sp:Sprite = new Sprite();
block_sp.name="block_sp";
block_sp.graphics.beginFill(_color[nums]);
block_sp.graphics.drawRect(0,0,50,50);
block_sp.graphics.endFill();
var _pic:drawFlower=new drawFlower(0xffffff);
block_sp.addChild(_pic);
_pic.scaleX=_pic.scaleY=2;
_pic.x=25;
_pic.y=25;
filters = [ new GlowFilter( _color1[nums], 1, 20, 20, 1,2,true ) ] ;
addChild(block_sp);
mouseChildren = false;
}}
class drawFlower extends Sprite{
public function drawFlower(colors:uint){
for(var j:int=0; j < 5; j++){
var fl_sp:Sprite = new Sprite();
fl_sp.graphics.beginFill(colors);
fl_sp.graphics.drawEllipse(-1,0.5,2,4);
fl_sp.graphics.endFill();
fl_sp.rotation=j*72+180;
addChild(fl_sp);
}
}
}
class back_ground extends Sprite{
public function back_ground(){
var bg:Sprite=new Sprite();
bg.graphics.beginFill(0xFFFFFF,1);
bg.graphics.drawRect(0,0,465,465);
bg.graphics.endFill();
addChild(bg);
for(var j:uint=0; j < 100; j++) {
var panel:drawFlower=new drawFlower(0xFFFFFF);
panel.x=Math.floor (Math.random() * 465);
panel.y=Math.floor (Math.random() * 465);
panel.scaleX=panel.scaleY=Math.floor (Math.random() * 3)+2;
panel.rotation=Math.floor (Math.random() * 465);
panel.filters = [ new GlowFilter(0xFFB8A2, 1, 20, 20, 1,2,false ) ] ;
bg.addChild(panel);
}
}
}