JAVASCRIPT   7

Transform.js

Guest on 31st May 2021 02:29:57 AM

  1. /**
  2.  * @author       Richard Davey <rich@photonstorm.com>
  3.  * @copyright    2020 Photon Storm Ltd.
  4.  * @license      {@link https://opensource.org/licenses/MIT|MIT License}
  5.  */
  6.  
  7. var Body = require('../lib/body/Body');
  8. var MATH_CONST = require('../../../math/const');
  9. var WrapAngle = require('../../../math/angle/Wrap');
  10. var WrapAngleDegrees = require('../../../math/angle/WrapDegrees');
  11.  
  12. //  global bitmask flag for GameObject.renderMask (used by Scale)
  13. var _FLAG = 4; // 0100
  14.  
  15. //  Transform Component
  16.  
  17. /**
  18.  * Provides methods used for getting and setting the position, scale and rotation of a Game Object.
  19.  *
  20.  * @namespace Phaser.Physics.Matter.Components.Transform
  21.  * @since 3.0.0
  22.  */
  23. var Transform = {
  24.  
  25.     /**
  26.      * The x position of this Game Object.
  27.      *
  28.      * @name Phaser.Physics.Matter.Components.Transform#x
  29.      * @type {number}
  30.      * @since 3.0.0
  31.      */
  32.     x: {
  33.  
  34.         get: function ()
  35.         {
  36.             return this.body.position.x;
  37.         },
  38.  
  39.         set: function (value)
  40.         {
  41.             this._tempVec2.set(value, this.y);
  42.  
  43.             Body.setPosition(this.body, this._tempVec2);
  44.         }
  45.  
  46.     },
  47.  
  48.     /**
  49.      * The y position of this Game Object.
  50.      *
  51.      * @name Phaser.Physics.Matter.Components.Transform#y
  52.      * @type {number}
  53.      * @since 3.0.0
  54.      */
  55.     y: {
  56.  
  57.         get: function ()
  58.         {
  59.             return this.body.position.y;
  60.         },
  61.  
  62.         set: function (value)
  63.         {
  64.             this._tempVec2.set(this.x, value);
  65.  
  66.             Body.setPosition(this.body, this._tempVec2);
  67.         }
  68.  
  69.     },
  70.  
  71.     /**
  72.      * The horizontal scale of this Game Object.
  73.      *
  74.      * @name Phaser.Physics.Matter.Components.Transform#scaleX
  75.      * @type {number}
  76.      * @since 3.0.0
  77.      */
  78.     scaleX: {
  79.  
  80.         get: function ()
  81.         {
  82.             return this._scaleX;
  83.         },
  84.  
  85.         set: function (value)
  86.         {
  87.             var factorX = 1 / this._scaleX;
  88.             var factorY = 1 / this._scaleY;
  89.    
  90.             this._scaleX = value;
  91.  
  92.             if (this._scaleX === 0)
  93.             {
  94.                 this.renderFlags &= ~_FLAG;
  95.             }
  96.             else
  97.             {
  98.                 this.renderFlags |= _FLAG;
  99.             }
  100.  
  101.             //  Reset Matter scale back to 1 (sigh)
  102.             Body.scale(this.body, factorX, factorY);
  103.  
  104.             Body.scale(this.body, value, this._scaleY);
  105.         }
  106.  
  107.     },
  108.  
  109.     /**
  110.      * The vertical scale of this Game Object.
  111.      *
  112.      * @name Phaser.Physics.Matter.Components.Transform#scaleY
  113.      * @type {number}
  114.      * @since 3.0.0
  115.      */
  116.     scaleY: {
  117.  
  118.         get: function ()
  119.         {
  120.             return this._scaleY;
  121.         },
  122.  
  123.         set: function (value)
  124.         {
  125.             var factorX = 1 / this._scaleX;
  126.             var factorY = 1 / this._scaleY;
  127.  
  128.             this._scaleY = value;
  129.  
  130.             if (this._scaleY === 0)
  131.             {
  132.                 this.renderFlags &= ~_FLAG;
  133.             }
  134.             else
  135.             {
  136.                 this.renderFlags |= _FLAG;
  137.             }
  138.  
  139.             Body.scale(this.body, factorX, factorY);
  140.  
  141.             Body.scale(this.body, this._scaleX, value);
  142.         }
  143.  
  144.     },
  145.  
  146.     /**
  147.      * Use `angle` to set or get rotation of the physics body associated to this GameObject.
  148.      * Unlike rotation, when using set the value can be in degrees, which will be converted to radians internally.
  149.      *
  150.      * @name Phaser.Physics.Matter.Components.Transform#angle
  151.      * @type {number}
  152.      * @since 3.0.0
  153.      */
  154.     angle: {
  155.  
  156.         get: function ()
  157.         {
  158.             return WrapAngleDegrees(this.body.angle * MATH_CONST.RAD_TO_DEG);
  159.         },
  160.  
  161.         set: function (value)
  162.         {
  163.             //  value is in degrees
  164.             this.rotation = WrapAngleDegrees(value) * MATH_CONST.DEG_TO_RAD;
  165.         }
  166.     },
  167.  
  168.     /**
  169.      * Use `rotation` to set or get the rotation of the physics body associated with this GameObject.
  170.      * The value when set must be in radians.
  171.      *
  172.      * @name Phaser.Physics.Matter.Components.Transform#rotation
  173.      * @type {number}
  174.      * @since 3.0.0
  175.      */
  176.     rotation: {
  177.  
  178.         get: function ()
  179.         {
  180.             return this.body.angle;
  181.         },
  182.  
  183.         set: function (value)
  184.         {
  185.             //  value is in radians
  186.             this._rotation = WrapAngle(value);
  187.  
  188.             Body.setAngle(this.body, this._rotation);
  189.         }
  190.     },
  191.  
  192.     /**
  193.      * Sets the position of the physics body along x and y axes.
  194.      * Both the parameters to this function are optional and if not passed any they default to 0.
  195.      * Velocity, angle, force etc. are unchanged.
  196.      *
  197.      * @method Phaser.Physics.Matter.Components.Transform#setPosition
  198.      * @since 3.0.0
  199.      *
  200.      * @param {number} [x=0] - The horizontal position of the body.
  201.      * @param {number} [y=x] - The vertical position of the body.
  202.      *
  203.      * @return {this} This Game Object.
  204.      */
  205.     setPosition: function (x, y)
  206.     {
  207.         if (x === undefined) { x = 0; }
  208.         if (y === undefined) { y = x; }
  209.  
  210.         this._tempVec2.set(x, y);
  211.  
  212.         Body.setPosition(this.body, this._tempVec2);
  213.  
  214.         return this;
  215.     },
  216.  
  217.     /**
  218.      * Immediately sets the angle of the Body.
  219.      * Angular velocity, position, force etc. are unchanged.
  220.      *
  221.      * @method Phaser.Physics.Matter.Components.Transform#setRotation
  222.      * @since 3.0.0
  223.      *
  224.      * @param {number} [radians=0] - The angle of the body, in radians.
  225.      *
  226.      * @return {this} This Game Object.
  227.      */
  228.     setRotation: function (radians)
  229.     {
  230.         if (radians === undefined) { radians = 0; }
  231.  
  232.         this._rotation = WrapAngle(radians);
  233.  
  234.         Body.setAngle(this.body, radians);
  235.  
  236.         return this;
  237.     },
  238.  
  239.     /**
  240.      * Setting fixed rotation sets the Body inertia to Infinity, which stops it
  241.      * from being able to rotate when forces are applied to it.
  242.      *
  243.      * @method Phaser.Physics.Matter.Components.Transform#setFixedRotation
  244.      * @since 3.0.0
  245.      *
  246.      * @return {this} This Game Object.
  247.      */
  248.     setFixedRotation: function ()
  249.     {
  250.         Body.setInertia(this.body, Infinity);
  251.  
  252.         return this;
  253.     },
  254.  
  255.     /**
  256.      * Immediately sets the angle of the Body.
  257.      * Angular velocity, position, force etc. are unchanged.
  258.      *
  259.      * @method Phaser.Physics.Matter.Components.Transform#setAngle
  260.      * @since 3.0.0
  261.      *
  262.      * @param {number} [degrees=0] - The angle to set, in degrees.
  263.      *
  264.      * @return {this} This Game Object.
  265.      */
  266.     setAngle: function (degrees)
  267.     {
  268.         if (degrees === undefined) { degrees = 0; }
  269.  
  270.         this.angle = degrees;
  271.  
  272.         Body.setAngle(this.body, this.rotation);
  273.  
  274.         return this;
  275.     },
  276.  
  277.     /**
  278.      * Sets the scale of this Game Object.
  279.      *
  280.      * @method Phaser.Physics.Matter.Components.Transform#setScale
  281.      * @since 3.0.0
  282.      *
  283.      * @param {number} [x=1] - The horizontal scale of this Game Object.
  284.      * @param {number} [y=x] - The vertical scale of this Game Object. If not set it will use the x value.
  285.      * @param {Phaser.Math.Vector2} [point] - The point (Vector2) from which scaling will occur.
  286.      *
  287.      * @return {this} This Game Object.
  288.      */
  289.     setScale: function (x, y, point)
  290.     {
  291.         if (x === undefined) { x = 1; }
  292.         if (y === undefined) { y = x; }
  293.  
  294.         var factorX = 1 / this._scaleX;
  295.         var factorY = 1 / this._scaleY;
  296.  
  297.         this._scaleX = x;
  298.         this._scaleY = y;
  299.  
  300.         Body.scale(this.body, factorX, factorY, point);
  301.  
  302.         Body.scale(this.body, x, y, point);
  303.  
  304.         return this;
  305.     }
  306.  
  307. };
  308.  
  309. module.exports = Transform;

Raw Paste


Login or Register to edit or fork this paste. It's free.