前回ソースからRPGっぽいのをめざしてる中です

by siouxcitizen forked from Bitmap練習05:Bitmapデータの3DPlaneオブジェクトへの貼りつけ (diff: 707)
前回ソースからRPG的なのをめざしてる中です
♥0 | Line 740 | Modified 2010-08-10 15:03:29 | MIT License | (replaced)
play

ActionScript3 source code

/**
 * Copyright siouxcitizen ( http://wonderfl.net/user/siouxcitizen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/r0yQ
 */

// forked from siouxcitizen's Bitmap練習05:Bitmapデータの3DPlaneオブジェクトへの貼りつけ
//
//前回ソースからRPG的なのをめざしてる中です
//
//
package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.filters.GlowFilter;
    import org.papervision3d.view.Viewport3D;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.BitmapMaterial;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.events.InteractiveScene3DEvent;
    import org.papervision3d.materials.special.Letter3DMaterial;
    import org.papervision3d.typography.Text3D;
    import org.papervision3d.typography.fonts.HelveticaBold;
    import org.papervision3d.objects.parsers.DAE;
    import flash.display.Loader;
    import flash.system.LoaderContext;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    [SWF(width="500", height="500", backgroundColor="#0055FF")]
    public class Spring extends Sprite {
        private var goLandScrnBtn : SimpleButton;//ランド画面への遷移ボタン 
        private var goTitleScrnBtn : SimpleButton;//タイトル画面への遷移ボタン
        private var battleBtn : SimpleButton;//ブルーランド画面への遷移ボタン
        private var forwardBtn : SimpleButton;//前進ボタン
        private var leftRotBtn : SimpleButton;//左回転ボタン
        private var rightRotBtn : SimpleButton;//右回転ボタン
        private var backwardBtn : SimpleButton;//後退ボタン
        private var bDashBtn : SimpleButton;//Bダッシュボタン
        private var forwardState : Boolean = false; //操作矢印「前進」制御用
        private var backwardState : Boolean = false; //操作矢印「後退」制御用
        private var bDashState : Boolean = false; //操作矢印「Bダッシュ」制御用
        private var leftRotState : Boolean = false; //操作矢印「左回転」制御用
        private var rightRotState : Boolean = false; //操作矢印「右回転」制御用
        private var screenId : int = 0;  //0:タイトル画面 1:ランド画面
        private const TITLE_SCREEN_ID : int = 0;  //タイトル画面ID
        private const LAND_SCREEN_ID : int = 1;  //ランド画面ID
        private const RED_LAND_SCREEN_ID : int = 2;  //レッドランド画面ID
        private const BLUE_LAND_SCREEN_ID : int = 3;  //ブルーランド画面ID
        private var txtField:Object=new Object(); //テキストフィールド用
        private var txtFormat:Object=new Object();//テキストフォーマット用
        //3D表示用
        private var Objs : Object = new Object(); //3Dオブジェクト保持用
        private var container : Sprite;
        private var viewport : Viewport3D;
        private var scene : Scene3D;
        private var camera : Camera3D;
        private var material : ColorMaterial;
        private var bitmapMaterial : BitmapMaterial;
        private var planeObj : Plane; 
        private var crystalBoxCube : Cube; //透明キューブ 飾り用クリスタル役
        private var renderer : BasicRenderEngine;
        private var materialList : MaterialsList = new MaterialsList();
        private var cameraPitch: int = 90; //カメラのX軸回転の値 
        private var cameraYaw : int = 270; //カメラのY軸回転の値 
        private var cameraDistStat: int = 1; //カメラ設置場所の距離種類 0~2
        private var title3DText : Text3D //タイトル3D文字
        private var letterformat : Letter3DMaterial; //タイトル3D文字フォーマット
        private var tempPlaneName : String;
        private var daeNomaneko : DAE; //操作「のまねこ」用DAE
        //「バトる」用データ
        private var battleStatus : Boolean = false; //「バトる」状態判定
        private var statusKeeper : StatusKeeper = new StatusKeeper(); //操作用キャラステータス保持用
        private var planeEnemy : Enemy; //3D敵オブジェクト
        //「バトる」ふきだし用
        private var battleFukidashiContena : Sprite = new Sprite(); //ふきだし入れ物
        private var battleFukidashi : Shape = new Shape(); //ふきだしの黒い外枠
        private var onBattleFukidashi : Shape = new Shape(); //ふきだしのテキスト表示部分
        private var battleComandFukidashiContena : Sprite = new Sprite(); //「バトる」状態コマンド用ふきだし入れ物
        private var battleComandFukidashi : Shape = new Shape(); //「バトる」状態コマンド用ふきだし
        private var attackBtn : SimpleButton; //「たたかう」ボタン
        private var escapeBtn : SimpleButton; //「にげる」ボタン
        //Bitmap貼りつけ用
        private var url : String;
        private var urlReq : URLRequest;
        private var loader : Loader;
        private var bmp : Bitmap;
        private var bmd : BitmapData;
        private var count:int = 0;    // 読み込んだ画像数
        private var images : Array;    // 読み込んだBitmap画像保持用配列
        private const LAND_MAP_DATA:Array = //ランド画面用マップデータ配列
            [[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
             [ 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [ 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2],
             [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]; //0:芝生Bitmap指定、1:砂Bitmap指定、2:水Bitmap指定
        private const IMAGE_URL:Array =
        [
            "http://assets.wonderfl.net/images/related_images/5/55/558c/558cd91da870c2bc32c54502d98d2d447833e1fd",
            "http://assets.wonderfl.net/images/related_images/0/02/0210/02104ec89c3ba9dd49dc3e636d3e97a8d55674f8",
            "http://assets.wonderfl.net/images/related_images/f/f6/f696/f696b4e7bc2541a9812b81fe89a27a1c00fbe447"
        ]; //Bitmap画像URL配列
        public function Spring() {
            //情報表示用テキストフィールドを初期化
            txtFieldInit();
            //「ふきだし」を初期化
            initFukidashi()
            //3D空間を初期化
            init3DSpace();
            //タイトル画面ボタンを初期化
            initTitleScrnBtn();
            //ランド画面ボタンを初期化
            initLandScrnBtn();
        }
        //テキストフィールドの初期化処理
        private function txtFieldInit():void{
            //テキストフィールド(ラベル)の生成
            txtField["titleScrnInfo"]=Util.makeTxtField(10,10,10,10);//タイトル画面表示用のテキストを設定
            txtField["landScrnInfo"]=Util.makeTxtField(10,10,300,40);//ランド画面表示用のテキストを設定
            txtField["battleFukidashiText"]=Util.makeTxtField(10,10,450,150);//「バトる」ふきだし用のテキストを設定
            txtField["battleStatus"]=Util.makeTxtField(10,10,120,75);//「バトる」ステータス表示用のテキストを設定
            //テキストフォーマットの生成
            txtFormat["titleScrnInfo"]=Util.makeTextFormat(15,0x000000);
            txtFormat["landScrnInfo"]=Util.makeTextFormat(15,0x000000);
            txtFormat["battleFukidashiText"]=Util.makeTextFormat(15,0x000000);
            txtFormat["battleStatus"]=Util.makeTextFormat(15,0xEEEEEE);
        }
        //テキストフィールドの編集
        private function editLabel(key:String,text:String):void {
            txtField[key].text=text;
            txtField[key].setTextFormat(txtFormat[key]);        
        }

        //タイトル画面情報テキストを設定
        public function addTitleScrnTxt():void {
            addChild(txtField["titleScrnInfo"]); //タイトル画面のテキストを表示(今回は使用せず)
            editLabel("titleScrnInfo", "");
        }
        //タイトル画面情報テキストを設定解除
        public function removeTitleScrnTxt():void {
            editLabel("titleScrnInfo", "");
            removeChild(txtField["titleScrnInfo"]); //タイトル画面のテキストを非表示(今回は使用せず)
        }
        //ランド画面情報テキストを設定
        private function addLandScrnTxt():void {
            addChild(txtField["landScrnInfo"]);
            var landScrnInfoTxt : String = "A,W,S,D,F,Rボタンで視点変更します" + "\n";
            editLabel("landScrnInfo",landScrnInfoTxt);
        }
        //ランド画面情報テキストを設定解除
        private function removeLandScrnTxt():void {
            editLabel("landScrnInfo", "");
            removeChild(txtField["landScrnInfo"]);
        }
        //「バトる」用ふきだし表示オブジェクトの初期化処理
        private function initFukidashi():void{
            //「バトる」用ふきだし入れ物
            battleFukidashiContena.x = 5;
            battleFukidashiContena.y = 370;
            //「バトる」用ふきだしの黒外枠用の画像を描画
            battleFukidashi.graphics.beginFill(0x333333, 1.0);
            battleFukidashi.graphics.drawRoundRect(0, 0, 480+10, 100 + 10 * 2, 10, 10);
            battleFukidashi.graphics.endFill();
            //「バトる」用ふきだしのテキスト表示部分を描画
            onBattleFukidashi.graphics.beginFill(0xeeeeee, 1.0);
            onBattleFukidashi.graphics.drawRoundRect(0, 0, 480, 100 + 10 , 10, 10);
            onBattleFukidashi.x = onBattleFukidashi.y = 10 / 2;

            //「たたかう」ボタン
            attackBtn = new CustomButton("たたかう");
            attackBtn.addEventListener(MouseEvent.CLICK,attackBtnClick);
            //attackBtn.addEventListener(MouseEvent.MOUSE_OVER,overAttackBtn);
            attackBtn.x = 10;
            attackBtn.y = 95;
            //「にげる」ボタン
            escapeBtn = new CustomButton("にげる");
            escapeBtn.addEventListener(MouseEvent.CLICK,escapeBtnClick);
            //escapeBtn.addEventListener(MouseEvent.MOUSE_OVER,overEscapeBtn);
            escapeBtn.x = 10;
            escapeBtn.y = 125;
            //「バトる」状態コマンド用ふきだし入れ物
            battleComandFukidashiContena.x = 370;
            battleComandFukidashiContena.y = 200;
            //「バトる」状態コマンド用ふきだし
            battleComandFukidashi.graphics.beginFill(0x333333, 1.0);
            battleComandFukidashi.graphics.drawRoundRect(0, 0, 120, 160, 10, 10);
            battleComandFukidashi.graphics.endFill();
            //「バトる」状態コマンド用ふきだしのボタンを設定

            battleComandFukidashiContena.addChild(battleComandFukidashi);
            battleComandFukidashiContena.addChild(attackBtn);
            battleComandFukidashiContena.addChild(escapeBtn);
        }
        //「たたかう」ボタン押下時処理
        private function attackBtnClick(event:MouseEvent):void {
            if (battleStatus == false) { //敵を倒したか、倒された場合に戦闘終了している状態時
                endBattleCommand(); //「バトる」コマンド状態終了処理をよびだす
                return;
            }
            var fukidashiTxt : String;
            var attackPoint : int = 0; //敵に与えたダメージ
            var attackedPoint : int = 0; //敵から受けたダメージ

            attackPoint = statusKeeper.getAttackPoint() - planeEnemy.getDiffencePoint(); //敵に与えるダメージを計算 
            attackedPoint = planeEnemy.getAttackPoint() - statusKeeper.getDiffencePoint(); //敵から受けたダメージを計算
            //上記減算の差が、正の値(攻撃力>防御力)の場合ダメージを追加 負の値(防御力>攻撃力)の場合ダメージは0に設定
            if(attackPoint <= 0) {attackPoint = 0; }
            if(attackedPoint <= 0) {attackedPoint = 0; }

            fukidashiTxt = "「のまねこ」のこうげき\n"
                         + "「白い紙切れ」に" + attackPoint + "のダメージをあたえた\n";
            planeEnemy.minusLifePoint(attackPoint);
            if (planeEnemy.getLifePoint() == 0) {
                fukidashiTxt += "「白い紙切れ」をたおした!\n";
                statusKeeper.fillLifePoint();
                fukidashiTxt += planeEnemy.getKanePoint() + "カーネを手に入れた!\n";
                statusKeeper.addKanePoint(planeEnemy.getKanePoint());
                battleFukidashiTextDisplay(fukidashiTxt); //「バトる」用ふきだしテキスト表示
                battleStatus = false;
                return;
            }

            fukidashiTxt += "「白い紙切れ」のこうげき\n"
                          + "「のまねこ」は" + attackedPoint + "のダメージをうけた\n";
            statusKeeper.minusLifePoint(attackedPoint);
            if (statusKeeper.getLifePoint() == 0) {
                fukidashiTxt += "「のまねこ」はやられてしまった!\n";
                statusKeeper.fillLifePoint();
                battleFukidashiTextDisplay(fukidashiTxt); //「バトる」用ふきだしテキスト表示
                battleStatusDisplay(); //「バトる」状態ステータス表示
                battleStatus = false;
                return;
            }
            battleFukidashiTextDisplay(fukidashiTxt); //「バトる」用ふきだしテキスト表示
            battleStatusDisplay(); //「バトる」状態ステータス表示
        }
        //「バトる」用ふきだしテキスト表示
        private function battleFukidashiTextDisplay(dispTxt:String):void {
            editLabel("battleFukidashiText",dispTxt);
            battleFukidashiContena.addChild(txtField["battleFukidashiText"]);
        }
        //「バトる」状態ステータス表示
        private function battleStatusDisplay():void {
            var battleStatusTxt : String;
            battleStatusTxt = "ライフ: " + statusKeeper.getLifePoint() + "\n" 
                            + "攻撃力: " + statusKeeper.getAttackPoint() + "\n"
                            + "防御力: " + statusKeeper.getDiffencePoint() + "\n"
                            + "カーネ: " + statusKeeper.getKanePoint() + "\n";
            editLabel("battleStatus",battleStatusTxt);
            battleComandFukidashiContena.addChild(txtField["battleStatus"]);
        }
        //「にげる」ボタン押下時処理
        private function escapeBtnClick(event:MouseEvent):void {
            endBattleCommand(); //「バトる」コマンド状態終了処理をよびだす
            battleStatus = false;
        }
        //「バトる」コマンド状態終了処理
        private function endBattleCommand():void {
            //敵オブジェクトリリース
            scene.removeChildByName("planeEnemyBody");
            //「バトる」用ふきだしリリース
            removeChild(battleFukidashiContena);
            //「バトる」状態コマンド用ふきだしリリース
            removeChild(battleComandFukidashiContena);
        }
//■■■タイトル画面ボタン初期化スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //タイトル画面ボタンを初期化する
        public function initTitleScrnBtn():void {
            goLandScrnBtn = new CustomButton("ランド画面");
            goLandScrnBtn.x = 400;
            goLandScrnBtn.y = 280;
            goLandScrnBtn.addEventListener(MouseEvent.MOUSE_DOWN,goLandScrnBtnDown);
            addChild(goLandScrnBtn);
        }
        //タイトル画面ボタンを非表示にする
        public function hideTitleScrnBtn():void {
            goLandScrnBtn.visible = false;
        }
        //タイトル画面ボタンを表示する
        public function dispTitleScrnBtn():void {
            goLandScrnBtn.visible = true;
        }
//■■■タイトル画面ボタン初期化エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■ランド画面ボタン初期化スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //ランド画面ボタンを初期化する
        public function initLandScrnBtn():void {
            //「タイトル画面」ボタン設定
            goTitleScrnBtn = new CustomButton("タイトル画面");
            goTitleScrnBtn.x = 390;
            goTitleScrnBtn.y = 280;
            goTitleScrnBtn.addEventListener(MouseEvent.MOUSE_DOWN,goTitleScrnBtnDown);
            addChild(goTitleScrnBtn);
            battleBtn = new CustomButton("バトる");
            battleBtn.x = 390;
            battleBtn.y = 310;
            battleBtn.addEventListener(MouseEvent.MOUSE_DOWN,battleBtnDown);
            addChild(battleBtn);

            forwardBtn = new CustomButton("前進");
            forwardBtn.x = 200;
            forwardBtn.y = 410;
            leftRotBtn = new CustomButton("左回転");
            leftRotBtn.x = 90;
            leftRotBtn.y = 440;
            bDashBtn = new CustomButton("Bダッシュ");
            bDashBtn.x = 200;
            bDashBtn.y = 440;
            rightRotBtn = new CustomButton("右回転");
            rightRotBtn.x = 310;
            rightRotBtn.y = 440;
            backwardBtn = new CustomButton("後退");
            backwardBtn.x = 200;
            backwardBtn.y = 470;
            forwardBtn.addEventListener(MouseEvent.MOUSE_DOWN,onForwardBtnDown);
            forwardBtn.addEventListener(MouseEvent.MOUSE_UP,onForwardBtnUp);
            forwardBtn.addEventListener(MouseEvent.MOUSE_OUT,onForwardBtnOut);
            leftRotBtn.addEventListener(MouseEvent.MOUSE_DOWN,onLeftRotBtnDown);
            leftRotBtn.addEventListener(MouseEvent.MOUSE_UP,onLeftRotBtnUp);
            leftRotBtn.addEventListener(MouseEvent.MOUSE_OUT,onLeftRotBtnOut);
            bDashBtn.addEventListener(MouseEvent.MOUSE_DOWN,onBDashBtnDown);
            bDashBtn.addEventListener(MouseEvent.MOUSE_UP,onBDashBtnUp);
            bDashBtn.addEventListener(MouseEvent.MOUSE_OUT,onBDashBtnOut);
            rightRotBtn.addEventListener(MouseEvent.MOUSE_DOWN,onRightRotBtnDown);
            rightRotBtn.addEventListener(MouseEvent.MOUSE_UP,onRightRotBtnUp);
            rightRotBtn.addEventListener(MouseEvent.MOUSE_OUT,onRightRotBtnOut);
            backwardBtn.addEventListener(MouseEvent.MOUSE_DOWN,onBackwardBtnDown);
            backwardBtn.addEventListener(MouseEvent.MOUSE_UP,onBackwardBtnUp);
            backwardBtn.addEventListener(MouseEvent.MOUSE_OUT,onBackwardBtnOut);
            addChild(forwardBtn);
            addChild(leftRotBtn);
            addChild(bDashBtn);
            addChild(rightRotBtn);
            addChild(backwardBtn);

            //ランド画面に遷移するまでボタンを非表示
            hideLandScrnBtn();
        }
        //ランド画面ボタンを非表示
        private function hideLandScrnBtn():void {
            goTitleScrnBtn.visible = false;
            battleBtn.visible = false;
            forwardBtn.visible = false;
            leftRotBtn.visible = false;
            rightRotBtn.visible = false;
            backwardBtn.visible = false;
            bDashBtn.visible = false;
        }
        //ランド画面ボタンを再表示
        private function dispLandScrnBtn():void {
            goTitleScrnBtn.visible = true;
            battleBtn.visible = true;
            forwardBtn.visible = true;
            leftRotBtn.visible = true;
            rightRotBtn.visible = true;
            backwardBtn.visible = true;
            bDashBtn.visible = true;
        }
//■■■ランド画面ボタン初期化エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■3D空間を初期化スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //3D空間を初期化
        private function init3DSpace():void {
            //ビューポート生成
            viewport = new Viewport3D(500, 500, false, true);
            viewport.opaqueBackground = 0x0055FF;
            addChild(viewport);
            //シーン生成
            scene = new Scene3D();
            //レンダリングエンジン生成
            renderer = new BasicRenderEngine();

             //カメラ設定
            camera = new Camera3D();
            camera.y = 200;
            camera.target = DisplayObject3D.ZERO;

            //タイトル文字の設定 引数は色コード・透過度
            letterformat = new Letter3DMaterial(0xFF9999 , 0.9);
            letterformat.doubleSided = true
            title3DText = new Text3D("3D RPG Practice 01" , new HelveticaBold() , letterformat);
            title3DText.y += 400;
            title3DText.scale = 1;
            Objs["title3DText"] = title3DText;
            scene.addChild(Objs["title3DText"], "title3DText");

            //最初のクエスト画面用に3Dオブジェクト初期化
            var tempBlueColorMaterial : ColorMaterial = new ColorMaterial(0x0000EE, 0.5);
            tempBlueColorMaterial.doubleSided = true;
            var tempRedColorMaterial : ColorMaterial = new ColorMaterial(0xEE0000, 0.5);
            tempRedColorMaterial.doubleSided = true;
            var tempGreenColorMaterial : ColorMaterial = new ColorMaterial(0x00EE00, 0.5);
            tempGreenColorMaterial.doubleSided = true;
            materialList.addMaterial(tempRedColorMaterial, "front");
            materialList.addMaterial(tempBlueColorMaterial, "back");
            materialList.addMaterial(tempGreenColorMaterial, "right");
            materialList.addMaterial(tempBlueColorMaterial, "left");
            materialList.addMaterial(tempRedColorMaterial, "top");
            materialList.addMaterial(tempGreenColorMaterial, "bottom");
            //飾り用3Dクリスタル表示
            crystalBoxCube = new Cube(materialList, 350, 350, 350);
            Objs["crystalBoxCube"] = crystalBoxCube;
            scene.addChild(Objs["crystalBoxCube"], "crystalBoxCube");

            //ランド画面土台3DPlaneオブジェクトにBitmap画像を貼りつけ
            images = new Array(IMAGE_URL.length);
            for (var i:int = 0; i < IMAGE_URL.length; i++) {
                var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
                loader.load(new URLRequest(IMAGE_URL[i]), new LoaderContext(true));
                loader.name = i.toString();
            }

            //DAEファイルより3D「のまねこ」を読み込み生成
            daeNomaneko = new DAE();
            daeNomaneko.load("http://oretaikan.atukan.com/ActionScript/DaeTest/DaeTest10/nomaNeko.dae");
            daeNomaneko.scale = 50;
            Objs["daeNomaneko"] = daeNomaneko;

            //マウスイベント処理用リスナを設定
            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }

        private function onComplete(e:Event):void
        {
            bmp = new Bitmap();
            bmd = new BitmapData(e.currentTarget.loader.width, e.currentTarget.loader.height, true, 0x000000)
            bmd.draw(e.currentTarget.loader);
            bmp.bitmapData = bmd;
            images[e.currentTarget.loader.name] = bmp;           
            if (++count == IMAGE_URL.length) initPlaneWithBitmap();
        }

        //ランド画面土台3DPlaneオブジェクトにBitmap画像を貼りつけ
        private function initPlaneWithBitmap():void { 
            //ランド画面土台Planeオブジェクト設定
            for (var xIndex:int = 0; xIndex < LAND_MAP_DATA.length; xIndex++) {
                var horizontalMapData:Array = LAND_MAP_DATA[xIndex];
                for (var zIndex:int = 0; zIndex < horizontalMapData.length; zIndex++) {
                    var mapData:int = horizontalMapData[zIndex]
                    var bmp:Bitmap = images[mapData];
                    var material:BitmapMaterial = new BitmapMaterial(bmp.bitmapData, true);

                    planeObj = new Plane(material, 300, 300, 1, 1);
                    planeObj.name = "planeX" + xIndex + "_Z" + zIndex;
                    planeObj.x = (300 * xIndex);
                    planeObj.z = 300 * zIndex;
                    planeObj.rotationX += 90;
                    planeObj.name = "green";
                    //Planeオブジェクトに名前を割り振り
                    //7行3列の場合の名前例:"planeX7_Z3"
                    tempPlaneName = "planeX" + xIndex + "_Z" + zIndex;
                    Objs[tempPlaneName] = planeObj;
                }
            }
        }
//■■■イベント処理スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //イベント処理
        private function onEnterFrame(e:Event):void{
            if (screenId == TITLE_SCREEN_ID) {
                title3DText.rotationX -= 10;
                crystalBoxCube.rotationX = viewport.mouseY; //飾り用クリスタルの座標をマウス座標から設定
                crystalBoxCube.rotationY = viewport.mouseX; //飾り用クリスタルの座標をマウス座標から設定
                setCameraForTitleScrn(); //カメラ視点設定
                renderer.renderScene(scene, camera, viewport);
            } else if (screenId == LAND_SCREEN_ID) {
                calcNomanekoPos(); //操作用3D「のまねこ」座標を設定
                setCameraForLandScrn(); //カメラ視点設定
                renderer.renderScene(scene, camera, viewport);
            } else if (screenId == RED_LAND_SCREEN_ID) {
                calcNomanekoPos(); //操作用3D「のまねこ」座標を設定
                setCameraForLandScrn(); //カメラ視点設定
                renderer.renderScene(scene, camera, viewport);
            } else if (screenId == BLUE_LAND_SCREEN_ID) {
                calcNomanekoPos(); //操作用3D「のまねこ」座標を設定
                setCameraForLandScrn(); //カメラ視点設定
                renderer.renderScene(scene, camera, viewport);
            }
        }
        //ランド画面の操作用3D「のまねこ」座標を設定
        private function calcNomanekoPos():void{
            if (forwardState == true) {//前進
                daeNomaneko.moveForward(-30);
            } else if (backwardState == true) {//後退
                daeNomaneko.moveForward(30);
            } else if (leftRotState == true) {//左回転
                daeNomaneko.rotationY -= 10;
            } else if (rightRotState == true) {//右回転
                daeNomaneko.rotationY += 10;
            } else if (bDashState == true) {//Bダッシュ
                daeNomaneko.moveForward(-60);
            }
        }
        //タイトル画面用カメラ視点設定
        private function setCameraForTitleScrn():void{
            var cameraDist : int = -1000; //デフォルトの距離、ステータスは1
            camera.x=cameraDist*Math.cos((90-crystalBoxCube.rotationY)*Math.PI/180)+crystalBoxCube.x;  
            camera.z=cameraDist*Math.sin((90-crystalBoxCube.rotationY)*Math.PI/180)+crystalBoxCube.z;
            camera.target.x=crystalBoxCube.x;
            camera.target.y=crystalBoxCube.y;
            camera.target.z=crystalBoxCube.z;
            cameraPitch = 90; //カメラのX軸回転の値 
            cameraYaw = 270; //カメラのY軸回転の値
            camera.orbit(cameraPitch, cameraYaw, true, crystalBoxCube);
        }
        //ランド画面用カメラ視点設定
        private function setCameraForLandScrn():void{
            var cameraDist : int = -1000; //デフォルトの距離、ステータスは1
            if (cameraDistStat == 0) {
                cameraDist = -500;
            } else if (cameraDistStat == 1) {
                cameraDist = -1000;
            } else if (cameraDistStat == 2) {
                cameraDist = -1500;
            }
            camera.x=cameraDist*Math.cos((90-daeNomaneko.rotationY)*Math.PI/180)+daeNomaneko.x;  
            camera.z=cameraDist*Math.sin((90-daeNomaneko.rotationY)*Math.PI/180)+daeNomaneko.z;
            camera.target.x=daeNomaneko.x;
            camera.target.y=daeNomaneko.y;
            camera.target.z=daeNomaneko.z;
            camera.orbit(cameraPitch, cameraYaw, true, daeNomaneko);
        }

        private function onKeyDown(event:KeyboardEvent):void{
            if (screenId == TITLE_SCREEN_ID) {return;} //タイトル画面の場合は何も処理を行わない
            //Aボタン 操作オブジェクトを中心にカメラを左回り回転(Y軸回転)
            //4度づつ左回り回転  
            if (event.keyCode == 65) {
                cameraYaw = cameraYaw - 4;
                if(cameraYaw < 0) {cameraYaw = 360;}
            //Dボタン 操作オブジェクトを中心にカメラを右回り回転(Y軸回転)    
            //4度づつ右回り回転
            } else if (event.keyCode == 68) {
                cameraYaw = cameraYaw + 4;
                if(cameraYaw > 360) {cameraYaw = 0;}
            //Wボタン 操作オブジェクトを中心にカメラを上下移動(X軸回転)
            } else if (event.keyCode == 87) {        
                if(cameraPitch > 50) {cameraPitch = cameraPitch - 10;}
            //Sボタン 操作オブジェクトを中心にカメラを上下移動(X軸回転)
            } else if (event.keyCode == 83) {           
                if(cameraPitch < 100) {cameraPitch = cameraPitch + 10;}
            //Fボタン カメラと操作オブジェクトの距離を変更
            } else if (event.keyCode == 70) {
                cameraDistStat -= 1;
                if(cameraDistStat < 0) {cameraDistStat = 2;}
            //Rボタン カメラの初期化 カメラの距離と高さを初期状態に設定&操作矢印の真後ろに設定
            } else if (event.keyCode == 82) {
                cameraDistStat = 1;
                cameraPitch = 80; //カメラのX軸回転の値 
                cameraYaw = 90 - daeNomaneko.rotationY;
            }
        }
//■■■イベント処理エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■3D空間を初期化エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■ボタン処理スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■画面遷移ボタン処理スタート■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■タイトル画面←→ランド画面 遷移処理スタート■■■■■■■■■■■■■■■■■■
        //タイトル画面からランド画面に遷移
        private function goLandScrnBtnDown(evt:MouseEvent):void {
            hideTitleScrnBtn(); //タイトル画面ボタンを非表示
            dispLandScrnBtn(); //ランド画面ボタンを表示

            //3Dオブジェクト表示・非表示処理
            removeTitleScrn3DObjs();
            addLandScrn3DObjs();

            addLandScrnTxt(); //ランド画面情報テキストを設定

            //スクリーンID設定
            screenId = LAND_SCREEN_ID;

            //ランド画面用のカメラ初期設定
            cameraDistStat = 1;
            cameraPitch = 80;
        }
        //ランド画面からタイトル画面に遷移
        private function goTitleScrnBtnDown(evt:MouseEvent):void {
            hideLandScrnBtn(); //ランド画面ボタンを非表示
            dispTitleScrnBtn(); //タイトル画面ボタンを表示

            //3Dオブジェクト表示・非表示処理
            removeLandScrn3DObjs();
            addTitleScrn3DObjs();

            removeLandScrnTxt(); //ランド画面情報テキストを設定解除

            //スクリーンID設定
            screenId = TITLE_SCREEN_ID;
        }
        //タイトル画面に3Dオブジェクトをセット
        private function addTitleScrn3DObjs():void {
            scene.addChild(Objs["title3DText"], "title3DText");
            scene.addChild(Objs["crystalBoxCube"], "crystalBoxCube");
        }
        //タイトル画面の3Dオブジェクトをリリース
        private function removeTitleScrn3DObjs():void {
            scene.removeChildByName("title3DText");
            scene.removeChildByName("crystalBoxCube");
        }
        //ランド画面に3Dオブジェクトをセット
        private function addLandScrn3DObjs():void {
            var horizontalLength:int = LAND_MAP_DATA[0].length; //ランドマップ配列内の子配列1つ分の長さ
            for (var xIndex:int = 0; xIndex < LAND_MAP_DATA.length; xIndex++){
                for (var zIndex:int = 0; zIndex < horizontalLength; zIndex++){
                    //3DPlaneオブジェクトを取り出す
                    //Planeオブジェクト取り出し用の名前を作成  7行3列の場合の名前例:"planeX7_Z3" 
                    tempPlaneName = "planeX" + xIndex + "_Z" + zIndex;
                    scene.addChild(Objs[tempPlaneName], tempPlaneName);
                }
            }
            //操作「のまねこ」を初期化
            daeNomaneko = Objs["daeNomaneko"];
            daeNomaneko.y = 180;
            daeNomaneko.x = 1350; 
            daeNomaneko.z = 1350;
            daeNomaneko.rotationY = 180;
            scene.addChild(daeNomaneko, "daeNomaneko");
        }
        //ランド画面から3Dオブジェクトをリリース
        private function removeLandScrn3DObjs():void {
            //ランドマップ配列分の処理
            var horizontalLength:int = LAND_MAP_DATA[0].length; //ランドマップ配列内の子配列1つ分の長さ
            for (var xIndex:int = 0; xIndex < LAND_MAP_DATA.length; xIndex++){
                //ランドマップ配列内の子配列1行分のデータを処理
                for (var zIndex:int = 0; zIndex < horizontalLength; zIndex++){
                    //3DPlaneオブジェクトをリリース
                    tempPlaneName = "planeX" + xIndex + "_Z" + zIndex;
                    scene.removeChildByName(tempPlaneName);
                }
            }
            scene.removeChildByName("daeNomaneko");
        }
//■■■タイトル画面←→ランド画面 遷移処理エンド■■■■■■■■■■■■■■■■■■
//■■■ランド画面←→「バトる」状態 遷移処理スタート■■■■■■■■■■■■■■■■
        private function battleBtnDown(evt:MouseEvent):void {
            if (!battleStatus) {
                //敵オブジェクト設定
                planeEnemy = new Enemy(daeNomaneko.rotationY + 180, daeNomaneko.x, daeNomaneko.z);
                scene.addChild(planeEnemy.body, "planeEnemyBody");

                //「バトる」用ふきだし設定
                var fukidashiTxt : String;
                fukidashiTxt = "「白い紙切れ」が現れた!"
                editLabel("battleFukidashiText",fukidashiTxt);
                addChild(battleFukidashiContena);
                battleFukidashiContena.addChild(battleFukidashi);
                battleFukidashiContena.addChild(onBattleFukidashi);
                battleFukidashiContena.addChild(txtField["battleFukidashiText"]);
                //「バトる」状態コマンド用ふきだし設定
                addChild(battleComandFukidashiContena);
                var battleStatusTxt : String;
                battleStatusTxt = "ライフ: " + statusKeeper.getLifePoint() + "\n" 
                                + "攻撃力: " + statusKeeper.getAttackPoint() + "\n"
                                + "防御力: " + statusKeeper.getDiffencePoint() + "\n"
                                + "カーネ: " + statusKeeper.getKanePoint() + "\n";
                editLabel("battleStatus",battleStatusTxt);
                battleComandFukidashiContena.addChild(txtField["battleStatus"]);
                battleStatus = true;
            }
        }
//■■■ランド画面←→「バトる」状態 遷移処理エンド■■■■■■■■■■■■■■■■■
//■■■画面遷移ボタン処理エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■ランド画面ボタン処理スタート(遷移ボタン以外)■■■■■■■■■■■■■■■■■■■■■■■■
        //前進ボタン押下時処理設定
        private function onForwardBtnDown(evt:MouseEvent):void {
            forwardState = true;
        }
        //前進ボタンリリース時処理設定1
        private function onForwardBtnUp(evt:MouseEvent):void {
            forwardState = false;
        }
        //前進ボタンリリース時処理設定2
        private function onForwardBtnOut(evt:MouseEvent):void {
            forwardState = false;
        }
        //後退ボタン押下時処理設定
        private function onBackwardBtnDown(evt:MouseEvent):void {
            backwardState = true;
        }
        //後退ボタンリリース時処理設定1
        private function onBackwardBtnUp(evt:MouseEvent):void {
            backwardState = false;
        }
        //後退ボタンリリース時処理設定2
        private function onBackwardBtnOut(evt:MouseEvent):void {
            backwardState = false;
        }
        //左回転ボタン押下時処理設定
        private function onLeftRotBtnDown(evt:MouseEvent):void {
            leftRotState = true;
        }
        //左回転ボタンリリース時処理設定1
        private function onLeftRotBtnUp(evt:MouseEvent):void {
            leftRotState = false;
        }
        //左回転ボタンリリース時処理設定2
        private function onLeftRotBtnOut(evt:MouseEvent):void {
            leftRotState = false;
        }
        //右回転ボタン押下時処理設定
        private function onRightRotBtnDown(evt:MouseEvent):void {
            rightRotState = true;
        }
        //右回転ボタンリリース時処理設定1
        private function onRightRotBtnUp(evt:MouseEvent):void {
            rightRotState = false;
        }
        //右回転ボタンリリース時処理設定2
        private function onRightRotBtnOut(evt:MouseEvent):void {
            rightRotState = false;
        }
        //Bダッシュボタン押下時処理設定
        private function onBDashBtnDown(evt:MouseEvent):void {
            bDashState = true;
        }
        //Bダッシュボタンリリース時処理設定1
        private function onBDashBtnUp(evt:MouseEvent):void {
            bDashState = false;
        }
        //Bダッシュボタンリリース時処理設定2
        private function onBDashBtnOut(evt:MouseEvent):void {
            bDashState = false;
        }
//■■■ランド画面ボタン処理エンド(遷移ボタン以外)■■■■■■■■■■■■■■■■■■■■■■■■■
//■■■ボタン処理エンド■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
    }
}
//■■■操作キャラステータス保持クラス■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
//操作キャラのステータス保持を行うクラスを作成
class StatusKeeper
{
    private const MAX_LIFE_POINT : int = 100; 
    private var lifePoint : int = 100; 
    private var attackPoint : int = 30;
    private var diffencePoint : int = 30;
    private var kanePoint : int = 100; 
    public function StatusKeeper(lifePoint:int=100,attackPoint:int=30,diffencePoint:int=30,kanePoint:int=100):void {
        this.lifePoint = lifePoint; 
        this.attackPoint = attackPoint;
        this.diffencePoint = diffencePoint;
        this.kanePoint = kanePoint;
    }
    
    //ライフポイント用メソッド
    public function fillLifePoint():void {
        this.lifePoint = MAX_LIFE_POINT;
    }
    public function addLifePoint(addPoint:int):void {
        this.lifePoint += addPoint;
    }   
    public function minusLifePoint(minusPoint:int):void {
        this.lifePoint -= minusPoint;
    }
    public function getLifePoint():int {
        return this.lifePoint;
    }

    //攻撃力・防御力用getteメソッド
    public function getAttackPoint():int {
        return this.attackPoint;
    }
    public function getDiffencePoint():int {
        return this.diffencePoint;
    }

    //カーネポイント用メソッド
    public function addKanePoint(addPoint:int):void {
        this.kanePoint += addPoint;
        if (this.kanePoint > 9999) {this.kanePoint = 9999}
    }
    public function minusKanePoint(minusPoint:int):void {
        this.kanePoint -= minusPoint;
        if (this.kanePoint < 0) {this.kanePoint = 0}
    }
    public function getKanePoint():int {
        return this.kanePoint;
    }
}  
//■■■Plane敵クラス■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
import flash.display.Sprite;
import org.papervision3d.objects.primitives.Plane;  
import org.papervision3d.materials.ColorMaterial;
//敵Planeを作成
class Enemy
{
    public var body : Plane; //敵本体Plane
    private var speedX : int = 0; //敵PlaneX方向速度
    private var speedZ : int = 0; //敵PlaneZ方向速度 
    private const SPEED : int = 30; //敵Planeの速度
    private var distX : int = 0; //敵PlaneX方向距離
    private var distZ : int = 0; //敵PlaneZ方向距離 
    private const DIST : int = 700; //ユーザー操作キャラからの敵Planeの距離
    private var colorMaterial : ColorMaterial;
    private var lifePoint : int = 20; 
    private var attackPoint : int = 35;
    private var diffencePoint : int = 20;
    private var kanePoint : int = 50; 
    public function Enemy(figRotation:int=0,figX:int=0,figZ:int=0):void {
        colorMaterial = new ColorMaterial(0xEEEEEE);
        colorMaterial.doubleSided = true;
        body = new Plane(colorMaterial, 300, 300, 1, 1);
        body.x = figX; // 弾丸のX軸の初期値を設定
        body.y = 250; // 弾丸のY軸の初期値を設定
        body.z = figZ; // 弾丸のZ軸の初期値を設定
        body.rotationY = figRotation; //角度を操作キャラと同じにする

        //弾丸速度をX軸の速度成分に分解
        speedX = SPEED * Math.cos((90-figRotation)*Math.PI/180);
        //弾丸速度をZ軸の速度成分に分解
        speedZ = SPEED * Math.sin((90-figRotation)*Math.PI/180);

        //敵Planeの距離をX軸の距離成分に分解
        distX = DIST * Math.cos((90-figRotation)*Math.PI/180);
        //敵Planeの距離をZ軸の距離成分に分解
        distZ = DIST * Math.sin((90-figRotation)*Math.PI/180);

        body.x += distX;
        body.z += distZ;
    }
    
    //X軸とZ軸の速度成分を加算して、X軸とZ軸の移動先の座標を計算
    public function move():void {
        body.x += speedX;
        body.z += speedZ;
    }
    //ライフポイントを返す
    public function getLifePoint():int {
        return lifePoint;
    }
    //ライフポイント減算メソッド
    public function minusLifePoint(minusPoint:int):void {
        lifePoint -= minusPoint;
    }

    //攻撃力を返す 
    public function getAttackPoint():int {
        return attackPoint;
    }
    //防御力を返す
    public function getDiffencePoint():int {
        return diffencePoint;
    }
    //カーネポイントを返す
    public function getKanePoint():int {
        return kanePoint;
    }
}                                         
//■■■カスタムボタンクラス■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
import flash.display.*;
import flash.system.*;
import flash.text.*;
//カスタムボタン
class CustomButton extends SimpleButton {
        private var btnName : String = "";//ボタン名
        private var btnNo : int = 0;//ボタン番号
        //コンストラクタ    
        public function CustomButton(label:String="",no:int=0) {
            btnName = label;
            btnNo = no;
            //状態
            upState = makeSprite(label,0x999999);
            overState = upState;
            downState = makeSprite(label,0x0000FF);
            hitTestState = upState;
        }
        public function getBtnName():String {
            return btnName;
        }
        public function getBtnNo():int {
            return btnNo;
        }
        //ボタン用スプライト作成
        private function makeSprite(text:String,color:uint):Sprite{
            //ボタン用ラベル作成
            var label : TextField = new TextField();
            label.text = text;
            label.autoSize = TextFieldAutoSize.CENTER;
            label.selectable = false;
            //ボタン用スプライト作成
            var sp:Sprite = new Sprite();
            sp.graphics.beginFill(color);
            sp.graphics.drawRoundRect(0, 0, 100, 20, 15);
            sp.graphics.endFill();
            sp.alpha = 0.8;            
            sp.addChild(label);
            //ラベル用フォーマット設定
            var format:TextFormat=new TextFormat();
            format.font = "Courier New";
            format.bold = true;
            format.size = 13;
            label.setTextFormat(format);
            return sp;
        }
}
//■■■ユーティリティクラス■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.AntiAliasType;
import flash.text.TextFormatAlign;
//ユーティリティ
class Util
{
    //テキストフィールドの生成
    public static function makeTxtField(posX:int,posY:int,width:int,height:int):TextField {
        var label:TextField=new TextField();
        label.selectable=false;
        label.x       =posX;
        label.y       =posY;
        label.width   =width;
        label.height  =height;
        label.antiAliasType=AntiAliasType.ADVANCED;
        return label;
    }
    //テキストフォーマットの生成
    public static function makeTextFormat(size:uint,color:uint,
        align:String=TextFormatAlign.LEFT):TextFormat {
        var format:TextFormat=new TextFormat();
        format.font ="Courier New"; // 等幅フォント
        format.size =size;  
        format.color=color;
        format.bold =true;
        format.align=align;
        return format;
    }
}

Forked