Files
moon-gaming/yes/Extensions/TopDownMovementBehavior/topdownmovementruntimebehavior.js
T
2023-11-23 21:26:01 +00:00

3 lines
8.4 KiB
JavaScript

var gdjs;(function(a){class m extends a.RuntimeBehavior{constructor(e,t,g){super(e,t,g);this._angle=0;this._xVelocity=0;this._yVelocity=0;this._angularSpeed=0;this._leftKey=!1;this._rightKey=!1;this._upKey=!1;this._downKey=!1;this._leftKeyPressedDuration=0;this._rightKeyPressedDuration=0;this._upKeyPressedDuration=0;this._downKeyPressedDuration=0;this._wasStickUsed=!1;this._stickAngle=0;this._stickForce=0;this._temporaryPointForTransformations=[0,0];this._topDownMovementHooks=[];this._allowDiagonals=t.allowDiagonals,this._acceleration=t.acceleration,this._deceleration=t.deceleration,this._maxSpeed=t.maxSpeed,this._angularMaxSpeed=t.angularMaxSpeed,this._rotateObject=t.rotateObject,this._angleOffset=t.angleOffset,this._ignoreDefaultControls=t.ignoreDefaultControls,this.setViewpoint(t.viewpoint,t.customIsometryAngle),this._movementAngleOffset=t.movementAngleOffset||0}updateFromBehaviorData(e,t){return e.allowDiagonals!==t.allowDiagonals&&(this._allowDiagonals=t.allowDiagonals),e.acceleration!==t.acceleration&&(this._acceleration=t.acceleration),e.deceleration!==t.deceleration&&(this._deceleration=t.deceleration),e.maxSpeed!==t.maxSpeed&&(this._maxSpeed=t.maxSpeed),e.angularMaxSpeed!==t.angularMaxSpeed&&(this._angularMaxSpeed=t.angularMaxSpeed),e.rotateObject!==t.rotateObject&&(this._rotateObject=t.rotateObject),e.angleOffset!==t.angleOffset&&(this._angleOffset=t.angleOffset),e.ignoreDefaultControls!==t.ignoreDefaultControls&&(this._ignoreDefaultControls=t.ignoreDefaultControls),(e.platformType!==t.platformType||e.customIsometryAngle!==t.customIsometryAngle)&&this.setViewpoint(t.platformType,t.customIsometryAngle),e.movementAngleOffset!==t.movementAngleOffset&&(this._movementAngleOffset=t.movementAngleOffset),!0}setViewpoint(e,t){e==="PixelIsometry"?this._basisTransformation=new a.TopDownMovementRuntimeBehavior.IsometryTransformation(Math.atan(.5)):e==="TrueIsometry"?this._basisTransformation=new a.TopDownMovementRuntimeBehavior.IsometryTransformation(Math.PI/6):e==="CustomIsometry"?this._basisTransformation=new a.TopDownMovementRuntimeBehavior.IsometryTransformation(t*Math.PI/180):this._basisTransformation=null}setAcceleration(e){this._acceleration=e}getAcceleration(){return this._acceleration}setDeceleration(e){this._deceleration=e}getDeceleration(){return this._deceleration}setMaxSpeed(e){this._maxSpeed=e}getMaxSpeed(){return this._maxSpeed}setAngularMaxSpeed(e){this._angularMaxSpeed=e}getAngularMaxSpeed(){return this._angularMaxSpeed}setAngleOffset(e){this._angleOffset=e}getAngleOffset(){return this._angleOffset}allowDiagonals(e){this._allowDiagonals=e}diagonalsAllowed(){return this._allowDiagonals}setRotateObject(e){this._rotateObject=e}isObjectRotated(){return this._rotateObject}isMoving(){return this._xVelocity!==0||this._yVelocity!==0}getSpeed(){return Math.sqrt(this._xVelocity*this._xVelocity+this._yVelocity*this._yVelocity)}getXVelocity(){return this._xVelocity}setXVelocity(e){this._xVelocity=e}getYVelocity(){return this._yVelocity}setYVelocity(e){this._yVelocity=e}getAngle(){return this._angle}isMovementAngleAround(e,t){return Math.abs(a.evtTools.common.angleDifference(this._angle,e))<=t}setMovementAngleOffset(e){this._movementAngleOffset=e}getMovementAngleOffset(){return this._movementAngleOffset}doStepPreEvents(e){const t=37,g=38,p=39,r=40;this._leftKey|=!this._ignoreDefaultControls&&e.getGame().getInputManager().isKeyPressed(t),this._rightKey|=!this._ignoreDefaultControls&&e.getGame().getInputManager().isKeyPressed(p),this._downKey|=!this._ignoreDefaultControls&&e.getGame().getInputManager().isKeyPressed(r),this._upKey|=!this._ignoreDefaultControls&&e.getGame().getInputManager().isKeyPressed(g);const f=this.owner.getElapsedTime();this._leftKey?this._leftKeyPressedDuration+=f:this._leftKeyPressedDuration=0,this._rightKey?this._rightKeyPressedDuration+=f:this._rightKeyPressedDuration=0,this._downKey?this._downKeyPressedDuration+=f:this._downKeyPressedDuration=0,this._upKey?this._upKeyPressedDuration+=f:this._upKeyPressedDuration=0;let o=-1;this._allowDiagonals?this._upKey&&!this._downKey?this._leftKey&&!this._rightKey?o=5:!this._leftKey&&this._rightKey?o=7:o=6:!this._upKey&&this._downKey?this._leftKey&&!this._rightKey?o=3:!this._leftKey&&this._rightKey?o=1:o=2:this._leftKey&&!this._rightKey?o=4:!this._leftKey&&this._rightKey&&(o=0):(this._upKey&&!this._downKey?o=6:!this._upKey&&this._downKey&&(o=2),this._leftKey&&!this._rightKey&&(this._upKey===this._downKey||this._upKey&&this._leftKeyPressedDuration<this._upKeyPressedDuration||this._downKey&&this._leftKeyPressedDuration<this._downKeyPressedDuration)?o=4:this._rightKey&&!this._leftKey&&(this._upKey===this._downKey||this._upKey&&this._rightKeyPressedDuration<this._upKeyPressedDuration||this._downKey&&this._rightKeyPressedDuration<this._downKeyPressedDuration)&&(o=0));const h=a.TopDownMovementRuntimeBehavior._topDownMovementHookContext;for(const i of this._topDownMovementHooks)h._setDirection(o),o=i.overrideDirection(h);h._setDirection(o);for(const i of this._topDownMovementHooks)i.beforeSpeedUpdate(h);const l=this.owner,c=this.owner.getElapsedTime()/1e3,M=this._xVelocity,v=this._yVelocity;this._wasStickUsed=!1;let _=0,y=0,s=1,n=0;if(o!==-1)_=(o+this._movementAngleOffset/45)*Math.PI/4,y=o*45+this._movementAngleOffset,s=Math.cos(_),n=Math.sin(_),(s===-1||s===1)&&(n=0),(n===-1||n===1)&&(s=0),this._xVelocity+=this._acceleration*c*s,this._yVelocity+=this._acceleration*c*n;else if(this._stickForce!==0){this._allowDiagonals||(this._stickAngle=90*Math.floor((this._stickAngle+45)/90)),y=this._stickAngle+this._movementAngleOffset,_=y*Math.PI/180;const i=this._acceleration*c*this._stickForce;s=Math.cos(_),n=Math.sin(_),(s===-1||s===1)&&(n=0),(n===-1||n===1)&&(s=0),this._xVelocity+=i*s,this._yVelocity+=i*n,this._wasStickUsed=!0,this._stickForce=0}else if(this._yVelocity!==0||this._xVelocity!==0){_=Math.atan2(this._yVelocity,this._xVelocity),y=_*180/Math.PI;const i=this._xVelocity>=0,x=this._yVelocity>=0;s=Math.cos(_),n=Math.sin(_),(s===-1||s===1)&&(n=0),(n===-1||n===1)&&(s=0),this._xVelocity-=this._deceleration*c*s,this._yVelocity-=this._deceleration*c*n,this._xVelocity>0!==i&&(this._xVelocity=0),this._yVelocity>0!==x&&(this._yVelocity=0)}this._xVelocity*this._xVelocity+this._yVelocity*this._yVelocity>this._maxSpeed*this._maxSpeed&&(this._xVelocity=this._maxSpeed*s,this._yVelocity=this._maxSpeed*n),this._angularSpeed=this._angularMaxSpeed;for(const i of this._topDownMovementHooks)i.beforePositionUpdate(h);const u=(M+this._xVelocity)/2*c,d=(v+this._yVelocity)/2*c;if(this._basisTransformation===null)l.setX(l.getX()+u),l.setY(l.getY()+d);else{const i=this._temporaryPointForTransformations;i[0]=u,i[1]=d,this._basisTransformation.toScreen(i,i),l.setX(l.getX()+i[0]),l.setY(l.getY()+i[1])}(this._xVelocity!==0||this._yVelocity!==0)&&(this._angle=y,this._rotateObject&&l.rotateTowardAngle(y+this._angleOffset,this._angularSpeed)),this._leftKey=!1,this._rightKey=!1,this._upKey=!1,this._downKey=!1}simulateControl(e){e==="Left"?this._leftKey=!0:e==="Right"?this._rightKey=!0:e==="Up"?this._upKey=!0:e==="Down"&&(this._downKey=!0)}ignoreDefaultControls(e){this._ignoreDefaultControls=e}simulateLeftKey(){this._leftKey=!0}simulateRightKey(){this._rightKey=!0}simulateUpKey(){this._upKey=!0}simulateDownKey(){this._downKey=!0}simulateStick(e,t){this._stickAngle=e%360,this._stickForce=Math.max(0,Math.min(1,t))}isUsingControl(e){return e==="Left"?this._leftKeyPressedDuration>0:e==="Right"?this._rightKeyPressedDuration>0:e==="Up"?this._upKeyPressedDuration>0:e==="Down"?this._downKeyPressedDuration>0:e==="Stick"?this._wasStickUsed:!1}getLastStickInputAngle(){return this._stickAngle}registerHook(e){this._topDownMovementHooks.push(e)}}a.TopDownMovementRuntimeBehavior=m,function(g){class K{constructor(){this.direction=-1}getDirection(){return this.direction}_setDirection(r){this.direction=r}}g.TopDownMovementHookContext=K,g._topDownMovementHookContext=new a.TopDownMovementRuntimeBehavior.TopDownMovementHookContext;class t{constructor(r){if(r<=0||r>=Math.PI/4)throw new RangeError("An isometry angle must be in ]0; pi/4] but was: "+r);const f=Math.asin(Math.tan(r)),o=Math.sin(f),h=Math.cos(Math.PI/4),l=h;this._screen=[[h,-l],[o*l,o*h]]}toScreen(r,f){const o=this._screen[0][0]*r[0]+this._screen[0][1]*r[1],h=this._screen[1][0]*r[0]+this._screen[1][1]*r[1];f[0]=o,f[1]=h}}g.IsometryTransformation=t}(m=a.TopDownMovementRuntimeBehavior||(a.TopDownMovementRuntimeBehavior={})),a.registerBehavior("TopDownMovementBehavior::TopDownMovementBehavior",a.TopDownMovementRuntimeBehavior)})(gdjs||(gdjs={}));
//# sourceMappingURL=topdownmovementruntimebehavior.js.map