Advertisement
Guest User

behavior_moveObjectByKeyboardWithDoubleJump.js

a guest
Apr 26th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is a coppercube behavior which moves the node which
  2. // is controlled by the cursor keys and with space for 'jump'.
  3. //
  4. // The following embedded xml is for the editor and describes how the behavior can be edited:
  5. // Supported types are: int, float, string, bool, color, vect3d, scenenode, texture, action
  6. /*
  7.     <behavior jsname="behavior_moveObjectByKeyboardWithDoubleJump" description="Extended Move Object by Keyboard (with Double Jump)">
  8.         <property name="DisableMovementOnRightClickHeld" type="bool" default="true" />
  9.         <property name="Speed" type="float" default="0.02" />
  10.         <property name="JumpSpeed" type="float" default="0.1" />
  11.         <property name="JumpLengthMs" type="int" default="200" />
  12.         <property name="JumpSpeedForward" type="float" default="0.1" />
  13.         <property name="TimeForSecondJump" type="int" default="200" />
  14.         <property name="RotateSpeed" type="float" default="300" />     
  15.         <property name="JumpAction" type="action" />
  16.         <property name="DoubleJumpAction" type="action" />
  17.         <property name="StandAnimation" type="string" default="stand" />
  18.         <property name="WalkAnimation" type="string" default="walk" />
  19.         <property name="JumpAnimation" type="string" default="jump" />
  20.         <property name="DoubleJumpAnimation" type="string" default="jump" />
  21.     </behavior>
  22. */
  23.  
  24. var JumpCounter = 0;
  25.  
  26. Matrixhelper = function(bMakeIdentity)
  27. {
  28.     if (bMakeIdentity == null)
  29.         bMakeIdentity = true;
  30.        
  31.     this.m00 = 0;
  32.     this.m01 = 0;
  33.     this.m02 = 0;
  34.     this.m03 = 0;
  35.     this.m04 = 0;
  36.     this.m05 = 0;
  37.     this.m06 = 0;
  38.     this.m07 = 0;
  39.     this.m08 = 0;
  40.     this.m09 = 0;
  41.     this.m10 = 0;
  42.     this.m11 = 0;
  43.     this.m12 = 0;
  44.     this.m13 = 0;
  45.     this.m14 = 0;
  46.     this.m15 = 0;
  47.    
  48.     this.bIsIdentity=false;
  49.    
  50.     if (bMakeIdentity)
  51.     {
  52.         this.m00 = 1;
  53.         this.m05 = 1;
  54.         this.m10 = 1;
  55.         this.m15 = 1;
  56.         this.bIsIdentity = true;
  57.     }
  58. }
  59.  
  60. Matrixhelper.prototype.rotateVect = function(v)
  61. {
  62.     var tmp = new vector3d(v.x, v.y, v.z);
  63.     v.x = tmp.x*this.m00 + tmp.y*this.m04 + tmp.z*this.m08;
  64.     v.y = tmp.x*this.m01 + tmp.y*this.m05 + tmp.z*this.m09;
  65.     v.z = tmp.x*this.m02 + tmp.y*this.m06 + tmp.z*this.m10;
  66. }
  67.  
  68. Matrixhelper.prototype.setRotationDegrees = function(v)
  69. {
  70.     var c = 3.14159265359 / 180.0;
  71.     v.x *= c;
  72.     v.y *= c;
  73.     v.z *= c;
  74.     this.setRotationRadians(v);
  75. }
  76.  
  77. Matrixhelper.prototype.setRotationRadians = function(rotation)
  78. {
  79.     var cr = Math.cos( rotation.x );
  80.     var sr = Math.sin( rotation.x );
  81.     var cp = Math.cos( rotation.y );
  82.     var sp = Math.sin( rotation.y );
  83.     var cy = Math.cos( rotation.z );
  84.     var sy = Math.sin( rotation.z );
  85.  
  86.     this.m00 = ( cp*cy );
  87.     this.m01 = ( cp*sy );
  88.     this.m02 = ( -sp );
  89.  
  90.     var srsp = sr*sp;
  91.     var crsp = cr*sp;
  92.  
  93.     this.m04 = ( srsp*cy-cr*sy );
  94.     this.m05 = ( srsp*sy+cr*cy );
  95.     this.m06 = ( sr*cp );
  96.  
  97.     this.m08 = ( crsp*cy+sr*sy );
  98.     this.m09 = ( crsp*sy-sr*cy );
  99.     this.m10 = ( cr*cp );
  100.    
  101.     this.bIsIdentity = false;
  102. }
  103.  
  104.  
  105. behavior_moveObjectByKeyboardWithDoubleJump = function()
  106. {
  107.     this.ForwardKeyDown = false;
  108.     this.BackKeyDown = false;
  109.     this.PressedJump = false;
  110.     this.LastTime = null;
  111.     this.JumpForce = 0;
  112.     this.JumpLengthMs = 1000;
  113.    
  114.     this.leftKeyDown = false;
  115.     this.rightKeyDown = false;
  116.     this.jumpKeyDown = false;
  117.     this.downKeyDown = false;
  118.     this.upKeyDown = false;
  119.    
  120.     this.loopJumpAnimation = false
  121.    
  122.     // This variable is so the character does not move when the
  123.     // look around camera is activated by right mouse click hold.
  124.     this.MouseIsDown = false;
  125.    
  126.     // The jump counter
  127.     // this.JumpCounter = 0;
  128.    
  129.     // Reference variable for the current scene node
  130.     this.currentNode = null;
  131. };
  132.  
  133. // called every frame.
  134. //   'node' is the scene node where this behavior is attached to.
  135. //   'timeMs' the current time in milliseconds of the scene.
  136. // Returns 'true' if something changed, and 'false' if not.
  137. behavior_moveObjectByKeyboardWithDoubleJump.prototype.onAnimate = function(node, timeMs)
  138. {
  139.     // Variable to store the current scene node
  140.     this.currentNode = node;
  141.    
  142.     // get the time since the last frame
  143.     if (this.LastTime == null)
  144.     {
  145.         this.LastTime = timeMs; // we were never called before, so store the time and cancel
  146.         return false;
  147.     }
  148.    
  149.     this.LastNodeUsed = node;
  150.    
  151.     var timeDiff = timeMs - this.LastTime;
  152.     this.LastTime = timeMs;
  153.     if (timeDiff > 200) timeDiff = 200;
  154.    
  155.    
  156.     var currentRot = ccbGetSceneNodeProperty(node, 'Rotation');
  157.  
  158.     if (this.leftKeyDown )
  159.     {
  160.         currentRot.y -= timeDiff * this.RotateSpeed * 0.001;
  161.     }
  162.  
  163.     if (this.rightKeyDown )
  164.     {
  165.         currentRot.y += timeDiff * this.RotateSpeed * 0.001;
  166.     }
  167.    
  168.     if (this.MouseIsDown == false )
  169.     {
  170.         ccbSetSceneNodeProperty(node, 'Rotation', currentRot);
  171.     }
  172.  
  173.     // move forward/backward
  174.  
  175.     var pos = ccbGetSceneNodeProperty(node, 'Position');;
  176.  
  177.     var matrot = new Matrixhelper();
  178.     matrot.setRotationDegrees(currentRot);
  179.     var directionForward = new vector3d(0.0, 0.0, 1.0);
  180.  
  181.     //var matrot2 = new Matrixhelper();
  182.     //matrot2.setRotationDegrees(this.AdditionalRotationForLooking);
  183.     //matrot = matrot.multiply(matrot2);
  184.  
  185.     matrot.rotateVect(directionForward);
  186.  
  187.     var speed = this.Speed * timeDiff;
  188.     var origSpeed = 0;
  189.            
  190.     var bBackward = this.downKeyDown;
  191.     var bForward = this.upKeyDown;
  192.    
  193.    
  194.     directionForward.normalize();
  195.     directionForward.x *= speed;
  196.     directionForward.y *= speed;
  197.     directionForward.z *= speed;
  198.  
  199.     if (bForward || bBackward) // || (this.UseAcceleration && this.AcceleratedSpeed != 0))
  200.     {
  201.         var moveVect = new vector3d(directionForward.x, directionForward.y, directionForward.z);
  202.  
  203.         if (bBackward || (!(bForward || bBackward))) // && !this.AccelerationIsForward))
  204.         {
  205.             moveVect.x *= -1;
  206.             moveVect.y *= -1;
  207.             moveVect.z *= -1;
  208.         }
  209.  
  210.         pos.x += moveVect.x;
  211.         pos.y += moveVect.y;
  212.         pos.z += moveVect.z;
  213.     }
  214.    
  215.     if (this.JumpForce > 0 && this.MouseIsDown == false)
  216.     {
  217.         ccbSetSceneNodeProperty(node, 'Animation', this.JumpAnimation);    
  218.         if (!this.loopJumpAnimation)
  219.             ccbSetSceneNodeProperty(node, 'Looping', false);
  220.     }
  221.     else
  222.     if (bForward || bBackward && this.MouseIsDown == false)
  223.     {
  224.         ccbSetSceneNodeProperty(node, 'Animation', this.WalkAnimation);
  225.         ccbSetSceneNodeProperty(node, 'Looping', true);
  226.     }
  227.     else
  228.     {
  229.         if (this.MouseIsDown == false)
  230.         {
  231.         ccbSetSceneNodeProperty(node, 'Animation', this.StandAnimation);
  232.         ccbSetSceneNodeProperty(node, 'Looping', true);
  233.         }
  234.     }
  235.    
  236.    
  237.     // jump if jump was pressed
  238.     if (this.PressedJump && this.JumpForce == 0 && JumpCounter <= 2)
  239.     {
  240.         this.PressedJump = false;
  241.         this.JumpForce = this.JumpLengthMs;
  242.     }
  243.        
  244.     if (this.JumpForce > 0 && JumpCounter <= 2)
  245.     {
  246.         pos.y += this.JumpSpeed * timeDiff;
  247.         this.JumpForce -= timeDiff;
  248.        
  249.         if (this.JumpForce < 0)
  250.             this.JumpForce = 0;
  251.            
  252.         if (this.JumpSpeedForward > 0)
  253.         {
  254.             directionForward.normalize();
  255.             directionForward.x *= this.JumpSpeedForward;
  256.             directionForward.y *= this.JumpSpeedForward;
  257.             directionForward.z *= this.JumpSpeedForward;
  258.            
  259.             pos.x += directionForward.x;
  260.             pos.y += directionForward.y;
  261.             pos.z += directionForward.z;
  262.         }
  263.     }
  264.    
  265.     // set position
  266.     if (this.MouseIsDown == false )
  267.     {
  268.         ccbSetSceneNodeProperty(node, 'Position', pos);
  269.     }
  270.    
  271.     return true;
  272. }
  273.  
  274. // parameters: key: key id pressed or left up.  pressed: true if the key was pressed down, false if left up
  275. behavior_moveObjectByKeyboardWithDoubleJump.prototype.onKeyEvent = function(code, down)
  276. {
  277.     // store which key is down
  278.     // key codes are this: left=37, up=38, right=39, down=40
  279.  
  280.     if (code == 37 || code == 65 && this.MouseIsDown == false )
  281.     {
  282.         this.leftKeyDown = down;
  283.        
  284.         // fix chrome key down problem (key down sometimes doesn't arrive)
  285.         if (down) this.rightKeyDown = false;
  286.         return true;
  287.     }
  288.        
  289.     if (code == 39 || code == 68 && this.MouseIsDown == false )
  290.     {
  291.         this.rightKeyDown = down;
  292.        
  293.         // fix chrome key down problem (key down sometimes doesn't arrive)
  294.         if (down) this.leftKeyDown = false;
  295.         return true;
  296.     }
  297.        
  298.     if (code == 38 || code == 87 && this.MouseIsDown == false )
  299.     {
  300.         this.upKeyDown = down;         
  301.        
  302.         // fix chrome key down problem (key down sometimes doesn't arrive)
  303.         if (down) this.downKeyDown = false;
  304.         return true;
  305.     }
  306.        
  307.     if (code == 40 || code == 83 && this.MouseIsDown == false )
  308.     {
  309.         this.downKeyDown = down;
  310.        
  311.         // fix chrome key down problem (key down sometimes doesn't arrive)
  312.         if (down) this.upKeyDown = false;
  313.         return true;
  314.     }
  315.        
  316.     // jump when space pressed
  317.     if (code == 32 && !down && this.MouseIsDown == false )
  318.     {
  319.         if (JumpCounter = 1 )
  320.         {
  321.             ccbInvokeAction(this.JumpAction, this.currentNode);
  322.         }
  323.        
  324.         if (JumpCounter = 2 )
  325.         {
  326.             ccbInvokeAction(this.DoubleJumpAction, this.currentNode);
  327.         }
  328.        
  329.         if (JumpCounter <= 2 )
  330.         {
  331.                 this.PressedJump = true;
  332.                 return true;
  333.         }
  334.        
  335.         JumpCounter++;
  336.        
  337.         if (JumpCounter >= 3)
  338.         {
  339.             return false;
  340.         }
  341.     }
  342. }
  343.  
  344.  
  345. // mouseEvent: 0=move moved, 1=mouse clicked, 2=left mouse up,  3=left mouse down, 4=right mouse up, 5=right mouse down
  346. behavior_moveObjectByKeyboardWithDoubleJump.prototype.onMouseEvent = function(mouseEvent, mouseWheelDelta)
  347. {
  348.     if (this.DisableMovementOnRightClickHeld == true )
  349.     {
  350.         // This is to determine if right mouse is down.
  351.         if (mouseEvent == 5 )
  352.         {
  353.             this.MouseIsDown = true;
  354.         }
  355.        
  356.         if (mouseEvent == 4 )
  357.         {
  358.             this.MouseIsDown = false;
  359.         }
  360.     }
  361.     else
  362.     {
  363.         this.MouseIsDown = false;
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement