Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         MotionX Class by Raymond Irving (http://dyntools.shorturl.com)
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.  
  7.         requires: Dynlayer
  8. */
  9.  
  10. MotionX = {}; // used by dynapi.library
  11.  
  12. DynLayer.prototype.makeSolid=function(){
  13.         this.isHard=true
  14.         this._collideX=this.x
  15.         this._collideY=this.x
  16.         this.collideEvent ={
  17.                 onmove:function(e) {
  18.                         var me=e.getSource()
  19.                         var dirX='',dirY=''
  20.                         // get direction
  21.                         if (me._collideX!=me.x) {
  22.                                 if (me._collideX<me.x){dirX="E"}else{dirX="W"}
  23.                         }
  24.                         if (me._collideY!=me.y){
  25.                                 if (me._collideY<me.y){dirY="S"}else{dirY="N"}
  26.                         }
  27.                         // get angle direction
  28.                         me._setCollideAngleDirection(me._collideX,me._collideY,me.x,me.y)
  29.                         me._collideX=me.x;
  30.                         me._collideY=me.y;
  31.                         me._collideDirection=dirY+dirX;
  32.                         me._checkForCollision();
  33.                 }
  34.         }
  35.         this.addEventListener(this.collideEvent)
  36. };
  37. // overwrite setLocation to trigger onmove event
  38. DynLayer.prototype._setLocation=DynLayer.prototype.setLocation;
  39. DynLayer.prototype.setLocation=function(x,y) {
  40.         this._setLocation(x,y);
  41.         this.invokeEvent('move');
  42. };
  43. DynLayer.prototype._setCollideAngleDirection = function(x1,y1,x2,y2) {
  44.         var distx = (x2-x1),disty = (y1-y2),angle;
  45.         //if (distx==0 && disty==0) return 0;
  46.         var rad=Math.abs(Math.atan2(disty,distx))
  47.         if (disty>=0) {
  48.                 if (distx>=0) angle = 90-(rad*180/Math.PI)
  49.                 else angle = 270+(180-(rad*180/Math.PI));
  50.         }else{
  51.                 angle = 90+(rad*180/Math.PI)
  52.         }
  53.         this._collideAngle=Math.ceil(angle);
  54. };
  55. DynLayer.prototype._checkForCollision=function(){
  56.         if (!this.parent.children.length>0) return false;
  57.         var ch,chX,sX,sY,colX1,colX2,colY1,colY2,n1,n2;
  58.         this.collideObject==null
  59.         for (var i in this.parent.children) {
  60.                 ch=this.parent.children[i];
  61.                 if (ch!=this && ch.isHard==true) {
  62.                         chX=ch.x;
  63.                         chY=ch.y;
  64.                         sX=this.x;
  65.                         sY=this.y;
  66.                         colX1=(sX>=chX && sX<=chX+ch.w)
  67.                         colX2=(chX>=sX && chX<=sX+this.w)
  68.                         colY1=(sY>=chY && sY<=chY+ch.h)
  69.                         colY2=(chY>=sY && chY<=sY+this.h)
  70.                         if ((colX1 || colX2) && (colY1 || colY2)) {
  71.                                 if (this._collideDirection=='NE') {
  72.                                         n1=((chY+ch.h)-this.y);n2=((sX+this.w)-chX)
  73.                                         if (n1<n2) {face="S"}else{face="W"}
  74.                                 }else if (this._collideDirection=='NW') {
  75.                                         n1=((chY+ch.h)-this.y);n2=((chX+ch.w)-sX)
  76.                                         if (n1<n2) {face="S"}else{face="E"}
  77.                                 }else if (this._collideDirection=='SE') {
  78.                                         n1=((sY+this.h)-ch.y);n2=((sX+this.w)-chX)
  79.                                         if (n1<n2) {face="N"}else{face="W"}
  80.                                 }else if (this._collideDirection=='SW') {
  81.                                         n1=((sY+this.h)-ch.y);n2=((chX+ch.w)-sX)
  82.                                         if (n1<n2) {face="N"}else{face="E"}
  83.                                 }else if (this._collideDirection=='E') {
  84.                                         face="W"
  85.                                 }else if (this._collideDirection=='W') {
  86.                                         face="E"
  87.                                 }else if (this._collideDirection=='N') {
  88.                                         face="S"
  89.                                 }else if (this._collideDirection=='S') {
  90.                                         face="N"
  91.                                 }
  92.  
  93.                                 ch._impactSide=face
  94.                                 if (face=="W"){this._impactSide="E"}
  95.                                 if (face=="E"){this._impactSide="W"}
  96.                                 if (face=="N"){this._impactSide="S"}
  97.                                 if (face=="S"){this._impactSide="N"}
  98.  
  99.                                 this._collideObject=ch
  100.                                 this.invokeEvent("collide");
  101.                                 ch._collideObject=this
  102.                                 ch.invokeEvent("collide");
  103.                         }
  104.                 }
  105.         }
  106.         return false;
  107. };
  108. DynLayer.prototype.getImpactSide=function(){
  109.         return this._impactSide;
  110. }
  111. DynLayer.prototype.getObstacle=function(){
  112.         return this._collideObject;
  113. }
  114. DynLayer.prototype.getDirection=function(){
  115.         return this._collideDirection;
  116. }
  117. DynLayer.prototype.getDirectionAngle=function(){
  118.         return this._collideAngle;
  119. }
  120.