home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / baku100.zip / baku100 / Kernel / Map / Map.tonyu next >
Text File  |  2008-04-18  |  10KB  |  455 lines

  1. extends Sprite;
  2.  
  3. function set(xx,yy,p,f) {
  4.   var rx,ry,tx,ty;
  5.   if (!enabled) return; 
  6.   rx=amod(xx ,width);
  7.   ry=amod(yy ,height);
  8.   //print (xx+yy*width);
  9.   data.set(rx+ry*width,p);
  10.   
  11.   /*if(0){  print (strcat("xx=",xx));
  12.     print (strcat("yy=",yy));
  13.     print (strcat("mpx=",mpx));
  14.     print (strcat("mpy=",mpy));
  15.     print (strcat("scx=",scx));
  16.     print (strcat("scy=",scy));
  17.     print (strcat("scW=",scWidth));
  18.     print (strcat("scH=",scHeight));
  19.     print (strcat("W=",width));
  20.     print (strcat("H=",height));
  21.     
  22.     print (amod( scx+ (xx-mpx) ,scWidth));
  23.     print (amod( scy+ (yy-mpy),scHeight));
  24.   }*/
  25.   tx=xx-mpx;
  26.   if (tx<0 || tx>=scWidth) return;
  27.   tx=amod( scx+ tx,scWidth);
  28.   ty=yy-mpy;
  29.   if (ty<0 || ty>=scHeight) return;
  30.   ty=amod( scy+ ty ,scHeight);
  31.   panel.fillRect(tx*pwidth,ty*pheight,
  32.   tx*pwidth+pwidth,ty*pheight+pheight,   0);
  33.   panel.drawSprite(tx*pwidth+pwidth/2 ,ty*pheight+pheight/2  ,p );
  34.   
  35.   //  data=p;
  36. }
  37.  
  38. function get(xx,yy) {
  39.   if (!enabled) return; 
  40.   xx=amod(xx , width);
  41.   yy=amod(yy , height);
  42.   return data.get(xx+yy*width);
  43. }
  44.  
  45. function getAt(xx,yy) {
  46.   //print(xx);
  47.   //print(yy);
  48.   //print(floor(yy/pwidth));
  49.   //print("+++");s
  50.   return get(floor(xx/pwidth),floor(yy/pheight));
  51. }
  52.  
  53. function init(w,h,pw,ph) { 
  54.   var xx,yy,ndata; 
  55.   x=0;y=0;    
  56.   pwidth=pw;pheight=ph;
  57.   enabled=0; if (w*h==0) return;
  58.   enabled=1;
  59.   if (data is Array) {
  60.     ndata=new Array();  
  61.     yy=0;
  62.     while (yy<h) {
  63.       xx=0;
  64.       while (xx<w) {
  65.         ndata.add(get(xx,yy));
  66.         xx+=1;
  67.       } 
  68.       yy+=1; 
  69.     }
  70.     data=ndata;
  71.   } else {
  72.     data=new Array();
  73.     xx=0;
  74.     while (xx<w) {
  75.       yy=0;
  76.       while (yy<h) {
  77.         data.add(-1); 
  78.         yy=yy+1; 
  79.       } 
  80.       xx=xx+1;
  81.     }
  82.   }
  83.   width=w;height=h;
  84.   prevSW=0;prevSH=0;
  85.   //scWidth=floor($screenWidth/pwidth)+2;
  86.   //scHeight=floor($screenHeight/pheight)+2;
  87.   //panel=new Panel(scWidth*pwidth,scHeight*pheight);
  88. }
  89.  
  90. function draw() {
  91.   if (!enabled) return;
  92.   //fillRect(0,0,100,100,$clpink);
  93.   setAdjust(0,0);
  94.   panel.zOrder=200;
  95.   panel.draw(0,0,$screenWidth,$screenHeight,scfx,scfy); 
  96.   //  drawMapSprite(scfx,scfy,scWidth*pwidth,scHeight*pheight);
  97. }
  98.  
  99. function scrollLeft(){
  100.   var c,d_mpx,d_mpy,d_scx,d_scy;
  101.   d_mpx=amod (mpx-1 , width);
  102.   d_mpy=amod (mpy   , height);
  103.   
  104.   d_scx=amod (scx-1 , scWidth);  
  105.   d_scy=amod (scy , scHeight);  
  106.   
  107.   c=scHeight;
  108.   
  109.   while (c>0) {
  110.     panel.fillRect(d_scx*pwidth,d_scy*pheight ,d_scx*pwidth+pwidth,d_scy*pheight+pheight,   0);
  111.     panel.drawSprite(d_scx*pwidth+pwidth/2,d_scy*pheight+pheight/2 ,
  112.     get(d_mpx,d_mpy) );
  113.     d_mpy=amod (d_mpy+1 , height);
  114.     d_scy=amod (d_scy+1 , scHeight);
  115.     c+=-1;
  116.   }
  117.   mpx+=-1;
  118.   scx+=-1;
  119. }
  120.  
  121. function scrollRight(){
  122.   var c,d_mpx,d_mpy,d_scx,d_scy;
  123.   d_mpx=amod(mpx+scWidth , width);
  124.   d_mpy=amod(mpy , height);
  125.   
  126.   d_scx=amod(scx  ,scWidth);  
  127.   d_scy=amod(scy  ,scHight);  
  128.   
  129.   c=scHeight;
  130.   
  131.   while (c>0) {
  132.     panel.fillRect(d_scx*pwidth,d_scy*pheight ,d_scx*pwidth+pwidth,d_scy*pheight+pheight,   0);
  133.     panel.drawSprite(d_scx*pwidth+pwidth/2,d_scy*pheight+pheight/2
  134.     , get(d_mpx,d_mpy) );
  135.     d_mpy=amod (d_mpy+1 , height);
  136.     d_scy=amod (d_scy+1 , scHeight);
  137.     c+=-1;
  138.   }
  139.   mpx+=1;
  140.   scx+=1;
  141. }
  142. function scrollUp(){
  143.   var c,d_mpx,d_mpy,d_scx,d_scy;
  144.   d_mpx=amod (mpx   , width);
  145.   d_mpy=amod (mpy-1 , height);
  146.   
  147.   d_scx=amod (scx   , scWidth);  
  148.   d_scy=amod (scy-1 , scHeight);  
  149.   
  150.   c=scWidth;
  151.   
  152.   while (c>0) {
  153.     panel.fillRect(d_scx*pwidth,d_scy*pheight ,d_scx*pwidth+pwidth,d_scy*pheight+pheight,   0);
  154.     panel.drawSprite(d_scx*pwidth+pwidth/2,d_scy*pheight+pheight/2 
  155.     , get(d_mpx,d_mpy) );
  156.     d_mpx=amod (d_mpx+1 , width);
  157.     d_scx=amod (d_scx+1 , scWidth);
  158.     c+=-1;
  159.   }
  160.   mpy+=-1;
  161.   scy+=-1;
  162. }
  163.  
  164. function scrollDown(){
  165.   var c,d_mpx,d_mpy,d_scx,d_scy;
  166.   d_mpx=amod (mpx   , width);
  167.   d_mpy=amod (mpy+scHeight , height);
  168.   
  169.   d_scx=amod (scx   , scWidth);  
  170.   d_scy=amod (scy   , scHeight);  
  171.   
  172.   c=scWidth;
  173.   
  174.   while (c>0) {
  175.     panel.fillRect(d_scx*pwidth,d_scy*pheight ,d_scx*pwidth+pwidth,d_scy*pheight+pheight,   0);
  176.     panel.drawSprite(d_scx*pwidth+pwidth/2  ,d_scy*pheight+pheight/2 
  177.     , get(d_mpx,d_mpy) );
  178.     d_mpx=amod (d_mpx+1 , width);
  179.     d_scx=amod (d_scx+1 , scWidth);
  180.     c+=-1;
  181.   }
  182.   mpy+=1;
  183.   scy+=1;
  184. }
  185.  
  186. function scrollTo(d_mpfx,d_mpfy) {
  187.   var dpx,dpy;
  188.   if(prevSW!=$screenWidth || prevSH!=$screenHeight) {
  189.     refreshChips(d_mpfx,d_mpfy);
  190.     $viewX=mpfx;$viewY=mpfy;
  191.     setAdjust($viewX,$viewY);
  192.     return;
  193.   } 
  194.   d_mpfx=trunc(d_mpfx);
  195.   d_mpfy=trunc(d_mpfy);
  196.   if (enabled) {
  197.     mpx=floor(mpfx/pwidth);mpy=floor(mpfy/pheight);
  198.     d_mpx=floor(d_mpfx/pwidth);d_mpy=floor(d_mpfy/pheight);
  199.     while (mpx>d_mpx) scrollLeft();
  200.     while (mpx<d_mpx) scrollRight();
  201.     while (mpy>d_mpy) scrollUp();
  202.     while (mpy<d_mpy) scrollDown();
  203.     scfx=amod (scfx+d_mpfx-mpfx ,scWidth*pwidth);
  204.     scfy=amod (scfy+d_mpfy-mpfy ,scHeight*pheight);
  205.     scx=floor(scfx/pwidth); scy=floor(scfy/pheight);
  206.   }
  207.   mpfx=d_mpfx;
  208.   mpfy=d_mpfy;
  209.   $viewX=mpfx;$viewY=mpfy;
  210.   setAdjust($viewX,$viewY);
  211.   mpx=floor(mpfx/pwidth);mpy=floor(mpfy/pheight);
  212. }
  213.  
  214. function refreshChips(ampfx,ampfy) {
  215.   var c,nc,xx,yy,cw,ch,xc,yc;
  216.   mpfx=ampfx;mpfy=ampfy;
  217.   $viewX=mpfx;$viewY=mpfy;
  218.   if (!enabled) return; 
  219.   
  220.   scWidth=floor($screenWidth/pwidth)+2;
  221.   scHeight=floor($screenHeight/pheight)+2;
  222.   panel=new Panel(scWidth*pwidth,scHeight*pheight);
  223.   prevSW=$screenWidth;
  224.   prevSH=$screenHeight;
  225.   
  226.   mpx=floor(mpfx/pwidth);mpy=floor(mpfy/pheight);
  227.   //
  228.   scfx=amod(mpfx,pwidth);
  229.   scfy=amod(mpfy,pheight); 
  230.   scx=0; scy=0;
  231.   
  232.   yy=0;
  233.   yc=mpy;
  234.   
  235.   while (yc<mpy+scHeight) {
  236.     xx=0;
  237.     xc=mpx;
  238.     while (xc<mpx+scWidth) {
  239.       panel.fillRect(xx,yy,xx+pwidth,yy+pheight, 0);
  240.       panel.drawSprite(xx+pwidth/2,yy+pheight/2,get(xc,yc) );
  241.       xx+=pwidth;
  242.       xc+=1; 
  243.     }
  244.     yy+=pheight;
  245.     yc+=1;
  246.   }
  247. }
  248.  
  249. function setBGColor(c){
  250.   $bgcolor=c;
  251. }
  252. function getBGColor() {
  253.   return $bgcolor;
  254. }
  255.  
  256. function setBackGround(c) {
  257.   var w,h;
  258.   w=getPatWidth(c);
  259.   h=getPatHeight(c);
  260.   
  261.   init(1,1,w,h);
  262.   set(0,0,c);
  263. }
  264.  
  265. function mouseDrag(xx,yy,dx,dy,lb,rb) {
  266.   if ($_design_Mode){
  267.     if (rb) {
  268.       //       print(mpfx);
  269.       //       print(-dx+xx);
  270.       //       print(mpfy);
  271.       //       print(-dy+yy);
  272.       scrBufX+=xx-dx;
  273.       scrBufY+=yy-dy;
  274.       scrollto(mpfx+trunc(scrBufX/16)*16,mpfy+trunc(scrBufY/16)*16);
  275.       if (scrBufX>=16)  scrBufX-=16;
  276.       if (scrBufX<=-16) scrBufX+=16;
  277.       if (scrBufY>=16)  scrBufY-=16;
  278.       if (scrBufY<=-16) scrBufY+=16;
  279.     }
  280.     if (lb) {
  281.       mouseDown(dx,dy,lb,rb);
  282.     }
  283.   }
  284. }
  285. function mouseDown(xx,yy,lb,rb) {
  286.   var cx,cy; 
  287.   if (lb && $_editMode==$_emMap) {
  288.     cx=floor((xx+mpfx) / pwidth);
  289.     cy=floor((yy+mpfy) / pheight);
  290.     
  291.     yy=cy-$_mappen_width;
  292.     while (yy<=cy+$_mappen_width) {
  293.       xx=cx-$_mappen_width;
  294.       while (xx<=cx+$_mappen_width) {
  295.         set(xx,yy,$curSelPat); xx+=1;
  296.       }
  297.       yy+=1;
  298.     }
  299.   }
  300.   if (rb) {
  301.     $curSelPat=getAt($viewX+xx,$viewY+yy);
  302.     print($curSelPat); 
  303.   }
  304. }
  305.  
  306. function getGenerator() {
  307.   return _generator;
  308. }
  309. function setGenerator(g) {
  310.   _generator=g;
  311.   return g;
  312. }
  313.  
  314. function makeTable(old) {
  315.   var i,j,oc,nc,no,o,tbl;
  316.   i=0;oc=0;tbl=new Array();
  317.   //old.sort();
  318.   $_tmp=0;
  319.   while (oc<old.size()) {
  320.     o=old.get(oc);
  321.     while (i<o.off)  {
  322.       tbl.add(new MapPatDesc("$_tmp",i));
  323.       i+=1;
  324.     }
  325.     if (oc<old.size()-1) {
  326.       no=old.get(oc+1);j=0;
  327.       while (i<no.off) {
  328.         tbl.add(new MapPatDesc(o.name,j));
  329.         i+=1;
  330.         j+=1;
  331.       }
  332.     } else {
  333.       tbl.add(new MapPatDesc(o.name,0)); 
  334.     }
  335.     oc+=1;
  336.   }
  337.   i=0; nc=0;
  338.   while (i<tbl.size()) {
  339.     t=tbl.get(i);
  340.     tbl.set(i,getGlobal(t.name)+t.off);
  341.     //print (strcat(i,strcat("->",getGlobal(t.name)+t.off) ));
  342.     i+=1; 
  343.   }
  344.   return tbl;
  345. }
  346.  
  347.  
  348. function loadMapFile() {
  349.   var tbl,i,ver,s,xx,yy,gen,lastofs;
  350.   print ("â}âbâvé≡ô╟é▌ì₧é±é┼éóé▄é╖");
  351.   gen=new Resource ( getGenerator() );
  352.   s=new ResourceReader(gen);
  353.   ver=s.readInt(); //version
  354.   if (ver==null) {
  355.     s.close();
  356.     return;
  357.   }
  358.   if (ver==1) {
  359.     i=s.readInt();
  360.     tbl=new Array();
  361.     while(i>0) {
  362.       tbl.add(new MapPatDesc
  363.       ( s.readString() , s.readInt() )
  364.       );
  365.       i-=1;
  366.     } 
  367.     tbl=makeTable(tbl);
  368.   }
  369.   data=new Array();
  370.   yy=0;
  371.   if (tbl && tbl.size()>0 ) lastofs=tbl.get(tbl.size()-1)-(tbl.size()-1); else lastofs=0;
  372.   while (yy<height) {
  373.     xx=0;
  374.     while (xx<width) {
  375.       t=s.readInt();//print(t);
  376.       if (tbl && t<tbl.size() && t>=0) data.add(tbl.get(t));
  377.       else data.add(t+lastofs); 
  378.       xx+=1;
  379.     } 
  380.     yy+=1; 
  381.   }
  382.   s.close();
  383.   //print ("");
  384.   
  385. }
  386.  
  387. function onGenerate() {
  388.   var s,xx,yy,t,gen;
  389.   init(width,height,pwidth,pheight);
  390.   /*if (datastr) {
  391.     s=new BinReader(datastr);
  392.     data=new Array();
  393.     yy=0;
  394.     while (yy<height) {
  395.       xx=0;
  396.       while (xx<width) {
  397.         t=s.readInt();//print(t);
  398.         data.add(t); 
  399.         xx+=1;
  400.       } 
  401.       yy+=1; 
  402.     }
  403.     s.close();
  404.   } else */
  405.   loadMapFile();
  406.   mpfx=0;mpfy=0;
  407.   refreshChips(mpfx,mpfy);
  408. }
  409.  
  410. function saveMapFile() {
  411.   var wrt,cc,gen,s,a,b; 
  412.   gen=new Resource ( getGenerator() );
  413.   if (gen && gen.getURL().length()>0) {
  414. //    wrt=new DataWriter(new ResourceWriter(gen));
  415.     wrt=new ResourceWriter(gen);
  416.     wrt.writeInt(1); //version 1 supports pattern é╕éΩéΘ
  417.     
  418.     a=getResource("usr.patterns");
  419.     b=a.getSubRes();
  420.     wrt.writeInt( b.size() );
  421.     for (a in b) {
  422.       wrt.writeString ( a.getName() );
  423.       wrt.writeInt( getGlobal(a.getName()) );
  424.     }
  425.     
  426.     cc=0;
  427.     while (cc<width*height) {
  428.       wrt.writeInt(data.get(cc));
  429.       cc+=1; 
  430.     }
  431.     wrt.close();
  432.   } 
  433. }
  434. function onSaveToGen() {
  435.   
  436.   setGenInstAttr("width",width);
  437.   setGenInstAttr("height",height);
  438.   setGenInstAttr("pwidth",pwidth);
  439.   setGenInstAttr("pheight",pheight);
  440.   setGenInstAttr("mpfx",mpfx);
  441.   setGenInstAttr("mpfy",mpfy);
  442.   
  443.   saveMapFile();
  444. }
  445. function getWidth() {
  446.   return width;
  447. }
  448. function getHeight() {
  449.   return height;
  450. }
  451.  
  452. function _refreshChips(ampfx,ampfy) {
  453. }
  454.  
  455.