|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Between = require('../../math/Between'); |
| 8 | +var ContainsRect = require('./ContainsRect'); |
| 9 | +var Point = require('../point/Point'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Calculates a random point that lies within the `outer` Rectangle, but outside of the `inner` Rectangle. |
| 13 | + * The inner Rectangle must be fully contained within the outer rectangle. |
| 14 | + * |
| 15 | + * @function Phaser.Geom.Rectangle.RandomOutside |
| 16 | + * @since 3.10.0 |
| 17 | + * |
| 18 | + * @generic {Phaser.Geom.Point} O - [out,$return] |
| 19 | + * |
| 20 | + * @param {Phaser.Geom.Rectangle} outer - The outer Rectangle to get the random point within. |
| 21 | + * @param {Phaser.Geom.Rectangle} inner - The inner Rectangle to exclude from the returned point. |
| 22 | + * @param {Phaser.Geom.Point} [out] - A Point, or Point-like object to store the result in. If not specified, a new Point will be created. |
| 23 | + * |
| 24 | + * @return {Phaser.Geom.Point} A Point object containing the random values in its `x` and `y` properties. |
| 25 | + */ |
| 26 | +var RandomOutside = function (outer, inner, out) |
| 27 | +{ |
| 28 | + if (out === undefined) { out = new Point(); } |
| 29 | + |
| 30 | + if (ContainsRect(outer, inner)) |
| 31 | + { |
| 32 | + // Pick a random quadrant |
| 33 | + // |
| 34 | + // The quadrants don't extend the full widths / heights of the outer rect to give |
| 35 | + // us a better uniformed distribution, otherwise you get clumping in the corners where |
| 36 | + // the 4 quads would overlap |
| 37 | + |
| 38 | + switch (Between(0, 3)) |
| 39 | + { |
| 40 | + // Top |
| 41 | + case 0: |
| 42 | + out.x = outer.x + (Math.random() * (inner.right - outer.x)); |
| 43 | + out.y = outer.y + (Math.random() * (inner.top - outer.y)); |
| 44 | + break; |
| 45 | + |
| 46 | + // Bottom |
| 47 | + case 1: |
| 48 | + out.x = inner.x + (Math.random() * (outer.right - inner.x)); |
| 49 | + out.y = inner.bottom + (Math.random() * (outer.bottom - inner.bottom)); |
| 50 | + break; |
| 51 | + |
| 52 | + // Left |
| 53 | + case 2: |
| 54 | + out.x = outer.x + (Math.random() * (inner.x - outer.x)); |
| 55 | + out.y = inner.y + (Math.random() * (outer.bottom - inner.y)); |
| 56 | + break; |
| 57 | + |
| 58 | + // Right |
| 59 | + case 3: |
| 60 | + out.x = inner.right + (Math.random() * (outer.right - inner.right)); |
| 61 | + out.y = outer.y + (Math.random() * (inner.bottom - outer.y)); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return out; |
| 67 | +}; |
| 68 | + |
| 69 | +module.exports = RandomOutside; |
0 commit comments