productItem.js 7.7 KB

1
  1. function ProductItem(e,t,n){ProductItem.superclass.constructor.apply(this);this.addAnimationFromCache("pdt"+e);var r={width:this.width,height:this.height};this.width=Math.round(this.width/this.cellSize.x)*this.cellSize.x;this.height=Math.round(this.height/this.cellSize.y)*this.cellSize.y;this.orign.x=(r.width-this.width)/2;this.orign.y=(r.height-this.height)/2;this.reset(this.fieldOffset.x+t*this.cellSize.x,this.fieldOffset.y+n*this.cellSize.y);this.boundsOffset.left=this.orign.x;this.boundsOffset.top=this.orign.y;this.updateBounds();this.state=ProductItem.prototype.S_PASSIVE;this.pickPoint={x:0,y:0};this.fromPoint={x:0,y:0};this.toPoint={x:0,y:0};this.moveDirection=ProductItem.prototype.HORIZONTAL;this.isFish=e==="Fish";if(this.isFish){this.updateSize=function(){};this.addAnimationFromCache("Fish1");this.addAnimationFromCache("Fish2");this.looped=false;this.animationSpeed=20;this.animTimer=10+Math.random()*10}this.moveable=true;var i=this.productsData[e].special;if(i!==undefined){var s=i.split(",");for(var o=0;o<s.length;o++){var u=s[o].trim();switch(u){case"immovable":this.moveable=false;break}}}ProductItem.prototype.addProduct(this)}extend(ProductItem,TrinSprite);ProductItem.prototype.update=function(){ProductItem.superclass.update.apply(this);var e=_TrinGame.mouse;switch(this.state){case ProductItem.prototype.S_PASSIVE:if(this.moveable&&ProductItem.prototype.pickedProduct===null&&e.isPressed()){if(this.bounds.intersects(e.x,e.y)){this.state=ProductItem.prototype.S_PICKED;this.pickPoint.x=e.x;this.pickPoint.y=e.y;this.hignLight();ProductItem.prototype.pickedProduct=this}}break;case ProductItem.prototype.S_PICKED:var t=Math.sqrt((e.x-this.pickPoint.x)*(e.x-this.pickPoint.x)+(e.y-this.pickPoint.y)*(e.y-this.pickPoint.y));if(t>=ProductItem.prototype.MOVE_DISTANCE){if(Math.abs(e.x-this.pickPoint.x)>Math.abs(e.y-this.pickPoint.y)){this.moveDirection=ProductItem.prototype.HORIZONTAL}else{this.moveDirection=ProductItem.prototype.VERTICAL}this.fromPoint.x=this.x;this.fromPoint.y=this.y;this.pickPoint.x-=this.x;this.pickPoint.y-=this.y;this.state=ProductItem.prototype.S_MOVED;PlayState.prototype.CURRENT.unHignLight();this.hignLight()}if(e.isReleased()){this.state=this.S_PASSIVE;PlayState.prototype.CURRENT.unHignLight()}break;case ProductItem.prototype.S_MOVED:if(e.isReleased()){this.state=ProductItem.prototype.S_RELEASED}var n;if(this.moveDirection===this.HORIZONTAL){var t=e.x-(this.x+this.pickPoint.x);if(t>0){t=Math.min(this.cellSize.x-1,t)}else{t=Math.max(-this.cellSize.x+1,t)}this.x+=t;this.updateBounds();if(this.bounds.left()<this.fieldOffset.x){this.x=this.fieldOffset.x}else if(this.bounds.right()>this.fieldOffset.x+this.cellSize.x*this.fieldSize.width){if(this.isFish&&this.y===this.fieldOffset.y+2*this.cellSize.y){if(this.bounds.right()>this.fieldOffset.x+this.cellSize.x*(this.fieldSize.width+1)){this.x=this.fieldOffset.x+(this.fieldSize.width+1)*this.cellSize.x-this.width}}else{this.x=this.fieldOffset.x+this.fieldSize.width*this.cellSize.x-this.width}}n=this.getCollisionProduct(this.moveDirection);if(n!==undefined){if(this.x>this.fromPoint.x){if(n!==null){this.x=n.bounds.left()-this.width}}else{if(n!==null){this.x=n.bounds.right()}}}}else{var t=e.y-(this.y+this.pickPoint.y);if(t>0){t=Math.min(this.cellSize.y-1,t)}else{t=Math.max(-this.cellSize.y+1,t)}this.y+=t;this.updateBounds();if(this.bounds.top()<this.fieldOffset.y){this.y=this.fieldOffset.y}else if(this.bounds.bottom()>this.fieldOffset.y+this.cellSize.y*this.fieldSize.height){this.y=this.fieldOffset.y+this.fieldSize.height*this.cellSize.y-this.height}n=this.getCollisionProduct(this.moveDirection);if(n!==undefined){if(this.y>this.fromPoint.y){if(n!==null){this.y=n.bounds.top()-this.height}}else{if(n!==null){this.y=n.bounds.bottom()}}}}this.updateBounds();break;case ProductItem.prototype.S_RELEASED:var r=this.x;this.toPoint.x=this.fieldOffset.x+Math.round((this.x-this.fieldOffset.x)/this.cellSize.x)*this.cellSize.x;this.toPoint.y=this.fieldOffset.y+Math.round((this.y-this.fieldOffset.y)/this.cellSize.y)*this.cellSize.y;this.state=ProductItem.prototype.S_RETURNING;this.updateBounds();PlayState.prototype.CURRENT.unHignLight();break;case this.S_RETURNING:var i=Math.min(8,Math.abs(this.toPoint.x-this.x));var s=Math.min(8,Math.abs(this.toPoint.y-this.y));if(this.x<this.toPoint.x){this.x+=Math.max((this.toPoint.x-this.x)/8,i)}else if(this.x>this.toPoint.x){this.x+=Math.min((this.toPoint.x-this.x)/8,-i)}if(this.y<this.toPoint.y){this.y+=Math.max((this.toPoint.y-this.y)/8,s)}else if(this.y>this.toPoint.y){this.y+=Math.min((this.toPoint.y-this.y)/8,-s)}if(Math.abs(this.x-this.toPoint.x)<4){this.x=this.toPoint.x}if(Math.abs(this.y-this.toPoint.y)<4){this.y=this.toPoint.y}this.updateBounds();if(this.x===this.toPoint.x&&this.y===this.toPoint.y){this.state=this.S_PASSIVE;if(this.isFish&&this.x===this.fieldOffset.x+(this.fieldSize.width-1)*this.cellSize.x){PlayState.prototype.state=PlayState.prototype.STATE_PAW_START;this.visible=false}}break}if(this.isFish){if(this.isFinished()&&this.animTimer--<=0){this.animTimer=10+Math.random()*10;this.switchAnimation("Fish"+(1+Math.floor(Math.random()*2)));this.play()}var o=PlayState.prototype.starsInstances;var u=this.bounds;var a;for(var f=0;f<o.length;f++){a=o[f];if(a.isPickable()&&a.bounds.intersects(u.x,u.y,u.width,u.height)){a.pick()}}}};ProductItem.prototype.S_PASSIVE=0;ProductItem.prototype.S_PICKED=1;ProductItem.prototype.S_MOVED=2;ProductItem.prototype.S_RELEASED=3;ProductItem.prototype.S_RETURNING=4;ProductItem.prototype.MOVE_DISTANCE=10;ProductItem.prototype.HORIZONTAL=0;ProductItem.prototype.VERTICAL=1;ProductItem.prototype.fieldOffset=Global.prototype.fieldOffset;ProductItem.prototype.cellSize=Global.prototype.cellSize;ProductItem.prototype.fieldSize=Global.prototype.fieldSize;ProductItem.prototype.products=[];ProductItem.prototype.addProduct=function(e){ProductItem.prototype.products[ProductItem.prototype.products.length]=e};ProductItem.prototype.getCollisionProduct=function(e){var t=this.bounds.x;var n=this.bounds.y;var r=this.bounds.width;var i=this.bounds.height;if(e===this.HORIZONTAL){}else{}for(var s=0;s<this.products.length;s++){var o=this.products[s];if(o!==null&&o!==this&&o.bounds.intersects(t,n,r,i)){return o}}};ProductItem.prototype.hignLight=function(){var e=new TrinRectangle(this.x,this.y,this.width,this.height);var t=new TrinRectangle(this.x,this.y,this.width,this.height);var n=true;if(this.state===this.S_PICKED||this.moveDirection===this.HORIZONTAL){while(n){e.x-=this.cellSize.x;for(var r=0;r<this.products.length;r++){var i=this.products[r];if(i!==null&&i!==this&&i.bounds.intersectsRect(e)){n=false}}if(e.x<this.fieldOffset.x){n=false}}e.x+=this.cellSize.x;n=true;e.width=this.bounds.right()-e.x;while(n){e.width+=this.cellSize.x;for(var r=0;r<this.products.length;r++){var i=this.products[r];if(i!==null&&i!==this&&i.bounds.intersectsRect(e)){n=false}}if(this.isFish&&this.y===this.fieldOffset.y+2*this.cellSize.y){if(e.right()>this.fieldOffset.x+(this.fieldSize.width+1)*this.cellSize.x){n=false}}else if(e.right()>this.fieldOffset.x+this.fieldSize.width*this.cellSize.x){n=false}}e.width-=this.cellSize.x}if(this.state===this.S_PICKED||this.moveDirection===this.VERTICAL){n=true;while(n){t.y-=this.cellSize.y;for(var r=0;r<this.products.length;r++){var i=this.products[r];if(i!==null&&i!==this&&i.bounds.intersectsRect(t)){n=false}}if(t.y<this.fieldOffset.y){n=false}}t.y+=this.cellSize.y;n=true;t.height=this.bounds.bottom()-t.y;while(n){t.height+=this.cellSize.y;for(var r=0;r<this.products.length;r++){var i=this.products[r];if(i!==null&&i!==this&&i.bounds.intersectsRect(t)){n=false}}if(t.bottom()>this.fieldOffset.y+this.fieldSize.height*this.cellSize.y){n=false}}t.height-=this.cellSize.y}PlayState.prototype.CURRENT.hignLight(e,t)};ProductItem.prototype.productsData=[];ProductItem.prototype.pickedProduct=null