enchant();
window.onload = function(){
    // ----- ゲーム内共通利用変数 ---------
    var game = new Core(320, 320);
    game.preload('chara7.png', 'chara6.png', 'icon0.png','map0.png','effect0.png'); // preload image
    game.fps = 15;
    var INFO_LIFE = 3; // ライフ
    var INFO_SCORE = 0; // スコア
    var INFO_LEVEL = 1;
    var Info = { // 良い名前募集中
        life:  INFO_LIFE,
        score: INFO_SCORE,
        level: INFO_LEVEL,
        init: function(){
            this.life =  INFO_LIFE;
            this.score =  INFO_SCORE;
            this.level = INFO_LEVEL;
        },
        gamecontinue: function(){
            this.life =INFO_LIFE;
        },
        minuslife: function(){
            this.life --;
        },
        plusscore: function(){
            this.score ++;
        },
        pluslevel: function(){
            this.level ++;
        },
    };
    game.onload = function(){

        // ******* キャラ *******
        // プレイキャラクター
        var Player = enchant.Class.create(enchant.Sprite, {
            initialize: function(){
                enchant.Sprite.call(this, 32, 32);
                this.image = game.assets['chara7.png'];
                this.x = 288;// add:右側に表示。320-32の位置
                this.frame = 10; // キャラクター
                this.attackframe = -1; // 攻撃状態確認用。0未満がデフォルト
                this.renda = 0;
            },
            attackstart: function(ly){
                if(Math.abs(this.y - ly ) < 10 ){ // 移動距離が小さいなら攻撃状態に移れる
                    this.frame = 16; // 斬っている瞬間の画像
                    this.attackframe = 0; // 攻撃状態確認0回目
                    this.renda++; // 連打数
                } else {
                    this.renda = 0; // 動いたら連打数無効
                }
                this.moving(ly);
            },
            attackend: function(){
                if( this.attackframe < 0 ) return; // 攻撃状態では無い場合は後続処理をしない
                if( this.attackframe == 3 ) {
                    this.frame = 10; // 普段の画像
                    this.attackframe = -1;
                } else {
                    this.attackframe ++;
                }
            },
            moving: function(ly){
                this.y = ly;
            },
            isattacking: function(){
                if(this.attackframe === -1) return false;
                return true;
            },
            isenoughrenda: function(){
                if (this.renda > 10 ){
                    this.renda = 0;
                    return true;
                }
                return false;
            },
            onenterframe:function(){ // フレーム単位でのプレイキャラクター監視
                this.attackend(); // attackstartしてから終わるまでをみはる
            },
            endgraphic: function(){ // ゲームオーバー時の表示
                this.frame = 29;
                this.rotation = -90;
                this.x = 140;
                this.y = 160;
                this.scale(3,3);
            }
        });

        // 敵キャラ
        var Enemy = enchant.Class.create(enchant.Sprite, {
            initialize: function(){
                enchant.Sprite.call(this, 32, 32);
                this.image = game.assets['chara6.png']; // set image
                this.moveTo(0, Math.floor(Math.random() * 295)); // 320だと実質見えない
                this.scaleX = -1;
                this.speed = 160 - Math.min(Info.level , 159 ); // 移動速度. レベル依存
                this.tl.moveBy(360, 0, this.speed );
                this.frame = 9;                   // 敵キャラ画像
            }
        });

        // 爆発
        var Crash = enchant.Class.create(enchant.Sprite, {
            initialize: function(scene,ex,ey){
                enchant.Sprite.call(this, 32, 32);
                this.image = game.assets['effect0.png']; // set image
                this.frame = 0;
                this.scene = scene;
                this.x = ex;
                this.y = ey;
                this.scaleX = 2;
                this.scaleY = 2;
                this.scene.addChild(this);
            },
            onenterframe:function(){ // 爆発動作と終了処理
                this.frame++;
                if(this.frame > 4){
                    this.scene.removeChild(this); // いけるかしら
                }
                
            },
        });

        // ******* シーン *******
        // メインシーン
        var MainScene = function(){
            var scene = new Scene();
            
            // 背景
            var bgmap = new Map(16,16);
            bgmap.image = game.assets['map0.png'];
            bgmap.backgroundColor="#d3ffdd";
            var bgbase = [];
            for(var i = 0; i < 320/16 ; i++ ){
                bgbase[i]=[];
                var frm = ( i === 0 ? 19 : 0); // 画面一番上の方だけ画像変える
                for(var j = 0 ; j < 320/16; j++ ) bgbase[i][j]=frm;
            }
            bgmap.opacity = 0.6; // ちょっとだけ透明処理
            bgmap.loadData(bgbase);
            scene.addChild(bgmap);

            // プレイキャラクター
            var player = new Player();
            scene.addChild(player);
            
            // ラベル
            var label = new Label();
            label.x = 5;
            label.y = 288;
            label.color = "black";
            label.opacity = 0.6;
            label.settext = function(addtxt){
                this.text = 'Life : ' + Info.life
                    + '
' + 'Score : ' + Info.score + (addtxt === undefined ? '' : addtxt); }; label.settext(); // 初期テキスト設定 scene.addChild(label); // -- シーン内動作(キャラクター動作) -- // - タッチスタート scene.addEventListener(Event.TOUCH_START, function(evt) { player.attackstart(evt.localY); }); // - タッチムーブ scene.addEventListener(Event.TOUCH_MOVE, function(evt){ player.moving(evt.localY); }); // 一定時間ごと // - 敵の作成 var enemies = []; scene.tl.then(function() { enemies.push(new Enemy()); scene.addChild(enemies[enemies.length -1 ]); Info.pluslevel(); // 時間とともにレベルもあげる }).delay(30).loop(); // フレーム監視 - 敵を作ったり消したりスコアいじったり scene.addEventListener(Event.ENTER_FRAME, function(evt){ // 連打数が十分な場合、全ての敵を削除 var chkflg = player.isenoughrenda(); // 敵とプレイキャラクターが当たっているかどうかを1件ずつ確かめる for(var i = 0; i < enemies.length; i ++){ if(chkflg || player.within(enemies[i]) && player.isattacking()) { if(chkflg){ new Crash(scene,enemies[i].x , enemies[i].y); } scene.removeChild(enemies[i]); enemies.splice(i--,1); Info.plusscore(); // スコアカウントアップ label.settext(); // 表示更新 } else if(enemies[i].x >= 315){ scene.removeChild(enemies[i]); // 画面から削除 enemies.splice(i--,1); Info.minuslife(); // ライフカウントダウン label.settext(); // 表示更新 } } }); // シーン切替処理 scene.addEventListener(Event.ENTER_FRAME, function(evt){ // メイン -> ゲームオーバー if(Info.life <= 0){ game.replaceScene(new EndScene()); } }); return scene; }; // ゲームオーバーシーン var EndScene = function(){ var scene = new Scene(); scene.backgroundColor = '#666666'; // シーン内フラグ var buttons = { iscontinue:false, isnewgame:false, }; // プレイキャラクター表示 var player = new Player(); player.endgraphic(); scene.addChild(player); // スコア表示追加 var label = new Label('Score : ' + Info.score + '
ヤラレタ...'); label.color='#cccccc'; label.y = 100; label.textAlign = 'center'; scene.addChild(label); // ボタン(コンティニュー) var btnContinue = new Label('こんてぬー'); btnContinue.color='#eeeeee'; btnContinue.y = 210; btnContinue.x = -50; btnContinue.textAlign = 'center'; btnContinue.addEventListener(Event.TOUCH_START, function(evt) { buttons.iscontinue = true; }); scene.addChild(btnContinue); // ボタン(ニューゲーム) var btnNewgame = new Label('ぬーげーむ'); btnNewgame.color='#eeeeee'; btnNewgame.y = 210; btnNewgame.x = 50; btnNewgame.textAlign = 'center'; btnNewgame.addEventListener(Event.TOUCH_START, function(evt) { buttons.isnewgame = true; }); scene.addChild(btnNewgame); // シーン切替処理 scene.addEventListener(Event.ENTER_FRAME, function(evt){ if(buttons.isnewgame === true ){ Info.init(); game.replaceScene(new MainScene()); } else if(buttons.iscontinue === true ){ Info.gamecontinue(); game.replaceScene(new MainScene()); } }); return scene; }; // ******** 最初のシーン設定 ********** game.replaceScene(new MainScene()); }; game.start(); };