3 lines
10 KiB
JavaScript
3 lines
10 KiB
JavaScript
var gdjs;(function(f){class d extends f.RuntimeBehavior{constructor(e,t,s){super(e,t,s);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._clearInputsBetweenFrames=!0;this._ignoreDefaultControlsAsSyncedByNetwork=!1;this._wasLeftKeyPressed=!1;this._wasRightKeyPressed=!1;this._wasUpKeyPressed=!1;this._wasDownKeyPressed=!1;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,this._useLegacyTurnBack=t.useLegacyTurnBack===void 0?!0:t.useLegacyTurnBack}getNetworkSyncData(e){return this._clearInputsBetweenFrames=!0,this._ignoreDefaultControlsAsSyncedByNetwork=!1,{...super.getNetworkSyncData(e),props:{a:this._angle,xv:this._xVelocity,yv:this._yVelocity,as:this._angularSpeed,lk:this._wasLeftKeyPressed,rk:this._wasRightKeyPressed,uk:this._wasUpKeyPressed,dk:this._wasDownKeyPressed,wsu:this._wasStickUsed,sa:this._stickAngle,sf:this._stickForce}}}updateFromNetworkSyncData(e,t){super.updateFromNetworkSyncData(e,t);const s=e.props;s.a!==void 0&&(this._angle=s.a),s.xv!==void 0&&(this._xVelocity=s.xv),s.yv!==void 0&&(this._yVelocity=s.yv),s.as!==void 0&&(this._angularSpeed=s.as),s.lk!==void 0&&(this._leftKey=s.lk),s.rk!==void 0&&(this._rightKey=s.rk),s.uk!==void 0&&(this._upKey=s.uk),s.dk!==void 0&&(this._downKey=s.dk),s.wsu!==void 0&&(this._wasStickUsed=s.wsu),s.sa!==void 0&&(this._stickAngle=s.sa),s.sf!==void 0&&(this._stickForce=s.sf),this._clearInputsBetweenFrames=!!t.clearInputs,this._ignoreDefaultControlsAsSyncedByNetwork=!t.keepControl}applyBehaviorOverriding(e){return e.allowDiagonals!==void 0&&(this._allowDiagonals=e.allowDiagonals),e.acceleration!==void 0&&(this._acceleration=e.acceleration),e.deceleration!==void 0&&(this._deceleration=e.deceleration),e.maxSpeed!==void 0&&(this._maxSpeed=e.maxSpeed),e.angularMaxSpeed!==void 0&&(this._angularMaxSpeed=e.angularMaxSpeed),e.rotateObject!==void 0&&(this._rotateObject=e.rotateObject),e.angleOffset!==void 0&&(this._angleOffset=e.angleOffset),e.ignoreDefaultControls!==void 0&&(this._ignoreDefaultControls=e.ignoreDefaultControls),(e.platformType!==void 0||e.customIsometryAngle!==void 0)&&this.setViewpoint(e.platformType,e.customIsometryAngle),e.movementAngleOffset!==void 0&&(this._movementAngleOffset=e.movementAngleOffset),!0}setViewpoint(e,t){e==="PixelIsometry"?this._basisTransformation=new f.TopDownMovementRuntimeBehavior.IsometryTransformation(Math.atan(.5)):e==="TrueIsometry"?this._basisTransformation=new f.TopDownMovementRuntimeBehavior.IsometryTransformation(Math.PI/6):e==="CustomIsometry"?this._basisTransformation=new f.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.hypot(this._xVelocity,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(f.evtTools.common.angleDifference(this._angle,e))<=t}setMovementAngleOffset(e){this._movementAngleOffset=e}getMovementAngleOffset(){return this._movementAngleOffset}doStepPreEvents(e){const t=37,s=38,h=39,r=40;this._leftKey||(this._leftKey=!this.shouldIgnoreDefaultControls()&&e.getGame().getInputManager().isKeyPressed(t)),this._rightKey||(this._rightKey=!this.shouldIgnoreDefaultControls()&&e.getGame().getInputManager().isKeyPressed(h)),this._downKey||(this._downKey=!this.shouldIgnoreDefaultControls()&&e.getGame().getInputManager().isKeyPressed(r)),this._upKey||(this._upKey=!this.shouldIgnoreDefaultControls()&&e.getGame().getInputManager().isKeyPressed(s));const a=this.owner.getElapsedTime();this._leftKey?this._leftKeyPressedDuration+=a:this._leftKeyPressedDuration=0,this._rightKey?this._rightKeyPressedDuration+=a:this._rightKeyPressedDuration=0,this._downKey?this._downKeyPressedDuration+=a:this._downKeyPressedDuration=0,this._upKey?this._upKeyPressedDuration+=a: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 i=f.TopDownMovementRuntimeBehavior._topDownMovementHookContext;for(const n of this._topDownMovementHooks)i._setDirection(o),o=n.overrideDirection(i);i._setDirection(o);for(const n of this._topDownMovementHooks)n.beforeSpeedUpdate(i);const l=this.owner,p=this.owner.getElapsedTime()/1e3,x=this._xVelocity,D=this._yVelocity;this._wasStickUsed=!1;let _=0,y=0,u=!1,g=0;if(o!==-1?(u=!0,_=(o+this._movementAngleOffset/45)*Math.PI/4,y=o*45+this._movementAngleOffset,g=this._maxSpeed):this._stickForce!==0?(u=!0,this._allowDiagonals||(this._stickAngle=90*Math.floor((this._stickAngle+45)/90)),y=this._stickAngle+this._movementAngleOffset,_=y*Math.PI/180,g=this._maxSpeed*this._stickForce,this._wasStickUsed=!0):(this._yVelocity!==0||this._xVelocity!==0)&&(u=!0,_=Math.atan2(this._yVelocity,this._xVelocity),y=_*180/Math.PI),u){let n=Math.cos(_),c=Math.sin(_);(n===-1||n===1)&&(c=0),(c===-1||c===1)&&(n=0);let v=Math.hypot(this._xVelocity,this._yVelocity);const M=this._xVelocity*n+this._yVelocity*c;M<0&&(v=M);const k=d.getAcceleratedSpeed(v,g,this._maxSpeed,this._acceleration,this._deceleration,p,this._useLegacyTurnBack);this._xVelocity=k*n,this._yVelocity=k*c}const m=this._xVelocity*this._xVelocity+this._yVelocity*this._yVelocity;if(m>this._maxSpeed*this._maxSpeed){const n=this._maxSpeed/Math.sqrt(m);this._xVelocity*=n,this._yVelocity*=n}this._angularSpeed=this._angularMaxSpeed;for(const n of this._topDownMovementHooks)n.beforePositionUpdate(i);const w=(x+this._xVelocity)/2*p,K=(D+this._yVelocity)/2*p;if(this._basisTransformation===null)l.setX(l.getX()+w),l.setY(l.getY()+K);else{const n=this._temporaryPointForTransformations;n[0]=w,n[1]=K,this._basisTransformation.toScreen(n,n),l.setX(l.getX()+n[0]),l.setY(l.getY()+n[1])}(this._xVelocity!==0||this._yVelocity!==0)&&(this._angle=y,this._rotateObject&&l.rotateTowardAngle(y+this._angleOffset,this._angularSpeed)),this._wasLeftKeyPressed=this._leftKey,this._wasRightKeyPressed=this._rightKey,this._wasUpKeyPressed=this._upKey,this._wasDownKeyPressed=this._downKey,this._clearInputsBetweenFrames&&(this._leftKey=!1,this._rightKey=!1,this._upKey=!1,this._downKey=!1,this._stickForce=0)}static getAcceleratedSpeed(e,t,s,h,r,a,o=!1){let i=e;const l=o?h:Math.max(h,r);return t<0?e<=t?i=Math.min(t,e+l*a):e<=0?i-=Math.max(-s,h*a):i=Math.max(t,e-l*a):t>0?e>=t?i=Math.max(t,e-l*a):e>=0?i=Math.min(s,e+h*a):i=Math.min(t,e+l*a):(e<0&&(i=Math.min(e+r*a,0)),e>0&&(i=Math.max(e-r*a,0))),i}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}shouldIgnoreDefaultControls(){return this._ignoreDefaultControls||this._ignoreDefaultControlsAsSyncedByNetwork}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)}}f.TopDownMovementRuntimeBehavior=d,function(s){class P{constructor(){this.direction=-1}getDirection(){return this.direction}_setDirection(r){this.direction=r}}s.TopDownMovementHookContext=P,s._topDownMovementHookContext=new f.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 a=Math.asin(Math.tan(r)),o=Math.sin(a),i=Math.cos(Math.PI/4),l=i;this._screen=[[i,-l],[o*l,o*i]]}toScreen(r,a){const o=this._screen[0][0]*r[0]+this._screen[0][1]*r[1],i=this._screen[1][0]*r[0]+this._screen[1][1]*r[1];a[0]=o,a[1]=i}}s.IsometryTransformation=t}(d=f.TopDownMovementRuntimeBehavior||(f.TopDownMovementRuntimeBehavior={})),f.registerBehavior("TopDownMovementBehavior::TopDownMovementBehavior",f.TopDownMovementRuntimeBehavior)})(gdjs||(gdjs={}));
|
|
//# sourceMappingURL=topdownmovementruntimebehavior.js.map
|