JAVASCRIPT   9

SetBody.js

Guest on 31st May 2021 02:32:07 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 Bodies = require('../lib/factory/Bodies');
  8. var Body = require('../lib/body/Body');
  9. var FuzzyEquals = require('../../../math/fuzzy/Equal');
  10. var GetFastValue = require('../../../utils/object/GetFastValue');
  11. var PhysicsEditorParser = require('../PhysicsEditorParser');
  12. var PhysicsJSONParser = require('../PhysicsJSONParser');
  13. var Vertices = require('../lib/geometry/Vertices');
  14.  
  15. /**
  16.  * Enables a Matter-enabled Game Object to set its Body. Should be used as a mixin and not directly.
  17.  *
  18.  * @namespace Phaser.Physics.Matter.Components.SetBody
  19.  * @since 3.0.0
  20.  */
  21. var SetBody = {
  22.  
  23.     /**
  24.      * Set the body on a Game Object to a rectangle.
  25.      *
  26.      * Calling this methods resets previous properties you may have set on the body, including
  27.      * plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
  28.      *
  29.      * @method Phaser.Physics.Matter.Components.SetBody#setRectangle
  30.      * @since 3.0.0
  31.      *
  32.      * @param {number} width - Width of the rectangle.
  33.      * @param {number} height - Height of the rectangle.
  34.      * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
  35.      *
  36.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  37.      */
  38.     setRectangle: function (width, height, options)
  39.     {
  40.         return this.setBody({ type: 'rectangle', width: width, height: height }, options);
  41.     },
  42.  
  43.     /**
  44.      * Set the body on a Game Object to a circle.
  45.      *
  46.      * Calling this methods resets previous properties you may have set on the body, including
  47.      * plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
  48.      *
  49.      * @method Phaser.Physics.Matter.Components.SetBody#setCircle
  50.      * @since 3.0.0
  51.      *
  52.      * @param {number} radius - The radius of the circle.
  53.      * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
  54.      *
  55.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  56.      */
  57.     setCircle: function (radius, options)
  58.     {
  59.         return this.setBody({ type: 'circle', radius: radius }, options);
  60.     },
  61.  
  62.     /**
  63.      * Set the body on the Game Object to a polygon shape.
  64.      *
  65.      * Calling this methods resets previous properties you may have set on the body, including
  66.      * plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
  67.      *
  68.      * @method Phaser.Physics.Matter.Components.SetBody#setPolygon
  69.      * @since 3.0.0
  70.      *
  71.      * @param {number} sides - The number of sides the polygon will have.
  72.      * @param {number} radius - The "radius" of the polygon, i.e. the distance from its center to any vertex. This is also the radius of its circumcircle.
  73.      * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
  74.      *
  75.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  76.      */
  77.     setPolygon: function (radius, sides, options)
  78.     {
  79.         return this.setBody({ type: 'polygon', sides: sides, radius: radius }, options);
  80.     },
  81.  
  82.     /**
  83.      * Set the body on the Game Object to a trapezoid shape.
  84.      *
  85.      * Calling this methods resets previous properties you may have set on the body, including
  86.      * plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
  87.      *
  88.      * @method Phaser.Physics.Matter.Components.SetBody#setTrapezoid
  89.      * @since 3.0.0
  90.      *
  91.      * @param {number} width - The width of the trapezoid Body.
  92.      * @param {number} height - The height of the trapezoid Body.
  93.      * @param {number} slope - The slope of the trapezoid. 0 creates a rectangle, while 1 creates a triangle. Positive values make the top side shorter, while negative values make the bottom side shorter.
  94.      * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
  95.      *
  96.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  97.      */
  98.     setTrapezoid: function (width, height, slope, options)
  99.     {
  100.         return this.setBody({ type: 'trapezoid', width: width, height: height, slope: slope }, options);
  101.     },
  102.  
  103.     /**
  104.      * Set this Game Object to use the given existing Matter Body.
  105.      *
  106.      * The body is first removed from the world before being added to this Game Object.
  107.      *
  108.      * @method Phaser.Physics.Matter.Components.SetBody#setExistingBody
  109.      * @since 3.0.0
  110.      *
  111.      * @param {MatterJS.BodyType} body - The Body this Game Object should use.
  112.      * @param {boolean} [addToWorld=true] - Should the body be immediately added to the World?
  113.      *
  114.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  115.      */
  116.     setExistingBody: function (body, addToWorld)
  117.     {
  118.         if (addToWorld === undefined) { addToWorld = true; }
  119.  
  120.         if (this.body)
  121.         {
  122.             this.world.remove(this.body, true);
  123.         }
  124.  
  125.         this.body = body;
  126.  
  127.         for (var i = 0; i < body.parts.length; i++)
  128.         {
  129.             body.parts[i].gameObject = this;
  130.         }
  131.  
  132.         var _this = this;
  133.  
  134.         body.destroy = function destroy ()
  135.         {
  136.             _this.world.remove(_this.body, true);
  137.             _this.body.gameObject = null;
  138.         };
  139.  
  140.         if (addToWorld)
  141.         {
  142.             if (this.world.has(body))
  143.             {
  144.                 //  Because it could be part of another Composite
  145.                 this.world.remove(body, true);
  146.             }
  147.  
  148.             this.world.add(body);
  149.         }
  150.  
  151.         if (this._originComponent)
  152.         {
  153.             var rx = body.render.sprite.xOffset;
  154.             var ry = body.render.sprite.yOffset;
  155.  
  156.             var comx = body.centerOfMass.x;
  157.             var comy = body.centerOfMass.y;
  158.  
  159.             if (FuzzyEquals(comx, 0.5) && FuzzyEquals(comy, 0.5))
  160.             {
  161.                 this.setOrigin(rx + 0.5, ry + 0.5);
  162.             }
  163.             else
  164.             {
  165.                 var cx = body.centerOffset.x;
  166.                 var cy = body.centerOffset.y;
  167.  
  168.                 this.setOrigin(rx + (cx / this.displayWidth), ry + (cy / this.displayHeight));
  169.             }
  170.         }
  171.  
  172.         return this;
  173.     },
  174.  
  175.     /**
  176.      * Set this Game Object to create and use a new Body based on the configuration object given.
  177.      *
  178.      * Calling this method resets previous properties you may have set on the body, including
  179.      * plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
  180.      *
  181.      * @method Phaser.Physics.Matter.Components.SetBody#setBody
  182.      * @since 3.0.0
  183.      *
  184.      * @param {(string|Phaser.Types.Physics.Matter.MatterSetBodyConfig)} config - Either a string, such as `circle`, or a Matter Set Body Configuration object.
  185.      * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
  186.      *
  187.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  188.      */
  189.     setBody: function (config, options)
  190.     {
  191.         if (!config)
  192.         {
  193.             return this;
  194.         }
  195.  
  196.         var body;
  197.  
  198.         //  Allow them to do: shape: 'circle' instead of shape: { type: 'circle' }
  199.         if (typeof config === 'string')
  200.         {
  201.             //  Using defaults
  202.             config = { type: config };
  203.         }
  204.  
  205.         var shapeType = GetFastValue(config, 'type', 'rectangle');
  206.         var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
  207.         var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
  208.         var bodyWidth = GetFastValue(config, 'width', this.width);
  209.         var bodyHeight = GetFastValue(config, 'height', this.height);
  210.  
  211.         switch (shapeType)
  212.         {
  213.             case 'rectangle':
  214.                 body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
  215.                 break;
  216.  
  217.             case 'circle':
  218.                 var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
  219.                 var maxSides = GetFastValue(config, 'maxSides', 25);
  220.                 body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
  221.                 break;
  222.  
  223.             case 'trapezoid':
  224.                 var slope = GetFastValue(config, 'slope', 0.5);
  225.                 body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
  226.                 break;
  227.  
  228.             case 'polygon':
  229.                 var sides = GetFastValue(config, 'sides', 5);
  230.                 var pRadius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
  231.                 body = Bodies.polygon(bodyX, bodyY, sides, pRadius, options);
  232.                 break;
  233.  
  234.             case 'fromVertices':
  235.             case 'fromVerts':
  236.  
  237.                 var verts = GetFastValue(config, 'verts', null);
  238.  
  239.                 if (verts)
  240.                 {
  241.                     //  Has the verts array come from Vertices.fromPath, or is it raw?
  242.                     if (typeof verts === 'string')
  243.                     {
  244.                         verts = Vertices.fromPath(verts);
  245.                     }
  246.  
  247.                     if (this.body && !this.body.hasOwnProperty('temp'))
  248.                     {
  249.                         Body.setVertices(this.body, verts);
  250.  
  251.                         body = this.body;
  252.                     }
  253.                     else
  254.                     {
  255.                         var flagInternal = GetFastValue(config, 'flagInternal', false);
  256.                         var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
  257.                         var minimumArea = GetFastValue(config, 'minimumArea', 10);
  258.    
  259.                         body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
  260.                     }
  261.                 }
  262.  
  263.                 break;
  264.  
  265.             case 'fromPhysicsEditor':
  266.                 body = PhysicsEditorParser.parseBody(bodyX, bodyY, config, options);
  267.                 break;
  268.  
  269.             case 'fromPhysicsTracer':
  270.                 body = PhysicsJSONParser.parseBody(bodyX, bodyY, config, options);
  271.                 break;
  272.         }
  273.  
  274.         if (body)
  275.         {
  276.             this.setExistingBody(body, config.addToWorld);
  277.         }
  278.  
  279.         return this;
  280.     }
  281.  
  282. };
  283.  
  284. module.exports = SetBody;

Raw Paste


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