package {
    import flash.display.*;
    import flash.events.*;
    import flash.ui.Keyboard;

    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
 
    public class MultipleObjects extends Sprite
    {
        private var container  : Sprite;
        private var scene      : MovieScene3D;
        private var camera     : Camera3D;
        private var rootNode   : DisplayObject3D;
        private var cubeObj1   : Cube;
        private var cubeObj2   : Cube;
        private var cubeObj3   : Cube;
        private var cubeObj4   : Cube;
        private var cubeObj5   : Cube;
        private var cubeObj6   : Cube;
        private var cubeObj7   : Cube;
        private var cubeObj8   : Cube;
        private var cubeObj9   : Cube;
        private var cubeObj10  : Cube;
        private var material1  : ColorMaterial;
        private var material2  : ColorMaterial;
        private var material3  : ColorMaterial;
        private var material4  : ColorMaterial;
        private var material5  : ColorMaterial;
        private var material6  : ColorMaterial;
        private var material7  : ColorMaterial;
        private var material8  : ColorMaterial;
        private var material9  : ColorMaterial;
        private var material10 : ColorMaterial;

        //---------------------------------------------Constructor
        public function MultipleObjects():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
 
            //コンテナ生成
            container = new Sprite();
            container.x = stage.stageWidth / 2;
            container.y = stage.stageHeight / 2;
            stage.addChild( container );
 
            //シーン生成
            scene = new MovieScene3D( container );
 
            //rootNode生成
            rootNode = new DisplayObject3D();
            scene.addChild( rootNode );
 
            //マテリアル1設定 最前列3つのオブジェクトの左側(白色)
            material1 = new ColorMaterial( 0xffffff, 1 );
            material1.doubleSided = true;
            material1.smooth = true;
            //Cube1オブジェクト生成
            cubeObj1 = new Cube(material1, 400, 400, 400, 2, 2, 2);
            cubeObj1.x = -400;
            rootNode.addChild(cubeObj1);

            //マテリアル2設定 最前列2つのオブジェクトの中心(灰色)
            material2 = new ColorMaterial( 0x888888, 1 );
            material2.doubleSided = true;
            material2.smooth = true;
            //Cube2オブジェクト生成
            cubeObj2 = new Cube(material2, 400, 400, 400, 2, 2, 2);
            rootNode.addChild(cubeObj2);

            //マテリアル3設定 最前列3つのオブジェクトの右側(白色)
            material3 = new ColorMaterial( 0xffffff, 1 );
            material3.doubleSided = true;
            material3.smooth = true;
            //Cube3オブジェクト生成
            cubeObj3 = new Cube(material3, 400, 400, 400, 2, 2, 2);
            cubeObj3.x = 400;
            rootNode.addChild(cubeObj3);

            //マテリアル4設定 2列目3つのオブジェクトの左側(灰色)
            material4 = new ColorMaterial( 0x888888, 1 );
            material4.doubleSided = true;
            material4.smooth = true;
            //Cube4オブジェクト生成
            cubeObj4 = new Cube(material4, 400, 400, 400, 2, 2, 2);
            cubeObj4.x = -400;
            cubeObj4.z = 400;
            rootNode.addChild(cubeObj4);

            //マテリアル5設定 2列目3つのオブジェクトの中心(白色)
            material5 = new ColorMaterial( 0xffffff, 1 );
            material5.doubleSided = true;
            material5.smooth = true;
            //Cube5オブジェクト生成
            cubeObj5 = new Cube(material5, 400, 400, 400, 2, 2, 2);
            cubeObj5.z = 400;
            rootNode.addChild(cubeObj5);

            //マテリアル6設定 2列目3つのオブジェクトの右側(灰色)
            material6 = new ColorMaterial( 0x888888, 1 );
            material6.doubleSided = true;
            material6.smooth = true;
            //Cube4オブジェクト生成
            cubeObj6 = new Cube(material6, 400, 400, 400, 2, 2, 2);
            cubeObj6.x = 400;
            cubeObj6.z = 400;
            rootNode.addChild(cubeObj6);

            //マテリアル7設定 3列目3つのオブジェクトの左側(白色)
            material7 = new ColorMaterial( 0xffffff, 1 );
            material7.doubleSided = true;
            material7.smooth = true;
            //Cube7オブジェクト生成
            cubeObj7 = new Cube(material7, 400, 400, 400, 2, 2, 2);
            cubeObj7.x = -400;
            cubeObj7.z = 800;
            rootNode.addChild(cubeObj7);

            //マテリアル8設定 3列目3つのオブジェクトの中心(灰色)
            material8 = new ColorMaterial( 0x888888, 1 );
            material8.doubleSided = true;
            material8.smooth = true;
            //Cube8オブジェクト生成
            cubeObj8 = new Cube(material8, 400, 400, 400, 2, 2, 2);
            cubeObj8.z = 800;
            rootNode.addChild(cubeObj8);

            //マテリアル9設定 3列目3つのオブジェクトの右側(白色)
            material9 = new ColorMaterial( 0xffffff, 1 );
            material9.doubleSided = true;
            material9.smooth = true;
            //Cube9オブジェクト生成
            cubeObj9 = new Cube(material9, 400, 400, 400, 2, 2, 2);
            cubeObj9.x = 400;
            cubeObj9.z = 800;
            rootNode.addChild(cubeObj9);

            //マテリアル10設定 操作用オブジェクト(青色)
            material10 = new ColorMaterial( 0x0000ff, 1 );
            material10.doubleSided = true;
            material10.smooth = true;
            //Cube10オブジェクト生成
            cubeObj10 = new Cube(material10, 400, 400, 400, 2, 2, 2);
            cubeObj10.y = 400;
            rootNode.addChild(cubeObj10);

            //camera設定
            camera = new Camera3D();
            camera.zoom = 1;
 
            stage.addEventListener(Event.ENTER_FRAME, loop);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(Event.RESIZE, onStageResize);
        }

        //---------------------------------------------loop
        private function loop(event:Event):void{
            camera.x = container.mouseX * 10;
            camera.y = container.mouseY * 10;
            scene.renderCamera( camera );
        }

        //---------------------------------------------onKeyDown
        private function onKeyDown(event:KeyboardEvent):void{       
            if( event.keyCode == Keyboard.LEFT ) {
               if (cubeObj10.x >= -350) {
                   cubeObj10.x -= 50;
               }
            } else if ( event.keyCode == Keyboard.RIGHT ) {
               if (cubeObj10.x <= 350) {
                   cubeObj10.x += 50;
               }
               
            } else if ( event.keyCode == Keyboard.UP ) {
               if (cubeObj10.z <= 750) {
                   cubeObj10.z += 50;
               }
            } else if ( event.keyCode == Keyboard.DOWN ) {
               if (cubeObj10.z >= 50) {
                   cubeObj10.z -= 50;
               }
            }

            scene.renderCamera( camera );
        }

        //---------------------------------------------onStageResize
        private function onStageResize(event:Event):void{
             container.x = this.stage.stageWidth  / 2;
             container.y = this.stage.stageHeight / 2;
        }
    }
}