CCActionGrid3D.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * cc.Waves3D action. <br />
  24. * Reference the test cases (Effects Advanced Test)
  25. * @class
  26. * @extends cc.Grid3DAction
  27. * @param {Number} duration
  28. * @param {cc.Size} gridSize
  29. * @param {Number} waves
  30. * @param {Number} amplitude
  31. */
  32. cc.Waves3D = cc.Grid3DAction.extend(/** @lends cc.Waves3D# */{
  33. _waves: 0,
  34. _amplitude: 0,
  35. _amplitudeRate: 0,
  36. /**
  37. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  38. * Create a wave 3d action with duration, grid size, waves and amplitude.
  39. * @param {Number} duration
  40. * @param {cc.Size} gridSize
  41. * @param {Number} waves
  42. * @param {Number} amplitude
  43. */
  44. ctor:function (duration, gridSize, waves, amplitude) {
  45. cc.GridAction.prototype.ctor.call(this);
  46. amplitude !== undefined && this.initWithDuration(duration, gridSize, waves, amplitude);
  47. },
  48. /**
  49. * get Amplitude
  50. * @return {Number}
  51. */
  52. getAmplitude:function () {
  53. return this._amplitude;
  54. },
  55. /**
  56. * set Amplitude
  57. * @param {Number} amplitude
  58. */
  59. setAmplitude:function (amplitude) {
  60. this._amplitude = amplitude;
  61. },
  62. /**
  63. * get Amplitude Rate
  64. * @return {Number}
  65. */
  66. getAmplitudeRate:function () {
  67. return this._amplitudeRate;
  68. },
  69. /**
  70. * set Amplitude Rate
  71. * @param {Number} amplitudeRate
  72. */
  73. setAmplitudeRate:function (amplitudeRate) {
  74. this._amplitudeRate = amplitudeRate;
  75. },
  76. /**
  77. * initializes an action with duration, grid size, waves and amplitude
  78. * @param {Number} duration
  79. * @param {cc.Size} gridSize
  80. * @param {Number} waves
  81. * @param {Number} amplitude
  82. * @return {Boolean}
  83. */
  84. initWithDuration:function (duration, gridSize, waves, amplitude) {
  85. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  86. this._waves = waves;
  87. this._amplitude = amplitude;
  88. this._amplitudeRate = 1.0;
  89. return true;
  90. }
  91. return false;
  92. },
  93. /**
  94. * Called once per frame. Time is the number of seconds of a frame interval.
  95. *
  96. * @param {Number} dt
  97. */
  98. update:function (dt) {
  99. var locGridSize = this._gridSize;
  100. var locAmplitude = this._amplitude, locPos = cc.p(0, 0);
  101. var locAmplitudeRate = this._amplitudeRate, locWaves = this._waves;
  102. for (var i = 0; i < locGridSize.width + 1; ++i) {
  103. for (var j = 0; j < locGridSize.height + 1; ++j) {
  104. locPos.x = i;
  105. locPos.y = j;
  106. var v = this.originalVertex(locPos);
  107. v.z += (Math.sin(Math.PI * dt * locWaves * 2 + (v.y + v.x) * 0.01) * locAmplitude * locAmplitudeRate);
  108. //cc.log("v.z offset is" + (Math.sin(Math.PI * dt * this._waves * 2 + (v.y + v.x) * 0.01) * this._amplitude * this._amplitudeRate));
  109. this.setVertex(locPos, v);
  110. }
  111. }
  112. }
  113. });
  114. /**
  115. * Create a wave 3d action with duration, grid size, waves and amplitude.
  116. * @function
  117. * @param {Number} duration
  118. * @param {cc.Size} gridSize
  119. * @param {Number} waves
  120. * @param {Number} amplitude
  121. */
  122. cc.waves3D = function (duration, gridSize, waves, amplitude) {
  123. return new cc.Waves3D(duration, gridSize, waves, amplitude);
  124. };
  125. /**
  126. * Please use cc.waves3D instead. <br />
  127. * Create a wave 3d action with duration, grid size, waves and amplitude.
  128. * @param {Number} duration
  129. * @param {cc.Size} gridSize
  130. * @param {Number} waves
  131. * @param {Number} amplitude
  132. * @static
  133. * @deprecated since v3.0 <br /> Please use cc.waves3D instead.
  134. */
  135. cc.Waves3D.create = cc.waves3D;
  136. /**
  137. * cc.FlipX3D action. <br />
  138. * Flip around. <br />
  139. * Reference the test cases (Effects Test)
  140. * @class
  141. * @extends cc.Grid3DAction
  142. * @param {Number} duration
  143. */
  144. cc.FlipX3D = cc.Grid3DAction.extend(/** @lends cc.FlipX3D# */{
  145. /**
  146. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  147. * Create a Flip X 3D action with duration.
  148. * @param {Number} duration
  149. */
  150. ctor: function(duration) {
  151. if (duration !== undefined)
  152. cc.GridAction.prototype.ctor.call(this, duration, cc.size(1, 1));
  153. else cc.GridAction.prototype.ctor.call(this);
  154. },
  155. /**
  156. * initializes the action with duration
  157. * @param {Number} duration
  158. * @return {Boolean}
  159. */
  160. initWithDuration:function (duration) {
  161. return cc.Grid3DAction.prototype.initWithDuration.call(this, duration, cc.size(1, 1));
  162. },
  163. /**
  164. * initializes the action with gridSize and duration
  165. * @param {cc.Size} gridSize
  166. * @param {Number} duration
  167. * @return {Boolean}
  168. */
  169. initWithSize:function (gridSize, duration) {
  170. if (gridSize.width != 1 || gridSize.height != 1) {
  171. // Grid size must be (1,1)
  172. cc.log("Grid size must be (1,1)");
  173. return false;
  174. }
  175. return cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize);
  176. },
  177. /**
  178. * Called once per frame. Time is the number of seconds of a frame interval.
  179. *
  180. * @param {Number} dt
  181. */
  182. update:function (dt) {
  183. var angle = Math.PI * dt; // 180 degrees
  184. var mz = Math.sin(angle);
  185. angle = angle / 2.0; // x calculates degrees from 0 to 90
  186. var mx = Math.cos(angle);
  187. var diff = new cc.Vertex3F();
  188. var tempVer = cc.p(0, 0);
  189. tempVer.x = tempVer.y = 1;
  190. var v0 = this.originalVertex(tempVer);
  191. tempVer.x = tempVer.y = 0;
  192. var v1 = this.originalVertex(tempVer);
  193. var x0 = v0.x;
  194. var x1 = v1.x;
  195. var x;
  196. var a, b, c, d;
  197. if (x0 > x1) {
  198. // Normal Grid
  199. a = cc.p(0, 0);
  200. b = cc.p(0, 1);
  201. c = cc.p(1, 0);
  202. d = cc.p(1, 1);
  203. x = x0;
  204. } else {
  205. // Reversed Grid
  206. c = cc.p(0, 0);
  207. d = cc.p(0, 1);
  208. a = cc.p(1, 0);
  209. b = cc.p(1, 1);
  210. x = x1;
  211. }
  212. diff.x = ( x - x * mx );
  213. diff.z = Math.abs(parseFloat((x * mz) / 4.0));
  214. // bottom-left
  215. var v = this.originalVertex(a);
  216. v.x = diff.x;
  217. v.z += diff.z;
  218. this.setVertex(a, v);
  219. // upper-left
  220. v = this.originalVertex(b);
  221. v.x = diff.x;
  222. v.z += diff.z;
  223. this.setVertex(b, v);
  224. // bottom-right
  225. v = this.originalVertex(c);
  226. v.x -= diff.x;
  227. v.z -= diff.z;
  228. this.setVertex(c, v);
  229. // upper-right
  230. v = this.originalVertex(d);
  231. v.x -= diff.x;
  232. v.z -= diff.z;
  233. this.setVertex(d, v);
  234. }
  235. });
  236. /**
  237. * Create a Flip X 3D action with duration. <br />
  238. * Flip around.
  239. * @function
  240. * @param {Number} duration
  241. * @return {cc.FlipX3D}
  242. */
  243. cc.flipX3D = function (duration) {
  244. return new cc.FlipX3D(duration);
  245. };
  246. /**
  247. * Please use cc.flipX3D instead. <br />
  248. * Create a Flip X 3D action with duration. <br />
  249. * Flip around.
  250. * @param {Number} duration
  251. * @return {cc.FlipX3D}
  252. * @static
  253. * @deprecated since v3.0 <br /> Please use cc.flipX3D instead.
  254. */
  255. cc.FlipX3D.create = cc.flipX3D;
  256. /**
  257. * cc.FlipY3D action. <br />
  258. * Upside down. <br />
  259. * Reference the test cases (Effects Test)
  260. * @class
  261. * @extends cc.FlipX3D
  262. * @param {Number} duration
  263. */
  264. cc.FlipY3D = cc.FlipX3D.extend(/** @lends cc.FlipY3D# */{
  265. /**
  266. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  267. * Create a flip Y 3d action with duration.
  268. * @param {Number} duration
  269. */
  270. ctor: function(duration) {
  271. if (duration !== undefined)
  272. cc.GridAction.prototype.ctor.call(this, duration, cc.size(1, 1));
  273. else cc.GridAction.prototype.ctor.call(this);
  274. },
  275. /**
  276. * Called once per frame. Time is the number of seconds of a frame interval.
  277. *
  278. * @param {Number} dt
  279. */
  280. update:function (dt) {
  281. var angle = Math.PI * dt; // 180 degrees
  282. var mz = Math.sin(angle);
  283. angle = angle / 2.0; // x calculates degrees from 0 to 90
  284. var my = Math.cos(angle);
  285. var diff = new cc.Vertex3F();
  286. var tempP = cc.p(0, 0);
  287. tempP.x = tempP.y = 1;
  288. var v0 = this.originalVertex(tempP);
  289. tempP.x = tempP.y = 0;
  290. var v1 = this.originalVertex(tempP);
  291. var y0 = v0.y;
  292. var y1 = v1.y;
  293. var y;
  294. var a, b, c, d;
  295. if (y0 > y1) {
  296. // Normal Grid
  297. a = cc.p(0, 0);
  298. b = cc.p(0, 1);
  299. c = cc.p(1, 0);
  300. d = cc.p(1, 1);
  301. y = y0;
  302. } else {
  303. // Reversed Grid
  304. b = cc.p(0, 0);
  305. a = cc.p(0, 1);
  306. d = cc.p(1, 0);
  307. c = cc.p(1, 1);
  308. y = y1;
  309. }
  310. diff.y = y - y * my;
  311. diff.z = Math.abs(parseFloat(y * mz) / 4.0);
  312. // bottom-left
  313. var v = this.originalVertex(a);
  314. v.y = diff.y;
  315. v.z += diff.z;
  316. this.setVertex(a, v);
  317. // upper-left
  318. v = this.originalVertex(b);
  319. v.y -= diff.y;
  320. v.z -= diff.z;
  321. this.setVertex(b, v);
  322. // bottom-right
  323. v = this.originalVertex(c);
  324. v.y = diff.y;
  325. v.z += diff.z;
  326. this.setVertex(c, v);
  327. // upper-right
  328. v = this.originalVertex(d);
  329. v.y -= diff.y;
  330. v.z -= diff.z;
  331. this.setVertex(d, v);
  332. }
  333. });
  334. /**
  335. * Create a flip Y 3d action with duration. <br />
  336. * Upside down.
  337. * @function
  338. * @param {Number} duration
  339. * @return {cc.FlipY3D}
  340. */
  341. cc.flipY3D = function (duration) {
  342. return new cc.FlipY3D(duration);
  343. };
  344. /**
  345. * Please use cc.flipY3D instead. <br />
  346. * Create a flip Y 3d action with duration.
  347. * @param {Number} duration
  348. * @return {cc.FlipY3D}
  349. * @static
  350. * @deprecated since v3.0 <br /> Please use cc.flipY3D instead.
  351. */
  352. cc.FlipY3D.create = cc.flipY3D;
  353. /**
  354. * cc.Lens3D action. <br />
  355. * Upside down. <br />
  356. * Reference the test cases (Effects Test)
  357. * @class
  358. * @extends cc.FlipX3D
  359. * @param {Number} duration
  360. * @param {cc.Size} gridSize
  361. * @param {cc.Point} position
  362. * @param {Number} radius
  363. */
  364. cc.Lens3D = cc.Grid3DAction.extend(/** @lends cc.Lens3D# */{
  365. //lens center position
  366. _position:null,
  367. _radius:0,
  368. //lens effect. Defaults to 0.7 - 0 means no effect, 1 is very strong effect
  369. _lensEffect:0,
  370. //lens is concave. (true = concave, false = convex) default is convex i.e. false
  371. _concave:false,
  372. _dirty:false,
  373. /**
  374. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  375. * creates a lens 3d action with center position, radius.
  376. * @param {Number} duration
  377. * @param {cc.Size} gridSize
  378. * @param {cc.Point} position
  379. * @param {Number} radius
  380. */
  381. ctor:function (duration, gridSize, position, radius) {
  382. cc.GridAction.prototype.ctor.call(this);
  383. this._position = cc.p(0, 0);
  384. radius !== undefined && this.initWithDuration(duration, gridSize, position, radius);
  385. },
  386. /**
  387. * Get lens center position
  388. * @return {Number}
  389. */
  390. getLensEffect:function () {
  391. return this._lensEffect;
  392. },
  393. /**
  394. * Set lens center position
  395. * @param {Number} lensEffect
  396. */
  397. setLensEffect:function (lensEffect) {
  398. this._lensEffect = lensEffect;
  399. },
  400. /**
  401. * Set whether lens is concave
  402. * @param {Boolean} concave
  403. */
  404. setConcave:function (concave) {
  405. this._concave = concave;
  406. },
  407. /**
  408. * get Position
  409. * @return {cc.Point}
  410. */
  411. getPosition:function () {
  412. return this._position;
  413. },
  414. /**
  415. * set Position
  416. * @param {cc.Point} position
  417. */
  418. setPosition:function (position) {
  419. if (!cc.pointEqualToPoint(position, this._position)) {
  420. this._position.x = position.x;
  421. this._position.y = position.y;
  422. this._dirty = true;
  423. }
  424. },
  425. /**
  426. * initializes the action with center position, radius, a grid size and duration
  427. * @param {Number} duration
  428. * @param {cc.Size} gridSize
  429. * @param {cc.Point} position
  430. * @param {Number} radius
  431. * @return {Boolean}
  432. */
  433. initWithDuration:function (duration, gridSize, position, radius) {
  434. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  435. this.setPosition(position);
  436. this._radius = radius;
  437. this._lensEffect = 0.7;
  438. this._dirty = true;
  439. return true;
  440. }
  441. return false;
  442. },
  443. /**
  444. * Called once per frame. Time is the number of seconds of a frame interval.
  445. *
  446. * @param {Number} dt
  447. */
  448. update:function (dt) {
  449. if (this._dirty) {
  450. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  451. var locRadius = this._radius, locLensEffect = this._lensEffect;
  452. var locPos = cc.p(0, 0);
  453. var vect = cc.p(0, 0);
  454. var v, r, l, new_r, pre_log;
  455. for (var i = 0; i < locGridSizeWidth + 1; ++i) {
  456. for (var j = 0; j < locGridSizeHeight + 1; ++j) {
  457. locPos.x = i;
  458. locPos.y = j;
  459. v = this.originalVertex(locPos);
  460. vect.x = this._position.x - v.x;
  461. vect.y = this._position.y - v.y;
  462. r = cc.pLength(vect);
  463. if (r < locRadius) {
  464. r = locRadius - r;
  465. pre_log = r / locRadius;
  466. if (pre_log == 0)
  467. pre_log = 0.001;
  468. l = Math.log(pre_log) * locLensEffect;
  469. new_r = Math.exp(l) * locRadius;
  470. r = cc.pLength(vect);
  471. if (r > 0) {
  472. vect.x = vect.x / r;
  473. vect.y = vect.y / r;
  474. vect.x = vect.x * new_r;
  475. vect.y = vect.y * new_r;
  476. v.z += cc.pLength(vect) * locLensEffect;
  477. }
  478. }
  479. this.setVertex(locPos, v);
  480. }
  481. }
  482. this._dirty = false;
  483. }
  484. }
  485. });
  486. /**
  487. * creates a lens 3d action with center position, radius
  488. * @function
  489. * @param {Number} duration
  490. * @param {cc.Size} gridSize
  491. * @param {cc.Point} position
  492. * @param {Number} radius
  493. * @return {cc.Lens3D}
  494. */
  495. cc.lens3D = function (duration, gridSize, position, radius) {
  496. return new cc.Lens3D(duration, gridSize, position, radius);
  497. };
  498. /**
  499. * Please use cc.lens3D instead
  500. * creates a lens 3d action with center position, radius
  501. * @param {Number} duration
  502. * @param {cc.Size} gridSize
  503. * @param {cc.Point} position
  504. * @param {Number} radius
  505. * @return {cc.Lens3D}
  506. * @static
  507. * @deprecated since v3.0 <br /> Please use cc.lens3D instead.
  508. */
  509. cc.Lens3D.create = cc.lens3D;
  510. /**
  511. * cc.Ripple3D action. <br />
  512. * Reference the test cases (Effects Test)
  513. * @class
  514. * @extends cc.Grid3DAction
  515. * @param {Number} duration
  516. * @param {cc.Size} gridSize
  517. * @param {cc.Point} position
  518. * @param {Number} radius
  519. * @param {Number} waves
  520. * @param {Number} amplitude
  521. */
  522. cc.Ripple3D = cc.Grid3DAction.extend(/** @lends cc.Ripple3D# */{
  523. /* center position */
  524. _position: null,
  525. _radius: 0,
  526. _waves: 0,
  527. _amplitude: 0,
  528. _amplitudeRate: 0,
  529. /**
  530. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  531. * creates a ripple 3d action with radius, number of waves, amplitude.
  532. * @param {Number} duration
  533. * @param {cc.Size} gridSize
  534. * @param {cc.Point} position
  535. * @param {Number} radius
  536. * @param {Number} waves
  537. * @param {Number} amplitude
  538. */
  539. ctor:function (duration, gridSize, position, radius, waves, amplitude) {
  540. cc.GridAction.prototype.ctor.call(this);
  541. this._position = cc.p(0, 0);
  542. amplitude !== undefined && this.initWithDuration(duration, gridSize, position, radius, waves, amplitude);
  543. },
  544. /**
  545. * get center position
  546. * @return {cc.Point}
  547. */
  548. getPosition:function () {
  549. return this._position;
  550. },
  551. /**
  552. * set center position
  553. * @param {cc.Point} position
  554. */
  555. setPosition:function (position) {
  556. this._position.x = position.x;
  557. this._position.y = position.y;
  558. },
  559. /**
  560. * get Amplitude
  561. * @return {Number}
  562. */
  563. getAmplitude:function () {
  564. return this._amplitude;
  565. },
  566. /**
  567. * set Amplitude
  568. * @param {Number} amplitude
  569. */
  570. setAmplitude:function (amplitude) {
  571. this._amplitude = amplitude;
  572. },
  573. /**
  574. * get Amplitude rate
  575. * @return {*}
  576. */
  577. getAmplitudeRate:function () {
  578. return this._amplitudeRate;
  579. },
  580. /**
  581. * get amplitude rate
  582. * @param {Number} amplitudeRate
  583. */
  584. setAmplitudeRate:function (amplitudeRate) {
  585. this._amplitudeRate = amplitudeRate;
  586. },
  587. /**
  588. * initializes the action with radius, number of waves, amplitude, a grid size and duration
  589. * @param {Number} duration
  590. * @param {cc.Size} gridSize
  591. * @param {cc.Point} position
  592. * @param {Number} radius
  593. * @param {Number} waves
  594. * @param {Number} amplitude
  595. * @return {Boolean}
  596. */
  597. initWithDuration:function (duration, gridSize, position, radius, waves, amplitude) {
  598. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  599. this.setPosition(position);
  600. this._radius = radius;
  601. this._waves = waves;
  602. this._amplitude = amplitude;
  603. this._amplitudeRate = 1.0;
  604. return true;
  605. }
  606. return false;
  607. },
  608. /**
  609. * Called once per frame. Time is the number of seconds of a frame interval.
  610. *
  611. * @param {Number} dt
  612. */
  613. update:function (dt) {
  614. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  615. var locPos = cc.p(0, 0), locRadius = this._radius;
  616. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  617. var v, r, tempPos = cc.p(0, 0);
  618. for (var i = 0; i < (locGridSizeWidth + 1); ++i) {
  619. for (var j = 0; j < (locGridSizeHeight + 1); ++j) {
  620. locPos.x = i;
  621. locPos.y = j;
  622. v = this.originalVertex(locPos);
  623. tempPos.x = this._position.x - v.x;
  624. tempPos.y = this._position.y - v.y;
  625. r = cc.pLength(tempPos);
  626. if (r < locRadius) {
  627. r = locRadius - r;
  628. var rate = Math.pow(r / locRadius, 2);
  629. v.z += (Math.sin(dt * Math.PI * locWaves * 2 + r * 0.1) * locAmplitude * locAmplitudeRate * rate);
  630. }
  631. this.setVertex(locPos, v);
  632. }
  633. }
  634. }
  635. });
  636. /**
  637. * creates a ripple 3d action with radius, number of waves, amplitude
  638. * @function
  639. * @param {Number} duration
  640. * @param {cc.Size} gridSize
  641. * @param {cc.Point} position
  642. * @param {Number} radius
  643. * @param {Number} waves
  644. * @param {Number} amplitude
  645. * @return {cc.Ripple3D}
  646. */
  647. cc.ripple3D = function (duration, gridSize, position, radius, waves, amplitude) {
  648. return new cc.Ripple3D(duration, gridSize, position, radius, waves, amplitude);
  649. };
  650. /**
  651. * Please use cc.ripple3D instead
  652. * creates a ripple 3d action with radius, number of waves, amplitude
  653. * @param {Number} duration
  654. * @param {cc.Size} gridSize
  655. * @param {cc.Point} position
  656. * @param {Number} radius
  657. * @param {Number} waves
  658. * @param {Number} amplitude
  659. * @return {cc.Ripple3D}
  660. * @static
  661. * @deprecated since v3.0 <br /> Please use cc.ripple3D instead.
  662. */
  663. cc.Ripple3D.create = cc.ripple3D;
  664. /**
  665. * cc.Shaky3D action. <br />
  666. * Reference the test cases (Effects Test)
  667. * @class
  668. * @extends cc.Grid3DAction
  669. * @param {Number} duration
  670. * @param {cc.Size} gridSize
  671. * @param {Number} range
  672. * @param {Boolean} shakeZ
  673. */
  674. cc.Shaky3D = cc.Grid3DAction.extend(/** @lends cc.Shaky3D# */{
  675. _randRange: 0,
  676. _shakeZ: false,
  677. /**
  678. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  679. * Create a shaky3d action with a range, shake Z vertices.
  680. * @param {Number} duration
  681. * @param {cc.Size} gridSize
  682. * @param {Number} range
  683. * @param {Boolean} shakeZ
  684. */
  685. ctor:function (duration, gridSize, range, shakeZ) {
  686. cc.GridAction.prototype.ctor.call(this);
  687. shakeZ !== undefined && this.initWithDuration(duration, gridSize, range, shakeZ);
  688. },
  689. /**
  690. * initializes the action with a range, shake Z vertices, a grid and duration
  691. * @param {Number} duration
  692. * @param {cc.Size} gridSize
  693. * @param {Number} range
  694. * @param {Boolean} shakeZ
  695. * @return {Boolean}
  696. */
  697. initWithDuration:function (duration, gridSize, range, shakeZ) {
  698. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  699. this._randRange = range;
  700. this._shakeZ = shakeZ;
  701. return true;
  702. }
  703. return false;
  704. },
  705. /**
  706. * Called once per frame. Time is the number of seconds of a frame interval.
  707. *
  708. * @param {Number} dt
  709. */
  710. update:function (dt) {
  711. var locGridSizeWidth = this._gridSize.width, locGridSizeHeight = this._gridSize.height;
  712. var locRandRange = this._randRange, locShakeZ = this._shakeZ, locP = cc.p(0, 0);
  713. var v;
  714. for (var i = 0; i < (locGridSizeWidth + 1); ++i) {
  715. for (var j = 0; j < (locGridSizeHeight + 1); ++j) {
  716. locP.x = i;
  717. locP.y = j;
  718. v = this.originalVertex(locP);
  719. v.x += (cc.rand() % (locRandRange * 2)) - locRandRange;
  720. v.y += (cc.rand() % (locRandRange * 2)) - locRandRange;
  721. if (locShakeZ)
  722. v.z += (cc.rand() % (locRandRange * 2)) - locRandRange;
  723. this.setVertex(locP, v);
  724. }
  725. }
  726. }
  727. });
  728. /**
  729. * creates the action with a range, shake Z vertices, a grid and duration
  730. * @function
  731. * @param {Number} duration
  732. * @param {cc.Size} gridSize
  733. * @param {Number} range
  734. * @param {Boolean} shakeZ
  735. * @return {cc.Shaky3D}
  736. */
  737. cc.shaky3D = function (duration, gridSize, range, shakeZ) {
  738. return new cc.Shaky3D(duration, gridSize, range, shakeZ);
  739. };
  740. /**
  741. * Please use cc.shaky3D instead
  742. * creates the action with a range, shake Z vertices, a grid and duration
  743. * @param {Number} duration
  744. * @param {cc.Size} gridSize
  745. * @param {Number} range
  746. * @param {Boolean} shakeZ
  747. * @return {cc.Shaky3D}
  748. * @static
  749. * @deprecated since v3.0 <br /> Please use cc.shaky3D instead.
  750. */
  751. cc.Shaky3D.create = cc.shaky3D;
  752. /**
  753. * cc.Liquid action. <br />
  754. * Reference the test cases (Effects Test)
  755. * @class
  756. * @extends cc.Grid3DAction
  757. * @param {Number} duration
  758. * @param {cc.Size} gridSize
  759. * @param {Number} waves
  760. * @param {Number} amplitude
  761. */
  762. cc.Liquid = cc.Grid3DAction.extend(/** @lends cc.Liquid# */{
  763. _waves: 0,
  764. _amplitude: 0,
  765. _amplitudeRate: 0,
  766. /**
  767. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  768. * Create a liquid action with amplitude, a grid and duration.
  769. * @param {Number} duration
  770. * @param {cc.Size} gridSize
  771. * @param {Number} waves
  772. * @param {Number} amplitude
  773. */
  774. ctor: function (duration, gridSize, waves, amplitude) {
  775. cc.GridAction.prototype.ctor.call(this);
  776. amplitude !== undefined && this.initWithDuration(duration, gridSize, waves, amplitude);
  777. },
  778. /**
  779. * get amplitude
  780. * @return {Number}
  781. */
  782. getAmplitude:function () {
  783. return this._amplitude;
  784. },
  785. /**
  786. * set amplitude
  787. * @param {Number} amplitude
  788. */
  789. setAmplitude:function (amplitude) {
  790. this._amplitude = amplitude;
  791. },
  792. /**
  793. * get amplitude rate
  794. * @return {Number}
  795. */
  796. getAmplitudeRate:function () {
  797. return this._amplitudeRate;
  798. },
  799. /**
  800. * set amplitude rate
  801. * @param {Number} amplitudeRate
  802. */
  803. setAmplitudeRate:function (amplitudeRate) {
  804. this._amplitudeRate = amplitudeRate;
  805. },
  806. /**
  807. * initializes the action with amplitude, a grid and duration
  808. * @param {Number} duration
  809. * @param {cc.Size} gridSize
  810. * @param {Number} waves
  811. * @param {Number} amplitude
  812. * @return {Boolean}
  813. */
  814. initWithDuration:function (duration, gridSize, waves, amplitude) {
  815. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  816. this._waves = waves;
  817. this._amplitude = amplitude;
  818. this._amplitudeRate = 1.0;
  819. return true;
  820. }
  821. return false;
  822. },
  823. /**
  824. * Called once per frame. Time is the number of seconds of a frame interval.
  825. *
  826. * @param {Number} dt
  827. */
  828. update:function (dt) {
  829. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  830. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  831. var v;
  832. for (var i = 1; i < locSizeWidth; ++i) {
  833. for (var j = 1; j < locSizeHeight; ++j) {
  834. locPos.x = i;
  835. locPos.y = j;
  836. v = this.originalVertex(locPos);
  837. v.x = (v.x + (Math.sin(dt * Math.PI * locWaves * 2 + v.x * .01) * locAmplitude * locAmplitudeRate));
  838. v.y = (v.y + (Math.sin(dt * Math.PI * locWaves * 2 + v.y * .01) * locAmplitude * locAmplitudeRate));
  839. this.setVertex(locPos, v);
  840. }
  841. }
  842. }
  843. });
  844. /**
  845. * creates the action with amplitude, a grid and duration
  846. * @function
  847. * @param {Number} duration
  848. * @param {cc.Size} gridSize
  849. * @param {Number} waves
  850. * @param {Number} amplitude
  851. * @return {cc.Liquid}
  852. */
  853. cc.liquid = function (duration, gridSize, waves, amplitude) {
  854. return new cc.Liquid(duration, gridSize, waves, amplitude);
  855. };
  856. /**
  857. * Please use cc.liquid instead
  858. * creates the action with amplitude, a grid and duration
  859. * @param {Number} duration
  860. * @param {cc.Size} gridSize
  861. * @param {Number} waves
  862. * @param {Number} amplitude
  863. * @return {cc.Liquid}
  864. * @static
  865. * @deprecated since v3.0 <br /> Please use cc.liquid instead.
  866. */
  867. cc.Liquid.create = cc.liquid;
  868. /**
  869. * cc.Waves action. <br />
  870. * Reference the test cases (Effects Test)
  871. * @class
  872. * @extends cc.Grid3DAction
  873. * @param {Number} duration
  874. * @param {cc.Size} gridSize
  875. * @param {Number} waves
  876. * @param {Number} amplitude
  877. * @param {Boolean} horizontal
  878. * @param {Boolean} vertical
  879. */
  880. cc.Waves = cc.Grid3DAction.extend(/** @lends cc.Waves# */{
  881. _waves: 0,
  882. _amplitude: 0,
  883. _amplitudeRate: 0,
  884. _vertical: false,
  885. _horizontal: false,
  886. /**
  887. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  888. * Create a wave action with amplitude, horizontal sin, vertical sin, a grid and duration.
  889. * @param {Number} duration
  890. * @param {cc.Size} gridSize
  891. * @param {Number} waves
  892. * @param {Number} amplitude
  893. * @param {Boolean} horizontal
  894. * @param {Boolean} vertical
  895. */
  896. ctor: function (duration, gridSize, waves, amplitude, horizontal, vertical) {
  897. cc.GridAction.prototype.ctor.call(this);
  898. vertical !== undefined && this.initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical);
  899. },
  900. /**
  901. * get amplitude
  902. * @return {Number}
  903. */
  904. getAmplitude:function () {
  905. return this._amplitude;
  906. },
  907. /**
  908. * set amplitude
  909. * @param {Number} amplitude
  910. */
  911. setAmplitude:function (amplitude) {
  912. this._amplitude = amplitude;
  913. },
  914. /**
  915. * get amplitude rate
  916. * @return {Number}
  917. */
  918. getAmplitudeRate:function () {
  919. return this._amplitudeRate;
  920. },
  921. /**
  922. * set amplitude rate
  923. * @param {Number} amplitudeRate
  924. */
  925. setAmplitudeRate:function (amplitudeRate) {
  926. this._amplitudeRate = amplitudeRate;
  927. },
  928. /**
  929. * initializes the action with amplitude, horizontal sin, vertical sin, a grid and duration
  930. * @param {Number} duration
  931. * @param {cc.Size} gridSize
  932. * @param {Number} waves
  933. * @param {Number} amplitude
  934. * @param {Boolean} horizontal
  935. * @param {Boolean} vertical
  936. * @return {Boolean}
  937. */
  938. initWithDuration:function (duration, gridSize, waves, amplitude, horizontal, vertical) {
  939. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  940. this._waves = waves;
  941. this._amplitude = amplitude;
  942. this._amplitudeRate = 1.0;
  943. this._horizontal = horizontal;
  944. this._vertical = vertical;
  945. return true;
  946. }
  947. return false;
  948. },
  949. /**
  950. * Called once per frame. Time is the number of seconds of a frame interval.
  951. *
  952. * @param {Number} dt
  953. */
  954. update:function (dt) {
  955. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  956. var locVertical = this._vertical, locHorizontal = this._horizontal;
  957. var locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  958. var v;
  959. for (var i = 0; i < locSizeWidth + 1; ++i) {
  960. for (var j = 0; j < locSizeHeight + 1; ++j) {
  961. locPos.x = i;
  962. locPos.y = j;
  963. v = this.originalVertex(locPos);
  964. if (locVertical)
  965. v.x = (v.x + (Math.sin(dt * Math.PI * locWaves * 2 + v.y * .01) * locAmplitude * locAmplitudeRate));
  966. if (locHorizontal)
  967. v.y = (v.y + (Math.sin(dt * Math.PI * locWaves * 2 + v.x * .01) * locAmplitude * locAmplitudeRate));
  968. this.setVertex(locPos, v);
  969. }
  970. }
  971. }
  972. });
  973. /**
  974. * initializes the action with amplitude, horizontal sin, vertical sin, a grid and duration
  975. * @function
  976. * @param {Number} duration
  977. * @param {cc.Size} gridSize
  978. * @param {Number} waves
  979. * @param {Number} amplitude
  980. * @param {Boolean} horizontal
  981. * @param {Boolean} vertical
  982. * @return {cc.Waves}
  983. */
  984. cc.waves = function (duration, gridSize, waves, amplitude, horizontal, vertical) {
  985. return new cc.Waves(duration, gridSize, waves, amplitude, horizontal, vertical);
  986. };
  987. /**
  988. * Please use cc.waves instead
  989. * initializes the action with amplitude, horizontal sin, vertical sin, a grid and duration
  990. * @param {Number} duration
  991. * @param {cc.Size} gridSize
  992. * @param {Number} waves
  993. * @param {Number} amplitude
  994. * @param {Boolean} horizontal
  995. * @param {Boolean} vertical
  996. * @return {cc.Waves}
  997. * @static
  998. * @deprecated since v3.0 <br /> Please use cc.waves instead.
  999. */
  1000. cc.Waves.create = cc.waves;
  1001. /** @brief */
  1002. /**
  1003. * cc.Twirl action. <br />
  1004. * Reference the test cases (Effects Test)
  1005. * @class
  1006. * @extends cc.Grid3DAction
  1007. * @param {Number} duration
  1008. * @param {cc.Size} gridSize
  1009. * @param {cc.Point} position
  1010. * @param {Number} twirls
  1011. * @param {Number} amplitude
  1012. */
  1013. cc.Twirl = cc.Grid3DAction.extend(/** @lends cc.Twirl# */{
  1014. /* twirl center */
  1015. _position: null,
  1016. _twirls: 0,
  1017. _amplitude: 0,
  1018. _amplitudeRate: 0,
  1019. /**
  1020. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  1021. * Create a grid 3d action with center position, number of twirls, amplitude, a grid size and duration.
  1022. * @param {Number} duration
  1023. * @param {cc.Size} gridSize
  1024. * @param {cc.Point} position
  1025. * @param {Number} twirls
  1026. * @param {Number} amplitude
  1027. */
  1028. ctor:function (duration, gridSize, position, twirls, amplitude) {
  1029. cc.GridAction.prototype.ctor.call(this);
  1030. this._position = cc.p(0, 0);
  1031. amplitude !== undefined && this.initWithDuration(duration, gridSize, position, twirls, amplitude);
  1032. },
  1033. /**
  1034. * get twirl center
  1035. * @return {cc.Point}
  1036. */
  1037. getPosition:function () {
  1038. return this._position;
  1039. },
  1040. /**
  1041. * set twirl center
  1042. * @param {cc.Point} position
  1043. */
  1044. setPosition:function (position) {
  1045. this._position.x = position.x;
  1046. this._position.y = position.y;
  1047. },
  1048. /**
  1049. * get amplitude
  1050. * @return {Number}
  1051. */
  1052. getAmplitude:function () {
  1053. return this._amplitude;
  1054. },
  1055. /**
  1056. * set amplitude
  1057. * @param {Number} amplitude
  1058. */
  1059. setAmplitude:function (amplitude) {
  1060. this._amplitude = amplitude;
  1061. },
  1062. /**
  1063. * get amplitude rate
  1064. * @return {Number}
  1065. */
  1066. getAmplitudeRate:function () {
  1067. return this._amplitudeRate;
  1068. },
  1069. /**
  1070. * set amplitude rate
  1071. * @param {Number} amplitudeRate
  1072. */
  1073. setAmplitudeRate:function (amplitudeRate) {
  1074. this._amplitudeRate = amplitudeRate;
  1075. },
  1076. /** initializes the action with center position, number of twirls, amplitude, a grid size and duration */
  1077. initWithDuration:function (duration, gridSize, position, twirls, amplitude) {
  1078. if (cc.Grid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  1079. this.setPosition(position);
  1080. this._twirls = twirls;
  1081. this._amplitude = amplitude;
  1082. this._amplitudeRate = 1.0;
  1083. return true;
  1084. }
  1085. return false;
  1086. },
  1087. /**
  1088. * Called once per frame. Time is the number of seconds of a frame interval.
  1089. *
  1090. * @param {Number} dt
  1091. */
  1092. update:function (dt) {
  1093. var c = this._position;
  1094. var locSizeWidth = this._gridSize.width, locSizeHeight = this._gridSize.height, locPos = cc.p(0, 0);
  1095. var amp = 0.1 * this._amplitude * this._amplitudeRate;
  1096. var locTwirls = this._twirls;
  1097. var v, a, dX, dY, avg = cc.p(0, 0);
  1098. for (var i = 0; i < (locSizeWidth + 1); ++i) {
  1099. for (var j = 0; j < (locSizeHeight + 1); ++j) {
  1100. locPos.x = i;
  1101. locPos.y = j;
  1102. v = this.originalVertex(locPos);
  1103. avg.x = i - (locSizeWidth / 2.0);
  1104. avg.y = j - (locSizeHeight / 2.0);
  1105. a = cc.pLength(avg) * Math.cos(Math.PI / 2.0 + dt * Math.PI * locTwirls * 2) * amp;
  1106. dX = Math.sin(a) * (v.y - c.y) + Math.cos(a) * (v.x - c.x);
  1107. dY = Math.cos(a) * (v.y - c.y) - Math.sin(a) * (v.x - c.x);
  1108. v.x = c.x + dX;
  1109. v.y = c.y + dY;
  1110. this.setVertex(locPos, v);
  1111. }
  1112. }
  1113. }
  1114. });
  1115. /**
  1116. * creates the action with center position, number of twirls, amplitude, a grid size and duration
  1117. * @function
  1118. * @param {Number} duration
  1119. * @param {cc.Size} gridSize
  1120. * @param {cc.Point} position
  1121. * @param {Number} twirls
  1122. * @param {Number} amplitude
  1123. * @return {cc.Twirl}
  1124. */
  1125. cc.twirl = function (duration, gridSize, position, twirls, amplitude) {
  1126. return new cc.Twirl(duration, gridSize, position, twirls, amplitude);
  1127. };
  1128. /**
  1129. * Please use cc.twirl instead
  1130. * creates the action with center position, number of twirls, amplitude, a grid size and duration
  1131. * @param {Number} duration
  1132. * @param {cc.Size} gridSize
  1133. * @param {cc.Point} position
  1134. * @param {Number} twirls
  1135. * @param {Number} amplitude
  1136. * @return {cc.Twirl}
  1137. * @static
  1138. * @deprecated since v3.0 <br /> Please use cc.twirl instead.
  1139. */
  1140. cc.Twirl.create = cc.twirl;