1+ /*!
2+ * matter-wrap 0.1.2 by Liam Brummitt 2017-02-12
3+ * https://github.com/liabru/matter-wrap
4+ * License MIT
5+ */
6+ ( function webpackUniversalModuleDefinition ( root , factory ) {
7+ if ( typeof exports === 'object' && typeof module === 'object' )
8+ module . exports = factory ( require ( "Matter" ) ) ;
9+ else if ( typeof define === 'function' && define . amd )
10+ define ( [ "Matter" ] , factory ) ;
11+ else if ( typeof exports === 'object' )
12+ exports [ "MatterWrap" ] = factory ( require ( "Matter" ) ) ;
13+ else
14+ root [ "MatterWrap" ] = factory ( root [ "Matter" ] ) ;
15+ } ) ( this , function ( __WEBPACK_EXTERNAL_MODULE_0__ ) {
16+ return /******/ ( function ( modules ) { // webpackBootstrap
17+ /******/ // The module cache
18+ /******/ var installedModules = { } ;
19+
20+ /******/ // The require function
21+ /******/ function __webpack_require__ ( moduleId ) {
22+
23+ /******/ // Check if module is in cache
24+ /******/ if ( installedModules [ moduleId ] )
25+ /******/ return installedModules [ moduleId ] . exports ;
26+
27+ /******/ // Create a new module (and put it into the cache)
28+ /******/ var module = installedModules [ moduleId ] = {
29+ /******/ i : moduleId ,
30+ /******/ l : false ,
31+ /******/ exports : { }
32+ /******/ } ;
33+
34+ /******/ // Execute the module function
35+ /******/ modules [ moduleId ] . call ( module . exports , module , module . exports , __webpack_require__ ) ;
36+
37+ /******/ // Flag the module as loaded
38+ /******/ module . l = true ;
39+
40+ /******/ // Return the exports of the module
41+ /******/ return module . exports ;
42+ /******/ }
43+
44+
45+ /******/ // expose the modules object (__webpack_modules__)
46+ /******/ __webpack_require__ . m = modules ;
47+
48+ /******/ // expose the module cache
49+ /******/ __webpack_require__ . c = installedModules ;
50+
51+ /******/ // identity function for calling harmony imports with the correct context
52+ /******/ __webpack_require__ . i = function ( value ) { return value ; } ;
53+
54+ /******/ // define getter function for harmony exports
55+ /******/ __webpack_require__ . d = function ( exports , name , getter ) {
56+ /******/ if ( ! __webpack_require__ . o ( exports , name ) ) {
57+ /******/ Object . defineProperty ( exports , name , {
58+ /******/ configurable : false ,
59+ /******/ enumerable : true ,
60+ /******/ get : getter
61+ /******/ } ) ;
62+ /******/ }
63+ /******/ } ;
64+
65+ /******/ // getDefaultExport function for compatibility with non-harmony modules
66+ /******/ __webpack_require__ . n = function ( module ) {
67+ /******/ var getter = module && module . __esModule ?
68+ /******/ function getDefault ( ) { return module [ 'default' ] ; } :
69+ /******/ function getModuleExports ( ) { return module ; } ;
70+ /******/ __webpack_require__ . d ( getter , 'a' , getter ) ;
71+ /******/ return getter ;
72+ /******/ } ;
73+
74+ /******/ // Object.prototype.hasOwnProperty.call
75+ /******/ __webpack_require__ . o = function ( object , property ) { return Object . prototype . hasOwnProperty . call ( object , property ) ; } ;
76+
77+ /******/ // __webpack_public_path__
78+ /******/ __webpack_require__ . p = "/libs" ;
79+
80+ /******/ // Load entry module and return exports
81+ /******/ return __webpack_require__ ( __webpack_require__ . s = 1 ) ;
82+ /******/ } )
83+ /************************************************************************/
84+ /******/ ( [
85+ /* 0 */
86+ /***/ ( function ( module , exports ) {
87+
88+ module . exports = __WEBPACK_EXTERNAL_MODULE_0__ ;
89+
90+ /***/ } ) ,
91+ /* 1 */
92+ /***/ ( function ( module , exports , __webpack_require__ ) {
93+
94+ "use strict" ;
95+
96+
97+ var Matter = __webpack_require__ ( 0 ) ;
98+
99+ /**
100+ * A coordinate wrapping plugin for matter.js.
101+ * See the readme for usage and examples.
102+ * @module MatterWrap
103+ */
104+ var MatterWrap = {
105+ // plugin meta
106+ name : 'matter-wrap' , // PLUGIN_NAME
107+ version : '0.1.0' , // PLUGIN_VERSION
108+ for : 'matter-js@^0.12.0' ,
109+
110+ // installs the plugin where `base` is `Matter`
111+ // you should not need to call this directly.
112+ install : function install ( base ) {
113+ base . after ( 'Engine.update' , function ( ) {
114+ MatterWrap . Engine . update ( this ) ;
115+ } ) ;
116+ } ,
117+
118+ Engine : {
119+ /**
120+ * Updates the engine by wrapping bodies inside `engine.world`.
121+ * This is called automatically by the plugin.
122+ * @function MatterWrap.Engine.update
123+ * @param {Matter.Engine } engine The engine to update.
124+ * @returns {void } No return value.
125+ */
126+ update : function update ( engine ) {
127+ var world = engine . world ,
128+ bodies = Matter . Composite . allBodies ( world ) ;
129+
130+ for ( var i = 0 ; i < bodies . length ; i += 1 ) {
131+ var body = bodies [ i ] ;
132+
133+ if ( body . plugin . wrap ) {
134+ MatterWrap . Body . wrap ( body , body . plugin . wrap ) ;
135+ }
136+ }
137+ }
138+ } ,
139+
140+ Body : {
141+ /**
142+ * Wraps the `body` position such that it always stay within the given bounds.
143+ * Upon crossing a boundary the body will appear on the opposite side of the bounds,
144+ * while maintaining its velocity.
145+ * This is called automatically by the plugin.
146+ * @function MatterAttractors.Body.wrap
147+ * @param {Matter.Body } body The body to wrap.
148+ * @param {Matter.Bounds } bounds The bounds to wrap the body inside.
149+ * @returns {void } No return value.
150+ */
151+ wrap : function wrap ( body , bounds ) {
152+ var x = null ,
153+ y = null ;
154+
155+ if ( typeof bounds . min . x !== 'undefined' && typeof bounds . max . x !== 'undefined' ) {
156+ if ( body . bounds . min . x > bounds . max . x ) {
157+ x = bounds . min . x - ( body . bounds . max . x - body . position . x ) ;
158+ } else if ( body . bounds . max . x < bounds . min . x ) {
159+ x = bounds . max . x - ( body . bounds . min . x - body . position . x ) ;
160+ }
161+ }
162+
163+ if ( typeof bounds . min . y !== 'undefined' && typeof bounds . max . y !== 'undefined' ) {
164+ if ( body . bounds . min . y > bounds . max . y ) {
165+ y = bounds . min . y - ( body . bounds . max . y - body . position . y ) ;
166+ } else if ( body . bounds . max . y < bounds . min . y ) {
167+ y = bounds . max . y - ( body . bounds . min . y - body . position . y ) ;
168+ }
169+ }
170+
171+ if ( x !== null || y !== null ) {
172+ Matter . Body . setPosition ( body , {
173+ x : x || body . position . x ,
174+ y : y || body . position . y
175+ } ) ;
176+ }
177+ }
178+ }
179+ } ;
180+
181+ Matter . Plugin . register ( MatterWrap ) ;
182+
183+ module . exports = MatterWrap ;
184+
185+ /**
186+ * @namespace Matter.Body
187+ * @see http://brm.io/matter-js/docs/classes/Body.html
188+ */
189+
190+ /**
191+ * This plugin adds a new property `body.plugin.wrap` to instances of `Matter.Body`.
192+ * This is a `Matter.Bounds` instance that specifies the wrapping region.
193+ * @property {Matter.Bounds } body.plugin.wrap
194+ * @memberof Matter.Body
195+ */
196+
197+ /***/ } )
198+ /******/ ] ) ;
199+ } ) ;
0 commit comments