자바스크립트로 만드는 객체지향 게임 - 폭탄 테이블 클래스

 폭탄 테이블은 폭탄들이 배열되어 있는 곳이다. 불꽃이 폭탄 테이블을 지날 때 불꽃과 인접한 폭탄에 폭발 메시지를 전달하며, 시간이 지났을 때 폭탄을 1줄 추가 생성하는 역할을 한다.

클래스는 아래와 같다.

/* 폭탄들이배열되는폭탄테이블클래스*/

$class({

        CBombTable : {

               arrBombTable : null,

               size : {width : 0,height : 0},

               element : null,

               parentElement : null,

              

               /* 이벤트맵*/

               EVENT_MAP : {

                       explosion : 'onExplosion',  // 폭탄폭발완료

                       dropComplete : 'onDropComplete', // 폭탄떨어짐완료

                       fireBomb : 'onFireBomb' // 폭탄폭발시작

               },

        // 폭탄테이블div를생성한다.

               init : function(width,height,parent) {

                       this.size.width=width;

                       this.size.height = height;

                       this.arrBombTable=new Array(width*height);

                       parent = parent || document.body;

                       this.parentElement=parent;

                       parent.innerHTML='<div id="_bomblink_bombtable" style="position:relative;float:left;border:1px solid #9090f0;width:'+width*29+'px;height:'+height*29+'px;">'+

                       '<div style="position:relative;top:0px;left:0px;border-bottom:1px solid red;z-index:2;width:'+width*29+'px;height:29px;" ></div>'+

                       '<div style="position:relative;top:'+(height-2)*29+'px;left:0px;border-top:1px solid blue;z-index:2;width:'+width*29+'px;height:29px;" ></div>'+

                       '</div>';

                       this.element=$('_bomblink_bombtable');

               },

               // 폭탄의심지방향을랜덤으로하여현재레벨에맞게폭탄을생성한다.

               createLevel : function(level) {

                       if (level<5) level=3;

                       var height = Math.min(level,10);

                       for(var y=this.size.height-(level+1);y<this.size.height;y++) {

                              for(var x=0;x<this.size.width;x++) {

                                      var bomb=new CBomb(this,y<this.size.height-1);

                                      this.arrBombTable[x+y*this.size.width]=bomb;

                                      bomb.setPos(x,y);

                                      bomb.setDirection((Math.round(Math.random()*10))%4);

                              }

                       }

               },

               // 지정위치의폭탄을반환

               getBomb : function(x,y) {

                   return this.arrBombTable[x+y*this.size.width];

               },

               // 지정폭탄의인접폭탄을배열로반환

               getSiblingBomb : function(x,y) {

                   return [(x<this.size.width-1)?this.arrBombTable[x+1+y*this.size.width]:null,  // right

                           (y<this.size.height-2)?this.arrBombTable[x+(y+1)*this.size.width]:null,  // bottom

                           (x>0)?this.arrBombTable[x-1+y*this.size.width]:null,  // left

                           (y>0)?this.arrBombTable[x+(y-1)*this.size.width]:null]; // top

               },

               // 불꽃이지나갈때마다체크하여해당위치의폭탄에폭발메시지전달

               fire : function(y,x) {   // x : 0 - left, 1 - right

                   if (y==this.size.height-1) return;

                   if (x==0) {  // left

                        var bomb=this.getBomb(0,y);

                        if (bomb) $event.fire(bomb,'fire',-1,y);

                   } else { // right

                        var bomb=this.getBomb(this.size.width-1,y);

                        if (bomb) $event.fire(bomb,'fire',this.size.width,y);

                   }

                   bomb=null;

               },

               // 폭탄을1줄추가한다

               tableUp : function() {

                   for(var y=0;y<this.size.height-1;y++) {

                        for(var x=0;x<this.size.width;x++) {

                            var index=x+y*this.size.width;

                            var bomb = this.arrBombTable[index]=this.arrBombTable[index+this.size.width];

                            if (bomb) {

                                bomb.setPos(x,y);

                                if (y==this.size.height-2) bomb.enable();

                            }

                        }

                   }

                   for(var x=0;x<this.size.width;x++) {

                              var bomb=new CBomb(this,false);

                              var y=this.size.height-1;

                              this.arrBombTable[x+y*this.size.width]=bomb;

                              bomb.setPos(x,y);

                              bomb.setDirection((Math.round(Math.random()*10))%4);

                   }

                   for(var x=0;x<this.size.width;x++) {

                        if (this.arrBombTable[x]) {

                            alert('Game Over');

                        }

                   }

               },

               // 폭탄폭발시작

               onFireBomb : function (evt,src,type,x,y) {

                    this.arrBombTable[x+y*this.size.width]=null;

               },

               // 폭탄폭발완료- 사라진폭탄의위에있는폭탄에떨어지는메시지를전달한다

               onExplosion : function(evt,src,type,x,y) {

                    var index=x+y*this.size.width;

                

                    for(;index>0;index-=this.size.width) {

                        var bomb=this.arrBombTable[index];

                        if (bomb) {

                                this.arrBombTable[index]=null;

                            $event.fire(bomb,'drop');

                        }

                    }

               },

               // 폭탄떨어짐완료- 만약해당폭탄의아래가비었으면계속떨어지게한다

               onDropComplete : function(evt,src,type,bomb,x,y) {

                    var index=x+(y+1)*this.size.width;

                    this.arrBombTable[index]=bomb;

                    if (this.arrBombTable[index+this.size.width]==null) {

                        this.arrBombTable[index]=null;

                     $event.fire(bomb,'drop');

                    }

                    bomb=null;

               },

               // 현재폭탄테이블의상태를디버그창에표시

               dumpTable : function() {

                        $debug.outputLn('=============================================');

                    for(var y=0;y<this.size.height;y++) {

                            var msg='';

                            for(var x=0;x<this.size.width;x++) {

                                msg+=(this.arrBombTable[x+y*this.size.width])?'o':'x';

                            }

                            $debug.outputLn(msg);

                        }

               }

        }

});

 



by 레이쥬 | 2007/12/21 17:19 | SWAF - 응용 | 트랙백 | 덧글(0)

트랙백 주소 : http://reizes.egloos.com/tb/1686174
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶